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

Commit 9b3dacf

Browse files
author
Mangled Deutz
committed
Config update + privileges drop (fix #440)
Docker-DCO-1.1-Signed-off-by: Mangled Deutz <[email protected]> (github: dmp42)
1 parent 884b418 commit 9b3dacf

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

docker_registry/run.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from argparse import ArgumentParser # noqa
1010
from argparse import RawTextHelpFormatter # noqa
1111
import distutils.spawn
12+
import getpass
13+
import logging
1214
import os
1315
import sys
1416

@@ -21,18 +23,22 @@
2123
from .search import * # noqa
2224

2325
cfg = config.load()
24-
if cfg.standalone is True:
25-
# If standalone mode is enabled (default), load the fake Index routes
26+
if cfg.standalone:
27+
# If standalone mode is enabled, load the fake Index routes
2628
from .index import * # noqa
2729

2830

31+
logger = logging.getLogger(__name__)
32+
2933
DESCRIPTION = """run the docker-registry with gunicorn, honoring the following
3034
environment variables:
31-
35+
REGISTRY_HOST: TCP host or ip to bind to; default is 0.0.0.0
36+
REGISTRY_PORT: TCP port to bind to; default is 5000
3237
GUNICORN_WORKERS: number of worker processes gunicorn should start
33-
REGISTRY_PORT: TCP port to bind to on all ipv4 addresses; default is 5000
3438
GUNICORN_GRACEFUL_TIMEOUT: timeout in seconds for graceful worker restart
3539
GUNiCORN_SILENT_TIMEOUT: timeout in seconds for restarting silent workers
40+
GUNiCORN_USER: unix user to downgrade priviledges to
41+
GUNiCORN_GROUP: unix group to downgrade priviledges to
3642
"""
3743

3844

@@ -48,20 +54,45 @@ def run_gunicorn():
4854
formatter_class=RawTextHelpFormatter)
4955
parser.parse_args()
5056

51-
workers = str(env.source('GUNICORN_WORKERS'))
52-
host = env.source('REGISTRY_HOST')
53-
port = env.source('REGISTRY_PORT')
54-
graceful_timeout = str(env.source('GUNICORN_GRACEFUL_TIMEOUT'))
55-
silent_timeout = str(env.source('GUNICORN_SILENT_TIMEOUT'))
56-
57-
address = '%s:%s' % (host, port)
58-
5957
gunicorn_path = distutils.spawn.find_executable('gunicorn')
60-
if gunicorn_path is None:
58+
if not gunicorn_path:
6159
print('error: gunicorn executable not found', file=sys.stderr)
6260
sys.exit(1)
6361

64-
os.execl(gunicorn_path, 'gunicorn', '--access-logfile', '-', '--debug',
65-
'--max-requests', '100', '--graceful-timeout', graceful_timeout,
66-
'-t', silent_timeout, '-k', 'gevent', '-b', address,
67-
'-w', workers, 'docker_registry.wsgi:application')
62+
address = '%s:%s' % (
63+
env.source('REGISTRY_HOST'),
64+
env.source('REGISTRY_PORT')
65+
)
66+
67+
args = [
68+
gunicorn_path, 'gunicorn',
69+
'--access-logfile', '-', '--debug',
70+
'--max-requests', '100',
71+
'-k', 'gevent',
72+
'--graceful-timeout', env.source('GUNICORN_GRACEFUL_TIMEOUT'),
73+
'-t', env.source('GUNICORN_SILENT_TIMEOUT'),
74+
'-w', env.source('GUNICORN_WORKERS'),
75+
'-b', address,
76+
'docker_registry.wsgi:application'
77+
]
78+
79+
user = env.source('GUNICORN_USER')
80+
group = env.source('GUNICORN_GROUP')
81+
if user or group:
82+
if getpass.getuser() == 'root':
83+
if user:
84+
logger.info('Downgrading privs to user %s' % user)
85+
args.append('-u')
86+
args.append(user)
87+
88+
if group:
89+
logger.info('Downgrading privs to group %s' % user)
90+
args.append('-g')
91+
args.append(group)
92+
else:
93+
logger.warn('You asked we drop priviledges, but we are not root!')
94+
95+
# Stringify all args
96+
for (k, v) in enumerate(args):
97+
args[k] = str(v)
98+
os.execl(*args)

docker_registry/server/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616

1717

18-
def source(key, override='None'):
18+
def source(key, override=''):
1919
# Using yaml gives us proper typage
2020
return yaml.load(
2121
os.environ.get(key, _DEFAULT[key] if key in _DEFAULT else override))

0 commit comments

Comments
 (0)