fix(core): skip malformed UTF-8 resources
* Catching the exception to skip all non UTF-8 resources (configmaps, secrets).
* Adding a check to skip symbolic links which were processed twice
Fix #593
diff --git a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
index 782c8f5..4e3788f 100644
--- a/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
+++ b/camel-k-core/support/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
+import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
@@ -350,7 +351,7 @@
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);
- if (Files.isDirectory(file)) {
+ if (Files.isDirectory(file) || Files.isSymbolicLink(file)) {
return FileVisitResult.CONTINUE;
}
@@ -361,9 +362,14 @@
p.forEach((key, value) -> properties.put(String.valueOf(key), String.valueOf(value)));
}
} else {
- properties.put(
- file.getFileName().toString(),
- Files.readString(file, StandardCharsets.UTF_8));
+ try {
+ properties.put(
+ file.getFileName().toString(),
+ Files.readString(file, StandardCharsets.UTF_8));
+ } catch (MalformedInputException mie){
+ // Just skip if it is not a UTF-8 encoded file (ie a binary)
+ LOGGER.info("Cannot transform {} into UTF-8 text, skipping.", file);
+ }
}
return FileVisitResult.CONTINUE;
diff --git a/camel-k-core/support/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java b/camel-k-core/support/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
index 632f3e6..f917dec 100644
--- a/camel-k-core/support/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
+++ b/camel-k-core/support/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
@@ -16,7 +16,11 @@
*/
package org.apache.camel.k.support;
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import org.apache.camel.CamelContext;
@@ -173,4 +177,19 @@
assertThat(customizers).hasSize(3);
assertThat(context.getName()).isEqualTo("camel-c2-c3-c1");
}
+
+ @Test
+ public void shouldLoadUsePropertiesFromTextConfigMap(){
+ System.setProperty(Constants.PROPERTY_CAMEL_K_CONF_D, getClass().getResource("/configmaps/my-cm").getFile());
+ Map<String, String> loadedProperties = RuntimeSupport.loadUserProperties();
+ assertThat(loadedProperties).hasSize(1);
+ assertThat(loadedProperties.get("my-property")).isEqualTo("my-cm-property");
+ }
+
+ @Test
+ public void shouldSkipLoadUsePropertiesFromBinaryConfigMap(){
+ System.setProperty(Constants.PROPERTY_CAMEL_K_CONF_D, getClass().getResource("/configmaps/my-binary-cm").getFile());
+ Map<String, String> loadedProperties = RuntimeSupport.loadUserProperties();
+ assertThat(loadedProperties).isEmpty();
+ }
}
diff --git a/camel-k-core/support/src/test/resources/configmaps/my-binary-cm/my-property.zip b/camel-k-core/support/src/test/resources/configmaps/my-binary-cm/my-property.zip
new file mode 100644
index 0000000..3961052
--- /dev/null
+++ b/camel-k-core/support/src/test/resources/configmaps/my-binary-cm/my-property.zip
Binary files differ