Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 5296ee8

Browse files
author
dmp42
committed
Enhance config error reporting and fixes #44
1 parent 6c9b48e commit 5296ee8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

docker_registry/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from . import toolkit
1414
from .lib import config
1515

16+
from .lib.core.exceptions import ConfigError
17+
1618

1719
VERSION = '0.6.8'
1820
app = flask.Flask('docker-registry')
@@ -46,7 +48,7 @@ def after_request(response):
4648
def init():
4749
# Configure the secret key
4850
if not cfg.secret_key:
49-
raise RuntimeError('Config error: `secret_key\' is not set')
51+
raise ConfigError('`secret_key\' is not set')
5052
app.secret_key = cfg.secret_key
5153
# Configure the email exceptions
5254
info = cfg.email_exceptions

docker_registry/lib/config.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import rsa
44
import yaml
55

6+
from .core.exceptions import FileNotFoundError, ConfigError
7+
68

79
class Config(object):
810

@@ -56,15 +58,35 @@ def load():
5658
if not os.path.isabs(config_path):
5759
config_path = os.path.join(os.path.dirname(__file__), '../../',
5860
'config', config_path)
59-
with open(config_path) as f:
61+
try:
62+
f = open(config_path)
63+
except:
64+
raise FileNotFoundError(
65+
'Heads-up! File is missing: %s' % config_path)
66+
67+
try:
6068
data = yaml.load(f)
69+
except:
70+
raise ConfigError(
71+
'Config file (%s) is not valid yaml' % config_path)
72+
6173
config = data.get('common', {})
6274
flavor = os.environ.get('SETTINGS_FLAVOR', 'dev')
6375
config.update(data.get(flavor, {}))
6476
config['flavor'] = flavor
6577
config = convert_env_vars(config)
6678
if 'privileged_key' in config:
67-
with open(config['privileged_key']) as f:
79+
try:
80+
f = open(config['privileged_key'])
81+
except:
82+
raise FileNotFoundError(
83+
'Heads-up! File is missing: %s' % config['privileged_key'])
84+
85+
try:
6886
config['privileged_key'] = rsa.PublicKey.load_pkcs1(f.read())
87+
except:
88+
raise ConfigError(
89+
'Key at %s is not a valid RSA key' % config['privileged_key'])
90+
6991
_config = Config(config)
7092
return _config

0 commit comments

Comments
 (0)