| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 19 | import base64 |
| 20 | |
| 21 | |
| 22 | async 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 | ) |
| 29 | self.logger.debug(f"Result from async call: { returned_secret_data }") |
| 30 | |
| 31 | self.logger.debug("Comparing secret values") |
| 32 | returned_secret_value = base64.b64decode( |
| 33 | returned_secret_data[secret_key] |
| 34 | ).decode("utf-8") |
| 35 | self.logger.debug(f"secret_data_original={secret_value}") |
| 36 | self.logger.debug(f"secret_data_received={returned_secret_value}") |
| 37 | self.logger.info( |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 38 | f"Result of secret comparison: {secret_value==returned_secret_value}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | self.logger.info( |
| 42 | f"Creating secret {secret_name} in namespace {secret_namespace} ..." |
| 43 | ) |
| 44 | secret_data = {secret_key: base64.b64encode(secret_value.encode()).decode("utf-8")} |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 45 | self.logger.info( |
| 46 | f"Calling N2VC kubectl to create secret. Namespace: {secret_namespace}. Secret name: {secret_name}. Secret data:{secret_data}." |
| 47 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 48 | await self._kubectl.create_secret( |
| 49 | name=secret_name, |
| 50 | data=secret_data, |
| 51 | namespace=secret_namespace, |
| 52 | secret_type="Opaque", |
| 53 | ) |
| 54 | self.logger.info(f"Secret {secret_name} CREATED") |
| 55 | |
| 56 | await check_secret(secret_name, secret_namespace, secret_key, secret_value) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def delete_secret(self, secret_name, secret_namespace): |
| 60 | try: |
| 61 | self._kubectl.delete_secret(name=secret_name, namespace=secret_namespace) |
| 62 | self.logger.info( |
| 63 | f"Deleted secret {secret_name} in namespace {secret_namespace}" |
| 64 | ) |
| 65 | except Exception as e: |
| 66 | self.logger.error( |
| 67 | f"Could not delete secret {secret_name} in namespace {secret_namespace}: {e}" |
| 68 | ) |