| 1 |
# Copyright (C) 2015-2017 Custodia Project Contributors - see LICENSE file |
| 2 |
from __future__ import absolute_import |
| 3 |
|
| 4 |
import configparser |
| 5 |
import glob |
| 6 |
import os |
| 7 |
import socket |
| 8 |
from urllib.parse import quote as url_escape |
| 9 |
|
| 10 |
|
| 11 |
class CustodiaConfig: |
| 12 |
CONFIG_SPECIALS = ['authenticators', 'authorizers', 'consumers', 'stores'] |
| 13 |
|
| 14 |
DEFAULT_PATHS = [ |
| 15 |
('libdir', '/var/lib/custodia/{instance}'), |
| 16 |
('logdir', '/var/log/custodia/{instance}'), |
| 17 |
('rundir', '/var/run/custodia/{instance}'), |
| 18 |
('socketdir', '/var/run/custodia'), |
| 19 |
] |
| 20 |
|
| 21 |
def __init__(self, args): |
| 22 |
self.args = args |
| 23 |
self.config = {} |
| 24 |
self.defaults = None |
| 25 |
self.parser = None |
| 26 |
|
| 27 |
def get_defaults(self): |
| 28 |
configpath = self.args.configfile.name |
| 29 |
instance = self.args.instance |
| 30 |
defaults = { |
| 31 |
# Do not use getfqdn(). Internaly it calls gethostbyaddr which |
| 32 |
# might perform a DNS query. |
| 33 |
'hostname': socket.gethostname(), |
| 34 |
'configdir': os.path.dirname(configpath), |
| 35 |
'confdpattern': os.path.join(configpath + '.d', '*.conf'), |
| 36 |
'instance': instance if instance else '', |
| 37 |
} |
| 38 |
for name, path in self.DEFAULT_PATHS: |
| 39 |
defaults[name] = os.path.abspath(path.format(**defaults)) |
| 40 |
return defaults |
| 41 |
|
| 42 |
def create_parser(self): |
| 43 |
parser = configparser.ConfigParser( |
| 44 |
interpolation=configparser.ExtendedInterpolation(), |
| 45 |
defaults=self.defaults |
| 46 |
) |
| 47 |
parser.optionxform = str |
| 48 |
|
| 49 |
# add env |
| 50 |
parser.add_section(u'ENV') |
| 51 |
for k, v in os.environ.items(): |
| 52 |
if set(v).intersection('\r\n\x00'): |
| 53 |
continue |
| 54 |
parser.set(u'ENV', k, v.replace(u'$', u'$$')) |
| 55 |
|
| 56 |
# default globals |
| 57 |
parser.add_section(u'global') |
| 58 |
parser.set(u'global', u'auditlog', u'${logdir}/audit.log') |
| 59 |
parser.set(u'global', u'debug', u'false') |
| 60 |
parser.set(u'global', u'umask', u'027') |
| 61 |
parser.set(u'global', u'makedirs', u'false') |
| 62 |
|
| 63 |
return parser |
| 64 |
|
| 65 |
def read_configs(self): |
| 66 |
with self.args.configfile as f: |
| 67 |
self.parser.read_file(f) |
| 68 |
|
| 69 |
configfiles = [self.args.configfile.name] |
| 70 |
|
| 71 |
pattern = self.parser.get(u'DEFAULT', u'confdpattern') |
| 72 |
if pattern: |
| 73 |
confdfiles = glob.glob(pattern) |
| 74 |
confdfiles.sort() |
| 75 |
for confdfile in confdfiles: |
| 76 |
with open(confdfile) as f: |
| 77 |
self.parser.read_file(f) |
| 78 |
configfiles.append(confdfile) |
| 79 |
|
| 80 |
return configfiles |
| 81 |
|
| 82 |
def makedirs(self): |
| 83 |
for name, _path in self.DEFAULT_PATHS: |
| 84 |
path = self.parser.get(u'DEFAULT', name) |
| 85 |
parent = os.path.dirname(path) |
| 86 |
# create parents according to umask |
| 87 |
if not os.path.isdir(parent): |
| 88 |
os.makedirs(parent) |
| 89 |
# create final directory with restricted permissions |
| 90 |
if not os.path.isdir(path): |
| 91 |
os.mkdir(path, 0o700) |
| 92 |
|
| 93 |
def populate_config(self): |
| 94 |
config = self.config |
| 95 |
|
| 96 |
for s in self.CONFIG_SPECIALS: |
| 97 |
config[s] = {} |
| 98 |
|
| 99 |
for opt, val in self.parser.items(u'global'): |
| 100 |
if opt in self.CONFIG_SPECIALS: |
| 101 |
raise ValueError('"%s" is an invalid ' |
| 102 |
'[global] option' % opt) |
| 103 |
config[opt] = val |
| 104 |
|
| 105 |
config['tls_verify_client'] = self.parser.getboolean( |
| 106 |
'global', 'tls_verify_client', fallback=False) |
| 107 |
config['debug'] = self.parser.getboolean( |
| 108 |
'global', 'debug', fallback=False) |
| 109 |
config['makedirs'] = self.parser.getboolean( |
| 110 |
'global', 'makedirs', fallback=False) |
| 111 |
if self.args.debug: |
| 112 |
config['debug'] = self.args.debug |
| 113 |
|
| 114 |
config['auditlog'] = os.path.abspath(config.get('auditlog')) |
| 115 |
config['umask'] = int(config.get('umask', '027'), 8) |
| 116 |
|
| 117 |
url = config.get('server_url') |
| 118 |
sock = config.get('server_socket') |
| 119 |
|
| 120 |
if url and sock: |
| 121 |
raise ValueError( |
| 122 |
"'server_url' and 'server_socket' are mutually exclusive.") |
| 123 |
|
| 124 |
if not url and not sock: |
| 125 |
# no option but, use default socket path |
| 126 |
socketdir = self.parser.get(u'DEFAULT', u'socketdir') |
| 127 |
name = self.args.instance if self.args.instance else 'custodia' |
| 128 |
sock = os.path.join(socketdir, name + '.sock') |
| 129 |
|
| 130 |
if sock: |
| 131 |
server_socket = os.path.abspath(sock) |
| 132 |
config['server_url'] = 'http+unix://{}/'.format( |
| 133 |
url_escape(server_socket, '')) |
| 134 |
|
| 135 |
def __call__(self): |
| 136 |
self.defaults = self.get_defaults() |
| 137 |
self.parser = self.create_parser() |
| 138 |
self.config['configfiles'] = self.read_configs() |
| 139 |
self.populate_config() |
| 140 |
if self.config[u'makedirs']: |
| 141 |
self.makedirs() |
| 142 |
return self.parser, self.config |
| 143 |
|
| 144 |
|
| 145 |
def parse_config(args): |
| 146 |
ccfg = CustodiaConfig(args) |
| 147 |
return ccfg() |
| 148 |
|
| 149 |
|
| 150 |
def test(arglist): |
| 151 |
from pprint import pprint |
| 152 |
from .args import parse_args |
| 153 |
args = parse_args(arglist) |
| 154 |
parser, config = parse_config(args) |
| 155 |
pprint(parser.items("DEFAULT")) |
| 156 |
pprint(config) |
| 157 |
|
| 158 |
|
| 159 |
if __name__ == '__main__': |
| 160 |
test(['--instance=demo', './tests/empty.conf']) |