Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api_authz contrib example to match current tutorial #76

Merged
merged 4 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion api_authz/docker/docker-compose-token.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
version: '2'
services:
opa:
image: openpolicyagent/opa:0.10.5
image: openpolicyagent/opa:0.13.2
ports:
- 8181:8181
# WARNING: OPA is NOT running with an authorization policy configured. This
# means that clients can read and write policies in OPA. If you are
# deploying OPA in an insecure environment, be sure to configure
# authentication and authorization on the daemon. See the Security page for
# details: https://www.openpolicyagent.org/docs/security.html.
command:
- "run"
- "--server"
Expand Down
7 changes: 6 additions & 1 deletion api_authz/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
version: '2'
services:
opa:
image: openpolicyagent/opa:0.10.5
image: openpolicyagent/opa:0.13.2
ports:
- 8181:8181
# WARNING: OPA is NOT running with an authorization policy configured. This
# means that clients can read and write policies in OPA. If you are
# deploying OPA in an insecure environment, be sure to configure
# authentication and authorization on the daemon. See the Security page for
# details: https://www.openpolicyagent.org/docs/security.html.
command:
- "run"
- "--server"
Expand Down
16 changes: 8 additions & 8 deletions api_authz/docker/policy/api_authz.rego
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package httpapi.authz
subordinates = {"alice": [], "charlie": [], "bob": ["alice"], "betty": ["charlie"]}

# HTTP API request
import input as http_api
# http_api = {
import input
# input = {
# "path": ["finance", "salary", "alice"],
# "user": "alice",
# "method": "GET"
Expand All @@ -14,14 +14,14 @@ default allow = false

# Allow users to get their own salaries.
allow {
http_api.method = "GET"
http_api.path = ["finance", "salary", username]
username = http_api.user
input.method = "GET"
input.path = ["finance", "salary", username]
input.user == username
}

# Allow managers to get their subordinates' salaries.
allow {
http_api.method = "GET"
http_api.path = ["finance", "salary", username]
subordinates[http_api.user][_] = username
input.method = "GET"
input.path = ["finance", "salary", username]
subordinates[input.user][_] == username
}
34 changes: 19 additions & 15 deletions api_authz/docker/policy/api_authz_token.rego
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package httpapi.authz

import input as http_api
# http_api = {
# "path": ["finance", "salary", "alice"],
# "user": "alice",
# "method": "GET",
# "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWxpY2UiLCJhenAiOiJhbGljZSIsInN1Ym9yZGluYXRlcyI6W10sImhyIjpmYWxzZX0.rz3jTY033z-NrKfwrK89_dcLF7TN4gwCMj-fVBDyLoM"
# }
import input

# io.jwt.decode takes one argument (the encoded token) and has three outputs:
# the decoded header, payload and signature, in that order. Our policy only
# cares about the payload, so we ignore the others.
token = {"payload": payload} { io.jwt.decode(http_api.token, _, payload, _) }
token = {"payload": payload} { io.jwt.decode(input.token, [_, payload, _]) }

# Ensure that the token was issued to the user supplying it.
user_owns_token { http_api.user = token.payload.azp }
user_owns_token { input.user == token.payload.azp }

default allow = false

# Allow users to get their own salaries.
allow {
http_api.method = "GET"
http_api.path = ["finance", "salary", username]
username = token.payload.user
some username
input.method == "GET"
input.path = ["finance", "salary", username]
token.payload.user == username
user_owns_token
}

# Allow managers to get their subordinate' salaries.
allow {
http_api.method = "GET"
http_api.path = ["finance", "salary", username]
token.payload.subordinates[_] = username
some username
input.method == "GET"
input.path = ["finance", "salary", username]
token.payload.subordinates[_] == username
user_owns_token
}

# Allow HR members to get anyone's salary.
allow {
input.method == "GET"
input.path = ["finance", "salary", _]
token.payload.hr == true
user_owns_token
}