Skip to content

Commit

Permalink
fixup! ping existing plugin if re-registration for same callback rece…
Browse files Browse the repository at this point in the history
…ived
  • Loading branch information
andrewazores committed Apr 24, 2024
1 parent 2dc1112 commit d6dc20e
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/main/java/io/cryostat/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -191,19 +192,6 @@ public Response register(@Context RoutingContext ctx, JsonObject body)
String realmName = body.getString("realm");
URI callbackUri = new URI(body.getString("callback"));

DiscoveryPlugin.<DiscoveryPlugin>find("callback", callbackUri)
.singleResultOptional()
.ifPresent(
plugin -> {
try {
var cb = PluginCallback.create(plugin);
cb.ping();
} catch (Exception e) {
logger.error(e);
plugin.delete();
}
});

// TODO apply URI range validation to the remote address
InetAddress remoteAddress = getRemoteAddress(ctx);
URI location;
Expand All @@ -222,6 +210,28 @@ public Response register(@Context RoutingContext ctx, JsonObject body)
location = jwtFactory.getPluginLocation(plugin);
jwtFactory.parseDiscoveryPluginJwt(plugin, priorToken, location, remoteAddress, false);
} else {
// check if a plugin record with the same callback already exists. If it does, ping it:
// if it's still there reject this request as a duplicate, otherwise delete the previous
// record and accept this new one as a replacement
URI unauthCallback = UriBuilder.fromUri(callbackUri).userInfo(null).build();
DiscoveryPlugin.<DiscoveryPlugin>find("callback", unauthCallback)
.singleResultOptional()
.ifPresent(
p -> {
try {
var cb = PluginCallback.create(p);
cb.ping();
throw new IllegalArgumentException(
String.format(
"Plugin with callback %s already exists and is"
+ " still reachable",
unauthCallback));
} catch (Exception e) {
logger.error(e);
p.delete();
}
});

// new plugin registration
plugin = new DiscoveryPlugin();
plugin.callback = callbackUri;
Expand Down

0 comments on commit d6dc20e

Please sign in to comment.