You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2021/02/17 08:03:51 UTC

[camel-k-runtime] branch master updated: fix(core): skip malformed UTF-8 resources

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git


The following commit(s) were added to refs/heads/master by this push:
     new 7e20ced  fix(core): skip malformed UTF-8 resources
7e20ced is described below

commit 7e20ced4dff318f5a28ac9482b89eb8b40655031
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Tue Feb 9 14:55:51 2021 +0100

    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
---
 .../org/apache/camel/k/support/RuntimeSupport.java   |  14 ++++++++++----
 .../apache/camel/k/support/RuntimeSupportTest.java   |  19 +++++++++++++++++++
 .../configmaps/my-binary-cm/my-property.zip          | Bin 0 -> 186 bytes
 3 files changed, 29 insertions(+), 4 deletions(-)

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 @@ package org.apache.camel.k.support;
 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 @@ public final class RuntimeSupport {
                 Objects.requireNonNull(file);
                 Objects.requireNonNull(attrs);
 
-                if (Files.isDirectory(file)) {
+                if (Files.isDirectory(file) || Files.isSymbolicLink(file)) {
                     return FileVisitResult.CONTINUE;
                 }
 
@@ -361,9 +362,14 @@ public final class RuntimeSupport {
                         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 @@ public class RuntimeSupportTest {
         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
Binary files /dev/null and b/camel-k-core/support/src/test/resources/configmaps/my-binary-cm/my-property.zip differ