diff --git a/internal/evaluation_target/application_snapshot_image/application_snapshot_image.go b/internal/evaluation_target/application_snapshot_image/application_snapshot_image.go index f7e14b93f..166997b56 100644 --- a/internal/evaluation_target/application_snapshot_image/application_snapshot_image.go +++ b/internal/evaluation_target/application_snapshot_image/application_snapshot_image.go @@ -63,10 +63,6 @@ type ApplicationSnapshotImage struct { snapshot app.SnapshotSpec } -func (a ApplicationSnapshotImage) GetReference() name.Reference { - return a.reference -} - // NewApplicationSnapshotImage returns an ApplicationSnapshotImage struct with reference, checkOpts, and evaluator ready to use. func NewApplicationSnapshotImage(ctx context.Context, component app.SnapshotComponent, p policy.Policy, snap app.SnapshotSpec) (*ApplicationSnapshotImage, error) { opts, err := p.CheckOpts() diff --git a/internal/policy/__snapshots__/policy_test.snap b/internal/policy/__snapshots__/policy_test.snap deleted file mode 100755 index 86c10188b..000000000 --- a/internal/policy/__snapshots__/policy_test.snap +++ /dev/null @@ -1,174 +0,0 @@ - -[TestJsonSchemaFromPolicySpec - 1] -{ - "$defs": { - "EnterpriseContractPolicyConfiguration": { - "additionalProperties": false, - "properties": { - "collections": { - "items": { - "type": "string" - }, - "type": "array" - }, - "exclude": { - "items": { - "type": "string" - }, - "type": "array" - }, - "include": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "type": "object" - }, - "EnterpriseContractPolicySpec": { - "additionalProperties": false, - "properties": { - "configuration": { - "$ref": "#/$defs/EnterpriseContractPolicyConfiguration" - }, - "description": { - "type": "string" - }, - "identity": { - "$ref": "#/$defs/Identity" - }, - "name": { - "type": "string" - }, - "publicKey": { - "type": "string" - }, - "rekorUrl": { - "type": "string" - }, - "sources": { - "items": { - "$ref": "#/$defs/Source" - }, - "type": "array" - } - }, - "type": "object" - }, - "Identity": { - "additionalProperties": false, - "properties": { - "issuer": { - "type": "string" - }, - "issuerRegExp": { - "type": "string" - }, - "subject": { - "type": "string" - }, - "subjectRegExp": { - "type": "string" - } - }, - "type": "object" - }, - "JSON": { - "additionalProperties": false, - "properties": {}, - "type": "object" - }, - "Source": { - "additionalProperties": false, - "properties": { - "config": { - "$ref": "#/$defs/SourceConfig" - }, - "data": { - "items": { - "type": "string" - }, - "type": "array" - }, - "name": { - "type": "string" - }, - "policy": { - "items": { - "type": "string" - }, - "type": "array" - }, - "ruleData": { - "$ref": "#/$defs/JSON" - }, - "volatileConfig": { - "$ref": "#/$defs/VolatileSourceConfig" - } - }, - "type": "object" - }, - "SourceConfig": { - "additionalProperties": false, - "properties": { - "exclude": { - "items": { - "type": "string" - }, - "type": "array" - }, - "include": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "type": "object" - }, - "VolatileCriteria": { - "additionalProperties": false, - "properties": { - "effectiveOn": { - "type": "string" - }, - "effectiveUntil": { - "type": "string" - }, - "imageRef": { - "type": "string" - }, - "value": { - "type": "string" - } - }, - "required": [ - "value" - ], - "type": "object" - }, - "VolatileSourceConfig": { - "additionalProperties": false, - "properties": { - "exclude": { - "items": { - "$ref": "#/$defs/VolatileCriteria" - }, - "type": "array" - }, - "include": { - "items": { - "$ref": "#/$defs/VolatileCriteria" - }, - "type": "array" - } - }, - "type": "object" - } - }, - "$id": "https://github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1/enterprise-contract-policy-spec", - "$ref": "#/$defs/EnterpriseContractPolicySpec", - "$schema": "https://json-schema.org/draft/2020-12/schema" -} ---- diff --git a/internal/policy/policy.go b/internal/policy/policy.go index c12807ed5..c6d432cd0 100644 --- a/internal/policy/policy.go +++ b/internal/policy/policy.go @@ -20,7 +20,6 @@ import ( "context" "crypto" _ "embed" - "encoding/json" "errors" "fmt" "os" @@ -29,7 +28,6 @@ import ( "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1" ecc "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1" - schemaExporter "github.com/invopop/jsonschema" "github.com/santhosh-tekuri/jsonschema/v5" "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio" "github.com/sigstore/cosign/v2/cmd/cosign/cli/rekor" @@ -55,27 +53,12 @@ const ( // allows controlling time in tests var now = time.Now -var ( - PolicySourcesFrom = source.PolicySourcesFrom - CreateWorkDir = utils.CreateWorkDir - PolicyCacheFromContext = cache.PolicyCacheFromContext -) +var PolicySourcesFrom = source.PolicySourcesFrom func ValidatePolicy(ctx context.Context, policyConfig string) error { return validatePolicyConfig(policyConfig) } -// Create a JSON schema from a Go type, and return the JSON as a byte slice -func jsonSchemaFromPolicySpec(ecp *ecc.EnterpriseContractPolicySpec) ([]byte, error) { - r := new(schemaExporter.Reflector) - schema := r.Reflect(ecp) - schemaJson, err := json.MarshalIndent(schema, "", " ") - if err != nil { - return nil, err - } - return schemaJson, nil -} - type SigstoreOpts struct { CertificateIdentity string `json:"certificate_identity"` CertificateIdentityRegExp string `json:"certificate_identity_regexp"` diff --git a/internal/policy/policy_test.go b/internal/policy/policy_test.go index acb7a456b..c46b953f4 100644 --- a/internal/policy/policy_test.go +++ b/internal/policy/policy_test.go @@ -29,7 +29,6 @@ import ( hd "github.com/MakeNowJust/heredoc" ecc "github.com/enterprise-contract/enterprise-contract-controller/api/v1alpha1" - "github.com/gkampitakis/go-snaps/snaps" "github.com/sigstore/cosign/v2/pkg/cosign" cosignSig "github.com/sigstore/cosign/v2/pkg/signature" sigstoreSig "github.com/sigstore/sigstore/pkg/signature" @@ -718,17 +717,6 @@ func TestIsConformant(t *testing.T) { } } -func TestJsonSchemaFromPolicySpec(t *testing.T) { - ecp := &ecc.EnterpriseContractPolicySpec{ - PublicKey: "testPublicKey", - RekorUrl: "testRekorUrl", - } - schemaJson, err := jsonSchemaFromPolicySpec(ecp) - assert.NoError(t, err) - - snaps.MatchJSON(t, schemaJson) -} - func TestSigstoreOpts(t *testing.T) { cases := []struct { name string diff --git a/internal/utils/helpers.go b/internal/utils/helpers.go index 7235a6094..d6cc5f46c 100644 --- a/internal/utils/helpers.go +++ b/internal/utils/helpers.go @@ -17,14 +17,12 @@ package utils import ( - "bytes" "context" "encoding/json" "fmt" "os" "path/filepath" "strings" - "unicode" isatty "github.com/mattn/go-isatty" log "github.com/sirupsen/logrus" @@ -32,30 +30,6 @@ import ( "sigs.k8s.io/yaml" ) -// ToJSON converts a single YAML document into a JSON document -// or returns an error. If the document appears to be JSON the -// YAML decoding path is not used. -func ToJSON(data []byte) ([]byte, error) { - if hasJSONPrefix(data) { - return data, nil - } - return yaml.YAMLToJSON(data) -} - -var jsonPrefix = []byte("{") - -// hasJSONPrefix returns true if the provided buffer appears to start with -// a JSON open brace. -func hasJSONPrefix(buf []byte) bool { - return hasPrefix(buf, jsonPrefix) -} - -// hasPrefix returns true if the first non-whitespace bytes in buf is prefix. -func hasPrefix(buf []byte, prefix []byte) bool { - trim := bytes.TrimLeftFunc(buf, unicode.IsSpace) - return bytes.HasPrefix(trim, prefix) -} - // CreateWorkDir creates the working directory in tmp and some subdirectories func CreateWorkDir(fs afero.Fs) (string, error) { workDir, err := afero.TempDir(fs, afero.GetTempDir(fs, ""), "ec-work-") diff --git a/internal/utils/helpers_test.go b/internal/utils/helpers_test.go index fa1ad8277..0c4283693 100644 --- a/internal/utils/helpers_test.go +++ b/internal/utils/helpers_test.go @@ -20,145 +20,12 @@ package utils import ( "context" - "reflect" "testing" "github.com/spf13/afero" "github.com/stretchr/testify/assert" ) -var testJSONPipelineData = `{ - "apiVersion": "tekton.dev/v1", - "kind": "Pipeline", - "metadata": { - "name": "run-component-build" - } -} -` - -var testYAMLPipelineData = `apiVersion: tekton.dev/v1 -kind: Pipeline -metadata: - name: run-component-build -` - -var testYamlConvertedToJSON = `{"apiVersion":"tekton.dev/v1","kind":"Pipeline","metadata":{"name":"run-component-build"}}` - -var testJSONMissingPrefix = `"apiVersion": "tekton.dev/v1", - "kind": "Pipeline", - "metadata": { - "name": "run-component-build" - } -} -` - -var testHasPrefixData = `[ - this is a test -]` - -func TestToJSON(t *testing.T) { - type args struct { - data []byte - } - tests := []struct { - name string - args args - want []byte - wantErr bool - }{ - { - name: "Returns JSON unchanged", - args: args{data: []byte(testJSONPipelineData)}, - want: []byte(testJSONPipelineData), - wantErr: false, - }, - { - name: "Converts YAML to JSON", - args: args{data: []byte(testYAMLPipelineData)}, - want: []byte(testYamlConvertedToJSON), - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := ToJSON(tt.args.data) - if (err != nil) != tt.wantErr { - t.Errorf("ToJSON() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("ToJSON() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_hasJSONPrefix(t *testing.T) { - type args struct { - buf []byte - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "Returns true when []byte begins with JSON prefix", - args: args{buf: []byte(testJSONPipelineData)}, - want: true, - }, - { - name: "Returns false when []byte begins with JSON prefix", - args: args{buf: []byte(testJSONMissingPrefix)}, - want: false, - }, - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := hasJSONPrefix(tt.args.buf); got != tt.want { - t.Errorf("hasJSONPrefix() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_hasPrefix(t *testing.T) { - type args struct { - buf []byte - prefix []byte - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "Returns true if []byte begins with the specified prefix", - args: args{ - buf: []byte(testHasPrefixData), - prefix: []byte("["), - }, - want: true, - }, - { - name: "Returns false if []byte doesn't begins with the specified prefix", - args: args{ - buf: []byte(testHasPrefixData), - prefix: []byte("{"), - }, - want: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := hasPrefix(tt.args.buf, tt.args.prefix); got != tt.want { - t.Errorf("hasPrefix() = %v, want %v", got, tt.want) - } - }) - } -} - func TestCreateWorkDir(t *testing.T) { temp, err := CreateWorkDir(afero.NewMemMapFs()) diff --git a/internal/utils/templates.go b/internal/utils/templates.go index 345e58d65..c2c575124 100644 --- a/internal/utils/templates.go +++ b/internal/utils/templates.go @@ -56,11 +56,6 @@ func SetupTemplateWithGlob(glob []string, efs embed.FS) (*template.Template, err return t, nil } -// Here we do the ExecuteTemplate for the caller and return just the output -func RenderFromTemplates(input any, efs embed.FS) ([]byte, error) { - return RenderFromTemplatesWithMain(input, defaultMainTemplate, efs) -} - func RenderFromTemplatesWithMain(input any, main string, efs embed.FS) ([]byte, error) { return RenderFromTemplatesWithGlob(input, main, []string{defaultGlob}, efs) } diff --git a/internal/utils/templates_test.go b/internal/utils/templates_test.go index 4e5e6499c..2107780e4 100644 --- a/internal/utils/templates_test.go +++ b/internal/utils/templates_test.go @@ -30,23 +30,6 @@ import ( //go:embed test_templates/*.tmpl var testTemplatesFS embed.FS -func TestTemplateRender(t *testing.T) { - tests := []struct { - expected string - input map[string]string - }{ - { - input: map[string]string{"name": "world"}, - expected: "✓ Hello and greetings, world.\n\n", - }, - } - for _, tt := range tests { - output, err := RenderFromTemplates(tt.input, testTemplatesFS) - assert.NoError(t, err) - assert.Equal(t, tt.expected, string(output)) - } -} - func TestSetupTemplate(t *testing.T) { tmpl, err := SetupTemplate(testTemplatesFS) assert.NoError(t, err)