This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 468c699f65a CAMEL-20789: Download binary content of Secret or Config Map in property resolver (#14195) 468c699f65a is described below commit 468c699f65aed8597fc6e1daf68b7485808dbce7 Author: Marco Carletti <mcarl...@redhat.com> AuthorDate: Tue May 21 06:29:09 2024 +0200 CAMEL-20789: Download binary content of Secret or Config Map in property resolver (#14195) --- .../camel/properties-function/configmap-binary | 2 + .../apache/camel/properties-function/secret-binary | 2 + ...n.java => BaseConfigMapPropertiesFunction.java} | 10 +---- .../properties/BasePropertiesFunction.java | 24 +++++++++- ...tion.java => BaseSecretPropertiesFunction.java} | 12 ++--- .../ConfigMapBinaryPropertiesFunction.java | 29 ++++++++++++ .../properties/ConfigMapPropertiesFunction.java | 44 +----------------- .../properties/SecretBinaryPropertiesFunction.java | 29 ++++++++++++ .../properties/SecretPropertiesFunction.java | 44 +----------------- .../kubernetes/KubernetesTestSupport.java | 5 +++ ...ConfigMapBinaryMountPropertiesFunctionTest.java | 49 +++++++++++++++++++++ .../ConfigMapPropertiesFunctionLocalModeTest.java | 17 +++++++ .../ConfigMapPropertiesFunctionRouteTest.java | 23 +++++++++- .../ConfigMapPropertiesFunctionTest.java | 2 +- .../SecretBinaryMountPropertiesFunctionTest.java | 48 ++++++++++++++++++++ .../SecretPropertiesFunctionRouteTest.java | 18 +++++++- .../src/test/resources/binary-example/binary.bin | Bin 0 -> 256 bytes .../ROOT/pages/using-propertyplaceholder.adoc | 15 ++++++- 18 files changed, 265 insertions(+), 108 deletions(-) diff --git a/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/configmap-binary b/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/configmap-binary new file mode 100644 index 00000000000..7262cf7a0e1 --- /dev/null +++ b/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/configmap-binary @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.kubernetes.properties.ConfigMapBinaryPropertiesFunction diff --git a/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/secret-binary b/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/secret-binary new file mode 100644 index 00000000000..f2669ea07a8 --- /dev/null +++ b/components/camel-kubernetes/src/generated/resources/META-INF/services/org/apache/camel/properties-function/secret-binary @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.kubernetes.properties.SecretBinaryPropertiesFunction diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseConfigMapPropertiesFunction.java similarity index 88% copy from components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java copy to components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseConfigMapPropertiesFunction.java index 45c557c2333..4c537125d9c 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseConfigMapPropertiesFunction.java @@ -26,13 +26,7 @@ import org.apache.camel.spi.PropertiesFunction; /** * A {@link PropertiesFunction} that can lookup from Kubernetes configmaps. */ -@org.apache.camel.spi.annotations.PropertiesFunction("configmap") -public class ConfigMapPropertiesFunction extends BasePropertiesFunction { - - @Override - public String getName() { - return "configmap"; - } +abstract class BaseConfigMapPropertiesFunction extends BasePropertiesFunction { @Override Path getMountPath() { @@ -55,7 +49,7 @@ public class ConfigMapPropertiesFunction extends BasePropertiesFunction { // need to decode base64 byte[] data = Base64.getDecoder().decode(answer); if (data != null) { - answer = new String(data); + answer = handleData(key, data); } } } diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java index 9c003005031..4b27ec00127 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java @@ -245,7 +245,11 @@ abstract class BasePropertiesFunction extends ServiceSupport implements Properti Path file = root.resolve(name.toLowerCase(Locale.US)).resolve(key); if (Files.exists(file) && !Files.isDirectory(file)) { try { - answer = Files.readString(file, StandardCharsets.UTF_8); + if (isBinaryProperty()) { + answer = writeDataToTempFile(file.getFileName().toString(), Files.readAllBytes(file)); + } else { + answer = Files.readString(file, StandardCharsets.UTF_8); + } } catch (IOException e) { // ignore } @@ -265,4 +269,22 @@ abstract class BasePropertiesFunction extends ServiceSupport implements Properti abstract Path getMountPath(); abstract String lookup(String name, String key, String defaultValue); + + protected String handleData(String key, byte[] raw) { + return isBinaryProperty() ? writeDataToTempFile(key, raw) : new String(raw); + } + + private boolean isBinaryProperty() { + return getName().endsWith("-binary"); + } + + protected String writeDataToTempFile(String fileName, byte[] data) { + try { + final Path filePath = Files.createTempDirectory("camel").resolve(fileName); + Files.write(filePath, data); + return filePath.toAbsolutePath().toString(); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } + } } diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseSecretPropertiesFunction.java similarity index 88% copy from components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java copy to components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseSecretPropertiesFunction.java index 1dc32727705..452458960db 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BaseSecretPropertiesFunction.java @@ -24,15 +24,9 @@ import io.fabric8.kubernetes.api.model.Secret; import org.apache.camel.spi.PropertiesFunction; /** - * A {@link PropertiesFunction} that can lookup from Kubernetes secret. + * A {@link PropertiesFunction} that can lookup from Kubernetes secrets. */ -@org.apache.camel.spi.annotations.PropertiesFunction("secret") -public class SecretPropertiesFunction extends BasePropertiesFunction { - - @Override - public String getName() { - return "secret"; - } +abstract class BaseSecretPropertiesFunction extends BasePropertiesFunction { @Override Path getMountPath() { @@ -55,7 +49,7 @@ public class SecretPropertiesFunction extends BasePropertiesFunction { if (answer != null) { byte[] data = Base64.getDecoder().decode(answer); if (data != null) { - answer = new String(data); + answer = handleData(key, data); } } } diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryPropertiesFunction.java new file mode 100644 index 00000000000..bcf4ecc977a --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryPropertiesFunction.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.properties; + +/** + * Resolves binary type configmap keys . + */ +@org.apache.camel.spi.annotations.PropertiesFunction("configmap-binary") +public class ConfigMapBinaryPropertiesFunction extends BaseConfigMapPropertiesFunction { + + @Override + public String getName() { + return "configmap-binary"; + } +} diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java index 45c557c2333..9f0df564235 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunction.java @@ -16,54 +16,14 @@ */ package org.apache.camel.component.kubernetes.properties; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; - -import io.fabric8.kubernetes.api.model.ConfigMap; -import org.apache.camel.spi.PropertiesFunction; - /** - * A {@link PropertiesFunction} that can lookup from Kubernetes configmaps. + * Resolves String type configmap keys . */ @org.apache.camel.spi.annotations.PropertiesFunction("configmap") -public class ConfigMapPropertiesFunction extends BasePropertiesFunction { +public class ConfigMapPropertiesFunction extends BaseConfigMapPropertiesFunction { @Override public String getName() { return "configmap"; } - - @Override - Path getMountPath() { - if (getMountPathConfigMaps() != null) { - return Paths.get(getMountPathConfigMaps()); - } - return null; - } - - @Override - String lookup(String name, String key, String defaultValue) { - String answer = null; - ConfigMap cm = getClient().configMaps().withName(name).get(); - if (cm != null) { - answer = cm.getData() != null ? cm.getData().get(key) : null; - if (answer == null) { - // maybe a binary data - answer = cm.getBinaryData() != null ? cm.getBinaryData().get(key) : null; - if (answer != null) { - // need to decode base64 - byte[] data = Base64.getDecoder().decode(answer); - if (data != null) { - answer = new String(data); - } - } - } - } - if (answer == null) { - return defaultValue; - } - - return answer; - } } diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretBinaryPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretBinaryPropertiesFunction.java new file mode 100644 index 00000000000..52f06565908 --- /dev/null +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretBinaryPropertiesFunction.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.properties; + +/** + * Resolves binary type secret keys . + */ +@org.apache.camel.spi.annotations.PropertiesFunction("secret-binary") +public class SecretBinaryPropertiesFunction extends BaseSecretPropertiesFunction { + + @Override + public String getName() { + return "secret-binary"; + } +} diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java index 1dc32727705..1c8e66e6760 100644 --- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java +++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunction.java @@ -16,54 +16,14 @@ */ package org.apache.camel.component.kubernetes.properties; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; - -import io.fabric8.kubernetes.api.model.Secret; -import org.apache.camel.spi.PropertiesFunction; - /** - * A {@link PropertiesFunction} that can lookup from Kubernetes secret. + * Resolves String type secret keys . */ @org.apache.camel.spi.annotations.PropertiesFunction("secret") -public class SecretPropertiesFunction extends BasePropertiesFunction { +public class SecretPropertiesFunction extends BaseSecretPropertiesFunction { @Override public String getName() { return "secret"; } - - @Override - Path getMountPath() { - if (getMountPathSecrets() != null) { - return Paths.get(getMountPathSecrets()); - } - return null; - } - - @Override - String lookup(String name, String key, String defaultValue) { - String answer = null; - Secret sec = getClient().secrets().withName(name).get(); - if (sec != null) { - // string data can be used as-is - answer = sec.getStringData() != null ? sec.getStringData().get(key) : null; - if (answer == null) { - // need to base64 decode from data - answer = sec.getData() != null ? sec.getData().get(key) : null; - if (answer != null) { - byte[] data = Base64.getDecoder().decode(answer); - if (data != null) { - answer = new String(data); - } - } - } - } - if (answer == null) { - return defaultValue; - } - - return answer; - } } diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesTestSupport.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesTestSupport.java index 37135c894dc..3a82aae1b09 100644 --- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesTestSupport.java +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/KubernetesTestSupport.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.kubernetes; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -52,4 +53,8 @@ public class KubernetesTestSupport extends CamelTestSupport { public static String toUrlEncoded(String str) throws UnsupportedEncodingException { return URLEncoder.encode(str, StandardCharsets.UTF_8); } + + protected byte[] readExampleBinaryFile() throws IOException { + return getClass().getResourceAsStream("/binary-example/binary.bin").readAllBytes(); + } } diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryMountPropertiesFunctionTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryMountPropertiesFunctionTest.java new file mode 100644 index 00000000000..8ba9c95c123 --- /dev/null +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapBinaryMountPropertiesFunctionTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.properties; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.camel.component.kubernetes.KubernetesTestSupport; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class ConfigMapBinaryMountPropertiesFunctionTest extends KubernetesTestSupport { + + @Test + @Order(1) + public void configMapMountPropertiesFunction() throws Exception { + try (ConfigMapBinaryPropertiesFunction cmf = new ConfigMapBinaryPropertiesFunction()) { + cmf.setMountPathConfigMaps("src/test/resources/"); + cmf.setClientEnabled(false); + cmf.setCamelContext(context); + cmf.start(); + + String out = cmf.apply("binary-example/binary.bin"); + Assertions.assertTrue(new File(out).exists()); + Assertions.assertArrayEquals(getClass().getResourceAsStream("/binary-example/binary.bin").readAllBytes(), + Files.readAllBytes(Path.of(out))); + } + } + +} diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java index 51b715f9d86..6880a561a99 100644 --- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java @@ -16,6 +16,10 @@ */ package org.apache.camel.component.kubernetes.properties; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; @@ -33,6 +37,8 @@ public class ConfigMapPropertiesFunctionLocalModeTest extends KubernetesTestSupp public void configure() throws Exception { from("direct:start") .transform().simple("Hello ${body} we are at {{configmap:myconfig/bar.txt}}"); + from("direct:binary") + .transform().simple("File saved to {{configmap-binary:myconfig/binary.bin}}"); } }; } @@ -42,6 +48,8 @@ public class ConfigMapPropertiesFunctionLocalModeTest extends KubernetesTestSupp CamelContext context = super.createCamelContext(); context.getPropertiesComponent().addInitialProperty(ConfigMapPropertiesFunction.LOCAL_MODE, "true"); context.getPropertiesComponent().addInitialProperty("myconfig/bar.txt", "The Local Bar"); + context.getPropertiesComponent().addInitialProperty("myconfig/binary.bin", + Path.of(getClass().getResource("/binary-example/binary.bin").toURI()).toAbsolutePath().toString()); return context; } @@ -52,4 +60,13 @@ public class ConfigMapPropertiesFunctionLocalModeTest extends KubernetesTestSupp Assertions.assertEquals("Hello Jack we are at The Local Bar", out); } + @Test + @Order(2) + public void configMapLocalModeUsingBinary() throws IOException { + String out = template.requestBody("direct:binary", null, String.class); + Assertions.assertTrue(out.matches("File saved to .*binary.bin")); + Path filePath = Path.of(out.substring("File saved to ".length())); + Assertions.assertArrayEquals(readExampleBinaryFile(), + Files.readAllBytes(filePath)); + } } diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionRouteTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionRouteTest.java index ed49a865c2a..82b962eb3ad 100644 --- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionRouteTest.java +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionRouteTest.java @@ -16,6 +16,10 @@ */ package org.apache.camel.component.kubernetes.properties; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Base64; import java.util.Map; import io.fabric8.kubernetes.api.model.ConfigMap; @@ -53,6 +57,8 @@ public class ConfigMapPropertiesFunctionRouteTest extends KubernetesTestSupport public void configure() throws Exception { from("direct:start") .transform().simple("Hello ${body} we are at {{configmap:myconfig/bar.txt}}"); + from("direct:binary") + .transform().simple("File saved to {{configmap-binary:myconfig/binary.dat}}"); } }; } @@ -67,8 +73,11 @@ public class ConfigMapPropertiesFunctionRouteTest extends KubernetesTestSupport client = new KubernetesClientBuilder().withConfig(builder.build()).build(); context.getRegistry().bind("KubernetesClient", client); - Map<String, String> data = Map.of("foo", "123", "bar", "Moes Bar"); - ConfigMap cm = new ConfigMapBuilder().editOrNewMetadata().withName("myconfig").endMetadata().withData(data).build(); + Map<String, String> data = Map.of("bar.txt", "Moes Bar"); + Map<String, String> binData = Map.of("binary.dat", + Base64.getEncoder().encodeToString(readExampleBinaryFile())); + ConfigMap cm = new ConfigMapBuilder().editOrNewMetadata().withName("myconfig").endMetadata().withData(data) + .withBinaryData(binData).build(); this.cm = client.resource(cm).serverSideApply(); return context; @@ -94,4 +103,14 @@ public class ConfigMapPropertiesFunctionRouteTest extends KubernetesTestSupport Assertions.assertEquals("Hello Jack we are at Moes Bar", out); } + @Test + @Order(2) + public void binarySecretPropertiesFunction() throws IOException { + String out = template.requestBody("direct:binary", null, String.class); + Assertions.assertTrue(out.matches("File saved to .*binary.dat")); + Path filePath = Path.of(out.substring("File saved to ".length())); + Assertions.assertArrayEquals(readExampleBinaryFile(), + Files.readAllBytes(filePath)); + Files.deleteIfExists(filePath); + } } diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionTest.java index 1ae5067b6c1..8b7798b02d9 100644 --- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionTest.java +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionTest.java @@ -49,7 +49,7 @@ public class ConfigMapPropertiesFunctionTest extends KubernetesTestSupport { KubernetesClient client = new KubernetesClientBuilder().withConfig(builder.build()).build(); - Map<String, String> data = Map.of("foo", "123", "bar", "Moes Bar"); + Map<String, String> data = Map.of("foo.txt", "123", "bar.txt", "Moes Bar"); ConfigMap cm = new ConfigMapBuilder().editOrNewMetadata().withName("myconfig").endMetadata().withData(data).build(); client.resource(cm).serverSideApply(); diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretBinaryMountPropertiesFunctionTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretBinaryMountPropertiesFunctionTest.java new file mode 100644 index 00000000000..3b753c84b64 --- /dev/null +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretBinaryMountPropertiesFunctionTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.kubernetes.properties; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.camel.component.kubernetes.KubernetesTestSupport; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class SecretBinaryMountPropertiesFunctionTest extends KubernetesTestSupport { + + @Test + @Order(1) + public void secretMountPropertiesFunction() throws Exception { + try (SecretBinaryPropertiesFunction cmf = new SecretBinaryPropertiesFunction()) { + cmf.setMountPathSecrets("src/test/resources/"); + cmf.setClientEnabled(false); + cmf.setCamelContext(context); + cmf.start(); + + String out = cmf.apply("binary-example/binary.bin"); + Assertions.assertTrue(new File(out).exists()); + Assertions.assertArrayEquals(getClass().getResourceAsStream("/binary-example/binary.bin").readAllBytes(), + Files.readAllBytes(Path.of(out))); + } + } +} diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunctionRouteTest.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunctionRouteTest.java index b233216ef7f..63edbcd721a 100644 --- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunctionRouteTest.java +++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/SecretPropertiesFunctionRouteTest.java @@ -16,7 +16,10 @@ */ package org.apache.camel.component.kubernetes.properties; +import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Base64; import java.util.Map; @@ -55,6 +58,8 @@ public class SecretPropertiesFunctionRouteTest extends KubernetesTestSupport { public void configure() throws Exception { from("direct:start") .transform().simple("Connect with {{secret:mysecret/myuser}}:{{secret:mysecret/mypass}}"); + from("direct:binary") + .transform().simple("File saved to {{secret-binary:mysecret/binary.dat}}"); } }; } @@ -71,7 +76,8 @@ public class SecretPropertiesFunctionRouteTest extends KubernetesTestSupport { Map<String, String> data = Map.of("myuser", Base64.getEncoder().encodeToString("scott".getBytes(StandardCharsets.UTF_8)), - "mypass", Base64.getEncoder().encodeToString("tiger".getBytes(StandardCharsets.UTF_8))); + "mypass", Base64.getEncoder().encodeToString("tiger".getBytes(StandardCharsets.UTF_8)), + "binary.dat", Base64.getEncoder().encodeToString(readExampleBinaryFile())); Secret sec = new SecretBuilder().editOrNewMetadata().withName("mysecret").endMetadata().withData(data).build(); this.sec = client.resource(sec).serverSideApply(); @@ -98,4 +104,14 @@ public class SecretPropertiesFunctionRouteTest extends KubernetesTestSupport { Assertions.assertEquals("Connect with scott:tiger", out); } + @Test + @Order(2) + public void binarySecretPropertiesFunction() throws IOException { + String out = template.requestBody("direct:binary", null, String.class); + Assertions.assertTrue(out.matches("File saved to .*binary.dat")); + Path filePath = Path.of(out.substring("File saved to ".length())); + Assertions.assertArrayEquals(readExampleBinaryFile(), + Files.readAllBytes(filePath)); + Files.deleteIfExists(filePath); + } } diff --git a/components/camel-kubernetes/src/test/resources/binary-example/binary.bin b/components/camel-kubernetes/src/test/resources/binary-example/binary.bin new file mode 100644 index 00000000000..c86626638e0 Binary files /dev/null and b/components/camel-kubernetes/src/test/resources/binary-example/binary.bin differ diff --git a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc index 4f20e952d98..000d8b86451 100644 --- a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc +++ b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc @@ -527,8 +527,10 @@ IMPORTANT: The method must be a public no-arg method (i.e. no parameters) and re The `camel-kubernetes` component include the following functions: -* `configmap` - A function to lookup the property from Kubernetes ConfigMaps. -* `secret` - A function to lookup the property from Kubernetes Secrets. +* `configmap` - A function to lookup the string property from Kubernetes ConfigMaps. +* `configmap-binary` - A function to lookup the binary property from Kubernetes ConfigMaps. +* `secret` - A function to lookup the string property from Kubernetes Secrets. +* `secret-binary` - A function to lookup the binary property from Kubernetes Secrets. The syntax for both functions are: @@ -552,6 +554,15 @@ In this example then it would not fail as a default value is provided: configmap:mymap/mykey:123 ---- +If the value stored in the configmap is in binary format, so it is stored as `Binary Data`, it will be downloaded in a file, and it returns the absolute path of the file + +[source] +---- +configmap-binary:mymap/mybinkey +---- + +it returns a path like `/tmp/camel11787545916150467474/mybinkey` + Before the Kubernetes property placeholder functions can be used they need to be configured with either (or both) - path - A _mount path_ that must be mounted to the running pod, to load the configmaps or secrets from local disk.