Skip to content

Commit

Permalink
convert shortform to full JMXServiceURL in TargetPostHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
aali309 committed Feb 15, 2024
1 parent d429b2d commit 6e42912
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
16 changes: 1 addition & 15 deletions src/main/java/io/cryostat/configuration/CredentialsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.cryostat.configuration;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
Expand All @@ -31,7 +30,6 @@
import java.util.Optional;
import java.util.Set;

import javax.management.remote.JMXServiceURL;
import javax.script.ScriptException;

import io.cryostat.core.log.Logger;
Expand All @@ -51,7 +49,6 @@
import dagger.Lazy;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.openjdk.jmc.rjmx.ConnectionToolkit;

public class CredentialsManager
extends AbstractEventEmitter<CredentialsManager.CredentialsEvent, String> {
Expand Down Expand Up @@ -166,25 +163,14 @@ public int removeCredentials(String matchExpression) throws MatchExpressionValid
return -1;
}

public JMXServiceURL createServiceURL(String host, int port) throws MalformedURLException {
return ConnectionToolkit.createServiceURL(host, port);
}

public Credentials getCredentialsByTargetId(String targetId) throws ScriptException {
try {
for (ServiceRef service : this.platformClient.listDiscoverableServices()) {
URI uri = service.getServiceUri();
boolean match = false;
boolean isJmx = URIUtil.isJmxUrl(uri);
boolean isShortForm = targetId.matches("localhost:\\d+");

if (isJmx) {
match = Objects.equals(uri.toString(), targetId);
} else if (isShortForm) {
String[] parts = targetId.split(":");
String host = parts[0];
int port = Integer.parseInt(parts[1]);
targetId = ConnectionToolkit.createServiceURL(host, port).toString();
} else {
URI in = new URI(targetId);
match = Objects.equals(uri, in);
Expand All @@ -196,7 +182,7 @@ public Credentials getCredentialsByTargetId(String targetId) throws ScriptExcept
}
}
return null;
} catch (URISyntaxException | MalformedURLException use) {
} catch (URISyntaxException use) {
throw new IllegalStateException(use);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmc.rjmx.ConnectionToolkit;

class TargetsPostHandler extends AbstractV2RequestHandler<ServiceRef> {

Expand Down Expand Up @@ -129,6 +130,14 @@ public IntermediateResponse<ServiceRef> handle(RequestParameters params) throws
try {
MultiMap attrs = params.getFormAttributes();
String connectUrl = attrs.get("connectUrl");
// check incase custom target has short form connection url (i.e `localhost:0`,
// etc)
if (connectUrl.matches("localhost:\\d+")) {
String[] connectUrlParts = connectUrl.split(":");
String host = connectUrlParts[0];
int port = Integer.parseInt(connectUrlParts[1]);
connectUrl = ConnectionToolkit.createServiceURL(host, port).toString();
}
if (StringUtils.isBlank(connectUrl)) {
throw new ApiException(400, "\"connectUrl\" form parameter must be provided");
}
Expand All @@ -144,19 +153,16 @@ public IntermediateResponse<ServiceRef> handle(RequestParameters params) throws
}

MultiMap queries = params.getQueryParams();
boolean dryRun =
StringUtils.isNotBlank(queries.get("dryrun"))
&& Boolean.valueOf(queries.get("dryrun"));
boolean storeCredentials =
StringUtils.isNotBlank(queries.get("storeCredentials"))
&& Boolean.valueOf(queries.get("storeCredentials"));
boolean dryRun = StringUtils.isNotBlank(queries.get("dryrun"))
&& Boolean.valueOf(queries.get("dryrun"));
boolean storeCredentials = StringUtils.isNotBlank(queries.get("storeCredentials"))
&& Boolean.valueOf(queries.get("storeCredentials"));

String username = attrs.get("username");
String password = attrs.get("password");
Optional<Credentials> credentials =
StringUtils.isBlank(username) || StringUtils.isBlank(password)
? Optional.empty()
: Optional.of(new Credentials(username, password));
Optional<Credentials> credentials = StringUtils.isBlank(username) || StringUtils.isBlank(password)
? Optional.empty()
: Optional.of(new Credentials(username, password));

if (storeCredentials && credentials.isPresent()) {
String matchExpression = CredentialsManager.targetIdToMatchExpression(connectUrl);
Expand Down

0 comments on commit 6e42912

Please sign in to comment.