Skip to content

Commit

Permalink
Hide indexed propertyNames from YAML source. (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jul 22, 2020
1 parent 3de8de8 commit b3eb149
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 27 deletions.
5 changes: 5 additions & 0 deletions sources/yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>smallrye-config</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.spi.ConfigSource;
Expand All @@ -28,20 +30,32 @@ public class YamlConfigSource extends MapBackedConfigSource {

private static final long serialVersionUID = -418186029484956531L;

private final Set<String> propertyNames;

public YamlConfigSource(String name, Map<String, String> source, int ordinal) {
super(name, source, ordinal, false);
this.propertyNames = filterIndexedNames(source.keySet());
}

public YamlConfigSource(String name, InputStream stream) throws IOException {
this(name, stream, ORDINAL);
}

public YamlConfigSource(String name, InputStream stream, int defaultOrdinal) throws IOException {
super(name, streamToMap(stream), defaultOrdinal, false);
this(name, streamToMap(stream), defaultOrdinal);
}

public YamlConfigSource(String name, String str) {
this(name, str, ORDINAL);
public YamlConfigSource(String name, String source) {
this(name, source, ORDINAL);
}

public YamlConfigSource(String name, String str, int defaultOrdinal) {
super(name, stringToMap(str), defaultOrdinal, false);
public YamlConfigSource(String name, String source, int ordinal) {
this(name, stringToMap(source), ordinal);
}

@Override
public Set<String> getPropertyNames() {
return propertyNames;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -136,4 +150,9 @@ private static void escapeCommas(StringBuilder b, String src, int escapeLevel) {
b.appendCodePoint(cp);
}
}

private static Set<String> filterIndexedNames(Set<String> names) {
final Pattern pattern = Pattern.compile(".*\\[[0-9]+].*");
return names.stream().filter(s -> !pattern.matcher(s).find()).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class BasicTest {

class BasicTest {
@Test
public void testBasicKeyValue() {
void basicKeyValue() {
String yaml = "foo:\n"
+ " bar:\n"
+ " baz: something\n"
Expand All @@ -23,7 +21,7 @@ public void testBasicKeyValue() {
}

@Test
public void testNullKeyValue() {
void nullKeyValue() {
String yaml = "foo:\n"
+ " ~: something\n";

Expand All @@ -33,7 +31,7 @@ public void testNullKeyValue() {
}

@Test
public void testListValue() {
void listValue() {
String yaml = "foo:\n"
+ " bar:\n"
+ " ~:\n"
Expand All @@ -47,22 +45,7 @@ public void testListValue() {
}

@Test
@Disabled
public void testListOfListValue() {
String yaml = "foo:\n"
+ " bar:\n"
+ " ~:\n"
+ " - [cat, dog]\n"
+ " - [mouse, rat]\n"
+ " - [chicken, turkey]\n";

ConfigSource src = new YamlConfigSource("Yaml", yaml);

assertEquals("cat\\,dog,mouse\\,rat,chicken\\,turkey", src.getValue("foo.bar"));
}

@Test
public void testEmptyFile() {
void EmptyFile() {
String yaml = "";
ConfigSource src = new YamlConfigSource("Yaml", yaml);
assertNotNull(src, "Should create config source for empty file correctly");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.eclipse.microprofile.config.spi.Converter;
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;

public class YamlConfigSourceTest {
@Test
void flatten() throws Exception {
Expand All @@ -34,6 +41,61 @@ void profiles() throws Exception {
assertEquals("prod", yaml.getValue("%prod.foo.bar"));
}

@Test
void list() {
String yaml = "quarkus:\n" +
" http:\n" +
" ssl:\n" +
" protocols:\n" +
" - TLSv1.2\n" +
" - TLSv1.3";

SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(new YamlConfigSource("Yaml", yaml)).build();
String[] values = config.getValue("quarkus.http.ssl.protocols", String[].class);
assertEquals(2, values.length);
assertEquals("TLSv1.2", values[0]);
assertEquals("TLSv1.3", values[1]);

List<String> list = config.getValues("quarkus.http.ssl.protocols", String.class, ArrayList::new);
assertEquals(2, list.size());
assertEquals("TLSv1.2", list.get(0));
assertEquals("TLSv1.3", list.get(1));
}

@Test
void config() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-216.yml")))
.withConverter(Users.class, 100, new UserConverter())
.build();

final Users users = config.getValue("admin.users", Users.class);
assertEquals(2, users.getUsers().size());
assertEquals(users.users.get(0).getEmail(), "[email protected]");
assertEquals(users.users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList()));

assertEquals("[email protected]", config.getRawValue("admin.users.[0].email"));
}

@Test
void propertyNames() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example.yml")))
.withConverter(Users.class, 100, new UserConverter())
.build();

final List<String> propertyNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.collect(toList());

assertTrue(propertyNames.contains("quarkus.http.port"));
assertTrue(propertyNames.contains("quarkus.http.ssl-port"));
assertTrue(propertyNames.contains("quarkus.http.ssl.protocols"));
assertFalse(propertyNames.contains("quarkus.http.ssl.protocols.[0]"));
assertEquals("TLSv1.2", config.getRawValue("quarkus.http.ssl.protocols.[0]"));
assertFalse(propertyNames.contains("quarkus.http.ssl.protocols.[1]"));
assertEquals("TLSv1.3", config.getRawValue("quarkus.http.ssl.protocols.[1]"));
}

public static class Users {
List<User> users;

Expand Down
22 changes: 22 additions & 0 deletions sources/yaml/src/test/resources/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
quarkus:
http:
port: 8081
ssl-port: 2443
cors:
~: true
access-control-max-age: 24H
exposed-headers: "SOME-HEADER"
methods: GET,PUT,POST,DELETE,OPTIONS
ssl:
protocols:
- TLSv1.2
- TLSv1.3
cipher-suites:
- TLS_AES_128_GCM_SHA256
- TLS_AES_256_GCM_SHA384
- TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
- TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
- TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
swagger-ui:
always-include: true

0 comments on commit b3eb149

Please sign in to comment.