Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom targets URI range refactoring #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/main/java/io/cryostat/Producers.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.cryostat.core.reports.InterruptibleReportGenerator;
import io.cryostat.core.sys.Clock;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.util.URIRange;

import io.quarkus.arc.DefaultBean;
import io.vertx.mutiny.core.Vertx;
Expand All @@ -44,17 +43,6 @@ public class Producers {

public static final String BASE64_URL = "BASE64_URL";

@ConfigProperty(name = ConfigProperties.URI_RANGE)
String uriRange;

@Produces
@ApplicationScoped
@DefaultBean
public URIUtil produceURIUtil() {
URIRange range = URIRange.fromString(uriRange);
return new URIUtil(range);
}

@Produces
@ApplicationScoped
@DefaultBean
Expand Down
70 changes: 0 additions & 70 deletions src/main/java/io/cryostat/URIUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
import javax.management.remote.JMXServiceURL;

import io.cryostat.ConfigProperties;
import io.cryostat.URIUtil;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.targets.Target;
import io.cryostat.targets.Target.Annotations;
import io.cryostat.targets.Target.EventKind;
import io.cryostat.util.URIUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down
67 changes: 17 additions & 50 deletions src/main/java/io/cryostat/discovery/CustomDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.remote.JMXServiceURL;

import io.cryostat.ConfigProperties;
import io.cryostat.URIUtil;
import io.cryostat.V2Response;
import io.cryostat.credentials.Credential;
import io.cryostat.expressions.MatchExpression;
import io.cryostat.targets.JvmIdException;
import io.cryostat.targets.Target;
import io.cryostat.targets.Target.Annotations;
import io.cryostat.targets.TargetConnectionManager;
import io.cryostat.util.URIUtil;

import io.quarkus.narayana.jta.QuarkusTransaction;
import io.quarkus.runtime.StartupEvent;
Expand Down Expand Up @@ -74,9 +72,6 @@ public class CustomDiscovery {
@Inject TargetConnectionManager connectionManager;
@Inject URIUtil uriUtil;

@ConfigProperty(name = ConfigProperties.URI_RANGE)
String uriRange;

@ConfigProperty(name = ConfigProperties.CONNECTIONS_FAILED_TIMEOUT)
Duration timeout;

Expand All @@ -103,28 +98,14 @@ void onStart(@Observes StartupEvent evt) {
public Response create(
Target target, @RestQuery boolean dryrun, @RestQuery boolean storeCredentials) {
try {
// Determine if the target.connectUrl is a JMX URL
if (target.connectUrl.toString().startsWith("service:jmx:")) {
JMXServiceURL jmxUrl = new JMXServiceURL(target.connectUrl.toString());
if (!uriUtil.validateJmxServiceURL(jmxUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided JMX URI \"%s\" is unacceptable with the"
+ " current URI range settings \"%s\".",
target.connectUrl, uriRange))
.build();
}
} else {
if (!uriUtil.validateUri(target.connectUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided URI \"%s\" is unacceptable with the"
+ " current URI range settings \"%s\".",
target.connectUrl, uriRange))
.build();
}
if (!uriUtil.validateUri(target.connectUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided URI \"%s\" is unacceptable with the"
+ " current URI range settings.",
target.connectUrl))
.build();
}
// Continue with target creation if URI is valid...
} catch (Exception e) {
Expand Down Expand Up @@ -179,28 +160,14 @@ Response doV2Create(
}
try {
target.connectUrl = sanitizeConnectUrl(target.connectUrl.toString());

if (target.connectUrl.toString().startsWith("service:jmx:")) {
JMXServiceURL jmxUrl = new JMXServiceURL(target.connectUrl.toString());
if (!uriUtil.validateJmxServiceURL(jmxUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided JMX URI \"%s\" is unacceptable with the"
+ " current URI range settings \"%s\".",
target.connectUrl, uriRange))
.build();
}
} else {
if (!uriUtil.validateUri(target.connectUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided URI \"%s\" is unacceptable with the"
+ " current URI range settings \"%s\".",
target.connectUrl, uriRange))
.build();
}
if (!uriUtil.validateUri(target.connectUrl)) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"The provided URI \"%s\" is unacceptable with the"
+ " current URI range settings.",
target.connectUrl))
.build();
}

try {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/io/cryostat/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
import java.util.Set;
import java.util.UUID;

import io.cryostat.ConfigProperties;
import io.cryostat.URIUtil;
import io.cryostat.discovery.DiscoveryPlugin.PluginCallback;
import io.cryostat.targets.TargetConnectionManager;
import io.cryostat.util.URIUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -98,9 +97,6 @@ public class Discovery {
@ConfigProperty(name = "cryostat.discovery.plugins.ping-period")
Duration discoveryPingPeriod;

@ConfigProperty(name = ConfigProperties.URI_RANGE)
String uriRange;

@Inject Logger logger;
@Inject ObjectMapper mapper;
@Inject EventBus bus;
Expand Down Expand Up @@ -188,6 +184,7 @@ public RestResponse<Void> checkRegistration(
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public Response register(@Context RoutingContext ctx, JsonObject body)
throws URISyntaxException,
MalformedURLException,
JOSEException,
UnknownHostException,
SocketException,
Expand All @@ -205,9 +202,9 @@ public Response register(@Context RoutingContext ctx, JsonObject body)
return Response.status(Response.Status.BAD_REQUEST)
.entity(
String.format(
"cryostat.target.callback URI of \"%s\" is unacceptable with"
+ " URI range \"%s\"",
callbackUri, uriRange))
"cryostat.target.callback of \"%s\" is unacceptable with the"
+ " current URI range settings",
callbackUri))
.build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/cryostat/discovery/JDPDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

import javax.management.remote.JMXServiceURL;

import io.cryostat.URIUtil;
import io.cryostat.core.net.discovery.JvmDiscoveryClient;
import io.cryostat.core.net.discovery.JvmDiscoveryClient.JvmDiscoveryEvent;
import io.cryostat.targets.Target;
import io.cryostat.targets.Target.Annotations;
import io.cryostat.util.URIUtil;

import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/io/cryostat/util/URIUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.util;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.management.remote.JMXServiceURL;

import org.openjdk.jmc.rjmx.common.ConnectionToolkit;

import io.cryostat.ConfigProperties;
import io.cryostat.core.net.JFRConnectionToolkit;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.UriBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class URIUtil {

@Inject JFRConnectionToolkit toolkit;

@ConfigProperty(name = ConfigProperties.URI_RANGE)
String uriRange;

public URI getRmiTarget(JMXServiceURL serviceUrl) throws URISyntaxException {
return UriBuilder.newInstance()
.host(toolkit.getHostName(serviceUrl))
.port(toolkit.getPort(serviceUrl))
.build();
}

public boolean validateUri(URI uri) throws MalformedURLException {
if (isJmxUrl(uri)) {
return validateJmxServiceURL(new JMXServiceURL(uri.toString()));
}
return URIRange.fromString(uriRange).validate(uri.getHost());
}

boolean isJmxUrl(URI uri) {
try {
new JMXServiceURL(uri.toString());
return true;
} catch (MalformedURLException e) {
return false;
}
}

boolean validateJmxServiceURL(JMXServiceURL jmxUrl) {
String hostname = ConnectionToolkit.getHostName(jmxUrl);
return URIRange.fromString(uriRange).validate(hostname);
}
}
31 changes: 29 additions & 2 deletions src/test/java/io/cryostat/util/URIRangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,40 @@

import javax.management.remote.JMXServiceURL;

import io.cryostat.URIUtil;
import io.cryostat.core.net.JFRConnectionToolkit;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class URIRangeTest {

JFRConnectionToolkit toolkit;

@BeforeEach
void setup() {
this.toolkit = Mockito.mock(JFRConnectionToolkit.class);
Mockito.when(toolkit.getHostName(Mockito.any()))
.thenAnswer(
new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
JMXServiceURL serviceUrl = invocation.getArgument(0);
String rmiPart = "/jndi/rmi://";
String pathPart = serviceUrl.getURLPath();
if (!pathPart.startsWith(rmiPart)) {
throw new IllegalArgumentException(serviceUrl.getURLPath());
}
return pathPart.substring("/jndi/".length(), pathPart.length());
}
});
}

@ParameterizedTest
@CsvSource({
"127.0.0.1, true",
Expand Down Expand Up @@ -173,7 +198,9 @@ public void testPublicRange(String s, String v)
private void test(URIRange range, String s, String v)
throws URISyntaxException, MalformedURLException {
boolean expected = Boolean.parseBoolean(v);
URIUtil uriUtil = new URIUtil(range);
URIUtil uriUtil = new URIUtil();
uriUtil.toolkit = toolkit;
uriUtil.uriRange = range.toString();

URI httpUri = new URI(String.format("http://%s:1234", s));
MatcherAssert.assertThat(s, uriUtil.validateUri(httpUri), Matchers.is(expected));
Expand Down
Loading