Skip to content

Commit

Permalink
fix(config): trim space from hostname tokens (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebaron authored Nov 25, 2024
1 parent fc86465 commit c4d90cf
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 c4d90cf

Please sign in to comment.