Skip to content

Commit

Permalink
fix(credentials): store credentials with non-conflicting matchExpress…
Browse files Browse the repository at this point in the history
…ion (#134)

* fix(credentials): store credentials with non-conflicting matchExpression

* fixup! fix(credentials): store credentials with non-conflicting matchExpression
  • Loading branch information
andrewazores authored May 31, 2023
1 parent 8d2eb41 commit da13d58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/main/java/io/cryostat/agent/CryostatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public CompletableFuture<PluginInfo> register(PluginInfo pluginInfo, URI callbac
}

public CompletableFuture<Integer> submitCredentialsIfRequired(
int prevId, Credentials credentials) {
int prevId, Credentials credentials, URI callback) {
if (prevId < 0) {
return submitCredentials(credentials);
return submitCredentials(credentials, callback);
}
HttpGet req = new HttpGet(baseUri.resolve(CREDENTIALS_API_PATH + "/" + prevId));
log.info("{}", req);
Expand All @@ -185,11 +185,11 @@ public CompletableFuture<Integer> submitCredentialsIfRequired(
if (exists) {
return CompletableFuture.completedFuture(prevId);
}
return submitCredentials(credentials);
return submitCredentials(credentials, callback);
});
}

private CompletableFuture<Integer> submitCredentials(Credentials credentials) {
private CompletableFuture<Integer> submitCredentials(Credentials credentials, URI callback) {
HttpPost req = new HttpPost(baseUri.resolve(CREDENTIALS_API_PATH));
MultipartEntityBuilder entityBuilder =
MultipartEntityBuilder.create()
Expand All @@ -211,7 +211,7 @@ private CompletableFuture<Integer> submitCredentials(Credentials credentials) {
FormBodyPartBuilder.create(
"matchExpression",
new StringBody(
selfMatchExpression(),
selfMatchExpression(callback),
ContentType.TEXT_PLAIN))
.build());
log.info("{}", req);
Expand Down Expand Up @@ -353,8 +353,8 @@ private CountingInputStream getRecordingInputStream(Path filePath) throws IOExce
return new CountingInputStream(new BufferedInputStream(Files.newInputStream(filePath)));
}

private String selfMatchExpression() {
return String.format("target.jvmId == \"%s\"", this.jvmId);
private String selfMatchExpression(URI callback) {
return String.format("target.connectUrl == \"%s\"", callback);
}

private boolean isOkStatus(HttpResponse res) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/cryostat/agent/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ public static WebServer provideWebServer(
ScheduledExecutorService executor,
@Named(ConfigModule.CRYOSTAT_AGENT_WEBSERVER_HOST) String host,
@Named(ConfigModule.CRYOSTAT_AGENT_WEBSERVER_PORT) int port,
@Named(ConfigModule.CRYOSTAT_AGENT_CALLBACK) URI callback,
Lazy<Registration> registration,
@Named(ConfigModule.CRYOSTAT_AGENT_REGISTRATION_RETRY_MS) int registrationRetryMs) {
return new WebServer(
remoteContexts, cryostat, executor, host, port, registration, registrationRetryMs);
remoteContexts,
cryostat,
executor,
host,
port,
callback,
registration,
registrationRetryMs);
}

@Provides
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/cryostat/agent/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -77,6 +78,7 @@ class WebServer {
private final String host;
private final int port;
private final Credentials credentials;
private final URI callback;
private final Lazy<Registration> registration;
private final int registrationRetryMs;
private HttpServer http;
Expand All @@ -92,6 +94,7 @@ class WebServer {
ScheduledExecutorService executor,
String host,
int port,
URI callback,
Lazy<Registration> registration,
int registrationRetryMs) {
this.remoteContexts = remoteContexts;
Expand All @@ -100,6 +103,7 @@ class WebServer {
this.host = host;
this.port = port;
this.credentials = new Credentials();
this.callback = callback;
this.registration = registration;
this.registrationRetryMs = registrationRetryMs;

Expand Down Expand Up @@ -145,7 +149,7 @@ CompletableFuture<Void> generateCredentials() throws NoSuchAlgorithmException {
this.credentials.regenerate();
return this.cryostat
.get()
.submitCredentialsIfRequired(this.credentialId, this.credentials)
.submitCredentialsIfRequired(this.credentialId, this.credentials, this.callback)
.handle(
(v, t) -> {
if (t != null) {
Expand Down

0 comments on commit da13d58

Please sign in to comment.