Skip to content

Commit a82091f

Browse files
Revert "Tighten file permissions for virtual MFA bootstrap output (#10194)"
This reverts commit 68811b5.
1 parent 68811b5 commit a82091f

3 files changed

Lines changed: 27 additions & 80 deletions

File tree

.changes/next-release/bugfix-iam-84710.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

awscli/customizations/iamvirtmfa.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,43 @@
2222
to the specified file. It will also remove the two bootstrap data
2323
fields from the response.
2424
"""
25-
2625
import base64
27-
import os
2826

29-
from awscli.compat import compat_open
30-
from awscli.customizations.arguments import (
31-
StatefulArgument,
32-
is_parsed_result_successful,
33-
resolve_given_outfile_path,
34-
)
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+
3531

3632
CHOICES = ('QRCodePNG', 'Base32StringSeed')
37-
OUTPUT_HELP = (
38-
'The output path and file name where the bootstrap '
39-
'information will be stored.'
40-
)
41-
BOOTSTRAP_HELP = (
42-
'Method to use to seed the virtual MFA. '
43-
'Valid values are: %s | %s' % CHOICES
44-
)
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)
4537

4638

4739
class FileArgument(StatefulArgument):
40+
4841
def add_to_params(self, parameters, value):
4942
# Validate the file here so we can raise an error prior
5043
# calling the service.
5144
value = resolve_given_outfile_path(value)
5245
super(FileArgument, self).add_to_params(parameters, value)
5346

5447

55-
class IAMVMFAWrapper:
48+
class IAMVMFAWrapper(object):
49+
5650
def __init__(self, event_handler):
5751
self._event_handler = event_handler
5852
self._outfile = FileArgument(
59-
'outfile', help_text=OUTPUT_HELP, required=True
60-
)
53+
'outfile', help_text=OUTPUT_HELP, required=True)
6154
self._method = StatefulArgument(
62-
'bootstrap-method',
63-
help_text=BOOTSTRAP_HELP,
64-
choices=CHOICES,
65-
required=True,
66-
)
55+
'bootstrap-method', help_text=BOOTSTRAP_HELP,
56+
choices=CHOICES, required=True)
6757
self._event_handler.register(
6858
'building-argument-table.iam.create-virtual-mfa-device',
69-
self._add_options,
70-
)
59+
self._add_options)
7160
self._event_handler.register(
72-
'after-call.iam.CreateVirtualMFADevice', self._save_file
73-
)
61+
'after-call.iam.CreateVirtualMFADevice', self._save_file)
7462

7563
def _add_options(self, argument_table, **kwargs):
7664
argument_table['outfile'] = self._outfile
@@ -83,9 +71,7 @@ def _save_file(self, parsed, **kwargs):
8371
outfile = self._outfile.value
8472
if method in parsed['VirtualMFADevice']:
8573
body = parsed['VirtualMFADevice'][method]
86-
with compat_open(outfile, 'wb', access_permissions=0o600) as fp:
87-
if hasattr(os, 'fchmod'):
88-
os.fchmod(fp.fileno(), 0o600)
74+
with open(outfile, 'wb') as fp:
8975
fp.write(base64.b64decode(body))
9076
for choice in CHOICES:
9177
if choice in parsed['VirtualMFADevice']:

tests/functional/iam/test_create_virtual_mfa_device.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@
1111
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
1212
# ANY KIND, either express or implied. See the License for the specific
1313
# language governing permissions and limitations under the License.
14+
from awscli.testutils import BaseAWSCommandParamsTest
1415
import os
1516

16-
from awscli.testutils import BaseAWSCommandParamsTest, skip_if_windows
17-
1817

1918
class TestCreateVirtualMFADevice(BaseAWSCommandParamsTest):
19+
2020
prefix = 'iam create-virtual-mfa-device'
2121

2222
def setUp(self):
2323
super(TestCreateVirtualMFADevice, self).setUp()
2424
self.parsed_response = {
2525
'ResponseMetadata': {
2626
'HTTPStatusCode': 200,
27-
'RequestId': 'requset-id',
27+
'RequestId': 'requset-id'
2828
},
2929
"VirtualMFADevice": {
3030
"Base32StringSeed": (
3131
"VFpYTVc2V1lIUFlFRFczSVhLUlpRUTJRVFdUSFRNRDNTQ0c3"
32-
"TkZDUVdQWDVETlNWM0IyUENaQVpWTEpQTlBOTA=="
33-
),
32+
"TkZDUVdQWDVETlNWM0IyUENaQVpWTEpQTlBOTA=="),
3433
"SerialNumber": "arn:aws:iam::419278470775:mfa/fiebaz",
3534
"QRCodePNG": (
3635
"iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAIAAAAHjs1qAAAFi"
@@ -75,13 +74,12 @@ def setUp(self):
7574
"R3EVwF8FdBHcR3EVwF8Fd5F/+AgASajf850wfAAAAAElFTkSu"
7675
"QmCC"
7776
),
78-
},
77+
}
7978
}
8079

8180
def getpath(self, filename):
82-
return os.path.join(
83-
os.path.abspath(os.path.dirname(__file__)), filename
84-
)
81+
return os.path.join(os.path.abspath(os.path.dirname(__file__)),
82+
filename)
8583

8684
def remove_file_if_exists(self, filename):
8785
if os.path.isfile(filename):
@@ -93,8 +91,7 @@ def test_base32(self):
9391
cmdline = self.prefix
9492
cmdline += ' --virtual-mfa-device-name fiebaz'
9593
cmdline += (
96-
' --outfile %s --bootstrap-method Base32StringSeed' % outfile
97-
)
94+
' --outfile %s --bootstrap-method Base32StringSeed' % outfile)
9895
result = {"VirtualMFADeviceName": 'fiebaz'}
9996
self.assert_params_for_cmd(cmdline, result)
10097
self.assertTrue(os.path.exists(outfile))
@@ -148,8 +145,7 @@ def test_bad_response(self):
148145
},
149146
'ResponseMetadata': {
150147
'HTTPStatusCode': 409,
151-
'RequestId': 'requset-id',
152-
},
148+
'RequestId': 'requset-id'}
153149
}
154150
self.http_response.status_code = 409
155151
cmdline = self.prefix
@@ -159,34 +155,4 @@ def test_bad_response(self):
159155
self.assert_params_for_cmd(
160156
cmdline,
161157
stderr_contains=self.parsed_response['Error']['Message'],
162-
expected_rc=255,
163-
)
164-
165-
@skip_if_windows("Permissions test not valid on Windows.")
166-
def test_output_file_permissions(self):
167-
outfile = self.getpath('fiebaz_perms.b32')
168-
self.addCleanup(self.remove_file_if_exists, outfile)
169-
cmdline = self.prefix
170-
cmdline += ' --virtual-mfa-device-name fiebaz'
171-
cmdline += (
172-
' --outfile %s --bootstrap-method Base32StringSeed' % outfile
173-
)
174-
result = {"VirtualMFADeviceName": 'fiebaz'}
175-
self.assert_params_for_cmd(cmdline, result)
176-
self.assertEqual(os.stat(outfile).st_mode & 0xFFF, 0o600)
177-
178-
@skip_if_windows("Permissions test not valid on Windows.")
179-
def test_output_file_permissions_existing_file(self):
180-
outfile = self.getpath('fiebaz_perms_existing.b32')
181-
self.addCleanup(self.remove_file_if_exists, outfile)
182-
with open(outfile, 'wb') as f:
183-
f.write(b'existing')
184-
os.chmod(outfile, 0o644)
185-
cmdline = self.prefix
186-
cmdline += ' --virtual-mfa-device-name fiebaz'
187-
cmdline += (
188-
' --outfile %s --bootstrap-method Base32StringSeed' % outfile
189-
)
190-
result = {"VirtualMFADeviceName": 'fiebaz'}
191-
self.assert_params_for_cmd(cmdline, result)
192-
self.assertEqual(os.stat(outfile).st_mode & 0xFFF, 0o600)
158+
expected_rc=255)

0 commit comments

Comments
 (0)