Skip to content

Commit

Permalink
style(basicauthmanager): deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jul 27, 2021
1 parent 73041a7 commit 3b8084e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
13 changes: 2 additions & 11 deletions src/main/java/io/cryostat/net/BasicAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,12 @@ public Future<Boolean> validateToken(
boolean granted = Objects.equals(users.getProperty(user), passHashHex);
// FIXME actually implement this
resourceActions.forEach(
action -> {
if (granted) {
action ->
logger.trace(
"user {} granted {} {}",
user,
action.getVerb(),
action.getResource());
} else {
logger.trace(
"user {} granted {} {}",
user,
action.getVerb(),
action.getResource());
}
});
action.getResource()));
return CompletableFuture.completedFuture(granted);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/cryostat/net/NetworkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*/
package io.cryostat.net;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/io/cryostat/net/OpenShiftAuthManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.cryostat.core.log.Logger;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.net.security.ResourceType;
import io.cryostat.net.security.ResourceVerb;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReview;
import io.fabric8.kubernetes.api.model.authorization.v1.SelfSubjectAccessReviewBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand All @@ -69,7 +71,6 @@
import jdk.jfr.Event;
import jdk.jfr.Label;
import jdk.jfr.Name;
import org.apache.commons.lang3.StringUtils;

public class OpenShiftAuthManager extends AbstractAuthManager {

Expand Down Expand Up @@ -105,7 +106,7 @@ public Future<Boolean> validateToken(
resourceAction -> {
try {
return CompletableFuture.<Boolean>completedFuture(
validateToken(authClient, resourceAction));
validateAction(authClient, resourceAction));
} catch (IOException | PermissionDeniedException e) {
return CompletableFuture.<Boolean>failedFuture(e);
}
Expand All @@ -123,6 +124,7 @@ public Future<Boolean> validateToken(
try {
return result.get();
} catch (InterruptedException | ExecutionException e) {
// should never actually end up in here due to the allOf
logger.error(e);
return false;
}
Expand All @@ -140,7 +142,7 @@ public Future<Boolean> validateToken(
}
}

private boolean validateToken(OpenShiftClient authClient, ResourceAction resourceAction)
private boolean validateAction(OpenShiftClient authClient, ResourceAction resourceAction)
throws IOException, PermissionDeniedException {
AuthRequest evt = new AuthRequest();
evt.begin();
Expand All @@ -153,7 +155,7 @@ private boolean validateToken(OpenShiftClient authClient, ResourceAction resourc
return true;
}
String namespace = getNamespace();
var accessReview =
SelfSubjectAccessReview accessReview =
new SelfSubjectAccessReviewBuilder()
.withNewSpec()
.withNewResourceAttributes()
Expand All @@ -164,15 +166,15 @@ private boolean validateToken(OpenShiftClient authClient, ResourceAction resourc
.endResourceAttributes()
.endSpec()
.build();
var response =
accessReview =
authClient.authorization().v1().selfSubjectAccessReview().create(accessReview);
boolean allowed = response.getStatus().getAllowed();
boolean allowed = accessReview.getStatus().getAllowed();
evt.setRequestSuccessful(true);
if (allowed) {
return true;
} else {
throw new PermissionDeniedException(
namespace, group, resource, verb, response.getStatus().getReason());
namespace, group, resource, verb, accessReview.getStatus().getReason());
}
} finally {
if (evt.shouldCommit()) {
Expand Down

0 comments on commit 3b8084e

Please sign in to comment.