|
27 | 27 | from vyos.configdiff import Diff
|
28 | 28 | from vyos.configdiff import get_config_diff
|
29 | 29 | from vyos.defaults import directories
|
| 30 | +from vyos.pki import encode_certificate |
30 | 31 | from vyos.pki import is_ca_certificate
|
31 | 32 | from vyos.pki import load_certificate
|
32 | 33 | from vyos.pki import load_public_key
|
|
36 | 37 | from vyos.pki import load_crl
|
37 | 38 | from vyos.pki import load_dh_parameters
|
38 | 39 | from vyos.utils.boot import boot_configuration_complete
|
| 40 | +from vyos.utils.configfs import add_cli_node |
39 | 41 | from vyos.utils.dict import dict_search
|
40 | 42 | from vyos.utils.dict import dict_search_args
|
41 | 43 | from vyos.utils.dict import dict_search_recursive
|
| 44 | +from vyos.utils.file import read_file |
42 | 45 | from vyos.utils.process import call
|
43 | 46 | from vyos.utils.process import cmd
|
44 | 47 | from vyos.utils.process import is_systemd_service_active
|
@@ -446,9 +449,37 @@ def generate(pki):
|
446 | 449 | # Get foldernames under vyos_certbot_dir which each represent a certbot cert
|
447 | 450 | if os.path.exists(f'{vyos_certbot_dir}/live'):
|
448 | 451 | for cert in certbot_list_on_disk:
|
| 452 | + # ACME certificate is no longer in use by CLI remove it |
449 | 453 | if cert not in certbot_list:
|
450 |
| - # certificate is no longer active on the CLI - remove it |
451 | 454 | certbot_delete(cert)
|
| 455 | + continue |
| 456 | + # ACME not enabled for individual certificate - bail out early |
| 457 | + if 'acme' not in pki['certificate'][cert]: |
| 458 | + continue |
| 459 | + |
| 460 | + # Read in ACME certificate chain information |
| 461 | + tmp = read_file(f'{vyos_certbot_dir}/live/{cert}/chain.pem') |
| 462 | + tmp = load_certificate(tmp, wrap_tags=False) |
| 463 | + cert_chain_base64 = "".join(encode_certificate(tmp).strip().split("\n")[1:-1]) |
| 464 | + |
| 465 | + # Check if CA chain certificate is already present on CLI to avoid adding |
| 466 | + # a duplicate. This only checks for manual added CA certificates and not |
| 467 | + # auto added ones with the AUTOCHAIN_ prefix |
| 468 | + autochain_prefix = 'AUTOCHAIN_' |
| 469 | + ca_cert_present = False |
| 470 | + if 'ca' in pki: |
| 471 | + for ca_base64, cli_path in dict_search_recursive(pki['ca'], 'certificate'): |
| 472 | + # Ignore automatic added CA certificates |
| 473 | + if any(item.startswith(autochain_prefix) for item in cli_path): |
| 474 | + continue |
| 475 | + if cert_chain_base64 == ca_base64: |
| 476 | + ca_cert_present = True |
| 477 | + |
| 478 | + if not ca_cert_present: |
| 479 | + tmp = dict_search_args(pki, 'ca', f'{autochain_prefix}{cert}', 'certificate') |
| 480 | + if not bool(tmp) or tmp != cert_chain_base64: |
| 481 | + print(f'Adding/replacing automatically imported CA certificate for "{cert}" ...') |
| 482 | + add_cli_node(['pki', 'ca', f'{autochain_prefix}{cert}', 'certificate'], value=cert_chain_base64) |
452 | 483 |
|
453 | 484 | return None
|
454 | 485 |
|
|
0 commit comments