Skip to content

Commit 26982e1

Browse files
3vilWindhenriquebastos
authored andcommitted
Support for docker secrets
1 parent fe26dda commit 26982e1

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

decouple.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ def __getitem__(self, key):
146146
return self.data[key]
147147

148148

149+
class RepositorySecret(RepositoryEmpty):
150+
"""
151+
Retrieves option keys from files,
152+
where title of file is a key, content of file is a value
153+
e.g. Docker swarm secrets
154+
"""
155+
156+
def __init__(self, source='/run/secrets/'):
157+
self.data = {}
158+
159+
ls = os.listdir(source)
160+
for file in ls:
161+
with open(os.path.join(source, file), 'r') as f:
162+
self.data[file] = f.read()
163+
164+
def __contains__(self, key):
165+
return key in os.environ or key in self.data
166+
167+
def __getitem__(self, key):
168+
return self.data[key]
169+
170+
149171
class AutoConfig(object):
150172
"""
151173
Autodetects the config file and type.

tests/secrets/db_password

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world

tests/secrets/db_user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

tests/test_secrets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# coding: utf-8
2+
import os
3+
4+
from decouple import Config, RepositorySecret
5+
6+
7+
def test_secrets():
8+
path = os.path.join(os.path.dirname(__file__), 'secrets')
9+
config = Config(RepositorySecret(path))
10+
11+
assert 'hello' == config('db_user')
12+
assert 'world' == config('db_password')
13+
14+
15+
def test_env_undefined_but_present_in_os_environ():
16+
path = os.path.join(os.path.dirname(__file__), 'secrets')
17+
config = Config(RepositorySecret(path))
18+
19+
os.environ['KeyOnlyEnviron'] = ''
20+
assert '' == config('KeyOnlyEnviron')
21+
del os.environ['KeyOnlyEnviron']

0 commit comments

Comments
 (0)