Ad hoc evaluation of input given a policy #72
-
Hi! package play
import future.keywords.in
default domain = false
default apiMethods = false
default rrTypes = false
default valuePatterns = false
domain {
startswith(input.domain,"_acme-challenge.")
endswith(input.domain,".")
}
apiMethods {
input.apiMethods in ["set","get","delete"]
}
rrTypes {
input.rrTypes in ["TXT"]
}
valuePatterns {
true
} and a query: {
"domain": "_acme-challenge.y.gy.",
"apiMethods": "set",
"rrTypes": "TXT",
"valuePatterns": "any"
} and want it to evaluate the input based on the policy like it can be seen on the playground. I tried doing exactly that with the REST API like that: curl http://[::]:8081/v1/query -X "POST" -v -H "Content-Type: application/json" \
--data-binary @- << EOF
{
"query": "package play\nimport future.keywords.in\n\ndefault domain = false\ndefault apiMethods = false\ndefault rrTypes = false\ndefault valuePatterns = false\n\n\ndomain {\n startswith(input.domain,\"_acme-challenge.\")\n endswith(input.domain,\".\")\n}\n\napiMethods {\n input.apiMethods in [\"set\",\"get\",\"delete\"]\n}\n\nrrTypes {\n input.rrTypes in [\"TXT\"]\n}\n\nvaluePatterns {\n true\n}",
"input": {
"domain":"_acme-challenge.y.gy.",
"apiMethods":"set",
"rrTypes":"TXT",
"valuePatterns": "any"
}
}
EOF with the response: {
"code": "internal_error",
"message": "expected body but got *ast.Package"
} I figured out that it is possibly only evaluating single lines, but this is not pointed out in the documentation. I looked at playgrounds HTTP request, but I think these are doing something else in the backend than the standard REST API because I couldn't reproduce these either. Is it possible to do what I want with one request? Am I missing something? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
this is my solution for now: it took forever to find out how it works... the documentation is everything but not helpful :( via the cli opa eval --data acme.rego --input input.json data.system via rest api: curl http://[::]:8081/v1/policies/x -X "PUT" -v -H "Content-Type: text/plain" --data-binary @acme.rego
curl http://[::]:8081/ -X "POST" -v -H "Content-Type: application/json" --data-binary @input.json for the files: Note that naming the package is done in the rego file! Here: package system.main
import future.keywords.in
default domain = false
default apiMethods = false
default rrTypes = false
default valuePatterns = false
domain {
startswith(input.domain,"_acme-challenge.")
endswith(input.domain,".")
}
apiMethods {
input.apiMethods in ["set","get","delete"]
}
rrTypes {
input.rrTypes in ["TXT"]
}
valuePatterns {
true
} and {
"domain": "_acme-challenge.y.gy.",
"apiMethods": "set",
"rrTypes": "TXT",
"valuePatterns": "any"
} |
Beta Was this translation helpful? Give feedback.
-
You'll want to query the data API. Given you original policy: package play
import future.keywords.in
# ... You'd then query OPA at the
The HTTP API docs provide a pretty good guide to running and querying OPA in a scenario like this. |
Beta Was this translation helpful? Give feedback.
this is my solution for now:
it took forever to find out how it works... the documentation is everything but not helpful :(
via the cli
opa eval --data acme.rego --input input.json data.system
via rest api:
for the files:
acme.rego
Note that naming the package is done in the rego file! Here:
system.main
When then contacting the root url:
http://[::]:8081/
it queries the packagesystem.main
per default