Sync with oslo-incubator 569979adf

This syncs keystonemiddleware with oslo-incubator commit hash
569979adfd4237869bb50c7aaa02bc3fd4f0b413

First, remove the existing code to cleanup:
    $ rm -r keystonemiddleware/openstack/*

Then, sync from oslo-incubator:
    $ python update.py ../keystonemiddleware

Commits since last sync (55c5629):
----------------------------------
3d90045 Backport code for i18n to check lazy at runtime
ef37e03 Added missing jsonutils.dump() function
cb5a804 Move `mask_password` to strutils
de4adbc pep8: fixed multiple violations

Change-Id: If4d060be886ff0c3585b50534d35c87271491b47
This commit is contained in:
Brant Knudson 2014-07-01 16:01:15 -05:00
parent ff0ca09e0c
commit d5121fcb08
3 changed files with 84 additions and 43 deletions

View File

@ -23,7 +23,6 @@ Usual usage in an openstack.common module:
""" """
import copy import copy
import functools
import gettext import gettext
import locale import locale
from logging import handlers from logging import handlers
@ -42,7 +41,7 @@ class TranslatorFactory(object):
"""Create translator functions """Create translator functions
""" """
def __init__(self, domain, lazy=False, localedir=None): def __init__(self, domain, localedir=None):
"""Establish a set of translation functions for the domain. """Establish a set of translation functions for the domain.
:param domain: Name of translation domain, :param domain: Name of translation domain,
@ -55,7 +54,6 @@ class TranslatorFactory(object):
:type localedir: str :type localedir: str
""" """
self.domain = domain self.domain = domain
self.lazy = lazy
if localedir is None: if localedir is None:
localedir = os.environ.get(domain.upper() + '_LOCALEDIR') localedir = os.environ.get(domain.upper() + '_LOCALEDIR')
self.localedir = localedir self.localedir = localedir
@ -75,16 +73,19 @@ class TranslatorFactory(object):
""" """
if domain is None: if domain is None:
domain = self.domain domain = self.domain
if self.lazy: t = gettext.translation(domain,
return functools.partial(Message, domain=domain) localedir=self.localedir,
t = gettext.translation( fallback=True)
domain, # Use the appropriate method of the translation object based
localedir=self.localedir, # on the python version.
fallback=True, m = t.gettext if six.PY3 else t.ugettext
)
if six.PY3: def f(msg):
return t.gettext """oslo.i18n.gettextutils translation function."""
return t.ugettext if USE_LAZY:
return Message(msg, domain=domain)
return m(msg)
return f
@property @property
def primary(self): def primary(self):
@ -147,19 +148,11 @@ def enable_lazy():
your project is importing _ directly instead of using the your project is importing _ directly instead of using the
gettextutils.install() way of importing the _ function. gettextutils.install() way of importing the _ function.
""" """
# FIXME(dhellmann): This function will be removed in oslo.i18n, global USE_LAZY
# because the TranslatorFactory makes it superfluous.
global _, _LI, _LW, _LE, _LC, USE_LAZY
tf = TranslatorFactory('keystonemiddleware', lazy=True)
_ = tf.primary
_LI = tf.log_info
_LW = tf.log_warning
_LE = tf.log_error
_LC = tf.log_critical
USE_LAZY = True USE_LAZY = True
def install(domain, lazy=False): def install(domain):
"""Install a _() function using the given translation domain. """Install a _() function using the given translation domain.
Given a translation domain, install a _() function using gettext's Given a translation domain, install a _() function using gettext's
@ -170,26 +163,14 @@ def install(domain, lazy=False):
a translation-domain-specific environment variable (e.g. a translation-domain-specific environment variable (e.g.
NOVA_LOCALEDIR). NOVA_LOCALEDIR).
Note that to enable lazy translation, enable_lazy must be
called.
:param domain: the translation domain :param domain: the translation domain
:param lazy: indicates whether or not to install the lazy _() function.
The lazy _() introduces a way to do deferred translation
of messages by installing a _ that builds Message objects,
instead of strings, which can then be lazily translated into
any available locale.
""" """
if lazy: from six import moves
from six import moves tf = TranslatorFactory(domain)
tf = TranslatorFactory(domain, lazy=True) moves.builtins.__dict__['_'] = tf.primary
moves.builtins.__dict__['_'] = tf.primary
else:
localedir = '%s_LOCALEDIR' % domain.upper()
if six.PY3:
gettext.install(domain,
localedir=os.environ.get(localedir))
else:
gettext.install(domain,
localedir=os.environ.get(localedir),
unicode=True)
class Message(six.text_type): class Message(six.text_type):
@ -373,8 +354,8 @@ def get_available_languages(domain):
'zh_Hant_HK': 'zh_HK', 'zh_Hant_HK': 'zh_HK',
'zh_Hant': 'zh_TW', 'zh_Hant': 'zh_TW',
'fil': 'tl_PH'} 'fil': 'tl_PH'}
for (locale, alias) in six.iteritems(aliases): for (locale_, alias) in six.iteritems(aliases):
if locale in language_list and alias not in language_list: if locale_ in language_list and alias not in language_list:
language_list.append(alias) language_list.append(alias)
_AVAILABLE_LANGUAGES[domain] = language_list _AVAILABLE_LANGUAGES[domain] = language_list

View File

@ -168,6 +168,10 @@ def dumps(value, default=to_primitive, **kwargs):
return json.dumps(value, default=default, **kwargs) return json.dumps(value, default=default, **kwargs)
def dump(obj, fp, *args, **kwargs):
return json.dump(obj, fp, *args, **kwargs)
def loads(s, encoding='utf-8', **kwargs): def loads(s, encoding='utf-8', **kwargs):
return json.loads(strutils.safe_decode(s, encoding), **kwargs) return json.loads(strutils.safe_decode(s, encoding), **kwargs)

View File

@ -50,6 +50,28 @@ SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]")
SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+") SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
# NOTE(flaper87): The following 3 globals are used by `mask_password`
_SANITIZE_KEYS = ['adminPass', 'admin_pass', 'password', 'admin_password']
# NOTE(ldbragst): Let's build a list of regex objects using the list of
# _SANITIZE_KEYS we already have. This way, we only have to add the new key
# to the list of _SANITIZE_KEYS and we can generate regular expressions
# for XML and JSON automatically.
_SANITIZE_PATTERNS = []
_FORMAT_PATTERNS = [r'(%(key)s\s*[=]\s*[\"\']).*?([\"\'])',
r'(<%(key)s>).*?(</%(key)s>)',
r'([\"\']%(key)s[\"\']\s*:\s*[\"\']).*?([\"\'])',
r'([\'"].*?%(key)s[\'"]\s*:\s*u?[\'"]).*?([\'"])',
r'([\'"].*?%(key)s[\'"]\s*,\s*\'--?[A-z]+\'\s*,\s*u?[\'"])'
'.*?([\'"])',
r'(%(key)s\s*--?[A-z]+\s*)\S+(\s*)']
for key in _SANITIZE_KEYS:
for pattern in _FORMAT_PATTERNS:
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL)
_SANITIZE_PATTERNS.append(reg_ex)
def int_from_bool_as_string(subject): def int_from_bool_as_string(subject):
"""Interpret a string as a boolean and return either 1 or 0. """Interpret a string as a boolean and return either 1 or 0.
@ -237,3 +259,37 @@ def to_slug(value, incoming=None, errors="strict"):
"ascii", "ignore").decode("ascii") "ascii", "ignore").decode("ascii")
value = SLUGIFY_STRIP_RE.sub("", value).strip().lower() value = SLUGIFY_STRIP_RE.sub("", value).strip().lower()
return SLUGIFY_HYPHENATE_RE.sub("-", value) return SLUGIFY_HYPHENATE_RE.sub("-", value)
def mask_password(message, secret="***"):
"""Replace password with 'secret' in message.
:param message: The string which includes security information.
:param secret: value with which to replace passwords.
:returns: The unicode value of message with the password fields masked.
For example:
>>> mask_password("'adminPass' : 'aaaaa'")
"'adminPass' : '***'"
>>> mask_password("'admin_pass' : 'aaaaa'")
"'admin_pass' : '***'"
>>> mask_password('"password" : "aaaaa"')
'"password" : "***"'
>>> mask_password("'original_password' : 'aaaaa'")
"'original_password' : '***'"
>>> mask_password("u'original_password' : u'aaaaa'")
"u'original_password' : u'***'"
"""
message = six.text_type(message)
# NOTE(ldbragst): Check to see if anything in message contains any key
# specified in _SANITIZE_KEYS, if not then just return the message since
# we don't have to mask any passwords.
if not any(key in message for key in _SANITIZE_KEYS):
return message
secret = r'\g<1>' + secret + r'\g<2>'
for pattern in _SANITIZE_PATTERNS:
message = re.sub(pattern, secret, message)
return message