Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/iamvirtmfa.py
1566 views
1
# Copyright 2013 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
"""
14
This customization makes it easier to deal with the bootstrapping
15
data returned by the ``iam create-virtual-mfa-device`` command.
16
You can choose to bootstrap via a QRCode or via a Base32String.
17
You specify your choice via the ``--bootstrap-method`` option
18
which should be either "QRCodePNG" or "Base32StringSeed". You
19
then specify the path to where you would like your bootstrapping
20
data saved using the ``--outfile`` option. The command will
21
pull the appropriate data field out of the response and write it
22
to the specified file. It will also remove the two bootstrap data
23
fields from the response.
24
"""
25
import base64
26
27
from awscli.customizations.arguments import StatefulArgument
28
from awscli.customizations.arguments import resolve_given_outfile_path
29
from awscli.customizations.arguments import is_parsed_result_successful
30
31
32
CHOICES = ('QRCodePNG', 'Base32StringSeed')
33
OUTPUT_HELP = ('The output path and file name where the bootstrap '
34
'information will be stored.')
35
BOOTSTRAP_HELP = ('Method to use to seed the virtual MFA. '
36
'Valid values are: %s | %s' % CHOICES)
37
38
39
class FileArgument(StatefulArgument):
40
41
def add_to_params(self, parameters, value):
42
# Validate the file here so we can raise an error prior
43
# calling the service.
44
value = resolve_given_outfile_path(value)
45
super(FileArgument, self).add_to_params(parameters, value)
46
47
48
class IAMVMFAWrapper(object):
49
50
def __init__(self, event_handler):
51
self._event_handler = event_handler
52
self._outfile = FileArgument(
53
'outfile', help_text=OUTPUT_HELP, required=True)
54
self._method = StatefulArgument(
55
'bootstrap-method', help_text=BOOTSTRAP_HELP,
56
choices=CHOICES, required=True)
57
self._event_handler.register(
58
'building-argument-table.iam.create-virtual-mfa-device',
59
self._add_options)
60
self._event_handler.register(
61
'after-call.iam.CreateVirtualMFADevice', self._save_file)
62
63
def _add_options(self, argument_table, **kwargs):
64
argument_table['outfile'] = self._outfile
65
argument_table['bootstrap-method'] = self._method
66
67
def _save_file(self, parsed, **kwargs):
68
if not is_parsed_result_successful(parsed):
69
return
70
method = self._method.value
71
outfile = self._outfile.value
72
if method in parsed['VirtualMFADevice']:
73
body = parsed['VirtualMFADevice'][method]
74
with open(outfile, 'wb') as fp:
75
fp.write(base64.b64decode(body))
76
for choice in CHOICES:
77
if choice in parsed['VirtualMFADevice']:
78
del parsed['VirtualMFADevice'][choice]
79
80