File: eval_test.go

package info (click to toggle)
cosign 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,960 kB
  • sloc: sh: 222; makefile: 170
file content (290 lines) | stat: -rw-r--r-- 9,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package policy

import (
	"context"
	"strings"
	"testing"
)

const (
	customAttestation = `
	{
		"_type": "https://in-toto.io/Statement/v0.1",
		"predicateType": "https://cosign.sigstore.dev/attestation/v1",
		"subject": [
		  {
			"name": "registry.local:5000/policy-controller/demo",
			"digest": {
			  "sha256": "416cc82c76114b1744ea58bcbf2f411a0f2de4b0456703bf1bb83d33656951bc"
			}
		  }
		],
		"predicate": {
		  "Data": "foobar e2e test",
		  "Timestamp": "2022-04-20T18:17:19Z"
		}
	  }`

	vulnAttestation = `
	{
		"_type": "https://in-toto.io/Statement/v0.1",
		"predicateType": "https://cosign.sigstore.dev/attestation/vuln/v1",
		"subject": [
		  {
			"name": "registry.local:5000/policy-controller/demo",
			"digest": {
			  "sha256": "416cc82c76114b1744ea58bcbf2f411a0f2de4b0456703bf1bb83d33656951bc"
			}
		  }
		],
		"predicate": {
		  "invocation": {
			"parameters": null,
			"uri": "invocation.example.com/cosign-testing",
			"event_id": "",
			"builder.id": ""
		  },
		  "scanner": {
			"uri": "fakescanner.example.com/cosign-testing",
			"version": "",
			"db": {
			  "uri": "",
			  "version": ""
			},
			"result": null
		  },
		  "metadata": {
			"scanStartedOn": "2022-04-12T00:00:00Z",
			"scanFinishedOn": "2022-04-12T00:10:00Z"
		  }
		}
	  }`

	cipAttestation = "{\"authorityMatches\":{\"keyatt\":{\"signatures\":null,\"attestations\":{\"vuln-key\":[{\"subject\":\"PLACEHOLDER\",\"issuer\":\"PLACEHOLDER\"}]}},\"keysignature\":{\"signatures\":[{\"subject\":\"PLACEHOLDER\",\"issuer\":\"PLACEHOLDER\"}],\"attestations\":null},\"keylessatt\":{\"signatures\":null,\"attestations\":{\"custom-keyless\":[{\"subject\":\"PLACEHOLDER\",\"issuer\":\"PLACEHOLDER\"}]}}}}"
)

func TestEvalPolicy(t *testing.T) {
	// TODO(vaikas): Consider moving the attestations/cue files into testdata
	// directory.
	tests := []struct {
		name        string
		json        string
		policyType  string
		policyFile  string
		wantErr     bool
		wantErrSub  string
		wantWarnSub string
	}{{
		name:       "custom attestation, mismatched predicateType",
		json:       customAttestation,
		policyType: "cue",
		policyFile: `predicateType: "https://cosign.sigstore.dev/attestation/vuln/v1"`,
		wantErr:    true,
		wantErrSub: `conflicting values "https://cosign.sigstore.dev/attestation/v1" and "https://cosign.sigstore.dev/attestation/vuln/v1"`,
	}, {
		name:       "custom attestation, predicateType and data checks out",
		json:       customAttestation,
		policyType: "cue",
		policyFile: `predicateType: "https://cosign.sigstore.dev/attestation/v1"
		predicate: Data: "foobar e2e test"`,
	}, {
		name:       "custom attestation, data mismatch",
		json:       customAttestation,
		policyType: "cue",
		policyFile: `predicateType: "https://cosign.sigstore.dev/attestation/v1"
		predicate: Data: "invalid data here"`,
		wantErr:    true,
		wantErrSub: `predicate.Data: conflicting values "foobar e2e test" and "invalid data here"`,
	}, {
		name:       "vuln attestation, wrong invocation url",
		json:       vulnAttestation,
		policyType: "cue",
		policyFile: `predicateType: "https://cosign.sigstore.dev/attestation/vuln/v1"
		predicate: invocation: uri: "invocation.example.com/wrong-url-here"`,
		wantErr:    true,
		wantErrSub: `conflicting values "invocation.example.com/cosign-testing" and "invocation.example.com/wrong-url-here"`,
	}, {
		name:       "vuln attestation, checks out",
		json:       vulnAttestation,
		policyType: "cue",
		policyFile: `predicateType: "https://cosign.sigstore.dev/attestation/vuln/v1"
		predicate: invocation: uri: "invocation.example.com/cosign-testing"`,
	}, {
		name:       "cluster image policy main policy, checks out",
		json:       cipAttestation,
		policyType: "cue",
		policyFile: `package sigstore
		import "struct"
		import "list"
		authorityMatches: {
		  keyatt: {
			attestations: struct.MaxFields(1) & struct.MinFields(1)
		  },
		  keysignature: {
			signatures: list.MaxItems(1) & list.MinItems(1)
		  },
		  keylessatt: {
			attestations: struct.MaxFields(1) & struct.MinFields(1)
		  },
		  keylesssignature: {
			signatures: list.MaxItems(1) & list.MinItems(1)
		  }
		}`,
	}, {
		name:       "cluster image policy main policy, fails",
		json:       cipAttestation,
		policyType: "cue",
		wantErr:    true,
		wantErrSub: `failed evaluating cue policy for cluster image policy main policy, fails: failed to evaluate the policy with error: authorityMatches.keylessattMinAttestations: conflicting values 2 and "Error" (mismatched types int and string)`,
		policyFile: `package sigstore
		import "struct"
		import "list"
		authorityMatches: {
		  keyatt: {
			attestations: struct.MaxFields(1) & struct.MinFields(1)
		  },
		  keysignature: {
			signatures: list.MaxItems(1) & list.MinItems(1)
		  },
		  if( len(authorityMatches.keylessatt.attestations) < 2) {
			keylessattMinAttestations: 2
			keylessattMinAttestations: "Error"
		  },
		  keylesssignature: {
			signatures: list.MaxItems(1) & list.MinItems(1)
		  }
		}`}, {
		name:       "Rego cluster image policy main policy, checks out",
		json:       cipAttestation,
		policyType: "rego",
		policyFile: `package sigstore
			default isCompliant = false
			isCompliant {
				attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
				count(attestationsKeylessATT) == 1
				attestationsKeyATT := input.authorityMatches.keyatt.attestations
				count(attestationsKeyATT) == 1
				keySignature := input.authorityMatches.keysignature.signatures
				count(keySignature) == 1
			}`,
	},
		{
			name:       "Rego cluster image policy main policy, fails",
			json:       cipAttestation,
			policyType: "rego",
			wantErr:    true,
			wantErrSub: `failed evaluating rego policy for type Rego cluster image policy main policy, fails: policy is not compliant for query 'isCompliant = data.sigstore.isCompliant'`,
			policyFile: `package sigstore
			default isCompliant = false
			isCompliant {
			    attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
				count(attestationsKeylessATT) == 2
				attestationsKeyATT := input.authorityMatches.keyatt.attestations
				count(attestationsKeyATT) == 1
				keySignature := input.authorityMatches.keysignature.signatures
				count(keySignature) == 1
			}`,
		}, {
			name:       "Rego cluster image policy main policy succeed with empty error msg",
			json:       cipAttestation,
			policyType: "rego",
			policyFile: `package sigstore
			isCompliant[response] {
			    attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
				result = (count(attestationsKeylessATT) == 1)
				attestationsKeyATT := input.authorityMatches.keyatt.attestations
				result = (count(attestationsKeyATT) == 1)
				keySignature := input.authorityMatches.keysignature.signatures
				result = (count(keySignature) == 1)

				errorMsg = ""
				warnMsg = ""

				response := {
					"result" : result,
					"error" : errorMsg,
					"warning" : warnMsg
				}
			}`,
		}, {
			name:       "Rego cluster image policy main policy, fails with custom error msg",
			json:       cipAttestation,
			policyType: "rego",
			wantErr:    true,
			wantErrSub: `Not found expected list of attestations`,
			policyFile: `package sigstore
			isCompliant[response] {
			    attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
				result = (count(attestationsKeylessATT) == 1000)

				errorMsg = "Not found expected list of attestations"
				warnMsg = ""

				response := {
					"result" : result,
					"error" : errorMsg,
					"warning" : warnMsg
				}
			}`,
		}, {
			name:        "Rego cluster image policy main policy, returns a custom warning msg",
			json:        cipAttestation,
			policyType:  "rego",
			wantErr:     false,
			wantWarnSub: `Throw warning error even if succeeded`,
			policyFile: `package sigstore
			isCompliant[response] {
				attestationsKeylessATT := input.authorityMatches.keylessatt.attestations
				result = (count(attestationsKeylessATT) == 1)
				attestationsKeyATT := input.authorityMatches.keyatt.attestations
				result = (count(attestationsKeyATT) == 1)
				keySignature := input.authorityMatches.keysignature.signatures
				result = (count(keySignature) == 1)

				errorMsg = ""
				warnMsg = "Throw warning error even if succeeded"

				response := {
					"result" : result,
					"error" : errorMsg,
					"warning" : warnMsg
				}
			}`,
		}}
	for _, tc := range tests {
		ctx := context.Background()
		warn, err := EvaluatePolicyAgainstJSON(ctx, tc.name, tc.policyType, tc.policyFile, []byte(tc.json))
		if tc.wantErr {
			if err == nil {
				t.Errorf("Did not get an error, wanted %s", tc.wantErrSub)
			} else if !strings.Contains(err.Error(), tc.wantErrSub) {
				t.Errorf("Unexpected error, want: %s got: %s", tc.wantErrSub, err.Error())
			}
			if tc.wantWarnSub != "" && !strings.Contains(warn.Error(), tc.wantWarnSub) {
				t.Errorf("Unexpected warning, want: %s got: %s", tc.wantErrSub, err.Error())
			}
		} else {
			if !tc.wantErr && err != nil {
				t.Errorf("Unexpected error, wanted none, got: %s", err.Error())
			}
			if tc.wantWarnSub != "" && !strings.Contains(warn.Error(), tc.wantWarnSub) {
				t.Errorf("Unexpected warning, want: %s got: %s", tc.wantWarnSub, warn.Error())
			}
		}
	}
}