summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorTushar Patil <tushar.vitthal.patil@gmail.com>2011-08-19 15:04:25 -0700
committerTushar Patil <tushar.vitthal.patil@gmail.com>2011-08-19 15:04:25 -0700
commit94b3055ade678a80d88b5e8ee8c3491c7bce95a2 (patch)
treee6f58b10c2973310569860ced123cc4917bada6e /nova/api
parentd27a4d4a59a0103762ece2776ddd484629a72d54 (diff)
parent2837a9f8e81fd67b01971a4d8c308f79ab7e7d68 (diff)
Merged from trunk and fixed review comments
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/auth.py75
-rw-r--r--nova/api/ec2/__init__.py53
-rw-r--r--nova/api/ec2/metadatarequesthandler.py1
-rw-r--r--nova/api/openstack/contrib/createserverext.py4
-rw-r--r--nova/api/openstack/servers.py35
5 files changed, 141 insertions, 27 deletions
diff --git a/nova/api/auth.py b/nova/api/auth.py
new file mode 100644
index 000000000..cd3e3e8a0
--- /dev/null
+++ b/nova/api/auth.py
@@ -0,0 +1,75 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2011 OpenStack, LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+"""
+Common Auth Middleware.
+
+"""
+
+import webob.dec
+import webob.exc
+
+from nova import context
+from nova import flags
+from nova import wsgi
+
+
+FLAGS = flags.FLAGS
+flags.DEFINE_boolean('use_forwarded_for', False,
+ 'Treat X-Forwarded-For as the canonical remote address. '
+ 'Only enable this if you have a sanitizing proxy.')
+
+
+class InjectContext(wsgi.Middleware):
+ """Add a 'nova.context' to WSGI environ."""
+
+ def __init__(self, context, *args, **kwargs):
+ self.context = context
+ super(InjectContext, self).__init__(*args, **kwargs)
+
+ @webob.dec.wsgify(RequestClass=wsgi.Request)
+ def __call__(self, req):
+ req.environ['nova.context'] = self.context
+ return self.application
+
+
+class KeystoneContext(wsgi.Middleware):
+ """Make a request context from keystone headers"""
+
+ @webob.dec.wsgify(RequestClass=wsgi.Request)
+ def __call__(self, req):
+ try:
+ user_id = req.headers['X_USER']
+ except KeyError:
+ return webob.exc.HTTPUnauthorized()
+ # get the roles
+ roles = [r.strip() for r in req.headers.get('X_ROLE', '').split(',')]
+ project_id = req.headers['X_TENANT']
+ # Get the auth token
+ auth_token = req.headers.get('X_AUTH_TOKEN',
+ req.headers.get('X_STORAGE_TOKEN'))
+
+ # Build a context, including the auth_token...
+ remote_address = req.remote_addr
+ if FLAGS.use_forwarded_for:
+ remote_address = req.headers.get('X-Forwarded-For', remote_address)
+ ctx = context.RequestContext(user_id,
+ project_id,
+ roles=roles,
+ auth_token=auth_token,
+ remote_address=remote_address)
+
+ req.environ['nova.context'] = ctx
+ return self.application
diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py
index 96df97393..17969099d 100644
--- a/nova/api/ec2/__init__.py
+++ b/nova/api/ec2/__init__.py
@@ -20,6 +20,7 @@ Starting point for routing EC2 requests.
"""
+import httplib2
import webob
import webob.dec
import webob.exc
@@ -37,15 +38,16 @@ from nova.auth import manager
FLAGS = flags.FLAGS
LOG = logging.getLogger("nova.api")
-flags.DEFINE_boolean('use_forwarded_for', False,
- 'Treat X-Forwarded-For as the canonical remote address. '
- 'Only enable this if you have a sanitizing proxy.')
flags.DEFINE_integer('lockout_attempts', 5,
'Number of failed auths before lockout.')
flags.DEFINE_integer('lockout_minutes', 15,
'Number of minutes to lockout if triggered.')
flags.DEFINE_integer('lockout_window', 15,
'Number of minutes for lockout window.')
+flags.DEFINE_string('keystone_ec2_url',
+ 'http://localhost:5000/v2.0/ec2tokens',
+ 'URL to get token from ec2 request.')
+flags.DECLARE('use_forwarded_for', 'nova.api.auth')
class RequestLogging(wsgi.Middleware):
@@ -138,6 +140,49 @@ class Lockout(wsgi.Middleware):
return res
+class ToToken(wsgi.Middleware):
+ """Authenticate an EC2 request with keystone and convert to token."""
+
+ @webob.dec.wsgify(RequestClass=wsgi.Request)
+ def __call__(self, req):
+ # Read request signature and access id.
+ try:
+ signature = req.params['Signature']
+ access = req.params['AWSAccessKeyId']
+ except KeyError:
+ raise webob.exc.HTTPBadRequest()
+
+ # Make a copy of args for authentication and signature verification.
+ auth_params = dict(req.params)
+ # Not part of authentication args
+ auth_params.pop('Signature')
+
+ # Authenticate the request.
+ client = httplib2.Http()
+ creds = {'ec2Credentials': {'access': access,
+ 'signature': signature,
+ 'host': req.host,
+ 'verb': req.method,
+ 'path': req.path,
+ 'params': auth_params,
+ }}
+ headers = {'Content-Type': 'application/json'},
+ resp, content = client.request(FLAGS.keystone_ec2_url,
+ 'POST',
+ headers=headers,
+ body=utils.dumps(creds))
+ # NOTE(vish): We could save a call to keystone by
+ # having keystone return token, tenant,
+ # user, and roles from this call.
+ result = utils.loads(content)
+ # TODO(vish): check for errors
+ token_id = result['auth']['token']['id']
+
+ # Authenticated!
+ req.headers['X-Auth-Token'] = token_id
+ return self.application
+
+
class Authenticate(wsgi.Middleware):
"""Authenticate an EC2 request and add 'nova.context' to WSGI environ."""
@@ -147,7 +192,7 @@ class Authenticate(wsgi.Middleware):
try:
signature = req.params['Signature']
access = req.params['AWSAccessKeyId']
- except KeyError, e:
+ except KeyError:
raise webob.exc.HTTPBadRequest()
# Make a copy of args for authentication and signature verification.
diff --git a/nova/api/ec2/metadatarequesthandler.py b/nova/api/ec2/metadatarequesthandler.py
index 1dc275c90..0198bf490 100644
--- a/nova/api/ec2/metadatarequesthandler.py
+++ b/nova/api/ec2/metadatarequesthandler.py
@@ -30,6 +30,7 @@ from nova.api.ec2 import cloud
LOG = logging.getLogger('nova.api.ec2.metadata')
FLAGS = flags.FLAGS
+flags.DECLARE('use_forwarded_for', 'nova.api.auth')
class MetadataRequestHandler(wsgi.Application):
diff --git a/nova/api/openstack/contrib/createserverext.py b/nova/api/openstack/contrib/createserverext.py
index 27ff89abb..ba72fdb0b 100644
--- a/nova/api/openstack/contrib/createserverext.py
+++ b/nova/api/openstack/contrib/createserverext.py
@@ -45,10 +45,8 @@ class Createserverext(extensions.ExtensionDescriptor):
resources = []
headers_serializer = servers.HeadersSerializer()
- metadata = servers._get_metadata()
body_serializers = {
- 'application/xml': wsgi.XMLDictSerializer(metadata=metadata,
- xmlns=wsgi.XMLNS_V11),
+ 'application/xml': servers.ServerXMLSerializer(),
}
body_deserializers = {
diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py
index b814bc24f..41e63ec3c 100644
--- a/nova/api/openstack/servers.py
+++ b/nova/api/openstack/servers.py
@@ -936,7 +936,21 @@ def create_resource(version='1.0'):
'1.1': ControllerV11,
}[version]()
- metadata = _get_metadata()
+ metadata = {
+ "attributes": {
+ "server": ["id", "imageId", "name", "flavorId", "hostId",
+ "status", "progress", "adminPass", "flavorRef",
+ "imageRef"],
+ "link": ["rel", "type", "href"],
+ },
+ "dict_collections": {
+ "metadata": {"item_name": "meta", "item_key": "key"},
+ },
+ "list_collections": {
+ "public": {"item_name": "ip", "item_key": "addr"},
+ "private": {"item_name": "ip", "item_key": "addr"},
+ },
+ }
xmlns = {
'1.0': wsgi.XMLNS_V10,
@@ -969,25 +983,6 @@ def create_resource(version='1.0'):
return wsgi.Resource(controller, deserializer, serializer)
-def _get_metadata():
- metadata = {
- "attributes": {
- "server": ["id", "imageId", "name", "flavorId", "hostId",
- "status", "progress", "adminPass", "flavorRef",
- "imageRef"],
- "link": ["rel", "type", "href"],
- },
- "dict_collections": {
- "metadata": {"item_name": "meta", "item_key": "key"},
- },
- "list_collections": {
- "public": {"item_name": "ip", "item_key": "addr"},
- "private": {"item_name": "ip", "item_key": "addr"},
- },
- }
- return metadata
-
-
def remove_invalid_options(context, search_options, allowed_search_options):
"""Remove search options that are not valid for non-admin API/context"""
if FLAGS.allow_admin_api and context.is_admin: