blob: 0debd7f8c61f37eb3dc3819e6e6eca6a1990431f [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18
19import base64
20
21
22async def create_secret(self, secret_name, secret_namespace, secret_key, secret_value):
23 async def check_secret(secret_name, secret_namespace, secret_key, secret_value):
24 self.logger.info(f"Checking content of secret {secret_name} ...")
25 returned_secret_data = await self._kubectl.get_secret_content(
26 name=secret_name,
27 namespace=secret_namespace,
28 )
garciadeblas96b94f52024-07-08 16:18:21 +020029 returned_secret_value = base64.b64decode(
30 returned_secret_data[secret_key]
31 ).decode("utf-8")
garciadeblasf1ad82e2025-03-05 17:06:49 +010032 # self.logger.debug(f"secret_data_original={secret_value}")
33 # self.logger.debug(f"secret_data_received={returned_secret_value}")
garciadeblas96b94f52024-07-08 16:18:21 +020034 self.logger.info(
garciadeblas28bff0f2024-09-16 12:53:07 +020035 f"Result of secret comparison: {secret_value==returned_secret_value}"
garciadeblas96b94f52024-07-08 16:18:21 +020036 )
37
38 self.logger.info(
39 f"Creating secret {secret_name} in namespace {secret_namespace} ..."
40 )
41 secret_data = {secret_key: base64.b64encode(secret_value.encode()).decode("utf-8")}
garciadeblas28bff0f2024-09-16 12:53:07 +020042 self.logger.info(
43 f"Calling N2VC kubectl to create secret. Namespace: {secret_namespace}. Secret name: {secret_name}. Secret data:{secret_data}."
44 )
garciadeblas96b94f52024-07-08 16:18:21 +020045 await self._kubectl.create_secret(
46 name=secret_name,
47 data=secret_data,
48 namespace=secret_namespace,
49 secret_type="Opaque",
50 )
51 self.logger.info(f"Secret {secret_name} CREATED")
52
53 await check_secret(secret_name, secret_namespace, secret_key, secret_value)
garciadeblas28bff0f2024-09-16 12:53:07 +020054
55
56def delete_secret(self, secret_name, secret_namespace):
57 try:
58 self._kubectl.delete_secret(name=secret_name, namespace=secret_namespace)
59 self.logger.info(
60 f"Deleted secret {secret_name} in namespace {secret_namespace}"
61 )
62 except Exception as e:
63 self.logger.error(
64 f"Could not delete secret {secret_name} in namespace {secret_namespace}: {e}"
65 )