Repository navigation
Expand file tree
/
Copy pathadcs_tool.py
More file actions
1265 lines (1146 loc) · 62.6 KB
/
Copy pathadcs_tool.py
File metadata and controls
1265 lines (1146 loc) · 62.6 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Non-interactive ADCS management, sharing data/operations with Textual UI.
No Textual imports: the server can use ``adcs-tool`` without installing a UI.
CA and KET creation write their YAML configuration directly to adcs.yaml.
"""
from __future__ import annotations
import argparse
import shutil
import json
import os
import sys
import stat
import tempfile
from io import StringIO
from datetime import datetime
from contextlib import contextmanager, nullcontext
from functools import lru_cache
from typing import Any, Callable, Dict, Literal, Mapping, TypedDict
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedSeq
from ruamel.yaml.error import YAMLError
from adcs_cli_progress import CliProgress
yaml = YAML(typ="safe")
from adcs_config import load_yaml_conf
from adcs_actions import (
_cmd_issue_cert_cli, _cmd_resign_all_crls, submit_csr_request,
_redact_configuration, public_ca_configuration, public_callback_configuration,
change_revocation, delete_certificate, resign_ca_crl,
)
from adcs_cert_store import (
MAX_ROWS_DEFAULT, ORDER_BY_FIELDS, _resolve_storage_paths_from_ca,
_sync_certificate_cache, cached_certificate_by_serial,
list_certificate_rows, parse_order_by,
)
from utils_crt import (
_cli_find_ca_by_id, _cmd_create_ca, _cmd_create_ket_cert,
_cmd_rotate_if_expiring, revoked_serials_set,
)
class FriendlyArgumentParser(argparse.ArgumentParser):
"""Concise CLI errors with contextual hints instead of a full usage dump."""
def error(self, message: str) -> None:
"""Show contextual help for invalid or incomplete commands."""
if (message.startswith('the following arguments are required:')
or message.startswith('unrecognized arguments:')):
context = getattr(self, "_active_help_parser", self)
context.print_help(sys.stderr)
print(f'\nError: {message}', file=sys.stderr)
if context.prog.endswith(' ca list') and message.startswith('unrecognized arguments:'):
extra = message.partition(':')[2].strip()
if extra and not extra.startswith('-') and len(extra.split()) == 1:
root = context.prog[:-len(' ca list')]
print(f'Hint: To display a CA, use: {root} ca show {extra}', file=sys.stderr)
print(f' To list all CAs, use: {root} ca list', file=sys.stderr)
self.exit(2)
print(f'Error: {message}', file=sys.stderr)
print(f"Run '{self.prog} --help' for usage.", file=sys.stderr)
self.exit(2)
OUTPUT_FORMATS = ('table', 'accessible', 'json')
def _default_output_format() -> str:
value = os.environ.get('ADCS_OUTPUT_FORMAT', 'table').strip().lower()
return value if value in OUTPUT_FORMATS else 'table'
def _add_output_format(parser: argparse.ArgumentParser, *, json_allowed: bool = True) -> None:
parser.add_argument('--format', choices=OUTPUT_FORMATS, default=None,
help='Output format (default: ADCS_OUTPUT_FORMAT or table).')
def _format(args: argparse.Namespace) -> str:
return 'json' if getattr(args, 'json', False) else (getattr(args, 'format', None) or _default_output_format())
def _default_limit() -> int:
try:
value = int(os.getenv('ADCS_MAX_ROWS', str(MAX_ROWS_DEFAULT)))
return value if value > 0 else MAX_ROWS_DEFAULT
except ValueError:
return MAX_ROWS_DEFAULT
def _add_global_config(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
'--confadcs', default=argparse.SUPPRESS,
help='Path of the adcs.yaml configuration (also accepted before the command).',
)
def build_parser(parser_class=FriendlyArgumentParser) -> argparse.ArgumentParser:
parser = parser_class(
description='Manage ADCS CAs, certificate cache, certificates, callbacks and CRLs without the GUI.',
)
parser.add_argument('--confadcs', default='/etc/adcs/adcs.yaml',
help='Path of adcs.yaml (default: /etc/adcs/adcs.yaml)')
commands = parser.add_subparsers(dest='command', metavar='<subcommand>', parser_class=parser_class)
# Argument definitions are private templates, not public CLI commands.
templates = argparse.ArgumentParser(add_help=False)
definitions = templates.add_subparsers(dest='command', parser_class=parser_class)
def command(name: str, help_text: str) -> argparse.ArgumentParser:
sub = definitions.add_parser(name, help=help_text, description=help_text)
_add_global_config(sub)
return sub
ca_list = command('ca-list', 'List CA identifiers, display names and parent CAs.')
ca_list.add_argument('--json', action='store_true', help='Output structured JSON.')
ca_show = command('ca-show', 'Show full configuration details for one CA (secrets redacted).')
ca_show.add_argument('ca_id', help='CA identifier from ca list (or display name).')
ca_show.add_argument('--json', action='store_true', help='Output structured JSON.')
callbacks = command('callback-list', 'List configured callbacks, templates and associated CAs.')
callbacks.add_argument('--json', action='store_true')
config = command('config-show', 'Show adcs.yaml options with secret values redacted.')
config.add_argument('--json', action='store_true')
cert_list = command('certificate-list', 'Search, filter, sort and limit certificates of a CA via SQLite.')
cert_list.add_argument('--ca', required=True, help='CA id or display name.')
cert_list.add_argument('--filter', action='store_true',
help='Optional for listing: --status, --revocation and --search always apply.')
cert_list.add_argument('--search', '--query', default='', help='Case-insensitive substring search in certificate metadata.')
cert_list.add_argument('--status', choices=('any', 'expired', 'expiring', 'valid'), default='any',
help='Expiration: expired; expiring within 30 days; valid for more than 30 days.')
cert_list.add_argument('--revocation', choices=('any', 'revoked', 'not_revoked'), default='any')
cert_list.add_argument('--limit', type=int, default=_default_limit(),
help='Maximum rows (default 1000 or ADCS_MAX_ROWS, 0 = unlimited).')
cert_list.add_argument('--order-by', metavar='FIELD [ASC|DESC][, ...]',
default='not_before ASC',
help='Allowed fields: ' + ', '.join(ORDER_BY_FIELDS)
+ '. Example: "expiration_date ASC, serial DESC".')
cert_list.add_argument('--json', action='store_true', help='Include total filtered count and certificate rows as JSON.')
cert_show = command('certificate-show', 'Show full SQLite-cached certificate details by CA and serial.')
cert_show.add_argument('--ca', required=True)
cert_show.add_argument('--serial', required=True, help='Certificate serial in hexadecimal (with or without 0x).')
cert_show.add_argument('--json', action='store_true')
for name, desc in (
('certificate-revoke', 'Revoke certificate serials from one CA; update CRL.'),
('certificate-unrevoke', 'Remove certificate serials from one CRL.'),
('certificate-delete', 'Move an expired or revoked certificate and its key to .trash.'),
):
sub = command(name, desc)
sub.add_argument('--ca', required=True)
selection = sub.add_mutually_exclusive_group(required=True)
selection.add_argument('--serial', nargs='+' if name != 'certificate-delete' else None,
help='Hexadecimal certificate serial(s) for revoke/unrevoke; one for delete.')
if name == 'certificate-delete':
selection.add_argument('--eligible', action='store_true',
help='Select certificates that are expired OR revoked.')
selection.add_argument('--filter', action='store_true',
help='Select certificates matching --status/--revocation/--search.')
sub.add_argument('--status', choices=('any', 'expired', 'expiring', 'valid'), default='any')
sub.add_argument('--revocation', choices=('any', 'revoked', 'not_revoked'), default='any')
sub.add_argument('--search', default='', help='Case-insensitive metadata search.')
sub.add_argument('--order-by', default='not_after ASC',
help='Allowed fields: ' + ', '.join(ORDER_BY_FIELDS))
sub.add_argument('--limit', type=int, default=0, help='Maximum selected rows (0 = all).')
sub.add_argument('--yes', action='store_true',
help='Execute filtered operation; otherwise show a preview.')
sub.add_argument('--dry-run', action='store_true',
help='Preview this operation without changing certificates, keys or CRLs.')
if name != 'certificate-delete':
sub.add_argument('--next-update-hours', type=int, default=None)
for source in (ca_list, ca_show, callbacks, config, cert_list, cert_show):
_add_output_format(source)
for name in ('certificate-revoke', 'certificate-unrevoke', 'certificate-delete'):
_add_output_format(definitions.choices[name])
resign = command('crl-resign', 'Re-sign CRL of one CA.')
resign.add_argument('--ca', required=True)
resign.add_argument('--no-bump-number', action='store_true')
resign.add_argument('--next-update-hours', type=int, default=None)
resign_all = command('crl-resign-all', 'Re-sign CRLs of all writable CAs.')
resign_all.add_argument('--no-bump-number', action='store_true')
resign_all.add_argument('--next-update-hours', type=int, default=None)
def add_key_options(sub, *, rsa_default):
sub.add_argument('--rsa-bits', type=int, default=rsa_default)
sub.add_argument('--key-type', default='rsa',
choices=('rsa', 'ec', 'ecc', 'ecdsa', 'mldsa', 'ml-dsa',
'mldsa44', 'mldsa65', 'mldsa87',
'ml-dsa-44', 'ml-dsa-65', 'ml-dsa-87'))
sub.add_argument('--ec-curve', default='secp256r1',
help='ECC curve: secp256r1, secp384r1 or secp521r1 (also p-256, p-384, p-521).')
sub.add_argument('--mldsa-variant', default='mldsa65',
help='ML-DSA variant: mldsa44, mldsa65 or mldsa87.')
ca_create = command('ca-create', 'Create a root/intermediate CA and update adcs.yaml.')
ca_create.add_argument('--cn', required=True)
ca_create.add_argument('--signer-ca-id', '--ca-id', '--ca', dest='ca', default=None,
help='Issuer CA, omit for a self-signed root CA.')
ca_create.add_argument('--aia-crl-base-url', required=True)
ca_create.add_argument('--crt-path')
ca_create.add_argument('--key-path')
ca_create.add_argument('--crl-path')
add_key_options(ca_create, rsa_default=4096)
ca_create.add_argument('--csr-path', help='Use only the CSR public key for the CA.')
ca_create.add_argument('--valid-days', type=int, default=3650)
cert_issue = command('certificate-issue', 'Issue a leaf certificate and private key.')
cert_issue.add_argument('--ca', '--signer-ca-id', required=True, dest='ca')
cert_issue.add_argument('--cn', required=True)
cert_issue.add_argument('--san', action='append', help='SANs; repeat or comma-separate.')
cert_issue.add_argument('--crt-path')
cert_issue.add_argument('--key-path')
add_key_options(cert_issue, rsa_default=3072)
cert_issue.add_argument('--valid-days', type=int, default=365)
ket_create = command('ket-create', 'Create a Microsoft CAExchange/KET certificate.')
ket_create.add_argument('--ca', '--ca-id', required=True, dest='ca')
ket_create.add_argument('--cn')
ket_create.add_argument('--crt-path')
ket_create.add_argument('--key-path')
ket_create.add_argument('--rsa-bits', type=int, default=4096)
ket_create.add_argument('--valid-days', type=int, default=3650)
submit = command('csr-submit', 'Submit a CSR through the same enrollment callback as the server.')
submit.add_argument('--ca', '--signer-ca-id', required=True, dest='ca')
submit.add_argument('--username', required=True)
submit.add_argument('--csr-path', help='Input PEM or DER file (stdin if omitted).')
group = submit.add_mutually_exclusive_group()
group.add_argument('--template-oid')
group.add_argument('--template-name')
rotate = command('certificate-rotate', 'Replace an expiring key and leaf certificate.')
rotate.add_argument('--ca', '--signer-ca-id', required=True, dest='ca')
rotate.add_argument('--crt-path', required=True)
rotate.add_argument('--key-path', required=True)
rotate.add_argument('--threshold-days', type=int, default=30)
rotate.add_argument('--valid-days', type=int, default=365)
rotate.add_argument('--no-write-fullchain-to-crt', action='store_true')
# Public commands are exclusively hierarchical; templates share arguments.
groups = {
'ca': ('Certificate authority management.', {'list': 'ca-list', 'show': 'ca-show', 'create': 'ca-create'}),
'certificate': ('Certificate management.', {
'list': 'certificate-list', 'show': 'certificate-show',
'revoke': 'certificate-revoke', 'unrevoke': 'certificate-unrevoke',
'delete': 'certificate-delete', 'issue': 'certificate-issue',
'rotate': 'certificate-rotate'}),
'crl': ('Certificate revocation list management.', {
'resign': 'crl-resign', 'resign-all': 'crl-resign-all'}),
'callback': ('Callback configuration.', {'list': 'callback-list'}),
'config': ('Global configuration.', {'show': 'config-show'}),
'ket': ('CAExchange certificate management.', {'create': 'ket-create'}),
'csr': ('Certificate signing requests.', {'submit': 'csr-submit'}),
}
# Reuse the internal argument templates without duplicating definitions.
originals = {name: definitions.choices[name] for _, (_, items) in groups.items()
for name in items.values()}
group_parsers = {}
action_parsers = {}
for group_name, (description, items) in groups.items():
group = commands.add_parser(group_name, help=description, description=description)
group_parsers[group_name] = group
_add_global_config(group)
nested = group.add_subparsers(dest='operation', metavar='<subcommand>', parser_class=parser_class)
for short_name, old_name in items.items():
old = originals[old_name]
sub = nested.add_parser(short_name, parents=[old], add_help=False,
help=old.description, description=old.description)
sub.set_defaults(command=old_name)
action_parsers[(group_name, short_name)] = sub
commands.metavar = '{' + ','.join(groups) + '}'
parser._group_parsers = group_parsers
parser._action_parsers = action_parsers
return parser
def _render_dict_lines(prefix: str, obj: Any) -> list[str]:
if isinstance(obj, dict):
result = []
for key, value in obj.items():
result.extend(_render_dict_lines(f'{prefix}.{key}' if prefix else str(key), value))
return result
if isinstance(obj, list):
return [f'{prefix}: ' + json.dumps(obj, ensure_ascii=False, default=str)]
return [f'{prefix}: {obj if obj is not None else "(unset)"}']
def _table(headers: list[str], rows: list[list[Any]], output_format: str = 'table', *, title: str = 'Record') -> None:
"""Dependency-free readable tables, without truncating certificate identifiers."""
if output_format == 'json':
print(json.dumps([dict(zip(headers, row)) for row in rows], ensure_ascii=False, indent=2, default=str))
return
if output_format == 'accessible':
if not rows:
print('No results.')
for index, row in enumerate(rows, 1):
if index > 1:
print()
print(f'{title} {index} of {len(rows)}')
for header, value in zip(headers, row):
print(f' {header}: {value if value is not None else "(unset)"}')
return
rendered = [[str(value) if value is not None else '' for value in row] for row in rows]
columns = [str(x) for x in headers]
widths = [max([len(columns[i])] + [len(row[i]) for row in rendered])
for i in range(len(columns))]
def line(values: list[str]) -> str:
return ' | '.join(value.ljust(width) for value, width in zip(values, widths)).rstrip()
print(line(columns))
print('-+-'.join('-' * width for width in widths))
for row in rendered:
print(line(row))
def _field_rows(obj: Any) -> list[list[str]]:
return [line.split(': ', 1) if ': ' in line else [line, '']
for line in _render_dict_lines('', obj)]
def _output_structure(obj: Any, output_format: str = "table") -> None:
if output_format == 'json':
print(json.dumps(obj, ensure_ascii=False, indent=2, default=str))
elif isinstance(obj, list):
if not obj:
print('No results.' if output_format == 'accessible' else '(no results)')
for index, entry in enumerate(obj):
if index:
print()
_table(['Field', 'Value'], _field_rows(entry), output_format)
else:
_table(['Field', 'Value'], _field_rows(obj), output_format)
def _ca(conf: Dict[str, Any], name: str) -> Dict[str, Any]:
ca = _cli_find_ca_by_id(conf, name)
if ca is None:
raise LookupError(f'CA not found: {name}')
return ca
def _ca_revocations(ca: Dict[str, Any]) -> set[int]:
return revoked_serials_set((ca.get('crl') or {}).get('path_crl'))
def _config_yaml(path):
editor = YAML()
editor.preserve_quotes = True
editor.indent(mapping=2, sequence=4, offset=2)
with open(path, encoding='utf-8') as stream:
document = editor.load(stream)
if not isinstance(document, dict):
raise ValueError('Invalid adcs.yaml: expected a mapping')
if document.get('cas') is None:
document['cas'] = CommentedSeq()
cas = document['cas']
if not isinstance(cas, list) or any(not isinstance(ca, dict) for ca in cas):
raise ValueError('Invalid adcs.yaml: cas must be a list of mappings')
return editor, document, cas
def _save_config(path, editor, document):
output = StringIO()
editor.dump(document, output)
target = os.path.realpath(path)
info = os.stat(target)
fd, temporary = tempfile.mkstemp(dir=os.path.dirname(target))
try:
with os.fdopen(fd, 'w', encoding='utf-8') as stream:
os.fchown(stream.fileno(), info.st_uid, info.st_gid)
os.fchmod(stream.fileno(), stat.S_IMODE(info.st_mode))
for attr in os.listxattr(target):
os.setxattr(temporary, attr, os.getxattr(target, attr))
stream.write(output.getvalue())
stream.flush()
os.fsync(stream.fileno())
os.replace(temporary, target)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
class BatchResult(TypedDict):
"""Bulk revoke/unrevoke/delete result (preview or applied).
All counts are integers; errors contain {serial, error} mappings.
``dry_run`` indicates a preview, not whether it succeeded.
"""
action: str
ca: str
dry_run: bool
total: int
selected: int
candidates: list[dict[str, Any]]
successes: int
failures: int
errors: list[dict[str, str]]
class CrlItemResult(TypedDict):
"""One CA: error is set on failure, CRL fields on success."""
ca: str
display_name: str
status: Literal['ok', 'error']
crl_number: int | None
crl_path: str | None
error: str | None
class CrlBatchResult(TypedDict):
"""Results of a CRL re-sign batch; skipped CAs are not counted."""
total: int
ok: int
failed: int
results: list[CrlItemResult]
class CertificateListResult(TypedDict):
"""Filtered certificate listing, with total before the requested limit."""
ca: str
total: int
shown: int
certificates: list[dict[str, Any]]
class CsrSubmissionResult(TypedDict):
"""CSR status: issued/pending/denied; request_id is a decimal string.
certificate_path is set only when issued; reason only when denied.
"""
status: Literal['issued', 'pending', 'denied']
request_id: str
ca: str
username: str
template: str
csr_path: str
certificate_path: str | None
reason: str | None
@lru_cache(maxsize=1)
def _callback_flask_app():
from flask import Flask
return Flask('adcs_tool_callbacks')
@contextmanager
def _callback_flask_context(values, request_headers=None):
"""Run unchanged Flask-g callbacks inside an isolated request context."""
from flask import current_app, g, has_app_context, has_request_context
app = current_app._get_current_object() if has_app_context() else _callback_flask_app()
scope = (nullcontext() if has_request_context() else
app.test_request_context(base_url='https://localhost/',
headers=dict(request_headers or {})))
with scope:
missing = object()
previous = {key: getattr(g, key, missing) for key in values}
for key, value in values.items():
setattr(g, key, value)
try:
yield
finally:
for key, value in previous.items():
if value is missing:
try:
delattr(g, key)
except AttributeError:
pass
else:
setattr(g, key, value)
class AdcsTool:
"""Python API: named arguments, native results and Python exceptions.
No CLI parsing or printing occurs in these methods. The CLI uses this same
API from run(), then formats its results in main().
"""
def __init__(self, config_path: str = '/etc/adcs/adcs.yaml', *,
cache_progress: Callable[[int, int, int], None] | None = None,
cache_status: Callable[[str], None] | None = None):
self.config_path = os.fspath(config_path)
# Optional display callbacks; the Python API remains silent by default.
self._cache_progress = cache_progress
self._cache_status = cache_status
def _sync_cache(self, cert_dir: str) -> dict[str, int]:
return _sync_certificate_cache(
cert_dir, progress=self._cache_progress, status=self._cache_status,
)
def _status(self, message: str) -> None:
if self._cache_status is not None:
self._cache_status(message)
def _settings(self) -> Dict[str, Any]:
# Listing configuration never loads private keys.
with open(self.config_path, encoding='utf-8') as stream:
return yaml.load(stream) or {}
def _conf(self, *, read_only: bool = False) -> Dict[str, Any]:
return load_yaml_conf(self.config_path, bypass_read_only=read_only)
def _record(self, conf: Dict[str, Any], ca_id: str, serial: str):
ca = _ca(conf, ca_id)
cert_dir, _ = _resolve_storage_paths_from_ca(ca)
self._sync_cache(cert_dir)
self._status("Looking up certificate in SQLite cache...")
record = cached_certificate_by_serial(cert_dir, serial)
return ca, cert_dir, record
@staticmethod
def _filters(status: str, revocation: str, limit: int, order_by: str):
if status not in ('any', 'expired', 'expiring', 'valid'):
raise ValueError(f'Invalid certificate status: {status}')
if revocation not in ('any', 'revoked', 'not_revoked'):
raise ValueError(f'Invalid revocation status: {revocation}')
if not isinstance(limit, int) or limit < 0:
raise ValueError('limit must be >= 0 (0 means unlimited)')
parse_order_by(order_by)
def list_cas(self) -> list[dict[str, Any]]:
return [
{'id': ca.get('id', ''), 'display_name': ca.get('display_name', ''),
'parent': ca.get('parent', '')}
for ca in public_ca_configuration(self._settings())
]
def show_ca(self, ca_id: str) -> dict[str, Any]:
return public_ca_configuration(self._settings(), ca_id)[0]
def show_config(self) -> dict[str, Any]:
return _redact_configuration(self._settings())
def list_callbacks(self) -> list[dict[str, Any]]:
return public_callback_configuration(self._settings())
def create_ca(self, cn: str, aia_crl_base_url: str, *, signer_ca_id=None,
csr_path=None, crt_path=None, key_path=None, crl_path=None,
rsa_bits=4096, key_type='rsa', ec_curve='secp256r1',
mldsa_variant='mldsa65', valid_days=3650) -> dict[str, Any]:
if aia_crl_base_url.lower().startswith('https://'):
raise ValueError('--aia-crl-base-url must not start with https://')
if valid_days <= 0:
raise ValueError('--valid-days must be positive')
if key_type not in ('rsa', 'ec', 'ecc', 'ecdsa', 'mldsa', 'ml-dsa', 'mldsa44',
'mldsa65', 'mldsa87', 'ml-dsa-44', 'ml-dsa-65', 'ml-dsa-87'):
raise ValueError(f'Invalid CA key type: {key_type}')
basename = cn.lower().replace(' ', '_')
crt_path = os.fspath(crt_path) if crt_path is not None else f'/var/lib/adcs/pki/certs/{basename}/{basename}.crt.pem'
key_path = os.fspath(key_path) if key_path is not None else f'/var/lib/adcs/pki/private/{basename}/{basename}.key.pem'
crl_path = os.fspath(crl_path) if crl_path is not None else f'/var/lib/adcs/pki/crl/{basename}/{basename}.crl'
csr_path = os.fspath(csr_path) if csr_path is not None else None
editor, document, cas = _config_yaml(self.config_path)
if any(basename in (ca.get('id'), ca.get('display_name'))
or cn.strip() in (ca.get('id'), ca.get('display_name')) for ca in cas):
raise ValueError(f"CA '{basename}' already exists")
conf = self._conf() if signer_ca_id else None
entry = _cmd_create_ca(
ca_id=signer_ca_id, crt_path=crt_path, key_path=key_path,
crl_path=crl_path, aia_crl_base_url=aia_crl_base_url,
valid_days=valid_days, rsa_key_size=rsa_bits,
key_type=key_type, ec_curve=ec_curve, mldsa_variant=mldsa_variant,
conf=conf, cn=cn, csr_path=csr_path,
)
if entry['id'] != basename or entry['display_name'] != cn.strip():
raise ValueError('Unexpected CA configuration')
cas.append(entry)
_save_config(self.config_path, editor, document)
return {'id': entry['id'], 'display_name': entry['display_name'],
'issuer_ca_id': signer_ca_id, 'certificate_path': crt_path,
'key_path': None if csr_path else key_path,
'configured_key_path': entry['pem']['key_path_pem'],
'csr_path': csr_path, 'crl_path': crl_path,
'config_path': self.config_path, 'ca': entry}
def create_ket(self, ca_id: str, *, cn=None, crt_path=None, key_path=None,
rsa_bits=4096, valid_days=3650) -> dict[str, Any]:
editor, document, cas = _config_yaml(self.config_path)
matches = [ca for ca in cas if ca_id in (ca.get('id'), ca.get('display_name'))]
if len(matches) != 1:
raise ValueError(f"CA '{ca_id}' not found or ambiguous in adcs.yaml")
paths = _cmd_create_ket_cert(
ca_id=ca_id, common_name=cn or f'{ca_id}-Xchg',
conf=self._conf(), crt_path=os.fspath(crt_path) if crt_path is not None else None,
key_path=os.fspath(key_path) if key_path is not None else None,
rsa_bits=rsa_bits, valid_days=valid_days,
)
if (set(paths) != {'ket_cert_pem', 'ket_key_pem'}
or not all(isinstance(v, str) and v for v in paths.values())):
raise ValueError('Unexpected KET configuration output')
matches[0].update(paths)
_save_config(self.config_path, editor, document)
return {'ca_id': ca_id, 'config_path': self.config_path, **paths}
def list_certificates(self, ca_id: str, *, status='any', revocation='any',
search='', order_by='not_before ASC', limit=None) -> CertificateListResult:
if limit is None:
limit = _default_limit()
self._filters(status, revocation, limit, order_by)
ca = _ca(self._conf(read_only=True), ca_id)
cert_dir, _ = _resolve_storage_paths_from_ca(ca)
records, total = list_certificate_rows(
cert_dir, query=search,
status='' if status == 'any' else status,
revocation='' if revocation == 'any' else revocation,
revoked_serials=_ca_revocations(ca), order_by=order_by,
limit=limit, synchronize=True,
cache_progress=self._cache_progress, cache_status=self._cache_status,
)
entries = [{
'serial': row.serial_nox, 'subject': row.subject,
'not_before': row.not_before.isoformat(),
'not_after': row.not_after.isoformat(),
'days_to_expiry': row.days_to_expiry,
'revoked': row.revoked, 'is_ca': row.is_ca,
'filename': row.filename, 'relative_path': row.cache_key,
'sha256': row.sha256_fingerprint,
} for row in records]
return {'ca': ca['id'], 'total': total, 'shown': len(entries), 'certificates': entries}
def show_certificate(self, ca_id: str, serial: str) -> dict[str, Any]:
conf = self._conf(read_only=True)
ca, cert_dir, record = self._record(conf, ca_id, serial)
detail = json.loads(record['details_json']) if record.get('details_json') else {}
detail['file'] = os.path.join(cert_dir, record['relative_path'])
detail['revoked'] = int(str(record['serial_hex']), 16) in _ca_revocations(ca)
return detail
def _change_revocation(self, ca_id: str, *, revoke_it: bool, serial=None,
status='any', revocation='any', search='',
order_by='not_after ASC', limit=0, apply=False,
dry_run=False, next_update_hours=None) -> dict[str, Any]:
action = 'revoke' if revoke_it else 'unrevoke'
if apply and dry_run:
raise ValueError('apply=True and dry_run=True cannot be combined')
if serial is None:
self._filters(status, revocation, limit, order_by)
if status == 'any' and revocation == 'any' and not search:
raise ValueError('--filter requires --status, --revocation or --search')
conf = self._conf()
if serial is not None:
if apply:
raise ValueError('apply is only for bulk operations')
if isinstance(serial, list) and len(serial) > 1:
ca, cert_dir, first = self._record(conf, ca_id, serial[0])
records = [first] + [cached_certificate_by_serial(cert_dir, s) for s in serial[1:]]
revoked = _ca_revocations(ca)
candidates = [
{'serial': str(r['serial_hex']), 'not_after': r['not_after'],
'revoked': int(str(r['serial_hex']), 16) in revoked,
'subject': r['subject']} for r in records
]
if not dry_run:
change_revocation(
ca, [r['serial'] for r in candidates], revoke_it=revoke_it,
next_update_hours=(next_update_hours or int(conf['next_update_hours_crl'])),
)
return {'action': action, 'ca': ca['id'], 'dry_run': dry_run,
'total': len(candidates), 'selected': len(candidates),
'candidates': candidates,
'successes': 0 if dry_run else len(candidates),
'failures': 0, 'errors': []}
if isinstance(serial, list):
serial = serial[0]
ca, _, record = self._record(conf, ca_id, serial)
serial = str(record['serial_hex'])
if dry_run:
currently_revoked = int(serial, 16) in _ca_revocations(ca)
return {'action': action, 'ca': ca['id'], 'serial': serial,
'dry_run': True, 'current_revoked': currently_revoked,
'planned_revoked': revoke_it}
self._status(f"{'Revoking' if revoke_it else 'Unrevoking'} certificate...")
change_revocation(
ca, serial, revoke_it=revoke_it,
next_update_hours=(next_update_hours or int(conf['next_update_hours_crl'])),
)
return {'action': action, 'ca': ca['id'], 'serial': serial,
'dry_run': False, 'revoked': revoke_it}
ca = _ca(conf, ca_id)
cert_dir, _ = _resolve_storage_paths_from_ca(ca)
rows, total = list_certificate_rows(
cert_dir, query=search,
status='' if status == 'any' else status,
revocation='' if revocation == 'any' else revocation,
revoked_serials=_ca_revocations(ca), order_by=order_by,
limit=limit, synchronize=True,
eligible_for=action,
cache_progress=self._cache_progress, cache_status=self._cache_status,
)
candidates = [
{'serial': row.serial_nox, 'not_after': row.not_after.isoformat(),
'revoked': row.revoked, 'subject': row.subject}
for row in rows
]
result = {'action': action, 'ca': ca['id'], 'dry_run': not apply,
'total': total, 'selected': len(candidates), 'candidates': candidates,
'successes': 0, 'failures': 0, 'errors': []}
if not apply:
return result
self._status(f"{'Revoking' if revoke_it else 'Unrevoking'} {len(rows)} certificate(s)...")
if rows:
try:
change_revocation(
ca, [row.serial_nox for row in rows], revoke_it=revoke_it,
next_update_hours=(next_update_hours or int(conf['next_update_hours_crl'])),
)
result['successes'] = len(rows)
except (OSError, ValueError, PermissionError) as exc:
result['failures'] = len(rows)
result['errors'] = [{'serial': row.serial_nox, 'error': str(exc)} for row in rows]
return result
def revoke_certificate(self, ca_id: str, serial: str | list[str], *, dry_run=False,
next_update_hours=None) -> dict[str, Any]:
return self._change_revocation(ca_id, revoke_it=True, serial=serial,
dry_run=dry_run, next_update_hours=next_update_hours)
def unrevoke_certificate(self, ca_id: str, serial: str | list[str], *, dry_run=False,
next_update_hours=None) -> dict[str, Any]:
return self._change_revocation(ca_id, revoke_it=False, serial=serial,
dry_run=dry_run, next_update_hours=next_update_hours)
def revoke_certificates(self, ca_id: str, *, status='any', revocation='any',
search='', order_by='not_after ASC', limit=0,
apply=False, dry_run=False, next_update_hours=None) -> BatchResult:
return self._change_revocation(ca_id, revoke_it=True, status=status,
revocation=revocation, search=search,
order_by=order_by, limit=limit, apply=apply,
dry_run=dry_run, next_update_hours=next_update_hours)
def unrevoke_certificates(self, ca_id: str, *, status='any', revocation='any',
search='', order_by='not_after ASC', limit=0,
apply=False, dry_run=False, next_update_hours=None) -> BatchResult:
return self._change_revocation(ca_id, revoke_it=False, status=status,
revocation=revocation, search=search,
order_by=order_by, limit=limit, apply=apply,
dry_run=dry_run, next_update_hours=next_update_hours)
def _delete(self, ca_id: str, *, serial=None, eligible=False, status='any',
revocation='any', search='', order_by='not_after ASC', limit=0,
apply=False, dry_run=False) -> dict[str, Any]:
if dry_run and apply:
raise ValueError('apply=True and dry_run=True cannot be combined')
if serial is None:
self._filters(status, revocation, limit, order_by)
if eligible and (status != 'any' or revocation != 'any'):
raise ValueError('--eligible cannot be combined with --status or --revocation; use --filter')
if not eligible and status == 'any' and revocation == 'any' and not search:
raise ValueError('--filter requires --status, --revocation or --search')
conf = self._conf(read_only=True)
ca = _ca(conf, ca_id)
cert_dir, _ = _resolve_storage_paths_from_ca(ca)
revocations = _ca_revocations(ca)
if serial is not None:
_, _, record = self._record(conf, ca_id, serial)
serial_int = int(str(record['serial_hex']), 16)
path = os.path.join(cert_dir, record['relative_path'])
expires_at = datetime.fromisoformat(record['not_after'])
if dry_run:
from adcs_actions import validate_certificate_deletion
validate_certificate_deletion(
ca, cert_path=path, revoked=serial_int in revocations,
expires_at=expires_at,
)
from datetime import timezone
expiry = expires_at.replace(tzinfo=timezone.utc) if expires_at.tzinfo is None else expires_at
return {'action': 'delete', 'ca': ca['id'], 'dry_run': True,
'serial': record['serial_hex'], 'not_after': expiry.isoformat(),
'revoked': serial_int in revocations, 'certificate_path': path}
cert_n, key_n = delete_certificate(
ca, cert_path=path, revoked=serial_int in revocations,
expires_at=expires_at,
)
self._sync_cache(cert_dir)
return {'action': 'delete', 'ca': ca['id'], 'dry_run': False,
'serial': record['serial_hex'], 'deleted_certificate': cert_n,
'deleted_key': key_n}
rows, total = list_certificate_rows(
cert_dir, query=search,
status='' if eligible or status == 'any' else status,
revocation='' if eligible or revocation == 'any' else revocation,
revoked_serials=revocations, order_by=order_by,
limit=limit, synchronize=True, eligible_for='delete',
cache_progress=self._cache_progress, cache_status=self._cache_status,
)
candidates = [
{'serial': r.serial_nox, 'not_after': r.not_after.isoformat(),
'revoked': r.revoked, 'subject': r.subject, 'filename': r.filename}
for r in rows
]
result = {'action': 'delete', 'ca': ca['id'], 'dry_run': not apply,
'total': total, 'selected': len(candidates), 'candidates': candidates,
'successes': 0, 'failures': 0, 'errors': []}
if not apply:
return result
self._status(f"Deleting {len(rows)} certificate(s)...")
for row in rows:
try:
delete_certificate(
ca, cert_path=os.path.join(cert_dir, row.cache_key),
revoked=row.revoked, expires_at=row.not_after,
)
result['successes'] += 1
except (OSError, ValueError, PermissionError) as exc:
result['failures'] += 1
result['errors'].append({'serial': row.serial_nox, 'error': str(exc)})
self._sync_cache(cert_dir)
return result
def delete_certificate(self, ca_id: str, serial: str, *, dry_run=False) -> dict[str, Any]:
return self._delete(ca_id, serial=serial, dry_run=dry_run)
def delete_certificates(self, ca_id: str, *, eligible=False, status='any',
revocation='any', search='', order_by='not_after ASC',
limit=0, apply=False, dry_run=False) -> BatchResult:
return self._delete(ca_id, eligible=eligible, status=status, revocation=revocation,
search=search, order_by=order_by, limit=limit,
apply=apply, dry_run=dry_run)
def resign_crl(self, ca_id: str, *, bump_number=True, next_update_hours=None) -> dict[str, Any]:
conf = self._conf()
ca = _ca(conf, ca_id)
number, serials = resign_ca_crl(
ca, next_update_hours=next_update_hours or int(conf['next_update_hours_crl']),
bump_number=bump_number,
)
return {'ca': ca['id'], 'crl_number': number, 'revoked_count': len(serials)}
def resign_all_crls(self, *, bump_number=True, next_update_hours=None) -> CrlBatchResult:
conf = self._conf()
return _cmd_resign_all_crls(
conf=conf, next_update_hours=next_update_hours or int(conf['next_update_hours_crl']),
bump_number=bump_number,
)
def issue_certificate(self, ca_id: str, cn: str, *, sans=None, crt_path=None,
key_path=None, rsa_bits=3072, key_type='rsa',
ec_curve='secp256r1', mldsa_variant='mldsa65',
valid_days=365) -> dict[str, Any]:
if isinstance(sans, str):
sans = [sans]
return _cmd_issue_cert_cli(
ca_id=ca_id, common_name=cn, sans=sans,
rsa_bits=rsa_bits, valid_days=valid_days,
key_type=key_type, ec_curve=ec_curve, mldsa_variant=mldsa_variant,
conf=self._conf(),
crt_path=os.fspath(crt_path) if crt_path is not None else None,
key_path=os.fspath(key_path) if key_path is not None else None,
)
def submit_csr(self, ca_id: str, username: str, csr_data: bytes, *,
template_oid: str | None = None, template_name: str | None = None,
auth_method: str = 'cli',
auth_context: Mapping[str, Any] | None = None,
request_headers: Mapping[str, str] | None = None) -> CsrSubmissionResult:
"""Submit PEM/DER CSR bytes for an already authenticated identity.
The caller must authenticate ``username`` and trust ``auth_context``
and ``request_headers``. This method never reads stdin. Callbacks
keep using flask.g; a request context is created when needed.
"""
if template_oid and template_name:
raise ValueError('template_oid and template_name are mutually exclusive')
if not username or not username.strip():
raise ValueError('username must not be empty')
context = dict(auth_context or {})
context.update(username=username.strip(), auth_method=auth_method)
with _callback_flask_context(context, request_headers):
return submit_csr_request(
ca_id=ca_id, username=username.strip(), csr_data=csr_data,
conf=self._conf(), template_oid=template_oid,
template_name=template_name, auth_method=auth_method,
request_headers=dict(request_headers or {}),
)
def rotate_certificate(self, ca_id: str, crt_path, key_path, *,
threshold_days=30, valid_days=365,
write_fullchain_to_crt=True) -> dict[str, Any]:
return _cmd_rotate_if_expiring(
ca_id=ca_id, crt_path=os.fspath(crt_path), key_path=os.fspath(key_path),
threshold_days=threshold_days, conf=self._conf(),
write_fullchain_to_crt=write_fullchain_to_crt, valid_days=valid_days,
)
def run(args: argparse.Namespace, *, progress: CliProgress | None = None) -> Any:
"""CLI adapter: call the Python API and return native Python data."""
adcs = AdcsTool(
args.confadcs,
cache_progress=progress.progress if progress is not None else None,
cache_status=progress.status if progress is not None else None,
)
cmd = args.command
if cmd == 'ca-list':
return adcs.list_cas()
if cmd == 'ca-show':
return adcs.show_ca(args.ca_id)
if cmd == 'ca-create':
return adcs.create_ca(
cn=args.cn, aia_crl_base_url=args.aia_crl_base_url,
signer_ca_id=args.ca, csr_path=args.csr_path,
crt_path=args.crt_path, key_path=args.key_path, crl_path=args.crl_path,
rsa_bits=args.rsa_bits, key_type=args.key_type, ec_curve=args.ec_curve,
mldsa_variant=args.mldsa_variant, valid_days=args.valid_days)
if cmd == 'ket-create':
return adcs.create_ket(args.ca, cn=args.cn, crt_path=args.crt_path,
key_path=args.key_path, rsa_bits=args.rsa_bits,
valid_days=args.valid_days)
if cmd == 'callback-list':
return adcs.list_callbacks()
if cmd == 'config-show':
return adcs.show_config()
if cmd == 'certificate-list':
return adcs.list_certificates(args.ca, status=args.status, revocation=args.revocation,
search=args.search, order_by=args.order_by, limit=args.limit)
if cmd == 'certificate-show':
return adcs.show_certificate(args.ca, args.serial)
if cmd == 'certificate-issue':
return adcs.issue_certificate(args.ca, args.cn, sans=args.san,
crt_path=args.crt_path, key_path=args.key_path,
rsa_bits=args.rsa_bits, key_type=args.key_type,
ec_curve=args.ec_curve, mldsa_variant=args.mldsa_variant,
valid_days=args.valid_days)
if cmd in ('certificate-revoke', 'certificate-unrevoke'):
method = adcs.revoke_certificate if cmd == 'certificate-revoke' else adcs.unrevoke_certificate
batch_method = adcs.revoke_certificates if cmd == 'certificate-revoke' else adcs.unrevoke_certificates
if args.serial:
return method(args.ca, args.serial if len(args.serial) > 1 else args.serial[0],
dry_run=args.dry_run,
next_update_hours=args.next_update_hours)
return batch_method(args.ca, status=args.status, revocation=args.revocation,
search=args.search, order_by=args.order_by, limit=args.limit,
apply=args.yes, dry_run=args.dry_run,
next_update_hours=args.next_update_hours)
if cmd == 'certificate-delete':
if args.serial:
return adcs.delete_certificate(args.ca, args.serial, dry_run=args.dry_run)
return adcs.delete_certificates(args.ca, eligible=args.eligible,
status=args.status, revocation=args.revocation,
search=args.search, order_by=args.order_by,
limit=args.limit, apply=args.yes, dry_run=args.dry_run)
if cmd == 'crl-resign':
return adcs.resign_crl(args.ca, bump_number=not args.no_bump_number,
next_update_hours=args.next_update_hours)
if cmd == 'crl-resign-all':
return adcs.resign_all_crls(bump_number=not args.no_bump_number,
next_update_hours=args.next_update_hours)
if cmd == 'csr-submit':
if args.csr_path:
with open(args.csr_path, 'rb') as stream:
csr_data = stream.read()
else:
if sys.stdin.isatty():
raise ValueError('provide --csr-path or pipe the CSR on stdin')
csr_data = sys.stdin.buffer.read()
return adcs.submit_csr(args.ca, args.username, csr_data,
template_oid=args.template_oid, template_name=args.template_name)
if cmd == 'certificate-rotate':
return adcs.rotate_certificate(args.ca, args.crt_path, args.key_path,
threshold_days=args.threshold_days,
valid_days=args.valid_days,
write_fullchain_to_crt=not args.no_write_fullchain_to_crt)
raise ValueError(f'Unknown command: {cmd}')
def _display_result(args: argparse.Namespace, result: Any) -> None:
"""CLI-only formatting; AdcsTool methods never print."""
cmd = args.command
output_format = _format(args)
if (output_format == 'json' and cmd in ('certificate-revoke', 'certificate-unrevoke',
'certificate-delete') and 'serial' not in result
and not result['dry_run']):
result = {**result, 'exit_code': _cli_exit_code(args, result)}
if cmd == 'ca-create':
print(f"CA '{result['display_name']}' created successfully (updated {result['config_path']}).")
elif cmd == 'ket-create':
print(f"KET certificate for CA '{result['ca_id']}' created successfully (updated {result['config_path']}).")
elif cmd == 'ca-list':
if output_format == 'json':
_output_structure(result, 'json')
elif result:
_table(['CA ID', 'Display name', 'Parent CA'], [
[item['id'], item['display_name'], item['parent']] for item in result
], output_format, title='CA')
else:
print('(no CAs configured)')
elif cmd in ('config-show', 'callback-list', 'ca-show', 'certificate-show'):
_output_structure(result, output_format)
elif cmd == 'certificate-list':
entries = result['certificates']