-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charith Ellawala <charith@cerbos.dev>
- Loading branch information
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,7 @@ issues: | |
- goconst | ||
- gomnd | ||
- govet | ||
- path: example_test\.go | ||
linters: | ||
- gocritic | ||
- errcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2021-2023 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package testutil_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/cerbos/cerbos-sdk-go/cerbos" | ||
"github.com/cerbos/cerbos-sdk-go/testutil" | ||
) | ||
|
||
func ExampleLaunchCerbosServer() { | ||
// Configure Cerbos with the SQLite storage driver | ||
conf := testutil.LaunchConf{ | ||
Cmd: []string{ | ||
"server", | ||
"--set=storage.driver=sqlite3", | ||
"--set=storage.sqlite3.dsn=:mem:?_fk=true", | ||
}, | ||
Env: []string{ | ||
"CERBOS_LOG_LEVEL=error", | ||
}, | ||
} | ||
|
||
// Set timeout for launching the server | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
s, err := testutil.LaunchCerbosServer(ctx, conf) | ||
if err != nil { | ||
log.Fatalf("Failed to launch Cerbos server: %v", err) | ||
} | ||
|
||
defer s.Stop() | ||
|
||
c, err := cerbos.New(s.GRPCAddr(), cerbos.WithPlaintext()) | ||
if err != nil { | ||
log.Fatalf("Failed to create Cerbos client: %v", err) | ||
} | ||
|
||
allowed, err := c.IsAllowed(context.TODO(), | ||
cerbos.NewPrincipal("john"). | ||
WithRoles("employee", "manager"). | ||
WithAttr("department", "marketing"). | ||
WithAttr("geography", "GB"), | ||
cerbos.NewResource("leave_request", "XX125"). | ||
WithAttributes(map[string]any{ | ||
"department": "marketing", | ||
"geography": "GB", | ||
"owner": "harry", | ||
"status": "DRAFT", | ||
}), | ||
"view", | ||
) | ||
if err != nil { | ||
log.Fatalf("API request failed: %v", err) | ||
} | ||
|
||
fmt.Println(allowed) | ||
// Output: false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters