diff --git a/admissionctrl/opa/opa.go b/admissionctrl/opa/opa.go index a7eccd7..fe963d2 100644 --- a/admissionctrl/opa/opa.go +++ b/admissionctrl/opa/opa.go @@ -25,12 +25,15 @@ func CreateQuery(filename string, query string, ctx context.Context, verifier no if err != nil { return nil, err } - - preparedQuery, err := rego.New( + options := []func(*rego.Rego){ rego.Query(query), - rego.Function1( + rego.Module(filename, string(module)), + } + if verifier != nil { + options = append(options, rego.Function1( + ®o.Function{ - Name: "valid_notation_image", + Name: "notation_verify_image", Decl: types.NewFunction(types.Args(types.S), types.B), }, func(bctx rego.BuiltinContext, a *ast.Term) (*ast.Term, error) { @@ -43,8 +46,10 @@ func CreateQuery(filename string, query string, ctx context.Context, verifier no } return ast.BooleanTerm(false), nil }), - rego.Module(filename, string(module)), - ).PrepareForEval(ctx) + ) + } + + preparedQuery, err := rego.New(options...).PrepareForEval(ctx) if err != nil { return nil, err diff --git a/admissionctrl/opa/opa_test.go b/admissionctrl/opa/opa_test.go index a785b35..24727f3 100644 --- a/admissionctrl/opa/opa_test.go +++ b/admissionctrl/opa/opa_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/nomad/api" + "github.com/mxab/nacp/admissionctrl/notation" "github.com/mxab/nacp/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -106,16 +107,19 @@ func TestNotationImageValidation(t *testing.T) { tt := []struct { name string image string + verifier notation.ImageVerifier expectedErrors []interface{} }{ { name: "valid image", image: "validimage:latest", + verifier: new(DummyVerifier), expectedErrors: []interface{}{}, }, { - name: "invalid image", - image: "invalidimage:latest", + name: "invalid image", + image: "invalidimage:latest", + verifier: new(DummyVerifier), expectedErrors: []interface{}{ "Image is not in valid", }, @@ -129,9 +133,11 @@ func TestNotationImageValidation(t *testing.T) { path := testutil.Filepath(t, "opa/test_notation.rego") - query, err := CreateQuery(path, ` - errors = data.opatest.errors - `, ctx, new(DummyVerifier)) + query, err := CreateQuery(path, + "errors = data.opatest.errors", + ctx, + tc.verifier, + ) job := &api.Job{ TaskGroups: []*api.TaskGroup{ { @@ -156,3 +162,18 @@ func TestNotationImageValidation(t *testing.T) { }) } } + +func TestCreateQueryIfNotationFnIsUsedButVerifierIsNil(t *testing.T) { + + ctx := context.Background() + + path := testutil.Filepath(t, "opa/test_notation.rego") + + _, err := CreateQuery(path, + "errors = data.opatest.errors", + ctx, + nil, + ) + assert.Error(t, err, "Error creating query") + +} diff --git a/example/notation/capabilities.json b/example/notation/capabilities.json index 58d51bd..b9287c6 100644 --- a/example/notation/capabilities.json +++ b/example/notation/capabilities.json @@ -1,7 +1,7 @@ { "builtins": [ { - "name": "valid_notation_image", + "name": "notation_verify_image", "decl": { "args": [ { diff --git a/example/notation/demo.tape b/example/notation/demo.tape new file mode 100644 index 0000000..3a76c04 --- /dev/null +++ b/example/notation/demo.tape @@ -0,0 +1,93 @@ +# VHS documentation +# +# Require: + +# +# Settings: +# Set FontSize Set the font size of the terminal +# Set FontFamily Set the font family of the terminal +# Set Height Set the height of the terminal +# Set Width Set the width of the terminal +# Set LetterSpacing Set the font letter spacing (tracking) +# Set LineHeight Set the font line height +# Set LoopOffset % Set the starting frame offset for the GIF loop +# Set Theme Set the theme of the terminal +# Set Padding Set the padding of the terminal +# Set Framerate Set the framerate of the recording +# Set PlaybackSpeed Set the playback speed of the recording +# Set MarginFill Set the file or color the margin will be filled with. +# Set Margin Set the size of the margin. Has no effect if MarginFill isn't set. +# Set BorderRadius Set terminal border radius, in pixels. +# Set WindowBar Set window bar type. (one of: Rings, RingsRight, Colorful, ColorfulRight) +# Set WindowBarSize Set window bar size, in pixels. Default is 40. +# Set TypingSpeed