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

fix(credentials): query for Agent HTTP credentials without specific userinfo #1674

Merged
merged 1 commit into from
Sep 25, 2023
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
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
Loading