Skip to content

Commit a4855ba

Browse files
author
Charles Daniels
committed
add support for changing the reason key
1 parent 2d9392c commit a4855ba

14 files changed

Lines changed: 47 additions & 12 deletions

File tree

148 KB
Binary file not shown.
0 Bytes
Binary file not shown.
1.66 KB
Binary file not shown.
0 Bytes
Binary file not shown.
18.6 KB
Binary file not shown.
0 Bytes
Binary file not shown.
522 Bytes
Binary file not shown.

‎.gradle/file-system.probe‎

0 Bytes
Binary file not shown.

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# OPA-SpringBoot Changelog
22

3+
## v0.0.2 (unreleased)
4+
5+
* Rather than hard-coding `en`, the preferred key to search for decision reasons for can now be changed with `OPAAuthorizationManager.setReasonKey()`. The default remains `en`.
6+
37
## v0.0.1
48

59
* Initial release of OPA-SpringBoot

‎src/main/java/com/styra/opa/springboot/OPAAuthorizationManager.java‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class OPAAuthorizationManager
4545
// If opaPath is null, then we assume the user wants to use the default path.
4646
private String opaPath;
4747

48+
private String reasonKey;
49+
4850
private ContextDataProvider ctxProvider;
4951

5052
private OPAClient opa;
@@ -64,6 +66,7 @@ public OPAAuthorizationManager() {
6466
OPAClient opac = new OPAClient(opaURL);
6567
this.opa = opac;
6668
this.opaPath = null;
69+
this.reasonKey = "en";
6770
}
6871

6972
/**
@@ -76,6 +79,7 @@ public OPAAuthorizationManager() {
7679
public OPAAuthorizationManager(OPAClient opa) {
7780
this.opa = opa;
7881
this.opaPath = null;
82+
this.reasonKey = "en";
7983
}
8084

8185
/**
@@ -90,6 +94,7 @@ public OPAAuthorizationManager(OPAClient opa) {
9094
public OPAAuthorizationManager(OPAClient opa, String newOpaPath) {
9195
this.opa = opa;
9296
this.opaPath = newOpaPath;
97+
this.reasonKey = "en";
9398
}
9499

95100
/**
@@ -104,6 +109,7 @@ public OPAAuthorizationManager(OPAClient opa, String newOpaPath) {
104109
public OPAAuthorizationManager(OPAClient opa, ContextDataProvider newProvider) {
105110
this.opa = opa;
106111
this.ctxProvider = newProvider;
112+
this.reasonKey = "en";
107113
}
108114

109115
/**
@@ -119,6 +125,23 @@ public OPAAuthorizationManager(OPAClient opa, String newOpaPath, ContextDataProv
119125
this.opa = opa;
120126
this.opaPath = newOpaPath;
121127
this.ctxProvider = newProvider;
128+
this.reasonKey = "en";
129+
}
130+
131+
public String getReasonKey() {
132+
return this.reasonKey;
133+
}
134+
135+
/**
136+
* Changes the "preferred" key where the access decision reason should be
137+
* searched for in the OPAResponse object. A default value of 'en' is used.
138+
* If the selected key is not present in the response, the key which sorts
139+
* lexicographically first is used instead.
140+
*
141+
* @param newReasonKey
142+
*/
143+
public void setReasonKey(String newReasonKey) {
144+
this.reasonKey = newReasonKey;
122145
}
123146

124147
private Map<String, Object> makeRequestInput(
@@ -250,9 +273,9 @@ public void verify(
250273
}
251274

252275
boolean allow = resp.getDecision();
253-
String reason = "access denied by policy";
254-
if (resp.getReasonForDecision() != null) {
255-
reason = resp.getReasonForDecision();
276+
String reason = resp.getReasonForDecision(this.reasonKey);
277+
if (reason == null) {
278+
reason = "access denied by policy";
256279
}
257280

258281
if (allow) {

0 commit comments

Comments
 (0)