- cred = client.CloudCredential(
- auth_type="certificate",
- attrs={"ClientCertificateData": cacert, "Token": token},
- )
- return await self.add_cloud(name, cloud, cred)
+ return await self.add_cloud(name, cloud, credential)
+
+ def get_k8s_cloud_credential(
+ self, configuration: Configuration,
+ ) -> client.CloudCredential:
+ attrs = {}
+ ca_cert = configuration.ssl_ca_cert or configuration.cert_file
+ key = configuration.key_file
+ api_key = configuration.api_key
+ token = None
+ username = configuration.username
+ password = configuration.password
+
+ if "authorization" in api_key:
+ authorization = api_key["authorization"]
+ if "Bearer " in authorization:
+ bearer_list = authorization.split(" ")
+ if len(bearer_list) == 2:
+ [_, token] = bearer_list
+ else:
+ raise JujuInvalidK8sConfiguration("unknown format of api_key")
+ else:
+ token = authorization
+ if ca_cert:
+ attrs["ClientCertificateData"] = open(ca_cert, "r").read()
+ if key:
+ attrs["ClientKeyData"] = open(key, "r").read()
+ if token:
+ if username or password:
+ raise JujuInvalidK8sConfiguration("Cannot set both token and user/pass")
+ attrs["Token"] = token
+
+ auth_type = None
+ if key:
+ auth_type = "oauth2"
+ if not token:
+ raise JujuInvalidK8sConfiguration(
+ "missing token for auth type {}".format(auth_type)
+ )
+ elif username:
+ if not password:
+ self.log.debug(
+ "credential for user {} has empty password".format(username)
+ )
+ attrs["username"] = username
+ attrs["password"] = password
+ if ca_cert:
+ auth_type = "userpasswithcert"
+ else:
+ auth_type = "userpass"
+ elif ca_cert and token:
+ auth_type = "certificate"
+ else:
+ raise JujuInvalidK8sConfiguration("authentication method not supported")
+ return client.CloudCredential(auth_type=auth_type, attrs=attrs,)