aboutsummaryrefslogtreecommitdiff
path: root/secretmgr
blob: 15070088e03def4c6925be0d9989cef977a73b28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages(ps: [ ps.pip ps.consul ps.ldap ps.passlib ps.requests ps.six ps.toml ])"

# DEPENDENCY: python-consul
import consul

# DEPENDENCY: python-ldap
import ldap

# DEPENDENCY: passlib
from passlib.hash import ldap_salted_sha1

# DEPENDENCY: toml
import toml

import os
import sys
import glob
import subprocess
import getpass
import base64
from secrets import token_bytes

"""
This is a utility to handle secrets in the Consul database
for the various components of the Deuxfleurs infrastructure

Functionnalities:
- check that secrets are correctly configured
- help user fill in secrets
- create LDAP service users and fill in corresponding secrets
- TODO: manage Garage buckets and access keys
- maybe one day: manage SSL certificates and keys
"""

# ---- UTIL ----

consul_server = consul.Consul()

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


# ---- SECRETS ----

class Secret:
    def __init__(self, key, config, description=None):
        self.config = config
        self.key = key
        if description != None:
            self.description = description
        else:
            self.description = None

    def check(self, value):
        return True

    def generate(self):
        pass

    def rotate(self):
        return None

    def print_info(self):
        print("Secret:         {}".format(self.key))
        print("Type:           {}".format(self.__class__.TYPE))
        if self.description != None:
            print("Description:    {}".format(self.description))

class UserSecret(Secret):
    TYPE = "user-entered secret"

    def __init__(self, example=None, multiline=False, **kwargs):
        Secret.__init__(self, **kwargs)
        self.example = example
        self.multiline = multiline

    def print_info(self):
        Secret.print_info(self)
        if self.key in self.config.user_values:
            print("Cluster value:  {}".format(self.config.user_values[self.key]))
        elif self.example != None:
            print("Example:        {}".format(self.example))

    def generate(self):
        if self.key in self.config.user_values:
            print("Using constant value from cluster's secretmgr.toml")
            return self.config.user_values[self.key]

        print("Enter value for secret, or ^C to skip:")
        if self.multiline:
            print("THIS IS A LONG VALUE, ENTER SEVERAL LINES AND FINISH WITH A LINE CONTAINING A SINGLE .")
            try:
                lines = []
                while True:
                    line = input().strip()
                    if line == ".":
                        break
                    lines.append(line)
                return "\n".join(lines)
            except KeyboardInterrupt:
                return None
        else:
            try:
                while True:
                    line = input().strip()
                    if line != "":
                        return line
                    else:
                        print("Please enter a non-empty value, or ^C to skip:")
            except KeyboardInterrupt:
                return None

class CommandSecret(Secret):
    TYPE = "command"

    def __init__(self, command, rotate=False, **kwargs):
        Secret.__init__(self, **kwargs)
        self.command = command
        self.rotate_value = rotate

    def print_info(self):
        Secret.print_info(self)
        print("Command:        {}".format(self.command))
        if self.rotate_value:
            print("Rotate:         True")

    def generate(self):
        print("Executing command:", self.command)
        return subprocess.check_output(["sh", "-c", self.command])

    def rotate(self):
        if self.rotate_value:
            return self.generate()
        else:
            return None

class ConstantSecret(Secret):
    TYPE = "constant value"

    def __init__(self, value, **kwargs):
        Secret.__init__(self, **kwargs)
        self.value = value

    def print_info(self):
        Secret.print_info(self)
        print("Value:          {}".format(self.value))

    def check(self, value):
        return value == self.value

    def generate(self):
        return self.value


# ---- SERVICE USERS ----

class ServiceUserPasswordSecret(Secret):
    TYPE = "service user's password"

    def __init__(self, service_user, **kwargs):
        Secret.__init__(self, **kwargs)
        self.service_user = service_user

    def print_info(self):
        Secret.print_info(self)
        print("Service user:   {}".format(self.service_user.username))

    def check(self, value):
        l = ldap.initialize(self.config.ldap_server)
        try:
            l.simple_bind_s(self.service_user.dn, value)
            return True
        except Exception as e:
            return False

    def generate(self):
        return self.service_user.password

    def rotate(self):
        return self.service_user.password

class ServiceUserNameSecret(Secret):
    TYPE = "service user's username (constant value)"

    def __init__(self, service_user, **kwargs):
        Secret.__init__(self, **kwargs)
        self.service_user = service_user

    def print_info(self):
        Secret.print_info(self)
        print("Value:          {}".format(self.service_user.username))

    def check(self, value):
        return value == self.service_user.username

    def generate(self):
        return self.service_user.username

class ServiceUserDNSecret(Secret):
    TYPE = "service user's DN (constant value)"

    def __init__(self, service_user, **kwargs):
        Secret.__init__(self, **kwargs)
        self.service_user = service_user

    def print_info(self):
        Secret.print_info(self)
        print("Service user:   {}".format(self.service_user.username))
        print("Value:          {}".format(self.service_user.dn))

    def check(self, value):
        return value == self.service_user.dn

    def generate(self):
        return self.service_user.dn

class ServiceUser:
    def __init__(self, username, password_secret, config, description=None, dn_secret=None, username_secret=None, rotate_password=False):
        self.config = config
        self.username = username
        self.description = description
        self.password = None
        self.dn = "cn={},{}".format(self.username, self.config.ldap_service_dn_suffix)
        self.rotate_password = rotate_password

        self.password_secret = ServiceUserPasswordSecret(config=config, service_user=self, key=password_secret)

        self.username_secret = None
        if username_secret != None:
            self.username_secret = ServiceUserNameSecret(config=config, service_user=self, key=username_secret)

        self.dn_secret = None
        if dn_secret != None:
            self.dn_secret = ServiceUserDNSecret(config=config, service_user=self, key=dn_secret)

    def secrets(self):
        secrets = {}
        secrets[self.password_secret.key] = self.password_secret
        if self.dn_secret != None:
            secrets[self.dn_secret.key] = self.dn_secret
        if self.username_secret != None:
            secrets[self.username_secret.key] = self.username_secret
        return secrets

    def configure(self, rotate):
        self.password = self.config.get_secret(self.password_secret.key)
        if self.password is None:
            good = False
        else:
            l = ldap.initialize(self.config.ldap_server)
            try:
                l.simple_bind_s(self.dn, self.password)
                good = True
            except:
                good = False

        if not good or (rotate and self.rotate_password):
            # Reset passsword
            self.password = base64.urlsafe_b64encode(token_bytes(12)).decode('ascii')
            pass_crypt = ldap_salted_sha1.hash(self.password).encode('ascii')

            l = self.config.get_ldap_admin_conn()
            res = l.search_s(self.dn, ldap.SCOPE_BASE, "objectclass=*")
            if res is None or len(res) == 0:
                print(bcolors.OKCYAN, "Creating entity", self.dn, bcolors.ENDC)
                if self.config.dry_run:
                    print(bcolors.OKBLUE, "Dry run, skipping. Add --do to actually do something.", bcolors.ENDC)
                    return
                l.add_s(self.dn,
                        [
                            ("objectclass",     [b"person", b"top"]),
                            ("displayname",     [self.description.encode('ascii')]),
                            ("userpassword",     [pass_crypt]),
                        ])
            else:
                print(bcolors.OKCYAN, "Resetting password for entity", self.dn, bcolors.ENDC)
                if self.config.dry_run:
                    print(bcolors.OKBLUE, "Dry run, skipping. Add --do to actually do something.", bcolors.ENDC)
                    return
                l.modify_s(self.dn,
                        [
                            (ldap.MOD_REPLACE, "userpassword", [pass_crypt])
                        ])
        else:
            print(bcolors.OKGREEN, "Entity is good: ", self.dn, bcolors.ENDC)


# ---- MAIN CONFIG CLASS ----

class Config:
    def __init__(self, cluster_name, dry_run):
        self.cluster_name = cluster_name
        self.app_path = os.path.join(".", "cluster", cluster_name, "app")

        self.service_users = {}
        self.secrets = {}
        self.modules = []
        self.dry_run = dry_run

        # Load config from secretmgr.toml in cluster directory
        secretmgr_toml_path = os.path.join(".", "cluster", cluster_name, "secretmgr.toml")
        if os.path.exists(secretmgr_toml_path):
            with open(secretmgr_toml_path) as f:
                secretmgr_toml = toml.load(f)
        else:
            secretmgr_toml = {}

        if "user_values" in secretmgr_toml:
            self.user_values = secretmgr_toml["user_values"]
        else:
            self.user_values = {}

        self.ldap_server = None
        self.ldap_service_dn_suffix = None
        self.ldap_admin_dn = None
        self.ldap_admin_password_secret = None
        self.ldap_admin_conn = None
        if "ldap" in secretmgr_toml:
            if "server" in secretmgr_toml["ldap"]:
                self.ldap_server = secretmgr_toml["ldap"]["server"]
            if "service_dn_suffix" in secretmgr_toml["ldap"]:
                self.ldap_service_dn_suffix = secretmgr_toml["ldap"]["service_dn_suffix"]
            if "admin_dn" in secretmgr_toml["ldap"]:
                self.ldap_admin_dn = secretmgr_toml["ldap"]["admin_dn"]
            if "admin_password_secret" in secretmgr_toml["ldap"]:
                self.ldap_admin_password_secret = secretmgr_toml["ldap"]["admin_password_secret"]

    def load_module(self, module_name):
        secrets_toml_path = os.path.join(self.app_path, module_name, "secrets.toml")

        with open(secrets_toml_path) as f:
            secrets_toml = toml.load(f)

        self.modules.append(module_name)

        # Service users, and their associated secrets
        if "service_users" in secrets_toml:
            for (uname, uargs) in secrets_toml["service_users"].items():
                service_user = ServiceUser(uname, config=self, **uargs)
                for (skey, secret) in service_user.secrets().items():
                    if skey in self.secrets:
                        raise Exception("Duplicate secret: {}".format(skey))
                    self.secrets[skey] = secret
                self.service_users[uname] = service_user

        # Other secrets
        if "secrets" in secrets_toml:
            for (skey, sargs) in secrets_toml["secrets"].items():
                ty = sargs["type"]
                del sargs["type"]
                if ty == "user":
                    secret = UserSecret(config=self, key=skey, **sargs)
                elif ty == "command":
                    secret = CommandSecret(config=self, key=skey, **sargs)
                elif ty == "constant":
                    secret = ConstantSecret(config=self, key=skey, **sargs)
                elif ty == "service_password":
                    service = sargs["service"]
                    del sargs["service"]
                    secret = ServiceUserPasswordSecret(
                        config=self,
                        key=skey,
                        service_user=self.service_users[service],
                        **sargs)
                elif ty == "service_username":
                    service = sargs["service"]
                    del sargs["service"]
                    secret = ServiceUserNameSecret(
                        config=self,
                        key=skey,
                        service_user=self.service_users[service],
                        **sargs)
                elif ty == "service_dn":
                    service = sargs["service"]
                    del sargs["service"]
                    secret = ServiceUserDNSecret(
                        config=self,
                        key=skey,
                        service_user=self.service_users[service],
                        **sargs)
                else:
                    description = "{}, {}".format(ty,
                                ", ".join([k + ": " + v for k, v in sargs.items()]))
                    secret = UserSecret(
                        config=self,
                        key=skey,
                        multiline=True,
                        description=description)
                if skey in self.secrets:
                    raise Exception("Duplicate secret: {}".format(skey))
                self.secrets[skey] = secret

    def add_user_values_secrets(self):
        for (skey, value) in self.user_values.items():
            self.secrets[skey] = ConstantSecret(
                config=self,
                key=skey,
                value=value,
                description="Cluster-defined user value")

    # -- consul and ldap helpers --

    def check_consul_cluster(self):
        # Check cluster name we are connected to
        consul_node = consul_server.agent.self()
        if consul_node["Config"]["Datacenter"] != self.cluster_name:
            print("You are not connected to the correct Consul cluster.")
            print("You are connected to cluster '{}' instead of '{}'.".format(consul_node["Config"]["Datacenter"], self.cluster_name))
            sys.exit(1)

    def get_ldap_admin_conn(self):
        if self.ldap_admin_conn is None:
            if self.ldap_admin_password_secret != None:
                ldap_pass = self.get_secret(self.ldap_admin_password_secret)
                if ldap_pass is None:
                    raise Exception("LDAP admin password could not be read at: {}".format(pass_key))
            else:
                ldap_pass = getpass.getpass("LDAP admin password: ")

            self.ldap_admin_conn = ldap.initialize(self.ldap_server)
            self.ldap_admin_conn.simple_bind_s(self.ldap_admin_dn, ldap_pass)
        return self.ldap_admin_conn

    def get_secret(self, key):
        _, data = consul_server.kv.get("secrets/" + key)
        if data is None:
            return None
        else:
            return data["Value"].decode('ascii').strip()

    def put_secret(self, key, value):
        if self.dry_run:
            print(bcolors.OKBLUE, "Dry run, not updating secrets/{}. Add --do to actually do something.".format(key), bcolors.ENDC)
            return
        consul_server.kv.put("secrets/" + key, value)

    # -- user actions --

    def print_info(self):
        print("== LIST OF SERVICE USERS ==")
        print()
        for (_, su) in self.service_users.items():
            print("Username:      {}".format(su.username))
            print("DN:            {}".format(su.dn))
            print("Pass. secret:  {}".format(su.password_secret.key))
            print()

        print("== LIST OF SECRETS ==")
        print()
        for (_, secret) in self.secrets.items():
            secret.print_info()
            print()

    def check_secrets(self):
        self.check_consul_cluster()
        print(":: Checking secrets...")
        must_gen = False
        for (_, secret) in self.secrets.items():
            value = self.get_secret(secret.key)
            if value is None:
                print(secret.key, bcolors.FAIL, "x  missing", bcolors.ENDC)
                must_gen = True
            elif not secret.check(value): 
                print(secret.key, bcolors.WARNING, "x  bad value", bcolors.ENDC)
                must_gen = True
            else:
                print(secret.key, bcolors.OKGREEN, "✓", bcolors.ENDC)
        print()
        if must_gen:
            print("To fix missing or invalid secrets, use `secretmgr gen <cluster_name> <app>...`")
            print()

    def gen_secrets(self):
        self.check_consul_cluster()
        if len(self.service_users) > 0:
            print(":: Configuring service users...")
            for (_, su) in self.service_users.items():
                su.configure(False)
            print()

        print(":: Generating missing/invalid secrets...")
        for (_, secret) in self.secrets.items():
            old_value = self.get_secret(secret.key)
            if old_value is None or not secret.check(old_value):
                print()
                secret.print_info()
                value = secret.generate()
                if value != None:
                    self.put_secret(secret.key, value)
                    print(bcolors.OKCYAN, "Value set.", bcolors.ENDC)
                else:
                    print(bcolors.WARNING, "Skipped.", bcolors.ENDC)

        print()
        self.check_secrets()

    def rotate_secrets(self):
        self.check_consul_cluster()
        if len(self.service_users) > 0:
            print(":: Regenerating service user passwords...")
            for (_, su) in self.service_users.items():
                su.configure(True)
            print()

        print(":: Rotating secrets...")
        for (_, secret) in self.secrets.items():
            print()
            secret.print_info()

            old_value = self.get_secret(secret.key)
            new_value = secret.rotate()

            if new_value != None and new_value != old_value:
                self.put_secret(secret.key, new_value)
                print(bcolors.OKCYAN, "Value set.", bcolors.ENDC)
            else:
                print(bcolors.OKGREEN, "Nothing to do.", bcolors.ENDC)

        print()
        self.check_secrets()


# ---- MAIN ----

def load_config(cluster_name, modules, **kwargs):
    # Load config
    cfg = Config(cluster_name, **kwargs)
    if len(modules) > 0:
        for mod in modules:
            cfg.load_module(mod)
    else:
        cfg.add_user_values_secrets()

    return cfg

if __name__ == "__main__":
    verb = None
    dry_run = True

    for i, val in enumerate(sys.argv):
        if val == "--do":
            dry_run = False
        elif val == "info":
            verb = lambda cfg: cfg.print_info()
            break
        elif val == "check":
            verb = lambda cfg: cfg.check_secrets()
            break
        elif val == "gen":
            verb = lambda cfg: cfg.gen_secrets()
            break
        elif val == "rotate":
            verb = lambda cfg: cfg.rotate_secrets()
            break

    if verb is None:
        print("Usage:")
        print("    secretmgr [--do] info|check|gen|rotate <cluster name> [<module name>...]")
    else:
        cfg = load_config(
                cluster_name=sys.argv[i+1],
                modules=sys.argv[i+2:],
                dry_run=dry_run)
        verb(cfg)


#  vim: set sts=4 ts=4 sw=4 tw=0 ft=python et :