Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/globalargs.py
2624 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import sys
14
import os
15
16
from awscli.customizations.argrename import HIDDEN_ALIASES
17
from awscli.customizations.utils import uni_print
18
from botocore.client import Config
19
from botocore import UNSIGNED
20
from botocore.endpoint import DEFAULT_TIMEOUT
21
from botocore.useragent import register_feature_id
22
import jmespath
23
24
from awscli.compat import urlparse
25
from awscli.utils import resolve_v2_debug_mode
26
27
28
def register_parse_global_args(cli):
29
cli.register('top-level-args-parsed', resolve_types,
30
unique_id='resolve-types')
31
cli.register('top-level-args-parsed', no_sign_request,
32
unique_id='no-sign')
33
cli.register('top-level-args-parsed', resolve_verify_ssl,
34
unique_id='resolve-verify-ssl')
35
cli.register('top-level-args-parsed', resolve_cli_read_timeout,
36
unique_id='resolve-cli-read-timeout')
37
cli.register('top-level-args-parsed', resolve_cli_connect_timeout,
38
unique_id='resolve-cli-connect-timeout')
39
cli.register('top-level-args-parsed', detect_migration_breakage,
40
unique_id='detect-migration-breakage')
41
42
43
def resolve_types(parsed_args, **kwargs):
44
# This emulates the "type" arg from argparse, but does so in a way
45
# that plugins can also hook into this process.
46
_resolve_arg(parsed_args, 'query')
47
_resolve_arg(parsed_args, 'endpoint_url')
48
49
50
def _resolve_arg(parsed_args, name):
51
value = getattr(parsed_args, name, None)
52
if value is not None:
53
new_value = getattr(sys.modules[__name__], '_resolve_%s' % name)(value)
54
setattr(parsed_args, name, new_value)
55
56
57
def _resolve_query(value):
58
try:
59
return jmespath.compile(value)
60
except Exception as e:
61
raise ValueError("Bad value for --query %s: %s" % (value, str(e)))
62
63
64
def _resolve_endpoint_url(value):
65
parsed = urlparse.urlparse(value)
66
# Our http library requires you specify an endpoint url
67
# that contains a scheme, so we'll verify that up front.
68
if not parsed.scheme:
69
raise ValueError('Bad value for --endpoint-url "%s": scheme is '
70
'missing. Must be of the form '
71
'http://<hostname>/ or https://<hostname>/' % value)
72
return value
73
74
75
def resolve_verify_ssl(parsed_args, session, **kwargs):
76
arg_name = 'verify_ssl'
77
arg_value = getattr(parsed_args, arg_name, None)
78
if arg_value is not None:
79
verify = None
80
# Only consider setting a custom ca_bundle if they
81
# haven't provided --no-verify-ssl.
82
if not arg_value:
83
verify = False
84
else:
85
# in case if `ca_bundle` not in args it'll be retrieved
86
# from config on session.client creation step
87
verify = getattr(parsed_args, 'ca_bundle', None)
88
setattr(parsed_args, arg_name, verify)
89
90
def no_sign_request(parsed_args, session, **kwargs):
91
if not parsed_args.sign_request:
92
# Disable request signing by setting the signature version to UNSIGNED
93
# in the default client configuration. This ensures all new clients
94
# will be created with signing disabled.
95
_update_default_client_config(session, 'signature_version', UNSIGNED)
96
97
def resolve_cli_connect_timeout(parsed_args, session, **kwargs):
98
arg_name = 'connect_timeout'
99
_resolve_timeout(session, parsed_args, arg_name)
100
101
def detect_migration_breakage(parsed_args, session, remaining_args, **kwargs):
102
if not resolve_v2_debug_mode(parsed_args):
103
return
104
region = parsed_args.region or session.get_config_variable('region')
105
s3_config = session.get_config_variable('s3')
106
if (
107
not session.get_scoped_config().get('cli_pager', None)
108
== '' and 'AWS_PAGER' not in os.environ
109
):
110
uni_print(
111
'\nAWS CLI v2 UPGRADE WARNING: By default, the AWS CLI v2 returns '
112
'all output through your operating system’s default pager '
113
'program. This is different from v1 behavior, where the system '
114
'pager is not used by default. To retain AWS CLI v1 behavior in '
115
'AWS CLI v2, set the `cli_pager` configuration setting, or the '
116
'`AWS_PAGER` environment variable, to the empty string. See '
117
'https://docs.aws.amazon.com/cli/latest/userguide/'
118
'cliv2-migration-changes.html#cliv2-migration-output-pager.\n',
119
out_file=sys.stderr
120
)
121
if 'PYTHONUTF8' in os.environ or 'PYTHONIOENCODING' in os.environ:
122
if 'AWS_CLI_FILE_ENCODING' not in os.environ:
123
uni_print(
124
'\nThe AWS CLI v2 does not support The `PYTHONUTF8` and '
125
'`PYTHONIOENCODING` environment variables, and instead uses '
126
'the `AWS_CLI_FILE_ENCODING` variable. This is different from '
127
'v1 behavior, where the former two variables are used '
128
'instead. To retain AWS CLI v1 behavior in AWS CLI v2, set '
129
'the `AWS_CLI_FILE_ENCODING` environment variable instead. '
130
'See https://docs.aws.amazon.com/cli/latest/userguide/'
131
'cliv2-migration-changes.html'
132
'#cliv2-migration-encodingenvvar.\n',
133
out_file=sys.stderr
134
)
135
if (
136
(
137
s3_config is None
138
or s3_config.get('us_east_1_regional_endpoint', 'legacy')
139
== 'legacy'
140
)
141
and region in ('us-east-1', None)
142
):
143
session.register(
144
'request-created.s3.*',
145
warn_if_east_configured_global_endpoint
146
)
147
session.register(
148
'request-created.s3api.*',
149
warn_if_east_configured_global_endpoint
150
)
151
if session.get_config_variable('api_versions'):
152
uni_print(
153
'\nAWS CLI v2 UPGRADE WARNING: AWS CLI v2 UPGRADE WARNING: '
154
'The AWS CLI v2 does not support calling older versions of AWS '
155
'service APIs via the `api_versions` configuration file setting. This '
156
'is different from v1 behavior, where this configuration setting '
157
'can be used to pin older API versions. To migrate to v2 '
158
'behavior, remove the `api_versions` configuration setting, and '
159
'test against the latest service API versions. See '
160
'https://docs.aws.amazon.com/cli/latest/userguide/'
161
'cliv2-migration-changes.html#cliv2-migration-api-versions.\n',
162
out_file = sys.stderr
163
)
164
if session.full_config.get('plugins', {}):
165
uni_print(
166
'\nAWS CLI v2 UPGRADE WARNING: In AWS CLI v2, plugins are '
167
'disabled by default, and support for plugins is provisional. '
168
'This is different from v1 behavior, where plugin support is URL '
169
'below to update your configuration to enable plugins in AWS CLI '
170
'v2. Also, be sure to lock into a particular version of the AWS '
171
'CLI and test the functionality of your plugins every time AWS '
172
'CLI v2 is upgraded. See https://docs.aws.amazon.com/cli/latest/'
173
'userguide/cliv2-migration-changes.html'
174
'#cliv2-migration-profile-plugins.\n',
175
out_file=sys.stderr
176
)
177
if (
178
parsed_args.command == 'ecr' and
179
remaining_args is not None and
180
remaining_args[0] == 'get-login'
181
):
182
uni_print(
183
'\nAWS CLI v2 UPGRADE WARNING: The `ecr get-login` command has '
184
'been removed in AWS CLI v2. You must use `ecr get-login-password` '
185
'instead. See https://docs.aws.amazon.com/cli/latest/userguide/'
186
'cliv2-migration-changes.html#cliv2-migration-ecr-get-login.\n',
187
out_file=sys.stderr
188
)
189
for working, obsolete in HIDDEN_ALIASES.items():
190
working_split = working.split('.')
191
working_service = working_split[0]
192
working_cmd = working_split[1]
193
working_param = working_split[2]
194
if (
195
parsed_args.command == working_service
196
and remaining_args is not None
197
and remaining_args[0] == working_cmd
198
and f"--{working_param}" in remaining_args
199
):
200
uni_print(
201
'\nAWS CLI v2 UPGRADE WARNING: You have entered command '
202
'arguments that use at least 1 of 21 built-in ("hidden") '
203
'aliases that were removed in AWS CLI v2. For this command '
204
'to work in AWS CLI v2, you must replace usage of the alias '
205
'with the corresponding parameter in AWS CLI v2. See '
206
'https://docs.aws.amazon.com/cli/latest/userguide/'
207
'cliv2-migration-changes.html#cliv2-migration-aliases.\n',
208
out_file=sys.stderr
209
)
210
# Register against the provide-client-params event to ensure that the
211
# feature ID is registered before any API requests are made. We
212
# cannot register the feature ID in this function because no
213
# botocore context is created at this point.
214
session.register(
215
'provide-client-params.*.*',
216
_register_v2_debug_feature_id
217
)
218
session.register('choose-signer.s3.*', warn_if_sigv2)
219
220
221
def _register_v2_debug_feature_id(params, model, **kwargs):
222
register_feature_id('CLI_V1_TO_V2_MIGRATION_DEBUG_MODE')
223
224
def warn_if_east_configured_global_endpoint(request, operation_name, **kwargs):
225
# The regional us-east-1 endpoint is used in certain cases (e.g.
226
# FIPS/Dual-Stack is enabled). Rather than duplicating this logic
227
# from botocore, we check the endpoint URL directly.
228
parsed_url = urlparse.urlparse(request.url)
229
if parsed_url.hostname.endswith('s3.amazonaws.com'):
230
uni_print(
231
'\nAWS CLI v2 UPGRADE WARNING: When you configure AWS CLI v2 to '
232
'use the `us-east-1` region, it uses the true regional endpoint '
233
'rather than the global endpoint. This is different from v1 '
234
'behavior, where the global endpoint would be used when the '
235
'region is `us-east-1`. To retain AWS CLI v1 behavior in AWS '
236
'CLI v2, configure the region setting to `aws-global`. See '
237
'https://docs.aws.amazon.com/cli/latest/userguide/'
238
'cliv2-migration-changes.html'
239
'#cliv2-migration-s3-regional-endpoint.\n',
240
out_file=sys.stderr
241
)
242
243
def warn_if_sigv2(
244
signing_name,
245
region_name,
246
signature_version,
247
context,
248
**kwargs
249
):
250
if context.get('auth_type', None) == 'v2':
251
uni_print(
252
'\nAWS CLI v2 UPGRADE WARNING: The AWS CLI v2 only uses Signature '
253
'v4 to authenticate Amazon S3 requests. This is different from '
254
'v1 behavior, where the signature used for Amazon S3 requests may '
255
'vary depending on configuration settings, region, and the '
256
'bucket being used. To migrate to AWS CLI v2 behavior, configure '
257
'the Signature Version S3 setting to version 4. See '
258
'https://docs.aws.amazon.com/cli/latest/userguide/'
259
'cliv2-migration-changes.html#cliv2-migration-sigv4.\n',
260
out_file=sys.stderr
261
)
262
263
def resolve_cli_read_timeout(parsed_args, session, **kwargs):
264
arg_name = 'read_timeout'
265
_resolve_timeout(session, parsed_args, arg_name)
266
267
def _resolve_timeout(session, parsed_args, arg_name):
268
arg_value = getattr(parsed_args, arg_name, None)
269
if arg_value is None:
270
arg_value = DEFAULT_TIMEOUT
271
arg_value = int(arg_value)
272
if arg_value == 0:
273
arg_value = None
274
setattr(parsed_args, arg_name, arg_value)
275
# Update in the default client config so that the timeout will be used
276
# by all clients created from then on.
277
_update_default_client_config(session, arg_name, arg_value)
278
279
280
def _update_default_client_config(session, arg_name, arg_value):
281
current_default_config = session.get_default_client_config()
282
new_default_config = Config(**{arg_name: arg_value})
283
if current_default_config is not None:
284
new_default_config = current_default_config.merge(new_default_config)
285
session.set_default_client_config(new_default_config)
286
287