Skip to content

Commit

Permalink
fix(core): skip malformed UTF-8 resources
Browse files Browse the repository at this point in the history
* 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 committed Feb 10, 2021
1 parent 965bb71 commit dab89b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 dab89b2

Please sign in to comment.