-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses IDX-142 Signed-off-by: philhassey <[email protected]>
- Loading branch information
1 parent
65eb620
commit 693e720
Showing
8 changed files
with
98 additions
and
71 deletions.
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
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
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
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
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
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,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() | ||
} |
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,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) | ||
} |
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