Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions constraint/pkg/client/drivers/rego/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (in *Source) ToUnstructured() map[string]interface{} {
// GetSource extracts Source from a templates.Code object.
func GetSource(code templates.Code) (*Source, error) {
rawCode := code.Source
if rawCode == nil || rawCode.Value == nil {
return nil, fmt.Errorf("%w: source", ErrMissingField)
}

v, ok := rawCode.Value.(map[string]interface{})
if !ok {
return nil, ErrBadType
Expand Down
23 changes: 23 additions & 0 deletions constraint/pkg/client/drivers/rego/schema/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema

import (
"errors"
"testing"

"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
Expand Down Expand Up @@ -77,3 +78,25 @@ func TestGetSourceVersions(t *testing.T) {
})
}
}

func TestGetSourceMissingSource(t *testing.T) {
testCases := map[string]templates.Code{
"nil source": {
Engine: Name,
Source: nil,
},
"nil source value": {
Engine: Name,
Source: &templates.Anything{},
},
}

for name, code := range testCases {
t.Run(name, func(t *testing.T) {
_, err := GetSource(code)
if !errors.Is(err, ErrMissingField) {
t.Fatalf("expected %v, got %v", ErrMissingField, err)
}
})
}
}
Loading