-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathkey_issuer.py
More file actions
executable file
·634 lines (519 loc) · 20.1 KB
/
key_issuer.py
File metadata and controls
executable file
·634 lines (519 loc) · 20.1 KB
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import json
import logging
import os
from requests import request
from .jwe.utils import alg2keytype as jwe_alg2keytype
from .jws.utils import alg2keytype as jws_alg2keytype
from .key_bundle import KeyBundle, build_key_bundle, key_diff, update_key_bundle
from .utils import httpc_params_loader, importer, qualified_name
__author__ = "Roland Hedberg"
logger = logging.getLogger(__name__)
class KeyIssuer:
"""A key issuer instance contains a number of KeyBundles."""
params = {
"ca_certs": None,
"httpc_params": None,
"keybundle_cls": KeyBundle,
"name": "",
"remove_after": 3600,
"spec2key": None,
}
def __init__(
self,
ca_certs=None,
keybundle_cls=KeyBundle,
remove_after=3600,
httpc=None,
httpc_params=None,
name="",
):
"""
KeyIssuer init function
:param ca_certs: CA certificates, to be used for HTTPS
:param keybundle_cls: The KeyBundle class
:param remove_after: How long keys marked as inactive will remain in the key Jar.
:param httpc: A HTTP client to use. Default is Requests request.
:param httpc_params: HTTP request parameters
:param name: Issuer identifier
:return: KeyIssuer instance
"""
self._bundles = []
self.ca_certs = ca_certs
self.httpc = httpc or request
self.httpc_params = httpc_params_loader(httpc_params)
self.keybundle_cls = keybundle_cls
self.name = name
self.remove_after = remove_after
self.spec2key = {}
def __repr__(self) -> str:
return f'<KeyIssuer "{self.name}" {self.key_summary()}>'
def __getitem__(self, item):
return self.get_bundles()[item]
def set(self, items):
self._bundles = items
def get_bundles(self):
return [kb for kb in self._bundles]
def add_url(self, url, **kwargs):
"""
Add a set of keys by url. This method will create a
:py:class:`oidcmsg.key_bundle.KeyBundle` instance with the
url as source specification. If no file format is given it's assumed
that what's on the other side is a JWKS.
:param url: Where can the key/-s be found
:param kwargs: extra parameters for instantiating KeyBundle
:return: A :py:class:`oidcmsg.oauth2.keybundle.KeyBundle` instance
"""
if not url:
raise KeyError("No url given")
logger.debug(f"add_url: httpc_params: {self.httpc_params}")
if "/localhost:" in url or "/localhost/" in url:
_params = self.httpc_params.copy()
_params["verify"] = False
kb = self.keybundle_cls(source=url, httpc=self.httpc, httpc_params=_params, **kwargs)
else:
kb = self.keybundle_cls(
source=url, httpc=self.httpc, httpc_params=self.httpc_params, **kwargs
)
kb.update()
self._bundles.append(kb)
return kb
def add_symmetric(self, key, usage=None):
"""
Add a symmetric key. This is done by wrapping it in a key bundle
cloak since KeyJar does not handle keys directly but only through
key bundles.
:param key: The key
:param usage: What the key can be used for signing/signature
verification (sig) and/or encryption/decryption (enc)
"""
if usage is None:
self._bundles.append(self.keybundle_cls([{"kty": "oct", "key": key}]))
else:
for use in usage:
self._bundles.append(self.keybundle_cls([{"kty": "oct", "key": key, "use": use}]))
def add_kb(self, kb):
"""
Add a key bundle.
:param kb: A :py:class:`oidcmsg.key_bundle.KeyBundle` instance
"""
self._bundles.append(kb)
def add(self, item, **kwargs):
if isinstance(item, KeyBundle):
self.add_kb(item)
elif (
item.startswith("http://") or item.startswith("file://") or item.startswith("https://")
):
self.add_url(item, **kwargs)
else:
self.add_symmetric(item, **kwargs)
return self
def all_keys(self):
"""
Get all the keys that belong to an entity.
:return: A possibly empty list of keys
"""
res = []
for kb in self._bundles:
res.extend(kb.keys())
return res
def __contains__(self, item):
return any(item in kb for kb in self._bundles)
def items(self):
_res = {}
for kb in self._bundles:
if kb.source in _res:
_res[kb.source].append(kb)
else:
_res[kb.source] = [kb]
return _res
def load_keys(self, jwks_uri="", jwks=None):
"""
Fetch keys from another server
:param jwks_uri: A URL pointing to a site that will return a JWKS
:param jwks: A dictionary representation of a JWKS
:return: Dictionary with usage as key and keys as values
"""
if jwks_uri:
self.add_url(jwks_uri)
elif jwks:
# jwks should only be considered if no jwks_uri is present
_keys = jwks["keys"]
self._bundles.append(self.keybundle_cls(_keys))
def find(self, source):
"""
Find a key bundle based on the source of the keys
:param source: A source url
:return: A list of :py:class:`oidcmsg.key_bundle.KeyBundle` instances, possibly empty
"""
return [kb for kb in self._bundles if kb.source == source]
def export_jwks(self, private=False, usage=None):
"""
Produces a dictionary that later can be easily mapped into a
JSON string representing a JWKS.
:param private: Whether it should be the private keys or the public
:param usage: If only keys for a special usage should be included
:return: A dictionary with one key: 'keys'
"""
keys = []
for kb in self._bundles:
keys.extend(
[
k.serialize(private)
for k in kb.keys() # noqa: SIM118
if k.inactive_since == 0
and (usage is None or (hasattr(k, "use") and k.use == usage))
]
)
return {"keys": keys}
def export_jwks_as_json(self, private=False, usage=None):
"""
Export a JWKS as a JSON document.
:param private: Whether it should be the private keys or the public
:return: A JSON representation of a JWKS
"""
return json.dumps(self.export_jwks(private, usage=usage))
def import_jwks(self, jwks):
"""
Imports all the keys that are represented in a JWKS
:param jwks: Dictionary representation of a JWKS
"""
try:
_keys = jwks["keys"]
except KeyError as exc:
raise ValueError("Not a proper JWKS") from exc
else:
self._bundles.append(
self.keybundle_cls(_keys, httpc=self.httpc, httpc_params=self.httpc_params)
)
def import_jwks_as_json(self, jwks):
"""
Imports all the keys that are represented in a JWKS expressed as a
JSON object
:param jwks: JSON representation of a JWKS
"""
return self.import_jwks(json.loads(jwks))
def import_jwks_from_file(self, filename):
with open(filename) as jwks_file:
self.import_jwks_as_json(jwks_file.read())
def remove_outdated(self, when=0):
"""
Goes through the complete list of issuers and for each of them removes
outdated keys.
Outdated keys are keys that has been marked as inactive at a time that
is longer ago then some set number of seconds (when). If when=0 the
the base time is set to now.
The number of seconds are carried in the remove_after parameter in the
key jar.
:param when: To facilitate testing
"""
kbl = []
changed = False
for kb in self._bundles:
if kb.remove_outdated(self.remove_after, when=when):
changed = True
kbl.append(kb)
if changed:
self._bundles = kbl
def get(self, key_use, key_type="", kid=None, alg="", **kwargs):
"""
Get all keys that matches a set of search criteria
:param key_use: A key useful for this usage (enc, dec, sig, ver)
:param key_type: Type of key (rsa, ec, oct, ..)
:param kid: A Key Identifier
:param alg: Algorithm
:return: A possibly empty list of keys
"""
use = "enc" if key_use in ["dec", "enc"] else "sig"
if not key_type:
if alg:
key_type = jws_alg2keytype(alg) if use == "sig" else jwe_alg2keytype(alg)
lst = []
for bundle in self._bundles:
if key_type:
if key_use in ["ver", "dec"]:
_bkeys = bundle.get(key_type, only_active=False)
else:
_bkeys = bundle.get(key_type)
else:
_bkeys = bundle.keys()
for key in _bkeys:
if key.inactive_since and key_use != "sig":
# Skip inactive keys unless for signature verification
continue
if not key.use or use == key.use:
if kid:
if key.kid == kid:
lst.append(key)
break
else:
continue
else:
lst.append(key)
# If key algorithm is defined only return keys that can be used.
if alg:
lst = [key for key in lst if not key.alg or key.alg == alg]
# if elliptic curve, have to check if I have a key of the right curve
if key_type and key_type.upper() == "EC":
if alg:
name = f"P-{alg[2:]}" # the type
_lst = []
for key in lst:
if name != key.crv:
continue
_lst.append(key)
lst = _lst
else:
_crv = kwargs.get("crv")
if _crv:
_lst = [k for k in lst if k.crv == _crv]
lst = _lst
return lst
def copy(self):
"""
Make deep copy of this key issuer.
:return: A :py:class:`oidcmsg.key_issuer.KeyIssuer` instance
"""
ki = KeyIssuer()
ki._bundles = [kb.copy() for kb in self._bundles]
ki.httpc_params = self.httpc_params
ki.httpc = self.httpc
ki.keybundle_cls = self.keybundle_cls
return ki
def __len__(self):
nr = 0
for kb in self._bundles:
nr += len(kb)
return nr
def dump(self, exclude_attributes: list[str] | None = None) -> dict:
"""
Returns the content as a dictionary.
:param exclude_attributes: List of attribute names for objects that should be ignored.
:return: A dictionary
"""
if exclude_attributes is None:
exclude_attributes = []
info = {}
for attr in self.params:
if attr in exclude_attributes:
continue
val = getattr(self, attr)
if attr == "keybundle_cls":
val = qualified_name(val)
info[attr] = val
if "bundles" not in exclude_attributes:
_bundles = []
for kb in self._bundles:
_bundles.append(kb.dump(exclude_attributes=exclude_attributes))
info["bundles"] = _bundles
return info
def load(self, info):
"""
:param items: A dictionary with the information to load
:return:
"""
for attr in self.params:
val = info.get(attr)
if val:
if attr == "keybundle_cls":
val = importer(val)
setattr(self, attr, val)
self._bundles = [KeyBundle().load(val) for val in info["bundles"]]
return self
def flush(self):
for attr, default in self.params.items():
setattr(self, attr, default)
self._bundles = []
return self
def update(self):
for kb in self._bundles:
kb.update()
def mark_as_inactive(self, kid):
kbl = []
changed = False
for kb in self._bundles:
if kb.mark_as_inactive(kid):
changed = True
kbl.append(kb)
if changed:
self._bundles = kbl
def mark_all_keys_as_inactive(self):
kbl = []
for kb in self._bundles:
kb.mark_all_as_inactive()
kbl.append(kb)
self._bundles = kbl
def key_summary(self) -> str:
"""
Return a text representation of all the keys.
:return: A text representation of the keys
"""
key_list = []
for kb in self._bundles:
for key in kb.keys(): # noqa: SIM118
if key.inactive_since:
key_list.append(f"*{key.kty}:{key.use}:{key.kid}")
else:
key_list.append(f"{key.kty}:{key.use}:{key.kid}")
return ", ".join(key_list)
def __iter__(self):
yield from self._bundles
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
if len(other.all_keys()) != len(self.all_keys()):
return False
for k in self.all_keys():
if k not in other:
return False
return all(k in self for k in other.all_keys())
def rotate_keys(self, key_conf, kid_template=""):
"""
:param key_conf: The configuration for the new keys
:param issuer: KeyIssuer instance
:param kid_template: A key id template
:return:
"""
new_keys = build_keyissuer(key_conf, kid_template)
self.mark_all_keys_as_inactive()
for kb in new_keys:
self.add_kb(kb)
return self
# =============================================================================
def build_keyissuer(key_conf, kid_template="", key_issuer=None, issuer_id=""):
"""
Builds a :py:class:`oidcmsg.key_issuer.KeyIssuer` instance or adds keys to
an existing KeyIssuer instance based on a key specification.
An example of such a specification::
keys = [
{"type": "RSA", "key": "cp_keys/key.pem", "use": ["enc", "sig"]},
{"type": "EC", "crv": "P-256", "use": ["sig"], "kid": "ec.1"},
{"type": "EC", "crv": "P-256", "use": ["enc"], "kid": "ec.2"}
{"type": "oct", "bytes": 32, "use":["sig"]}
]
Keys in this specification are:
type
The type of key. Presently only 'rsa', 'oct' and 'ec' supported.
key
A name of a file where a key can be found. Works with PEM encoded
RSA and EC private keys.
use
What the key should be used for
crv
The elliptic curve that should be used. Only applies to elliptic curve
keys :-)
kid
Key ID, can only be used with one usage type is specified. If there
are more the one usage type specified 'kid' will just be ignored.
:param key_conf: The key configuration
:param kid_template: A template by which to build the key IDs. If no
kid_template is given then the built-in function add_kid() will be used.
:param key_issuer: If an keyIssuer instance the new keys are added to this key issuer.
:param issuer_id: The identifier of the issuer
:return: A KeyIssuer instance
"""
bundle = build_key_bundle(key_conf, kid_template)
if bundle is None:
return None
if key_issuer is None:
key_issuer = KeyIssuer(name=issuer_id)
key_issuer.add(bundle)
return key_issuer
def init_key_issuer(public_path="", private_path="", key_defs="", read_only=True):
"""
A number of cases here:
1. A private path is given
a. The file exists and a JWKS is found there.
From that JWKS a KeyJar instance is built.
b.
If the private path file doesn't exit the key definitions are
used to build a KeyJar instance. A JWKS with the private keys are
written to the file named in private_path.
If a public path is also provided a JWKS with public keys are written
to that file.
2. A public path is given but no private path.
a. If the public path file exists then the JWKS in that file is used to
construct a KeyJar.
b. If no such file exists then a KeyJar will be built
based on the key_defs specification and a JWKS with the public keys
will be written to the public path file.
3. If neither a public path nor a private path is given then a KeyJar is
built based on the key_defs specification and no JWKS will be written
to file.
In all cases a KeyJar instance is returned
The keys stored in the KeyJar will be stored under the '' identifier.
:param public_path: A file path to a file that contains a JWKS with public
keys
:param private_path: A file path to a file that contains a JWKS with
private keys.
:param key_defs: A definition of what keys should be created if they are
not already available
:param read_only: This function should not attempt to write anything
to a file system.
:return: An instantiated :py:class;`oidcmsg.key_jar.KeyJar` instance
"""
if private_path:
if os.path.isfile(private_path):
with open(private_path) as fp:
_jwks = fp.read()
_issuer = KeyIssuer()
_issuer.import_jwks(json.loads(_jwks))
if key_defs:
_kb = _issuer[0]
_diff = key_diff(_kb, key_defs)
if _diff:
update_key_bundle(_kb, _diff)
if read_only:
logger.error("Not allowed to write to disc!")
else:
_issuer.set([_kb])
jwks = _issuer.export_jwks(private=True)
with open(private_path, "w") as fp:
json.dump(jwks, fp)
else:
_issuer = build_keyissuer(key_defs)
if not read_only:
jwks = _issuer.export_jwks(private=True)
head, tail = os.path.split(private_path)
if head and not os.path.isdir(head):
os.makedirs(head)
with open(private_path, "w") as fp:
json.dump(jwks, fp)
if public_path and not read_only:
jwks = _issuer.export_jwks() # public part
head, tail = os.path.split(public_path)
if head and not os.path.isdir(head):
os.makedirs(head)
with open(public_path, "w") as fp:
json.dump(jwks, fp)
elif public_path:
if os.path.isfile(public_path):
with open(public_path) as fp:
_jwks = fp.read()
_issuer = KeyIssuer()
_issuer.import_jwks(json.loads(_jwks))
if key_defs:
_kb = _issuer[0]
_diff = key_diff(_kb, key_defs)
if _diff:
if read_only:
logger.error("Not allowed to write to disc!")
else:
update_key_bundle(_kb, _diff)
_issuer.set([_kb])
jwks = _issuer.export_jwks()
with open(public_path, "w") as fp:
json.dump(jwks, fp)
else:
_issuer = build_keyissuer(key_defs)
if not read_only:
_jwks = _issuer.export_jwks()
head, tail = os.path.split(public_path)
if head and not os.path.isdir(head):
os.makedirs(head)
with open(public_path, "w") as fp:
json.dump(_jwks, fp)
else:
_issuer = build_keyissuer(key_defs)
if _issuer is None:
raise ValueError("Could not find any keys")
return _issuer