Debian Codegraph @

magnum_capi_helm/common/ca_certificates.py

raw
1 # Copyright (c) 2023 VEXXHOST, Inc.
2 # Copyright (c) 2023 StackHPC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # This code is making use of the good work done here:
17 # https://github.com/vexxhost/magnum-cluster-api/blob/main/magnum_cluster_api/resources.py
18
19 from magnum.common.x509 import operations as x509
20 from magnum.conductor.handlers.common import cert_manager
21 from oslo_utils import encodeutils
22
23
24 def _decode_cert(cert):
25 return encodeutils.safe_decode(cert.get_certificate())
26
27
28 def _decode_key(cert):
29 key = x509.decrypt_key(
30 cert.get_private_key(),
31 cert.get_private_key_passphrase(),
32 )
33 return encodeutils.safe_decode(key)
34
35
36 def get_certificate_string_data(context, cluster):
37 # Magnum creates CA certs for each of the Kubernetes components that
38 # must be trusted by the cluster
39 # In particular, this is required for "openstack coe cluster config"
40 # to work, as that doesn't communicate with the driver and instead
41 # relies on the correct CA being trusted by the cluster
42
43 # Cluster API looks for specific named secrets for each of the CAs,
44 # and generates them if they don't exist, so we create them here
45 # with the correct certificates in
46 certificates = {
47 "ca": cert_manager.get_cluster_ca_certificate(cluster, context),
48 "etcd": cert_manager.get_cluster_ca_certificate(
49 cluster, context, "etcd"
50 ),
51 "proxy": cert_manager.get_cluster_ca_certificate(
52 cluster, context, "front_proxy"
53 ),
54 "sa": cert_manager.get_cluster_magnum_cert(cluster, context),
55 }
56 certificate_string_data = {}
57 for name, cert in certificates.items():
58 certificate_string_data[name] = {
59 "tls.crt": _decode_cert(cert),
60 "tls.key": _decode_key(cert),
61 }
62
63 return certificate_string_data