Skip to content

Commit

Permalink
fix(credentials): query for Agent HTTP credentials without specific u…
Browse files Browse the repository at this point in the history
…serinfo
  • Loading branch information
andrewazores committed Sep 25, 2023
1 parent f7df109 commit cbe7f07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/main/java/io/cryostat/configuration/CredentialsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.cryostat.configuration;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -38,13 +40,15 @@
import io.cryostat.rules.MatchExpressionEvaluator;
import io.cryostat.rules.MatchExpressionValidationException;
import io.cryostat.rules.MatchExpressionValidator;
import io.cryostat.util.URIUtil;
import io.cryostat.util.events.AbstractEventEmitter;
import io.cryostat.util.events.EventType;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import dagger.Lazy;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;

public class CredentialsManager
extends AbstractEventEmitter<CredentialsManager.CredentialsEvent, String> {
Expand Down Expand Up @@ -160,12 +164,27 @@ public int removeCredentials(String matchExpression) throws MatchExpressionValid
}

public Credentials getCredentialsByTargetId(String targetId) throws ScriptException {
for (ServiceRef service : this.platformClient.listDiscoverableServices()) {
if (Objects.equals(targetId, service.getServiceUri().toString())) {
return getCredentials(service);
try {
for (ServiceRef service : this.platformClient.listDiscoverableServices()) {
URI uri = service.getServiceUri();
boolean match = false;
boolean isJmx = URIUtil.isJmxUrl(uri);
if (isJmx) {
match = Objects.equals(uri.toString(), targetId);
} else {
URI in = new URI(targetId);
match = Objects.equals(uri, in);
URI userless = new URIBuilder(uri).setUserInfo(null).build();
match |= Objects.equals(userless, in);
}
if (match) {
return getCredentials(service);
}
}
return null;
} catch (URISyntaxException use) {
throw new IllegalStateException(use);
}
return null;
}

public Credentials getCredentials(ServiceRef serviceRef) throws ScriptException {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/cryostat/util/URIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.cryostat.util;

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

Expand All @@ -39,6 +40,19 @@ public static URI convert(JMXServiceURL serviceUrl) throws URISyntaxException {
return new URI(serviceUrl.toString());
}

public static boolean isJmxUrl(URI uri) {
return isJmxUrl(uri.toString());
}

public static boolean isJmxUrl(String uri) {
try {
new JMXServiceURL(uri);
return true;
} catch (MalformedURLException mue) {
return false;
}
}

public static boolean isRmiUrl(JMXServiceURL serviceUrl) {
String rmiPart = "/jndi/rmi://";
String pathPart = serviceUrl.getURLPath();
Expand Down

0 comments on commit cbe7f07

Please sign in to comment.