summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Goetz <david.goetz@rackspace.com>2011-08-30 20:47:31 +0000
committerTarmac <>2011-08-30 20:47:31 +0000
commit418b57b729a74e2795fd7997c8be7a2c9965d925 (patch)
tree54453cb9846a0ba852fb2e09932a1b1035a3f28f
parentda3d53a1ef2941077a17334e3f0008b405cefcbf (diff)
parentbed607b07a6389fd12be11ba8543402124ddd4bb (diff)
downloadswift-418b57b729a74e2795fd7997c8be7a2c9965d925.tar.gz
Changes needed for SOS
-rw-r--r--swift/common/utils.py10
-rw-r--r--swift/common/wsgi.py30
-rw-r--r--test/unit/common/test_utils.py21
-rw-r--r--test/unit/common/test_wsgi.py14
4 files changed, 72 insertions, 3 deletions
diff --git a/swift/common/utils.py b/swift/common/utils.py
index 58e316719..1a9c74668 100644
--- a/swift/common/utils.py
+++ b/swift/common/utils.py
@@ -30,7 +30,8 @@ from contextlib import contextmanager
import ctypes
import ctypes.util
import struct
-from ConfigParser import ConfigParser, NoSectionError, NoOptionError
+from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \
+ RawConfigParser
from optparse import OptionParser
from tempfile import mkstemp
import cPickle as pickle
@@ -749,7 +750,7 @@ def cache_from_env(env):
return item_from_env(env, 'swift.cache')
-def readconf(conf, section_name=None, log_name=None, defaults=None):
+def readconf(conf, section_name=None, log_name=None, defaults=None, raw=False):
"""
Read config file and return config items as a dict
@@ -763,7 +764,10 @@ def readconf(conf, section_name=None, log_name=None, defaults=None):
"""
if defaults is None:
defaults = {}
- c = ConfigParser(defaults)
+ if raw:
+ c = RawConfigParser(defaults)
+ else:
+ c = ConfigParser(defaults)
if hasattr(conf, 'readline'):
c.readfp(conf)
else:
diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py
index 275a94cf6..dcf034045 100644
--- a/swift/common/wsgi.py
+++ b/swift/common/wsgi.py
@@ -26,6 +26,7 @@ import eventlet
from eventlet import greenio, GreenPool, sleep, wsgi, listen
from paste.deploy import loadapp, appconfig
from eventlet.green import socket, ssl
+from webob import Request
from swift.common.utils import get_logger, drop_privileges, \
validate_configuration, capture_stdio, NullLogger
@@ -188,3 +189,32 @@ def run_wsgi(conf_file, app_section, *args, **kwargs):
greenio.shutdown_safe(sock)
sock.close()
logger.notice('Exited')
+
+
+def make_pre_authed_request(env, method, path, body=None, headers=None,
+ agent='Swift'):
+ """
+ Makes a new webob.Request based on the current env but with the
+ parameters specified. Note that this request will be preauthorized.
+
+ :param env: Current WSGI environment dictionary
+ :param method: HTTP method of new request
+ :param path: HTTP path of new request
+ :param body: HTTP body of new request; None by default
+ :param headers: Extra HTTP headers of new request; None by default
+
+ :returns: webob.Request object
+ (Stolen from Swauth: https://github.com/gholt/swauth)
+ """
+ newenv = {'REQUEST_METHOD': method, 'HTTP_USER_AGENT': agent}
+ for name in ('swift.cache', 'HTTP_X_TRANS_ID'):
+ if name in env:
+ newenv[name] = env[name]
+ newenv['swift.authorize'] = lambda req: None
+ if not headers:
+ headers = {}
+ if body:
+ return Request.blank(path, environ=newenv, body=body,
+ headers=headers)
+ else:
+ return Request.blank(path, environ=newenv, headers=headers)
diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py
index 05707158a..356e9ce2d 100644
--- a/test/unit/common/test_utils.py
+++ b/test/unit/common/test_utils.py
@@ -536,6 +536,27 @@ log_name = yarr'''
os.unlink('/tmp/test')
self.assertRaises(SystemExit, utils.readconf, '/tmp/test')
+ def test_readconf_raw(self):
+ conf = '''[section1]
+foo = bar
+
+[section2]
+log_name = %(yarr)s'''
+ # setup a real file
+ with open('/tmp/test', 'wb') as f:
+ f.write(conf)
+ make_filename = lambda: '/tmp/test'
+ # setup a file stream
+ make_fp = lambda: StringIO(conf)
+ for conf_object_maker in (make_filename, make_fp):
+ result = utils.readconf(conf_object_maker(), raw=True)
+ expected = {'log_name': None,
+ 'section1': {'foo': 'bar'},
+ 'section2': {'log_name': '%(yarr)s'}}
+ self.assertEquals(result, expected)
+ os.unlink('/tmp/test')
+ self.assertRaises(SystemExit, utils.readconf, '/tmp/test')
+
def test_drop_privileges(self):
user = getuser()
# over-ride os with mock
diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py
index 2df6936a8..b7496e796 100644
--- a/test/unit/common/test_wsgi.py
+++ b/test/unit/common/test_wsgi.py
@@ -28,6 +28,7 @@ from StringIO import StringIO
from collections import defaultdict
from eventlet import sleep
+from webob import Request
from swift.common import wsgi
@@ -173,6 +174,19 @@ class TestWSGI(unittest.TestCase):
wsgi.sleep = old_sleep
wsgi.time = old_time
+ def test_pre_auth_req(self):
+ class FakeReq(object):
+ @classmethod
+ def fake_blank(cls, path, environ={}, body='', headers={}):
+ self.assertEquals(environ['swift.authorize']('test'), None)
+ self.assertEquals(environ['HTTP_X_TRANS_ID'], '1234')
+ was_blank = Request.blank
+ Request.blank = FakeReq.fake_blank
+ wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
+ 'PUT', '/', body='tester', headers={})
+ wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
+ 'PUT', '/', headers={})
+ Request.blank = was_blank
if __name__ == '__main__':
unittest.main()