1
# Copyright (c) 2010 Mitch Garnaat http://garnaat.org/
2
# Copyright (c) 2010, Eucalyptus Systems, Inc.
5
# Permission is hereby granted, free of charge, to any person obtaining a
6
# copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish, dis-
9
# tribute, sublicense, and/or sell copies of the Software, and to permit
10
# persons to whom the Software is furnished to do so, subject to the fol-
13
# The above copyright notice and this permission notice shall be included
14
# in all copies or substantial portions of the Software.
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
18
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
Some unit tests for S3 MfaDelete with versioning
30
from boto.s3.connection import S3Connection
31
from boto.exception import S3ResponseError
32
from boto.s3.deletemarker import DeleteMarker
34
class S3MFATest (unittest.TestCase):
37
self.conn = S3Connection()
38
self.bucket_name = 'mfa-%d' % int(time.time())
39
self.bucket = self.conn.create_bucket(self.bucket_name)
42
for k in self.bucket.list_versions():
43
self.bucket.delete_key(k.name, version_id=k.version_id)
46
def test_mfadel(self):
47
# Enable Versioning with MfaDelete
48
mfa_sn = raw_input('MFA S/N: ')
49
mfa_code = raw_input('MFA Code: ')
50
self.bucket.configure_versioning(True, mfa_delete=True, mfa_token=(mfa_sn, mfa_code))
52
# Check enabling mfa worked.
56
d = self.bucket.get_versioning_status()
57
if d['Versioning'] == 'Enabled' and d['MfaDelete'] == 'Enabled':
59
self.assertEqual('Enabled', d['Versioning'])
60
self.assertEqual('Enabled', d['MfaDelete'])
62
# Add a key to the bucket
63
k = self.bucket.new_key('foobar')
65
k.set_contents_from_string(s1)
68
# Now try to delete v1 without the MFA token
70
self.bucket.delete_key('foobar', version_id=v1)
71
self.fail("Must fail if not using MFA token")
72
except S3ResponseError:
75
# Now try delete again with the MFA token
76
mfa_code = raw_input('MFA Code: ')
77
self.bucket.delete_key('foobar', version_id=v1, mfa_token=(mfa_sn, mfa_code))
79
# Next suspend versioning and disable MfaDelete on the bucket
80
mfa_code = raw_input('MFA Code: ')
81
self.bucket.configure_versioning(False, mfa_delete=False, mfa_token=(mfa_sn, mfa_code))
83
# Lastly, check disabling mfa worked.
87
d = self.bucket.get_versioning_status()
88
if d['Versioning'] == 'Suspended' and d['MfaDelete'] != 'Enabled':
90
self.assertEqual('Suspended', d['Versioning'])
91
self.assertNotEqual('Enabled', d['MfaDelete'])