Skip to content
Permalink
Browse files
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
  • Loading branch information
squakez authored and lburgazzoli committed Feb 17, 2021
1 parent 2cfd20a commit 7e20ced4dff318f5a28ac9482b89eb8b40655031
Showing 3 changed files with 29 additions and 4 deletions.
@@ -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 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
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 FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
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;
@@ -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 void apply(CamelContext camelContext) {
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();
}
}
Binary file not shown.

0 comments on commit 7e20ced

Please sign in to comment.