| 1 |
#!/usr/bin/env python |
| 2 |
# SPDX-License-Identifier: MPL-2.0 |
| 3 |
# -*- coding: utf-8 -*- |
| 4 |
|
| 5 |
import argparse |
| 6 |
import collections |
| 7 |
|
| 8 |
import attr |
| 9 |
|
| 10 |
from cryptodatahub.common.entity import Entity, EntityRole |
| 11 |
from cryptodatahub.common.stores import ( |
| 12 |
RootCertificate, |
| 13 |
RootCertificateParams, |
| 14 |
RootCertificateTrustConstraintAction, |
| 15 |
RootCertificateTrustStoreConstraint, |
| 16 |
) |
| 17 |
from cryptodatahub.common.types import Base64Data, CryptoDataParamsBase |
| 18 |
|
| 19 |
from cryptodatahub.common.fetcher import FetcherBase, FetcherRootCertificateStore |
| 20 |
|
| 21 |
from updaters.common import UpdaterBase |
| 22 |
|
| 23 |
|
| 24 |
@attr.s |
| 25 |
class RootCertificateStore(CryptoDataParamsBase): |
| 26 |
certificates = attr.ib( |
| 27 |
default={}, |
| 28 |
validator=attr.validators.deep_mapping( |
| 29 |
key_validator=attr.validators.instance_of(Base64Data), |
| 30 |
value_validator=attr.validators.instance_of(RootCertificateTrustConstraintAction) |
| 31 |
) |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
def _get_trust_store_owner_choices(): |
| 36 |
return collections.OrderedDict([ |
| 37 |
('all', None), |
| 38 |
] + [ |
| 39 |
(trust_store_owner.name.lower(), trust_store_owner) |
| 40 |
for trust_store_owner in Entity.get_items_by_role(EntityRole.CA_TRUST_STORE_OWNER) |
| 41 |
]) |
| 42 |
|
| 43 |
|
| 44 |
def _parse_trust_store_owner(value): |
| 45 |
normalized_value = value.lower() |
| 46 |
trust_store_owner_choices = _get_trust_store_owner_choices() |
| 47 |
|
| 48 |
try: |
| 49 |
return trust_store_owner_choices[normalized_value] |
| 50 |
except KeyError as exc: |
| 51 |
raise argparse.ArgumentTypeError( |
| 52 |
f"invalid trust store {value!r}; expected one of: {', '.join(trust_store_owner_choices.keys())}" |
| 53 |
) from exc |
| 54 |
|
| 55 |
|
| 56 |
def _get_selected_trust_store_fetcher_class(trust_store_owner): |
| 57 |
store_fetcher_class = FetcherRootCertificateStore.get_root_certificate_store_updaters()[trust_store_owner] |
| 58 |
|
| 59 |
def _get_owner_constraints(existing_root_certificate, identifier): |
| 60 |
owner_constraints = collections.OrderedDict() |
| 61 |
for trust_store in existing_root_certificate.trust_stores: |
| 62 |
if trust_store.owner in owner_constraints: |
| 63 |
raise ValueError( |
| 64 |
f"duplicate trust-store owner {trust_store.owner.name} for certificate {identifier}" |
| 65 |
) |
| 66 |
|
| 67 |
owner_constraints[trust_store.owner] = trust_store.constraints |
| 68 |
|
| 69 |
return owner_constraints |
| 70 |
|
| 71 |
class FetcherRootCertificateStoreSelected(FetcherBase): |
| 72 |
@classmethod |
| 73 |
def _get_current_data(cls): |
| 74 |
current_root_store = store_fetcher_class.from_current_data() |
| 75 |
existing_root_certificates = collections.OrderedDict([ |
| 76 |
(root_certificate.identifier, root_certificate) |
| 77 |
for root_certificate in RootCertificate.get_json_records(RootCertificateParams).values() |
| 78 |
]) |
| 79 |
|
| 80 |
current_root_certificates = collections.OrderedDict() |
| 81 |
for certificate_data, constraints in current_root_store.parsed_data.items(): |
| 82 |
current_root_certificate = RootCertificateParams( |
| 83 |
certificate=certificate_data, |
| 84 |
trust_stores=(RootCertificateTrustStoreConstraint(trust_store_owner, constraints),), |
| 85 |
) |
| 86 |
current_root_certificates[current_root_certificate.identifier] = current_root_certificate |
| 87 |
|
| 88 |
merged_root_certificates = collections.OrderedDict() |
| 89 |
for identifier, existing_root_certificate in existing_root_certificates.items(): |
| 90 |
current_root_certificate = current_root_certificates.pop(identifier, None) |
| 91 |
selected_trust_store_constraints = None |
| 92 |
if current_root_certificate is not None: |
| 93 |
selected_trust_store_constraints = current_root_certificate.trust_stores[0].constraints |
| 94 |
|
| 95 |
owner_constraints = _get_owner_constraints(existing_root_certificate, identifier) |
| 96 |
|
| 97 |
if selected_trust_store_constraints is None: |
| 98 |
owner_constraints.pop(trust_store_owner, None) |
| 99 |
else: |
| 100 |
owner_constraints[trust_store_owner] = selected_trust_store_constraints |
| 101 |
|
| 102 |
merged_trust_stores = [ |
| 103 |
RootCertificateTrustStoreConstraint(owner, constraints) |
| 104 |
for owner, constraints in sorted( |
| 105 |
owner_constraints.items(), |
| 106 |
key=lambda item: item[0].name, |
| 107 |
) |
| 108 |
] |
| 109 |
|
| 110 |
if merged_trust_stores: |
| 111 |
merged_root_certificates[identifier] = RootCertificateParams( |
| 112 |
certificate=existing_root_certificate.certificate, |
| 113 |
trust_stores=tuple(merged_trust_stores), |
| 114 |
) |
| 115 |
|
| 116 |
for identifier in sorted(current_root_certificates.keys()): |
| 117 |
merged_root_certificates[identifier] = current_root_certificates[identifier] |
| 118 |
|
| 119 |
return list(merged_root_certificates.values()) |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def _transform_data(cls, current_data): |
| 123 |
return current_data |
| 124 |
|
| 125 |
return FetcherRootCertificateStoreSelected |
| 126 |
|
| 127 |
|
| 128 |
class UpdaterRootCertificateTrustStore(UpdaterBase): |
| 129 |
def __init__(self, trust_store_owner=None): |
| 130 |
if trust_store_owner is None: |
| 131 |
fetcher_class = FetcherRootCertificateStore |
| 132 |
else: |
| 133 |
fetcher_class = _get_selected_trust_store_fetcher_class(trust_store_owner) |
| 134 |
|
| 135 |
super().__init__( # pragma: no cover |
| 136 |
fetcher_class=fetcher_class, |
| 137 |
enum_class=RootCertificate, |
| 138 |
enum_param_class=RootCertificateParams, |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
parser = argparse.ArgumentParser() |
| 144 |
parser.add_argument( |
| 145 |
'--trust-store', |
| 146 |
default='all', |
| 147 |
type=_parse_trust_store_owner, |
| 148 |
help='trust store to update; use all to update every trust store (default: all)', |
| 149 |
) |
| 150 |
args = parser.parse_args() |
| 151 |
|
| 152 |
UpdaterRootCertificateTrustStore(trust_store_owner=args.trust_store)() # pragma: no cover |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == '__main__': |
| 156 |
main() # pragma: no cover |