Skip to content

Commit

Permalink
cedar-go: more window dressing
Browse files Browse the repository at this point in the history
Signed-off-by: philhassey <[email protected]>
  • Loading branch information
patjakdev authored and philhassey committed Aug 23, 2024
1 parent 8afc736 commit 61a8b9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import (
"fmt"
"log"

"github.com/cedar-policy/cedar-go"
cedar "github.com/cedar-policy/cedar-go"
)

const policyCedar = `permit (
Expand All @@ -80,10 +80,14 @@ const entitiesJSON = `[
]`

func main() {
ps, err := cedar.NewPolicySet("policy.cedar", []byte(policyCedar))
if err != nil {
var policy cedar.Policy
if err := policy.UnmarshalCedar([]byte(policyCedar)); err != nil {
log.Fatal(err)
}

ps := cedar.NewPolicySet()
ps.UpsertPolicy("policy0", &policy)

var entities cedar.Entities
if err := json.Unmarshal([]byte(entitiesJSON), &entities); err != nil {
log.Fatal(err)
Expand All @@ -94,6 +98,7 @@ func main() {
Resource: cedar.EntityUID{Type: "Photo", ID: "VacationPhoto94.jpg"},
Context: cedar.Record{},
}

ok, _ := ps.IsAuthorized(entities, req)
fmt.Println(ok)
}
Expand Down
39 changes: 20 additions & 19 deletions policy_set_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cedar
package cedar_test

import (
"bytes"
"fmt"
"testing"

"github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/ast"
"github.com/cedar-policy/cedar-go/internal/testutil"
)
Expand All @@ -13,19 +14,19 @@ func TestNewPolicySetFromFile(t *testing.T) {
t.Parallel()
t.Run("err-in-tokenize", func(t *testing.T) {
t.Parallel()
_, err := NewPolicySetFromFile("policy.cedar", []byte(`"`))
_, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`"`))
testutil.Error(t, err)
})
t.Run("err-in-parse", func(t *testing.T) {
t.Parallel()
_, err := NewPolicySetFromFile("policy.cedar", []byte(`err`))
_, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`err`))
testutil.Error(t, err)
})
t.Run("annotations", func(t *testing.T) {
t.Parallel()
ps, err := NewPolicySetFromFile("policy.cedar", []byte(`@key("value") permit (principal, action, resource);`))
ps, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`@key("value") permit (principal, action, resource);`))
testutil.OK(t, err)
testutil.Equals(t, ps.GetPolicy("policy0").Annotations(), Annotations{"key": "value"})
testutil.Equals(t, ps.GetPolicy("policy0").Annotations(), cedar.Annotations{"key": "value"})
})
}

Expand All @@ -34,14 +35,14 @@ func TestUpsertPolicy(t *testing.T) {
t.Run("insert", func(t *testing.T) {
t.Parallel()

policy0 := NewPolicyFromAST(ast.Forbid())
policy0 := cedar.NewPolicyFromAST(ast.Forbid())

var policy1 Policy
var policy1 cedar.Policy
testutil.OK(t, policy1.UnmarshalJSON(
[]byte(`{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"}}`),
))

ps := NewPolicySet()
ps := cedar.NewPolicySet()
ps.UpsertPolicy("policy0", policy0)
ps.UpsertPolicy("policy1", &policy1)

Expand All @@ -52,12 +53,12 @@ func TestUpsertPolicy(t *testing.T) {
t.Run("upsert", func(t *testing.T) {
t.Parallel()

ps := NewPolicySet()
ps := cedar.NewPolicySet()

p1 := NewPolicyFromAST(ast.Forbid())
p1 := cedar.NewPolicyFromAST(ast.Forbid())
ps.UpsertPolicy("a wavering policy", p1)

p2 := NewPolicyFromAST(ast.Permit())
p2 := cedar.NewPolicyFromAST(ast.Permit())
ps.UpsertPolicy("a wavering policy", p2)

testutil.Equals(t, ps.GetPolicy("a wavering policy"), p2)
Expand All @@ -69,17 +70,17 @@ func TestDeletePolicy(t *testing.T) {
t.Run("delete non-existent", func(t *testing.T) {
t.Parallel()

ps := NewPolicySet()
ps := cedar.NewPolicySet()

// Just verify that this doesn't crash
ps.DeletePolicy("not a policy")
})
t.Run("delete existing", func(t *testing.T) {
t.Parallel()

ps := NewPolicySet()
ps := cedar.NewPolicySet()

p1 := NewPolicyFromAST(ast.Forbid())
p1 := cedar.NewPolicyFromAST(ast.Forbid())
ps.UpsertPolicy("a policy", p1)
ps.DeletePolicy("a policy")

Expand All @@ -103,17 +104,17 @@ forbid (
resource
);`

var policies PolicySlice
var policies cedar.PolicySlice
testutil.OK(t, policies.UnmarshalCedar([]byte(policiesStr)))

ps := NewPolicySet()
ps := cedar.NewPolicySet()
for i, p := range policies {
p.SetSourceFile("example.cedar")
ps.UpsertPolicy(PolicyID(fmt.Sprintf("policy%d", i)), p)
ps.UpsertPolicy(cedar.PolicyID(fmt.Sprintf("policy%d", i)), p)
}

testutil.Equals(t, ps.GetPolicy("policy0").Effect(), Permit)
testutil.Equals(t, ps.GetPolicy("policy1").Effect(), Forbid)
testutil.Equals(t, ps.GetPolicy("policy0").Effect(), cedar.Permit)
testutil.Equals(t, ps.GetPolicy("policy1").Effect(), cedar.Forbid)

var buf bytes.Buffer
ps.MarshalCedar(&buf)
Expand Down
11 changes: 6 additions & 5 deletions policy_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cedar
package cedar_test

import (
"bytes"
"encoding/json"
"testing"

"github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/ast"
"github.com/cedar-policy/cedar-go/internal/testutil"
"github.com/cedar-policy/cedar-go/types"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestPolicyJSON(t *testing.T) {
}`,
))

var policy Policy
var policy cedar.Policy
testutil.OK(t, policy.UnmarshalJSON(jsonEncodedPolicy))

output, err := policy.MarshalJSON()
Expand All @@ -78,7 +79,7 @@ func TestPolicyCedar(t *testing.T) {
)
when { resource.owner == principal };`

var policy Policy
var policy cedar.Policy
testutil.OK(t, policy.UnmarshalCedar([]byte(policyStr)))

var buf bytes.Buffer
Expand All @@ -94,7 +95,7 @@ func TestPolicyAST(t *testing.T) {
ActionEq(types.NewEntityUID("Action", "editPhoto")).
When(ast.Resource().Access("owner").Equals(ast.Principal()))

_ = NewPolicyFromAST(astExample)
_ = cedar.NewPolicyFromAST(astExample)
}

func TestPolicySlice(t *testing.T) {
Expand All @@ -113,7 +114,7 @@ forbid (
resource
);`

var policies PolicySlice
var policies cedar.PolicySlice
testutil.OK(t, policies.UnmarshalCedar([]byte(policiesStr)))

var buf bytes.Buffer
Expand Down

0 comments on commit 61a8b9c

Please sign in to comment.