Skip to content

Commit 59ff252

Browse files
redboTarmac
authored and
Tarmac
committed
Adds param-signed URLs to swift3 middleware.
2 parents b9eea0f + eea6596 commit 59ff252

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

swift/common/middleware/swift3.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
"""
1717
The swift3 middleware will emulate the S3 REST api on top of swift.
1818
19-
The boto python library is necessary to use this middleware (install
20-
the python-boto package if you use Ubuntu).
21-
2219
The following opperations are currently supported:
2320
2421
* GET Service
@@ -438,32 +435,35 @@ def get_controller(self, path):
438435
return BucketController, d
439436
return ServiceController, d
440437

441-
def get_account_info(self, env, req):
442-
try:
443-
account, user, _junk = \
444-
req.headers['Authorization'].split(' ')[-1].split(':')
445-
except Exception:
446-
return None, None
447-
448-
h = canonical_string(req)
449-
token = base64.urlsafe_b64encode(h)
450-
return '%s:%s' % (account, user), token
451-
452438
def __call__(self, env, start_response):
453439
req = Request(env)
454-
if not'Authorization' in req.headers:
440+
441+
if 'AWSAccessKeyId' in req.GET:
442+
try:
443+
req.headers['Date'] = req.GET['Expires']
444+
req.headers['Authorization'] = \
445+
'AWS %(AWSAccessKeyId)s:%(Signature)s' % req.GET
446+
except KeyError:
447+
return get_err_response('InvalidArgument')(env, start_response)
448+
449+
if not 'Authorization' in req.headers:
455450
return self.app(env, start_response)
451+
452+
try:
453+
account, signature = \
454+
req.headers['Authorization'].split(' ')[-1].rsplit(':', 1)
455+
except Exception:
456+
return get_err_response('InvalidArgument')(env, start_response)
457+
456458
try:
457459
controller, path_parts = self.get_controller(req.path)
458460
except ValueError:
459461
return get_err_response('InvalidURI')(env, start_response)
460462

461-
account_name, token = self.get_account_info(env, req)
462-
if not account_name:
463-
return get_err_response('InvalidArgument')(env, start_response)
463+
token = base64.urlsafe_b64encode(canonical_string(req))
464+
465+
controller = controller(env, self.app, account, token, **path_parts)
464466

465-
controller = controller(env, self.app, account_name, token,
466-
**path_parts)
467467
if hasattr(controller, req.method):
468468
res = getattr(controller, req.method)(env, start_response)
469469
else:

test/unit/common/middleware/test_swift3.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,5 +594,21 @@ def verify(hash, path, headers):
594594
self.assertEquals(swift3.canonical_string(req2),
595595
swift3.canonical_string(req3))
596596

597+
def test_signed_urls(self):
598+
class FakeApp(object):
599+
def __call__(self, env, start_response):
600+
self.req = Request(env)
601+
start_response('200 OK')
602+
start_response([])
603+
app = FakeApp()
604+
local_app = swift3.filter_factory({})(app)
605+
req = Request.blank('/bucket/object?Signature=X&Expires=Y&'
606+
'AWSAccessKeyId=Z', environ={'REQUEST_METHOD': 'GET'})
607+
req.date = datetime.now()
608+
req.content_type = 'text/plain'
609+
resp = local_app(req.environ, lambda *args: None)
610+
self.assertEquals(app.req.headers['Authorization'], 'AWS Z:X')
611+
self.assertEquals(app.req.headers['Date'], 'Y')
612+
597613
if __name__ == '__main__':
598614
unittest.main()

0 commit comments

Comments
 (0)