Skip to content

Commit d10aee9

Browse files
wip
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent 0de511a commit d10aee9

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
- [AWS Policy as code OPA](https://github.com/ik-infrastructure-testing/aws-infra-policy-as-code-with-terraform-fork)
2424
- [Teraform rego](https://developer.hashicorp.com/terraform/cloud-docs/policy-enforcement/define-policies/opa)
2525

26+
27+
- [Blog with terraform](https://www.scalr.com/blog/opa-series-part-1-open-policy-agent-and-terraform)
28+
2629
---
2730

2831
![](https://img.shields.io/github/commit-activity/m/ik-learning/opa-learn)

playground/ex28/input.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"attributes": {
3+
"request": {
4+
"http": {
5+
"method": "GET",
6+
"path": "/people/",
7+
"headers": {
8+
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiZ3Vlc3QiLCJzdWIiOiJZV3hwWTJVPSIsIm5iZiI6MTUxNDg1MTEzOSwiZXhwIjoxNjQxMDgxNTM5fQ.K5DnnbbIOspRbpCr2IKXE9cPVatGOCBrBQobQmBmaeU"
9+
}
10+
}
11+
}
12+
}
13+
}

playground/ex28/policy.rego

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package envoy.authz
2+
3+
# https://www.openpolicyagent.org/docs/latest/envoy-primer/
4+
5+
import input.attributes.request.http
6+
7+
default allow := false
8+
9+
allow if {
10+
is_token_valid
11+
action_allowed
12+
}
13+
14+
is_token_valid if {
15+
token.valid
16+
now := time.now_ns() / 1000000000
17+
token.payload.nbf <= now
18+
now < token.payload.exp
19+
}
20+
21+
action_allowed if {
22+
http.method == "GET"
23+
token.payload.role == "guest"
24+
glob.match("/people/*", ["/"], http.path)
25+
}
26+
27+
action_allowed if {
28+
http.method == "GET"
29+
token.payload.role == "admin"
30+
glob.match("/people/*", ["/"], http.path)
31+
}
32+
33+
action_allowed if {
34+
http.method == "POST"
35+
token.payload.role == "admin"
36+
glob.match("/people", ["/"], http.path)
37+
lower(input.parsed_body.firstname) != base64url.decode(token.payload.sub)
38+
}
39+
40+
token := {"valid": valid, "payload": payload} if {
41+
[_, encoded] := split(http.headers.authorization, " ")
42+
[valid, _, payload] := io.jwt.decode_verify(encoded, {"secret": "secret"})
43+
}

playground/ex29/policy.rego

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package terraform.module
2+
3+
deny contains msg if {
4+
desc := resources[r].values.description
5+
contains(desc, "HTTP")
6+
msg := sprintf("No security groups should be using HTTP. Resource in violation: %v", [r.address])
7+
}
8+
9+
resources := {r |
10+
some path, value
11+
12+
# Walk over the JSON tree and check if the node we are
13+
# currently on is a module (either root or child) resources
14+
# value.
15+
walk(input.planned_values, [path, value])
16+
17+
# Look for resources in the current value based on path
18+
rs := module_resources(path, value)
19+
20+
# Aggregate them into `resources`
21+
r := rs[_]
22+
}
23+
24+
# Variant to match root_module resources
25+
module_resources(path, value) := rs if {
26+
# Expect something like:
27+
#
28+
# {
29+
# "root_module": {
30+
# "resources": [...],
31+
# ...
32+
# }
33+
# ...
34+
# }
35+
#
36+
# Where the path is [..., "root_module", "resources"]
37+
38+
reverse_index(path, 1) == "resources"
39+
reverse_index(path, 2) == "root_module"
40+
rs := value
41+
}
42+
43+
# Variant to match child_modules resources
44+
module_resources(path, value) := rs if {
45+
# Expect something like:
46+
#
47+
# {
48+
# ...
49+
# "child_modules": [
50+
# {
51+
# "resources": [...],
52+
# ...
53+
# },
54+
# ...
55+
# ]
56+
# ...
57+
# }
58+
#
59+
# Where the path is [..., "child_modules", 0, "resources"]
60+
# Note that there will always be an index int between `child_modules`
61+
# and `resources`. We know that walk will only visit each one once,
62+
# so we shouldn't need to keep track of what the index is.
63+
64+
reverse_index(path, 1) == "resources"
65+
reverse_index(path, 3) == "child_modules"
66+
rs := value
67+
}
68+
69+
reverse_index(path, idx) := value if {
70+
value := path[count(path) - idx]
71+
}

0 commit comments

Comments
 (0)