Skip to content

Commit e41ea57

Browse files
committed
make OpaResponse only contain bools
1 parent 1a76f89 commit e41ea57

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

authorizer/src/main/java/org/nifiopa/nifiopa/OPAResponse.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55

66
public class OPAResponse {
77

8-
private String allowed;
8+
private boolean allowed;
9+
private boolean resourceNotFound;
910
private boolean dumpCache;
1011
private String message;
1112

1213
@JsonCreator
1314
public OPAResponse(
14-
@JsonProperty("allowed") String allowed,
15-
@JsonProperty("dumpCache") boolean dumpCache,
16-
@JsonProperty("message") String message
15+
@JsonProperty("allowed") boolean allowed,
16+
@JsonProperty("resourceNotFound") boolean resourceNotFound,
17+
@JsonProperty("dumpCache") boolean dumpCache,
18+
@JsonProperty("message") String message
1719
) {
1820
this.allowed = allowed;
1921
this.dumpCache = dumpCache;
2022
this.message = message;
2123
}
2224

23-
public String allowed() {
25+
public boolean allowed() {
2426
return allowed;
2527
}
2628

29+
public boolean resourceNotFound() {
30+
return resourceNotFound;
31+
}
32+
2733
public boolean dumpCache() {
2834
return dumpCache;
2935
}

authorizer/src/main/java/org/nifiopa/nifiopa/OpaAuthorizer.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ public AuthorizationResult authorize(AuthorizationRequest request) throws Author
6666
request.getResourceContext() != null && !request.getUserContext().isEmpty() ? request.getUserContext() : Map.of("", ""));
6767
} catch (Exception e) {
6868
logger.error(
69-
"An error occured while trying to build the OPA-request", e);
70-
return AuthorizationResult.denied("An error occured while trying to build the OPA-request");
69+
"An error occured while trying to build the OPA-request.", e);
70+
return AuthorizationResult.denied("An error occured while trying to build the OPA-request.");
7171
}
7272

7373
OPAResponse opaResponse = null;
7474
try {
7575
opaResponse = opaClient.evaluate(OPA_RULE_HEAD, requestForm, OPAResponse.class);
7676
} catch (OPAException e) {
77-
logger.error(MessageFormat.format("An error occured while trying to query against OPA: {0}", e.toString()));
78-
return AuthorizationResult.denied("An error occured while trying to query against OPA");
77+
logger.error("An error occured while trying to query against OPA.", e);
78+
return AuthorizationResult.denied("An error occured while trying to query against OPA.");
7979
}
8080
if (opaResponse == null) {
8181
logger.error("An error occured while unmarshalling an OPA response.");
@@ -89,23 +89,22 @@ public AuthorizationResult authorize(AuthorizationRequest request) throws Author
8989
cache.clear();
9090
}
9191

92-
switch (opaResponse.allowed()) {
93-
case "true":
92+
if (opaResponse.resourceNotFound()) {
93+
cache.putCachedResult(request, AuthorizationResult.resourceNotFound());
94+
logger.debug("Authorizer-Result: Resource not found");
95+
return AuthorizationResult.resourceNotFound();
96+
}
97+
98+
if (opaResponse.allowed()) {
9499
cache.putCachedResult(request, AuthorizationResult.approved());
95100
logger.debug("Authorizer-Result: Access was approved");
96101
return AuthorizationResult.approved();
97-
case "unknown":
98-
cache.putCachedResult(request, AuthorizationResult.resourceNotFound());
99-
logger.debug("Authorizer-Result: No access resource found");
100-
return AuthorizationResult.resourceNotFound();
101-
default:
102+
} else {
102103
cache.putCachedResult(request, AuthorizationResult.denied());
103104
logger.debug("Authorizer-Result: Access was denied");
104105
return AuthorizationResult
105106
.denied(opaResponse.message() != null ? opaResponse.message() : "Access denied.");
106107
}
107-
108-
// enum - switch
109108
}
110109

111110
@Override

0 commit comments

Comments
 (0)