Skip to content

Commit

Permalink
test(credentials): add remaining CredentialsManagerTests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jun 28, 2022
1 parent 793e65c commit 16272a1
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 12 deletions.
71 changes: 67 additions & 4 deletions src/main/java/io/cryostat/configuration/CredentialsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public boolean removeCredentials(String matchExpression) throws IOException {
return deleted != null;
}

public Credentials getCredentials(String targetId) {
public Credentials getCredentialsByTargetId(String targetId) {
for (ServiceRef service : this.platformClient.listDiscoverableServices()) {
if (Objects.equals(targetId, service.getServiceUri().toString())) {
return getCredentials(service);
Expand Down Expand Up @@ -213,8 +213,6 @@ public List<MatchedCredentials> getMatchExpressionsWithMatchedTargets() {
List<ServiceRef> targets = platformClient.listDiscoverableServices();
for (String expr : getMatchExpressions()) {
Set<ServiceRef> matchedTargets = new HashSet<>();
MatchedCredentials match = new MatchedCredentials(expr, matchedTargets);
result.add(match);
for (ServiceRef target : targets) {
try {
if (matchExpressionEvaluator.applies(expr, target)) {
Expand All @@ -225,6 +223,8 @@ public List<MatchedCredentials> getMatchExpressionsWithMatchedTargets() {
continue;
}
}
MatchedCredentials match = new MatchedCredentials(expr, matchedTargets);
result.add(match);
}
return result;
}
Expand All @@ -240,7 +240,7 @@ public static class MatchedCredentials {
private final String matchExpression;
private final Collection<ServiceRef> targets;

private MatchedCredentials(String matchExpression, Collection<ServiceRef> targets) {
MatchedCredentials(String matchExpression, Collection<ServiceRef> targets) {
this.matchExpression = matchExpression;
this.targets = new HashSet<>(targets);
}
Expand All @@ -252,6 +252,27 @@ public String getMatchExpression() {
public Collection<ServiceRef> getTargets() {
return Collections.unmodifiableCollection(targets);
}

@Override
public int hashCode() {
return Objects.hash(matchExpression, targets);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MatchedCredentials other = (MatchedCredentials) obj;
return Objects.equals(matchExpression, other.matchExpression)
&& Objects.equals(targets, other.targets);
}
}

static class StoredCredentials {
Expand All @@ -270,6 +291,27 @@ String getMatchExpression() {
Credentials getCredentials() {
return this.credentials;
}

@Override
public int hashCode() {
return Objects.hash(credentials, matchExpression);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
StoredCredentials other = (StoredCredentials) obj;
return Objects.equals(credentials, other.credentials)
&& Objects.equals(matchExpression, other.matchExpression);
}
}

@Deprecated(since = "2.2", forRemoval = true)
Expand All @@ -289,5 +331,26 @@ String getTargetId() {
Credentials getCredentials() {
return this.credentials;
}

@Override
public int hashCode() {
return Objects.hash(credentials, targetId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TargetSpecificStoredCredentials other = (TargetSpecificStoredCredentials) obj;
return Objects.equals(credentials, other.credentials)
&& Objects.equals(targetId, other.targetId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected Future<Boolean> validateRequestAuthorization(HttpServerRequest req) th

protected ConnectionDescriptor getConnectionDescriptorFromContext(RoutingContext ctx) {
String targetId = ctx.pathParam("targetId");
Credentials credentials = credentialsManager.getCredentials(targetId);
Credentials credentials = credentialsManager.getCredentialsByTargetId(targetId);
if (ctx.request().headers().contains(JMX_AUTHORIZATION_HEADER)) {
String proxyAuth = ctx.request().getHeader(JMX_AUTHORIZATION_HEADER);
Matcher m = AUTH_HEADER_PATTERN.matcher(proxyAuth);
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/io/cryostat/rules/RuleProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ private void activate(Rule rule, ServiceRef serviceRef) {
"Activating rule {} for target {}", rule.getName(), serviceRef.getServiceUri());

vertx.<Credentials>executeBlocking(
promise ->
promise.complete(
credentialsManager.getCredentials(
serviceRef.getServiceUri().toString())))
promise -> promise.complete(credentialsManager.getCredentials(serviceRef)))
.onSuccess(c -> logger.trace("Rule activation successful"))
.onSuccess(
credentials -> {
Expand Down
Loading

0 comments on commit 16272a1

Please sign in to comment.