Skip to content

Commit

Permalink
fix(config): trim space from hostname tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ebaron committed Nov 22, 2024
1 parent fc86465 commit 3a84027
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/io/cryostat/agent/ConfigModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ private static List<String> computeHostNames(String[] hostNames) {
ScriptHost scriptHost = ScriptHost.newBuilder().build();
List<String> result = new ArrayList<>(hostNames.length);
for (String hostName : hostNames) {
hostName = hostName.trim();
Matcher m = HOST_SCRIPT_PATTERN.matcher(hostName);
if (!m.matches()) {
throw new RuntimeException(
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/io/cryostat/agent/ConfigModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,31 @@ void testCallbackComponentsNoMatch() throws Exception {
RuntimeException.class, () -> ConfigModule.provideCryostatAgentCallback(config));
}

@Test
void testCallbackComponentsWithSpace() throws Exception {
setupCallbackComponents(new String[] {"foo", " 10.2.3.4[replace(\".\", \"-\")]"});

when(addr.getHostAddress()).thenReturn("10.2.3.4");
addrMock.when(() -> InetAddress.getByName("foo.headless.svc.example.com"))
.thenThrow(new UnknownHostException("TEST"));
addrMock.when(() -> InetAddress.getByName("10-2-3-4.headless.svc.example.com"))
.thenReturn(addr);

URI result = ConfigModule.provideCryostatAgentCallback(config);
assertEquals("https://10-2-3-4.headless.svc.example.com:9977", result.toASCIIString());
}

private void setupCallbackComponents() {
setupCallbackComponents(new String[] {"foo", "10.2.3.4[replace(\".\", \"-\")]"});
}

private void setupCallbackComponents(String[] hostnames) {
when(config.getOptionalValue(
ConfigModule.CRYOSTAT_AGENT_CALLBACK_DOMAIN_NAME, String.class))
.thenReturn(Optional.of("headless.svc.example.com"));
when(config.getOptionalValue(
ConfigModule.CRYOSTAT_AGENT_CALLBACK_HOST_NAME, String[].class))
.thenReturn(Optional.of(new String[] {"foo", "10.2.3.4[replace(\".\", \"-\")]"}));
.thenReturn(Optional.of(hostnames));
when(config.getOptionalValue(ConfigModule.CRYOSTAT_AGENT_CALLBACK_PORT, Integer.class))
.thenReturn(Optional.of(9977));
when(config.getOptionalValue(ConfigModule.CRYOSTAT_AGENT_CALLBACK_SCHEME, String.class))
Expand Down

0 comments on commit 3a84027

Please sign in to comment.