Skip to content

Commit

Permalink
Support proxying api by using X-Forwarded-For
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvananda committed Dec 19, 2010
1 parent 800ecbd commit ca10179
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion nova/api/ec2/__init__.py
Expand Up @@ -37,6 +37,9 @@


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.')
_log = logging.getLogger("api")
_log.setLevel(logging.DEBUG)

Expand Down Expand Up @@ -81,9 +84,12 @@ def __call__(self, req):
raise webob.exc.HTTPForbidden()

# Authenticated!
remote_address = req.remote_addr
if FLAGS.use_forwarded_for:
remote_address = req.headers.get('X-Forwarded-For', remote_address)
ctxt = context.RequestContext(user=user,
project=project,
remote_address=req.remote_addr)
remote_address=remote_address)
req.environ['ec2.context'] = ctxt
return self.application

Expand Down
11 changes: 9 additions & 2 deletions nova/api/ec2/metadatarequesthandler.py
Expand Up @@ -23,9 +23,13 @@
import webob.dec
import webob.exc

from nova import flags
from nova.api.ec2 import cloud


FLAGS = flags.FLAGS


class MetadataRequestHandler(object):
"""Serve metadata from the EC2 API."""

Expand Down Expand Up @@ -63,10 +67,13 @@ def lookup(self, path, data):
@webob.dec.wsgify
def __call__(self, req):
cc = cloud.CloudController()
meta_data = cc.get_metadata(req.remote_addr)
remote_address = req.remote_addr
if FLAGS.use_forwarded_for:
remote_address = req.headers.get('X-Forwarded-For', remote_address)
meta_data = cc.get_metadata(remote_address)
if meta_data is None:
logging.error('Failed to get metadata for ip: %s' %
req.remote_addr)
remote_address)
raise webob.exc.HTTPNotFound()
data = self.lookup(req.path_info, meta_data)
if data is None:
Expand Down

0 comments on commit ca10179

Please sign in to comment.