|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from base64 import b64encode |
5 | 6 | from dataclasses import dataclass |
6 | 7 | from enum import StrEnum |
7 | 8 | from typing import Any, Final, Self, cast |
|
13 | 14 | from kr8s.objects import Secret |
14 | 15 | from kubernetes.client import V1Secret |
15 | 16 |
|
16 | | -from renku_data_services.errors import errors |
| 17 | +from renku_data_services.errors import ProgrammingError, errors |
17 | 18 | from renku_data_services.k8s.constants import DUMMY_TASK_RUN_USER_ID, ClusterId |
18 | 19 |
|
19 | 20 | sanitizer = kubernetes.client.ApiClient().sanitize_for_serialization |
@@ -235,12 +236,24 @@ def to_v1_secret(self) -> V1Secret: |
235 | 236 | type=self.manifest.get("type"), |
236 | 237 | ) |
237 | 238 |
|
| 239 | + def __b64encode_values(self, stringData: dict[str, Any], new_data: dict[str, str]) -> None: |
| 240 | + for k, v in stringData.items(): |
| 241 | + if k in new_data: |
| 242 | + raise ProgrammingError( |
| 243 | + message=f"Patching a secret with both stringData and data and conflicting key {k}." |
| 244 | + ) |
| 245 | + new_data[k] = b64encode(str(v).encode("utf-8")).decode("utf-8") |
| 246 | + |
238 | 247 | def to_patch(self) -> list[dict[str, Any]]: |
239 | 248 | """Create a rfc6902 patch that would take an existing secret and patch it to this state.""" |
240 | | - if self.manifest.get("stringData"): |
241 | | - raise NotImplementedError("Patching a secret with stringData field is not implemented.") |
| 249 | + secretData = self.manifest.get("data") or {} |
| 250 | + stringData = self.manifest.get("stringData") |
| 251 | + if stringData: |
| 252 | + secretData = secretData.copy() |
| 253 | + self.__b64encode_values(stringData, secretData) |
| 254 | + |
242 | 255 | patch = [ |
243 | | - {"op": "replace", "path": "/data", "value": self.manifest.data}, |
| 256 | + {"op": "replace", "path": "/data", "value": secretData}, |
244 | 257 | {"op": "replace", "path": "/type", "value": self.manifest.get("type", "Opaque")}, |
245 | 258 | ] |
246 | 259 | if "metadata" not in self.manifest: |
|
0 commit comments