Quickstart :: Cerbos Authorization Management Platform // Documentation
Quickstart
| This documentation is for a previous version of Cerbos. Choose 0.53.0 from the version picker at the top right or navigate to https://docs.cerbos.dev for the latest version. |
Create a directory to store the policies.
mkdir -p cerbos-quickstart/policies
Now start the Cerbos server. We are using the container image in this guide but you can follow along using the binary as well. See installation instructions for more information.
docker run --rm --name cerbos -d -v $(pwd)/cerbos-quickstart/policies:/policies -p 3592:3592 -p 3593:3593 ghcr.io/cerbos/cerbos:0.35.1
Time to try out a simple request.
| If you prefer to use Postman, Insomnia or any other software that supports OpenAPI, you can follow this guide along on those tools by downloading the OpenAPI definitions from http://localhost:3592/schema/swagger.json. You can also use the built-in API browser by pointing your browser to http://localhost:3592. |
- cURL
- .NET
- Go
- Java
- JS
- PHP
- Python
- Ruby
- Rust
cat <<EOF | curl --silent "http://localhost:3592/api/check/resources?pretty" -d @-
{
"requestId": "quickstart",
"principal": {
"id": "bugs_bunny",
"roles": [\
"user"\\
],
"attr": {
"beta_tester": true
}
},
"resources": [\
{\
"actions": [\
"view:public",\
"comment"\
],\
"resource": {\
"kind": "album:object",\
"id": "BUGS001",\
"attr": {\
"owner": "bugs_bunny",\
"public": false,\
"flagged": false\
}\
}\
},\
{\
"actions": [\
"view:public",\
"comment"\
],\
"resource": {\
"kind": "album:object",\
"id": "DAFFY002",\
"attr": {\
"owner": "daffy_duck",\
"public": true,\
"flagged": false\
}\
}\
}\
]
}
EOF
using Cerbos.Api.V1.Effect;
using Cerbos.Sdk.Response;
using Cerbos.Sdk.Builder;
using Cerbos.Sdk.Utility;
internal class Program
{
private static void Main(string[] args)
{
var client = CerbosClientBuilder.ForTarget("http://localhost:3593").WithPlaintext().Build();
var request = CheckResourcesRequest
.NewInstance()
.WithRequestId(RequestId.Generate())
.WithIncludeMeta(true)
.WithPrincipal(
Principal
.NewInstance("bugs_bunny", "user")
.WithAttribute("beta_tester", AttributeValue.BoolValue(true))
)
.WithResourceEntries(
ResourceEntry
.NewInstance("album:object", "BUGS001")
.WithAttribute("owner", AttributeValue.StringValue("bugs_bunny"))
.WithAttribute("public", AttributeValue.BoolValue(false))
.WithAttribute("flagged", AttributeValue.BoolValue(false))
.WithActions("comment", "view:public"),
ResourceEntry
.NewInstance("album:object", "DAFFY002")
.WithAttribute("owner", AttributeValue.StringValue("daffy_duck"))
.WithAttribute("public", AttributeValue.BoolValue(true))
.WithAttribute("flagged", AttributeValue.BoolValue(false))
.WithActions("comment", "view:public")
);
CheckResourcesResponse result = client.CheckResources(request);
foreach (var resourceId in new[] { "BUGS001", "DAFFY002" })
{
var resultEntry = result.Find(resourceId);
Console.Write($"\nResource ID: {resourceId}\n");
foreach (var actionEffect in resultEntry.Actions)
{
string action = actionEffect.Key;
Effect effect = actionEffect.Value;
Console.Write($"\t{action} -> {(effect == Effect.Allow ? "EFFECT_ALLOW" : "EFFECT_DENY")}\n");
}
}
}
}
package main
import (
"context"
"log"
"github.com/cerbos/cerbos-sdk-go/cerbos"
)
func main() {
c, err := cerbos.New("localhost:3593", cerbos.WithPlaintext())
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
principal := cerbos.NewPrincipal("bugs_bunny", "user")
principal.WithAttr("beta_tester", true)
kind := "album:object"
actions := []string{"view:public", "comment"}
r1 := cerbos.NewResource(kind, "BUGS001")
r1.WithAttributes(map[string]any{
"owner": "bugs_bunny",
"public": false,
"flagged": false,
})
r2 := cerbos.NewResource(kind, "DAFFY002")
r2.WithAttributes(map[string]any{
"owner": "daffy_duck",
"public": true,
"flagged": false,
})
batch := cerbos.NewResourceBatch()
batch.Add(r1, actions...)
batch.Add(r2, actions...)
resp, err := c.CheckResources(context.Background(), principal, batch)
if err != nil {
log.Fatalf("Failed to check resources: %v", err)
}
log.Printf("%v", resp)
}
package demo;
import static dev.cerbos.sdk.builders.AttributeValue.boolValue;
import static dev.cerbos.sdk.builders.AttributeValue.stringValue;
import java.util.Map;
import dev.cerbos.sdk.CerbosBlockingClient;
import dev.cerbos.sdk.CerbosClientBuilder;
import dev.cerbos.sdk.CheckResult;
import dev.cerbos.sdk.builders.Principal;
import dev.cerbos.sdk.builders.ResourceAction;
public class App {
public static void main(String[] args) throws CerbosClientBuilder.InvalidClientConfigurationException {
CerbosBlockingClient client=new CerbosClientBuilder("localhost:3593").withPlaintext().buildBlockingClient();
for (String n : new String[]{"BUGS001", "DAFFY002"}) {
CheckResult cr = client.batch(
Principal.newInstance("bugs_bunny", "user")
.withAttribute("beta_tester", boolValue(true))
)
.addResources(
ResourceAction.newInstance("album:object","BUGS001")
.withAttributes(
Map.of(
"owner", stringValue("bugs_bunny"),
"public", boolValue(false),
"flagged", boolValue(false)
)
)
.withActions("view:public", "comment"),
ResourceAction.newInstance("album:object","DAFFY002")
.withAttributes(
Map.of(
"owner", stringValue("daffy_duck"),
"public", boolValue(true),
"flagged", boolValue(false)
)
)
.withActions("view:public", "comment")
)
.check().find(n).orElse(null);
if (cr != null) {
System.out.printf("\nResource: %s\n", n);
cr.getAll().forEach((action, allowed) -> { System.out.printf("\t%s -> %s\n", action, allowed ? "EFFECT_ALLOW" : "EFFECT_DENY"); });
}
}
}
}
const { GRPC: Cerbos } = require("@cerbos/grpc");
const cerbos = new Cerbos("localhost:3593", { tls: false });
(async() => {
const kind = "album:object";
const actions = ["view:public", "comment"];
const cerbosPayload = {
principal: {
id: "bugs_bunny",
roles: ["user"],
attributes: {
beta_tester: true,
},
},
resources: [
{
resource: {
kind: kind,
id: "BUGS001",
attributes: {
owner: "bugs_bunny",
public: false,
flagged: false,
},
},
actions: actions,
},
{
resource: {
kind: kind,
id: "DAFFY002",
attributes: {
owner: "daffy_duck",
public: true,
flagged: false,
},
},
actions: actions,
},
],
};
const decision = await cerbos.checkResources(cerbosPayload);
console.log(decision.results)
})();
<?php
require __DIR__ . '/vendor/autoload.php';
use Cerbosfect he v1
tfect;
use Cerbos\v
duilder\ClientBuilder;
use Cerbos\v
duilder\