Skip to content

Commit

Permalink
cedar: minor renames
Browse files Browse the repository at this point in the history
Addresses IDX-142

Signed-off-by: philhassey <[email protected]>
  • Loading branch information
philhassey committed Aug 23, 2024
1 parent 65eb620 commit 693e720
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 71 deletions.
2 changes: 1 addition & 1 deletion authorize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func TestIsAuthorized(t *testing.T) {
tt := tt
t.Run(tt.Name, func(t *testing.T) {
t.Parallel()
ps, err := NewPolicySetFromFile("policy.cedar", []byte(tt.Policy))
ps, err := NewPolicySetFromBytes("policy.cedar", []byte(tt.Policy))
testutil.Equals(t, (err != nil), tt.ParseErr)
ok, diag := ps.IsAuthorized(tt.Entities, Request{
Principal: tt.Principal,
Expand Down
4 changes: 2 additions & 2 deletions corpus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestCorpus(t *testing.T) {
t.Fatal("error reading policy content", err)
}

policySet, err := NewPolicySetFromFile("policy.cedar", policyContent)
policySet, err := NewPolicySetFromBytes("policy.cedar", policyContent)
if err != nil {
t.Fatal("error parsing policy set", err)
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestCorpusRelated(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
policy, err := NewPolicySetFromFile("", []byte(tt.policy))
policy, err := NewPolicySetFromBytes("", []byte(tt.policy))
testutil.OK(t, err)
ok, diag := policy.IsAuthorized(entities2.Entities{}, tt.request)
testutil.Equals(t, ok, tt.decision)
Expand Down
36 changes: 1 addition & 35 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cedar

import (
"bytes"
"fmt"

"github.com/cedar-policy/cedar-go/ast"
internalast "github.com/cedar-policy/cedar-go/internal/ast"
Expand Down Expand Up @@ -100,39 +99,6 @@ func (p Policy) Position() Position {
return Position(p.ast.Position)
}

func (p *Policy) SetSourceFile(path string) {
func (p *Policy) SetFileName(path string) {
p.ast.Position.FileName = path
}

// PolicySlice represents a set of un-named Policy's. Cedar documents, unlike the JSON format, don't have a means of
// naming individual policies.
type PolicySlice []*Policy

// UnmarshalCedar parses a concatenation of un-named Cedar policy statements. Names can be assigned to these policies
// when adding them to a PolicySet.
func (p *PolicySlice) UnmarshalCedar(b []byte) error {
var res parser.PolicySlice
if err := res.UnmarshalCedar(b); err != nil {
return fmt.Errorf("parser error: %w", err)
}
policySlice := make([]*Policy, 0, len(res))
for _, p := range res {
newPolicy := newPolicy((*internalast.Policy)(p))
policySlice = append(policySlice, &newPolicy)
}
*p = policySlice
return nil
}

// MarshalCedar emits a concatenated Cedar representation of a PolicySlice
func (p PolicySlice) MarshalCedar() []byte {
var buf bytes.Buffer
for i, policy := range p {
buf.Write(policy.MarshalCedar())

if i < len(p)-1 {
buf.WriteString("\n\n")
}
}
return buf.Bytes()
}
12 changes: 5 additions & 7 deletions policy_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ func NewPolicySet() PolicySet {
return PolicySet{policies: map[PolicyID]*Policy{}}
}

// NewPolicySetFromFile will create a PolicySet from the given text document with the/ given file name used in Position
// NewPolicySetFromBytes will create a PolicySet from the given text document with the/ given file name used in Position
// data. If there is an error parsing the document, it will be returned.
//
// NewPolicySetFromFile assigns default PolicyIDs to the policies contained in fileName in the format "policy<n>" where
// NewPolicySetFromBytes assigns default PolicyIDs to the policies contained in fileName in the format "policy<n>" where
// <n> is incremented for each new policy found in the file.
func NewPolicySetFromFile(fileName string, document []byte) (PolicySet, error) {
var policySlice PolicySlice
if err := policySlice.UnmarshalCedar(document); err != nil {
func NewPolicySetFromBytes(fileName string, document []byte) (PolicySet, error) {
policySlice, err := NewPolicySliceFromBytes(fileName, document)
if err != nil {
return PolicySet{}, err
}

policyMap := make(map[PolicyID]*Policy, len(policySlice))
for i, p := range policySlice {
policyID := PolicyID(fmt.Sprintf("policy%d", i))
p.SetSourceFile(fileName)
policyMap[policyID] = p
}
return PolicySet{policies: policyMap}, nil
Expand Down
8 changes: 4 additions & 4 deletions policy_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func TestNewPolicySetFromFile(t *testing.T) {
t.Parallel()
t.Run("err-in-tokenize", func(t *testing.T) {
t.Parallel()
_, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`"`))
_, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`"`))
testutil.Error(t, err)
})
t.Run("err-in-parse", func(t *testing.T) {
t.Parallel()
_, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`err`))
_, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`err`))
testutil.Error(t, err)
})
t.Run("annotations", func(t *testing.T) {
t.Parallel()
ps, err := cedar.NewPolicySetFromFile("policy.cedar", []byte(`@key("value") permit (principal, action, resource);`))
ps, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`@key("value") permit (principal, action, resource);`))
testutil.OK(t, err)
testutil.Equals(t, ps.GetPolicy("policy0").Annotations(), cedar.Annotations{"key": "value"})
})
Expand Down Expand Up @@ -162,7 +162,7 @@ forbid (

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

Expand Down
55 changes: 55 additions & 0 deletions policy_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cedar

import (
"bytes"
"fmt"

internalast "github.com/cedar-policy/cedar-go/internal/ast"
"github.com/cedar-policy/cedar-go/internal/parser"
)

// PolicySlice represents a set of un-named Policy's. Cedar documents, unlike the JSON format, don't have a means of
// naming individual policies.
type PolicySlice []*Policy

// NewPolicySliceFromBytes will create a PolicySet from the given text document with the/ given file name used in Position
// data. If there is an error parsing the document, it will be returned.
func NewPolicySliceFromBytes(fileName string, document []byte) (PolicySlice, error) {
var policySlice PolicySlice
if err := policySlice.UnmarshalCedar(document); err != nil {
return nil, err
}
for _, p := range policySlice {
p.SetFileName(fileName)
}
return policySlice, nil
}

// UnmarshalCedar parses a concatenation of un-named Cedar policy statements. Names can be assigned to these policies
// when adding them to a PolicySet.
func (p *PolicySlice) UnmarshalCedar(b []byte) error {
var res parser.PolicySlice
if err := res.UnmarshalCedar(b); err != nil {
return fmt.Errorf("parser error: %w", err)
}
policySlice := make([]*Policy, 0, len(res))
for _, p := range res {
newPolicy := newPolicy((*internalast.Policy)(p))
policySlice = append(policySlice, &newPolicy)
}
*p = policySlice
return nil
}

// MarshalCedar emits a concatenated Cedar representation of a PolicySlice
func (p PolicySlice) MarshalCedar() []byte {
var buf bytes.Buffer
for i, policy := range p {
buf.Write(policy.MarshalCedar())

if i < len(p)-1 {
buf.WriteString("\n\n")
}
}
return buf.Bytes()
}
30 changes: 30 additions & 0 deletions policy_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cedar_test

import (
"testing"

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

func TestPolicySlice(t *testing.T) {
t.Parallel()

policiesStr := `permit (
principal,
action == Action::"editPhoto",
resource
)
when { resource.owner == principal };
forbid (
principal in Groups::"bannedUsers",
action,
resource
);`

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

testutil.Equals(t, string(policies.MarshalCedar()), policiesStr)
}
22 changes: 0 additions & 22 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,3 @@ func TestPolicyAST(t *testing.T) {

_ = cedar.NewPolicyFromAST(astExample)
}

func TestPolicySlice(t *testing.T) {
t.Parallel()

policiesStr := `permit (
principal,
action == Action::"editPhoto",
resource
)
when { resource.owner == principal };
forbid (
principal in Groups::"bannedUsers",
action,
resource
);`

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

testutil.Equals(t, string(policies.MarshalCedar()), policiesStr)
}

0 comments on commit 693e720

Please sign in to comment.