Advertisement
Guest User

Untitled

a guest
Nov 8th, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. """
  2. Starting point for routing EC2 requests.
  3.  
  4. """
  5.  
  6. from urlparse import urlparse
  7.  
  8. from eventlet.green import httplib
  9. import webob.dec
  10. import webob.exc
  11.  
  12. from nova import flags
  13. from nova import utils
  14. from nova import wsgi
  15.  
  16.  
  17. FLAGS = flags.FLAGS
  18. flags.DEFINE_string('keystone_ec2_url',
  19. 'http://localhost:5000/v2.0/ec2tokens',
  20. 'URL to get token from ec2 request.')
  21.  
  22.  
  23. class EC2Token(wsgi.Middleware):
  24. """Authenticate an EC2 request with keystone and convert to token."""
  25.  
  26. @webob.dec.wsgify(RequestClass=wsgi.Request)
  27. def __call__(self, req):
  28. # Read request signature and access id.
  29. try:
  30. signature = req.params['Signature']
  31. access = req.params['AWSAccessKeyId']
  32. except KeyError:
  33. raise webob.exc.HTTPBadRequest()
  34.  
  35. # Make a copy of args for authentication and signature verification.
  36. auth_params = dict(req.params)
  37. # Not part of authentication args
  38. auth_params.pop('Signature')
  39.  
  40. # Authenticate the request.
  41. creds = {'ec2Credentials': {'access': access,
  42. 'signature': signature,
  43. 'host': req.host,
  44. 'verb': req.method,
  45. 'path': req.path,
  46. 'params': auth_params,
  47. }}
  48. creds_json = utils.dumps(creds)
  49. headers = {'Content-Type': 'application/json'}
  50.  
  51. # Disable "has no x member" pylint error
  52. # for httplib and urlparse
  53. # pylint: disable-msg=E1101
  54. o = urlparse(FLAGS.keystone_ec1_url)
  55. if o.scheme == "http":
  56. conn = httplib.HTTPConnection(o.netloc)
  57. else:
  58. conn = httplib.HTTPSConnection(o.netloc)
  59. conn.request('POST', o.path, body=creds_json, headers=headers)
  60. response = conn.getresponse().read()
  61. conn.close()
  62.  
  63. # NOTE(vish): We could save a call to keystone by
  64. # having keystone return token, tenant,
  65. # user, and roles from this call.
  66. result = utils.loads(response)
  67. # TODO(vish): check for errors
  68.  
  69. token_id = result['auth']['token']['id']
  70. # Authenticated!
  71. req.headers['X-Auth-Token'] = token_id
  72. return self.application
  73.  
  74.  
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement