Permalink
Browse files

utils.py: Fixed a bug where older versions of cached files were not b…

…eing cleaned up properly.

utils.py:  Fixed a bug where get_settings() would run into troubles with strings containing two forward slashes (//).
auth.py:  Fixed a bug where in certain (really obscure) situations applicable_policies() could return incorrect results due to same-name attributes far down the tree.
gateone.py:  Added some error checking when reading in settings on startup.
  • Loading branch information...
liftoff committed Apr 5, 2013
1 parent 8bc6d7e commit 47816484b039289add85d35fa83f894d54deafd5
Showing with 35 additions and 9 deletions.
  1. +1 −1 gateone/auth.py
  2. +6 −2 gateone/gateone.py
  3. +28 −6 gateone/utils.py
View
@@ -102,7 +102,7 @@ def applicable_policies(application, user, policies):
""" """
# Start with the default policy # Start with the default policy
try: try:
policy = RUDict(policies['*'][application]) policy = RUDict(policies['*'][application].copy())
except KeyError: except KeyError:
# No default policy--not good but not mandatory # No default policy--not good but not mandatory
policy = RUDict() policy = RUDict()
View
@@ -305,7 +305,7 @@
from auth import NullAuthHandler, KerberosAuthHandler, GoogleAuthHandler from auth import NullAuthHandler, KerberosAuthHandler, GoogleAuthHandler
from auth import APIAuthHandler, SSLAuthHandler, PAMAuthHandler from auth import APIAuthHandler, SSLAuthHandler, PAMAuthHandler
from auth import require, authenticated, policies, applicable_policies from auth import require, authenticated, policies, applicable_policies
from utils import generate_session_id, mkdir_p from utils import generate_session_id, mkdir_p, SettingsError
from utils import gen_self_signed_ssl, killall, get_plugins, load_modules from utils import gen_self_signed_ssl, killall, get_plugins, load_modules
from utils import merge_handlers, none_fix, convert_to_timedelta, short_hash from utils import merge_handlers, none_fix, convert_to_timedelta, short_hash
from utils import FACILITIES, json_encode, recursive_chown, ChownError from utils import FACILITIES, json_encode, recursive_chown, ChownError
@@ -2813,7 +2813,11 @@ def main():
logging.error(_( logging.error(_(
"Could not find/create settings directory at %s" % settings_dir)) "Could not find/create settings directory at %s" % settings_dir))
sys.exit(1) sys.exit(1)
all_settings = get_settings(settings_dir) try:
all_settings = get_settings(settings_dir)
except SettingsError as e:
# The error will be logged to stdout inside all_settings
sys.exit(2)
enabled_plugins = [] enabled_plugins = []
enabled_applications = [] enabled_applications = []
go_settings = {} go_settings = {}
View
@@ -149,6 +149,12 @@ class ChownError(Exception):
""" """
pass pass
class SettingsError(Exception):
"""
Raised when we encounter an error parsing .conf files in the settings dir.
"""
pass
class RUDict(dict): class RUDict(dict):
""" """
A dict that will recursively update keys and values in a safe manner so that A dict that will recursively update keys and values in a safe manner so that
@@ -202,6 +208,25 @@ def noop(*args, **kwargs):
"""Do nothing (i.e. "No Operation")""" """Do nothing (i.e. "No Operation")"""
pass pass
# The following was taken from:
# http://stackoverflow.com/questions/241327/python-snippet-to-remove-c-and-c-comments
# Thank you Markus Jarderot!
def remove_comments(text):
"""
Removes C-style comments from *text* and returns the result.
"""
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return ""
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
def get_settings(path, add_default=True): def get_settings(path, add_default=True):
""" """
Reads any and all *.conf files containing JSON (JS-style comments are OK) Reads any and all *.conf files containing JSON (JS-style comments are OK)
@@ -212,10 +237,6 @@ def get_settings(path, add_default=True):
which indicates "all users". This behavior can be skipped by setting the which indicates "all users". This behavior can be skipped by setting the
*add_default* keyword argument to `False`. *add_default* keyword argument to `False`.
""" """
re_comment = re.compile( # This removes JavaScript-style comments
r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
re.DOTALL | re.MULTILINE
)
settings = RUDict() settings = RUDict()
if add_default: if add_default:
settings['*'] = {} settings['*'] = {}
@@ -236,7 +257,7 @@ def get_settings(path, add_default=True):
filepath = path filepath = path
with io.open(filepath, encoding='utf-8') as f: with io.open(filepath, encoding='utf-8') as f:
# Remove comments # Remove comments
proper_json = re_comment.sub('', f.read()) proper_json = remove_comments(f.read())
# Remove blank/empty lines # Remove blank/empty lines
proper_json = os.linesep.join([ proper_json = os.linesep.join([
s for s in proper_json.splitlines() if s.strip()]) s for s in proper_json.splitlines() if s.strip()])
@@ -263,6 +284,7 @@ def get_settings(path, add_default=True):
break break
else: else:
print(line) print(line)
raise SettingsError()
except (ValueError, IndexError): except (ValueError, IndexError):
print(_( print(_(
"Got an exception trying to display precisely where " "Got an exception trying to display precisely where "
@@ -1458,7 +1480,7 @@ def get_or_cache(cache_dir, path, minify=True):
for fname in os.listdir(cache_dir): for fname in os.listdir(cache_dir):
if fname == cached_filename: if fname == cached_filename:
continue continue
elif fname.split(':', 1)[0] == path.replace('/', '_'): elif fname.startswith(shortened_path):
# Older version present. Remove it. # Older version present. Remove it.
os.remove(os.path.join(cache_dir, fname)) os.remove(os.path.join(cache_dir, fname))
return data return data

0 comments on commit 4781648

Please sign in to comment.