Debian Codegraph @

magnum_capi_helm/common/app_creds.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 import yaml
20
21 import certifi
22 import keystoneauth1
23 from oslo_log import log as logging
24
25 from magnum.common import clients
26 from magnum.common import utils
27 import magnum.conf
28
29 CONF = magnum.conf.CONF
30 LOG = logging.getLogger(__name__)
31
32
33 def _get_openstack_ca_certificate():
34 # This function returns the CA bundle to use when verifying TLS
35 # connections to the OpenStack API in both the Cluster API provider
36 # and OpenStack integrations on the cluster (e.g. OCCM, Cinder CSI)
37 #
38 # If no CA bundle is specified in config we use the CA bundle from
39 # certifi
40 # This is because the Cluster API provider contains NO trusted CAs
41 # and, because it is a pod in Kubernetes, it does NOT pick up the
42 # trusted CAs from the host
43 ca_certificate = utils.get_openstack_ca()
44 if not ca_certificate:
45 with open(certifi.where(), "r") as ca_file:
46 ca_certificate = ca_file.read()
47 return ca_certificate
48
49
50 def _create_app_cred(context, cluster):
51 osc = clients.OpenStackClients(context)
52 # TODO(johngarbutt) be sure not to allow the admin role
53 # roles = [role for role in context.roles if role != "admin"]
54 app_cred = osc.keystone().client.application_credentials.create(
55 user=cluster.user_id,
56 name=f"magnum-{cluster.uuid}",
57 description=f"Magnum cluster ({cluster.uuid})",
58 # roles=roles,
59 )
60 return {
61 "clouds": {
62 "openstack": {
63 "identity_api_version": 3,
64 "region_name": osc.cinder_region_name(),
65 "interface": CONF.capi_helm.app_cred_interface_type,
66 # This config item indicates whether TLS should be
67 # verified when connecting to the OpenStack API
68 "verify": CONF.drivers.verify_ca,
69 "auth": {
70 "auth_url": osc.url_for(
71 service_type="identity", interface="public"
72 ),
73 "application_credential_id": app_cred.id,
74 "application_credential_secret": app_cred.secret,
75 },
76 "auth_type": "v3applicationcredential",
77 },
78 },
79 }
80
81
82 def get_app_cred_string_data(context, cluster):
83 app_cred_dict = _create_app_cred(context, cluster)
84 clouds_yaml_str = yaml.safe_dump(app_cred_dict)
85 return {
86 "cacert": _get_openstack_ca_certificate(),
87 "clouds.yaml": clouds_yaml_str,
88 }
89
90
91 def delete_app_cred(context, cluster):
92 osc = clients.OpenStackClients(context)
93 try:
94 appcred = osc.keystone().client.application_credentials.find(
95 name=f"magnum-{cluster.uuid}", user=cluster.user_id
96 )
97 except keystoneauth1.exceptions.http.NotFound:
98 # We don't want this to be a failure condition as it may prevent
99 # cleanup of broken clusters, e.g. if cluster creation fails
100 # before the appcred is created or cluster deletion fails after
101 # the appcred is deleted
102 LOG.warning("Appcred does not exist for %s", cluster.uuid)
103 else:
104 appcred.delete()