|
17 | 17 | package org.qubership.integration.platform.variables.management.service.serializer;
|
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.core.JsonGenerator;
|
20 |
| -import com.fasterxml.jackson.core.type.TypeReference; |
21 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
22 | 20 | import com.fasterxml.jackson.databind.SerializerProvider;
|
23 | 21 | import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
24 |
| -import org.qubership.integration.platform.variables.management.rest.exception.SecuredVariablesException; |
25 | 22 | import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
26 | 23 | import io.kubernetes.client.openapi.models.V1Secret;
|
27 | 24 | import lombok.extern.slf4j.Slf4j;
|
28 | 25 | import org.apache.commons.collections4.MapUtils;
|
29 |
| -import org.springframework.beans.factory.annotation.Autowired; |
30 |
| -import org.springframework.beans.factory.annotation.Qualifier; |
31 | 26 | import org.springframework.stereotype.Component;
|
32 | 27 |
|
33 | 28 | import java.io.IOException;
|
| 29 | +import java.util.HashMap; |
34 | 30 | import java.util.Map;
|
35 | 31 |
|
36 | 32 | @Slf4j
|
37 | 33 | @Component
|
38 | 34 | public class KubeSecretSerializer extends StdSerializer<V1Secret> {
|
39 | 35 |
|
40 |
| - private final ObjectMapper objectMapper; |
| 36 | + private static final String TEST_VARIABLE_KEY = "test"; |
41 | 37 |
|
42 |
| - @Autowired |
43 |
| - public KubeSecretSerializer(@Qualifier("primaryObjectMapper") ObjectMapper objectMapper) { |
44 |
| - this(null, objectMapper); |
| 38 | + public KubeSecretSerializer() { |
| 39 | + this(null); |
45 | 40 | }
|
46 | 41 |
|
47 |
| - public KubeSecretSerializer(Class<V1Secret> t, @Qualifier("primaryObjectMapper") ObjectMapper objectMapper) { |
| 42 | + public KubeSecretSerializer(Class<V1Secret> t) { |
48 | 43 | super(t);
|
49 |
| - this.objectMapper = objectMapper; |
50 | 44 | }
|
51 | 45 |
|
52 | 46 | @Override
|
@@ -75,33 +69,23 @@ public void serialize(V1Secret secret, JsonGenerator jsonGenerator, SerializerPr
|
75 | 69 | }
|
76 | 70 | jsonGenerator.writeEndObject();
|
77 | 71 | }
|
78 |
| - if (secret.getData() != null) { |
79 |
| - writeSecuredVariablesData(jsonGenerator, secret.getData()); |
80 |
| - } |
| 72 | + writeSecuredVariablesData(jsonGenerator, secret.getData()); |
81 | 73 |
|
82 | 74 | jsonGenerator.writeEndObject();
|
83 | 75 | }
|
84 | 76 |
|
85 | 77 | private void writeSecuredVariablesData(JsonGenerator jsonGenerator, Map<String, byte[]> data) throws IOException {
|
86 | 78 | if (MapUtils.isEmpty(data)) {
|
87 |
| - return; |
| 79 | + data = new HashMap<>(); |
| 80 | + data.put(TEST_VARIABLE_KEY, new byte[0]); |
88 | 81 | }
|
89 | 82 |
|
90 | 83 | jsonGenerator.writeFieldName(V1Secret.SERIALIZED_NAME_STRING_DATA);
|
91 | 84 | jsonGenerator.writeStartObject();
|
92 | 85 |
|
93 |
| - for (Map.Entry<String, byte[]> dataEntry : data.entrySet()) { |
94 |
| - String serializedVariables; |
95 |
| - try { |
96 |
| - Map<String, String> variables = objectMapper.readValue(new String(dataEntry.getValue()), new TypeReference<>() {}); |
97 |
| - variables.replaceAll((key, value) -> composeHelmChartExpressionFromKey(key)); |
98 |
| - serializedVariables = objectMapper.writeValueAsString(variables); |
99 |
| - } catch (IOException e) { |
100 |
| - log.error("Can't deserialize secured variables for tenant: {}", dataEntry.getKey(), e); |
101 |
| - throw new SecuredVariablesException("Can't deserialize secured variables for tenant: " + dataEntry.getKey(), e); |
102 |
| - } |
103 |
| - |
104 |
| - jsonGenerator.writeString(serializedVariables); |
| 86 | + for (String variableKey : data.keySet()) { |
| 87 | + jsonGenerator.writeFieldName(variableKey); |
| 88 | + jsonGenerator.writeString(composeHelmChartExpressionFromKey(variableKey)); |
105 | 89 | }
|
106 | 90 | jsonGenerator.writeEndObject();
|
107 | 91 | }
|
|
0 commit comments