{}
{
"account":{
"state":"open"
},
"user":{
"risk_score":"low"
},
"transaction":{
"amount":950
}
}
# Run your first Rego policy!
package payments
default allow:=false
allow if{
input.account.state=="open"
input.user.risk_score in["low","medium"]
input.transaction.amount<=1000
}
# Open in the Rego Playground to see the full example.
Created by
OPA is now maintained by Styra and a large community of contributors.
Developer Productivity: OPA helps teams focus on delivering business value by decoupling policy from application logic. Security & platform teams centrally manage shared policies, while developer teams extend them as needed within the policy system.
Performance: Rego, our domain-specific policy language, is built for speed. By operating on pre-loaded, in-memory data, OPA acts as a fast policy decision point for your applications.
Audit & Compliance: OPA generates comprehensive audit trails for every policy decision. This detailed history supports auditing and compliance efforts and enables decisions to be replayed for analysis or debugging.
Interested to see more? Checkout theMaintainer Track Session from KubeCon.
OPA is a general-purpose policy engine that unifies policy enforcement across the stack. OPA provides a high-level declarative language that lets you specify policy for a wide range of use cases. You can use OPA to enforce policies in applications, proxies, Kubernetes, CI/CD pipelines, API gateways, and more.
Applications can directly integrate with OPA using ourSDKs orREST API. This is great when yourapplication needs to make domain specific runtime decisions.
package application.authz
# Only owner can update the pet's information. Ownership
# information is provided as part of the request data from
# the application.
default allow:=false
allow if{
input.method=="PUT"
some petid
input.path=["pets", petid]
input.user== input.owner
}
{
"role":"staff",
"owner":"bob@example.com",
"path":[
"pets",
"pet113-987"
],
"user":"alice@example.com"
}
{}
OPA has anative integration with the Envoy ExternalAuthorization API. This example shows a simple policy using the input documentsupplied from that API to authorize requests.
Browse moreEnvoy Exampleson the playground.
package envoy.http.public
headers:= input.attributes.request.http.headers
default allow:=false
allow if{
input.attributes.request.http.method=="GET"
input.attributes.request.http.path=="/"
}
allow if headers.authorization=="Basic charlie"
{
"attributes":{
"request":{
"http":{
"headers":{
"authorization":"Basic bob"
},
"method":"GET",
"path":"/",
"protocol":"HTTP/1.1"
}
}
}
}
{}
OPA can be used to control which Kubernetes resources can be created in a givencluster. By configuring the Kubernetes API to send admission requests to OPA, youcan create custom policies to enforce your organization's rules.
Browse moreKubernetes Exampleson the playground.
package kubernetes.validating.existence
deny contains msg if{
value:= input.request.object.metadata.labels.costcenter
notstartswith(value,"cccode-")
msg:=sprintf("Costcenter must start `cccode-`; found `%v`",[value])
}
deny contains msg if{
not input.request.object.metadata.labels.costcenter
msg:="Every resource must have a costcenter label"
}
{
"kind":"AdmissionReview",
"request":{
"kind":{
"kind":"Pod",
"version":"v1"
},
"object":{
"metadata":{
"name":"myapp",
"labels":{
"costcenter":"engineering"
}
},
"spec":{
"containers":[
{
"image":"nginx",
"name":"nginx-frontend"
},
{
"image":"mysql",
"name":"mysql-backend"
}
]
}
}
}
}
{}
Controlling access to generative AI endpoints can be complicated. With OPA, youcan create custom rules to grant or deny access to specific users in yourorganization.
Browse moreAI API Exampleson the playground.
package ai.chat
deny contains message if{
every pattern in all_accessible_models{
notregex.match(pattern, input.parsed_body.model)
}
message:=sprintf(
"Model '%s' is not in your accessible models: %s",
[input.parsed_body.model,concat(", ", all_accessible_models)],
)
}
# model_access is a mapping of role to patterns which match models
# that users might be accessing.
model_access:={
"interns":{"model-1"},
"testers":{"model-1",`^model-\d+-stage$`},
"data-analysts":{"model-1",`^model-\d+-internal$`},
}
all_accessible_models contains m if{
some group in input.groups
some m in model_access[group]
}
{
"user":{
"email":"alice@example.com",
"groups":[
"testers"
]
},
"parsed_body":{
"model":"model-2",
"messages":[
{
"role":"user",
"content":"Tell me about OPA"
}
]
}
}
{}