Skip to content

Commit

Permalink
Add idprefix validation (#260)
Browse files Browse the repository at this point in the history
* add idprefix validation

Signed-off-by: Bailin He <[email protected]>

* fix teests

Signed-off-by: Bailin He <[email protected]>

* apply review suggestions

Signed-off-by: Bailin He <[email protected]>

---------

Signed-off-by: Bailin He <[email protected]>
  • Loading branch information
bailinhe authored Jun 6, 2024
1 parent ad02a3f commit 53c0413
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
6 changes: 5 additions & 1 deletion internal/iapl/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"go.infratographer.com/permissions-api/internal/types"

"go.infratographer.com/x/gidx"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -231,7 +232,6 @@ func LoadPolicyDocumentFromDirectory(directoryPath string) (PolicyDocument, erro

return nil
})

if err != nil {
return PolicyDocument{}, err
}
Expand Down Expand Up @@ -287,6 +287,10 @@ func (v *policy) validateUnions() error {

func (v *policy) validateResourceTypes() error {
for _, resourceType := range v.rt {
if _, err := gidx.NewID(resourceType.IDPrefix); err != nil {
return fmt.Errorf("%w: %s", err, resourceType.Name)
}

for _, rel := range resourceType.Relationships {
for _, tt := range rel.TargetTypes {
if _, ok := v.rt[tt.Name]; !ok {
Expand Down
58 changes: 44 additions & 14 deletions internal/iapl/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.infratographer.com/x/gidx"

"go.infratographer.com/permissions-api/internal/testingx"
"go.infratographer.com/permissions-api/internal/types"
Expand All @@ -19,7 +20,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
},
},
Unions: []Union{
Expand All @@ -35,6 +37,21 @@ func TestPolicy(t *testing.T) {
require.ErrorIs(t, res.Err, ErrorTypeExists)
},
},
{
Name: "Invalid prefix ID",
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
IDPrefix: "fooooooooooooooooooooo",
},
},
},
CheckFn: func(_ context.Context, t *testing.T, res testingx.TestResult[Policy]) {
require.Error(t, res.Err)
require.ErrorContains(t, res.Err, (&gidx.ErrInvalidID{}).Error())
},
},
{
Name: "UnknownTypeInUnion",
Input: PolicyDocument{
Expand All @@ -61,7 +78,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
},
},
Unions: []Union{
Expand All @@ -82,7 +100,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand All @@ -103,7 +122,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand Down Expand Up @@ -135,7 +155,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand Down Expand Up @@ -175,7 +196,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
},
},
Actions: []Action{
Expand Down Expand Up @@ -207,7 +229,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand All @@ -218,7 +241,8 @@ func TestPolicy(t *testing.T) {
},
},
{
Name: "baz",
Name: "baz",
IDPrefix: "permbaz",
},
},
Unions: []Union{
Expand Down Expand Up @@ -259,7 +283,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand All @@ -270,7 +295,8 @@ func TestPolicy(t *testing.T) {
},
},
{
Name: "baz",
Name: "baz",
IDPrefix: "permbaz",
Relationships: []Relationship{
{
Relation: "bar",
Expand Down Expand Up @@ -319,7 +345,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
Relationships: []Relationship{
{
Relation: "bar",
Expand All @@ -330,7 +357,8 @@ func TestPolicy(t *testing.T) {
},
},
{
Name: "baz",
Name: "baz",
IDPrefix: "permbaz",
Relationships: []Relationship{
{
Relation: "bar",
Expand Down Expand Up @@ -379,7 +407,8 @@ func TestPolicy(t *testing.T) {
Input: PolicyDocument{
ResourceTypes: []ResourceType{
{
Name: "foo",
Name: "foo",
IDPrefix: "permfoo",
},
{
Name: "rolev2",
Expand Down Expand Up @@ -428,7 +457,8 @@ func TestPolicy(t *testing.T) {
},
ResourceTypes: []ResourceType{
{
Name: "tenant",
Name: "tenant",
IDPrefix: "tnntten",
},
{
Name: "user",
Expand Down

0 comments on commit 53c0413

Please sign in to comment.