Skip to content

Commit 2a6b0df

Browse files
committed
ACC-2351 Update OPA version and add test for abac policy
1 parent 8e3f031 commit 2a6b0df

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

opa-async-java-client/src/test/java/com/contentgrid/opa/client/OpaClientIntegrationTests.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
@Testcontainers
4141
class OpaClientIntegrationTests {
4242

43-
private static final DockerImageName OPA_IMAGE = DockerImageName.parse("openpolicyagent/opa:0.70.0");
43+
private static final DockerImageName OPA_IMAGE = DockerImageName.parse("openpolicyagent/opa:1.9.0");
4444
private static final int OPA_EXPOSED_PORT = 8181;
4545

4646
@Container
4747
private static final GenericContainer<?> opaContainer = new GenericContainer<>(OPA_IMAGE)
48-
.withCommand(String.format("run --server --addr :%d", OPA_EXPOSED_PORT))
48+
.withCommand(String.format("run --server --addr :%d --v0-compatible", OPA_EXPOSED_PORT))
4949
.withExposedPorts(OPA_EXPOSED_PORT)
5050
.waitingFor(Wait.forHttp("/"));
5151

@@ -284,6 +284,54 @@ void compileExample() {
284284
})));
285285
}
286286

287+
@Test
288+
void abacExample() {
289+
opaClient.upsertPolicy("abac-example", loadResourceAsString("fixtures/openpolicyagent.org/docs/compile/abac-policy.txt")).join();
290+
291+
var request = new PartialEvaluationRequest(
292+
"data.abac.example.allow == true",
293+
Map.of("request",
294+
Map.of("headers", Map.of("content-type", "application/json"),
295+
"method", "GET",
296+
"path", List.of("tests")
297+
),
298+
"auth", Map.of(
299+
"authenticated", true,
300+
"principal", Map.of(
301+
"kind", "user",
302+
"contentgrid:att1", List.of("att1v1", "att1v2"),
303+
"contentgrid:att2", List.of("att2v1", "att2v2")
304+
)
305+
)
306+
), List.of("input.entity"));
307+
308+
var result = opaClient.compile(request).join();
309+
assertThat(result).isNotNull();
310+
assertThat(result.getResult()).isNotNull();
311+
312+
assertThat(result.getResult().getQueries())
313+
.isNotNull()
314+
// 4 queries are generated, because there are 4 combinations of the 2 attributes in the policy
315+
.hasSize(4)
316+
.allSatisfy(
317+
query -> assertThat(query)
318+
// There are 2 expressions per query, one for every user attribute in the policy
319+
.hasSize(2)
320+
.allSatisfy(expr -> {
321+
// each expression has 3 terms (the 'eq' operator, the attribute value and the input value)
322+
assertThat(expr.getTerms()).hasSize(3);
323+
assertThat(expr.getTerms()).anySatisfy(
324+
term -> {
325+
assertThat(term.getClass()).isEqualTo(Ref.class);
326+
var ref = (Ref) term;
327+
assertThat(ref.getValue()).singleElement().satisfies(
328+
t -> assertThat(t.toString()).isEqualTo("eq")
329+
);
330+
}
331+
);
332+
}));
333+
}
334+
287335
@Test
288336
void example_alwaysTrue() {
289337
opaClient.upsertPolicy("compile-example", loadResourceAsString(POLICY_PATH)).join();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package abac.example
2+
3+
util.extract_content_type(header) := content_type {
4+
mime_type := trim_space(split(header, ";")[0])
5+
content_type := lower(mime_type)
6+
}
7+
default util.content_type_in(headers, accepted_content_types) := false
8+
9+
util.content_type_in(headers, accepted_content_types) {
10+
count(headers) == 1
11+
extracted_mime_type := util.extract_content_type(headers[0])
12+
extracted_mime_type == accepted_content_types[_]
13+
}
14+
default util.request.content_type_in(content_types) := false
15+
16+
util.request.content_type_in(content_types) {
17+
util.content_type_in(input.request.headers["content-type"], content_types)
18+
}
19+
default can_read_test := false
20+
21+
# - input.entity is type 'test'
22+
can_read_test {
23+
input.auth.authenticated == true
24+
input.auth.principal.kind == "user"
25+
input.entity.a == input.auth.principal["contentgrid:att1"][_]
26+
input.entity.b == input.auth.principal["contentgrid:att2"][_]
27+
}
28+
29+
# End policy bprnvz6ldw4q
30+
# Policy nfgusztjwpba
31+
# - input.entity is type 'test'
32+
can_read_test {
33+
input.auth.authenticated == true
34+
input.auth.principal.kind == "user"
35+
input.auth.principal["contentgrid:admin"] == true
36+
}
37+
38+
# End policy nfgusztjwpba
39+
default allow := false
40+
41+
# Static definition Application Root
42+
allow {
43+
input.request.method == ["HEAD", "GET"][_]
44+
# Path /
45+
count(input.request.path) == 0
46+
}
47+
allow {
48+
input.request.method == ["HEAD", "GET"][_]
49+
# Path /tests
50+
count(input.request.path) == 1
51+
input.request.path[0] == "tests"
52+
can_read_test == true
53+
}
54+
allow {
55+
input.request.method == ["HEAD", "GET"][_]
56+
# Path /tests/{id}
57+
count(input.request.path) == 2
58+
input.request.path[0] == "tests"
59+
# variable component {id}
60+
can_read_test == true
61+
}

0 commit comments

Comments
 (0)