Skip to content

Commit 0c6836b

Browse files
authored
fix(policies): fix provider parsing for pinned policies (chainloop-dev#1410)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent cdd749a commit 0c6836b

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

app/controlplane/api/workflowcontract/v1/crafting_schema_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,22 @@ func TestValidateRefs(t *testing.T) {
244244
ref: "foobar:policy_name@foobar",
245245
wantErrString: "invalid digest",
246246
},
247+
{
248+
name: "valid digest",
249+
ref: "foobar:policy-name@sha256:133d39edc0f0d32780dd9c940951df0910ef53e6fd64942801ba6fb76494bbf9",
250+
},
247251
{
248252
name: "chainloop provider with valid digest",
249253
ref: "foobar:policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
250254
},
255+
{
256+
name: "custom policy with valid digest",
257+
ref: "readonly-demo/policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
258+
},
259+
{
260+
name: "builtin policy with valid digest",
261+
ref: "policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
262+
},
251263
{
252264
name: "unsupported protocol",
253265
ref: "unsupported://foobar/policy_name",

pkg/policies/loader.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,19 @@ type ProviderRef struct {
205205
}
206206

207207
// ProviderParts returns the provider information for a given reference
208-
func ProviderParts(ref string) *ProviderRef {
208+
func ProviderParts(reference string) *ProviderRef {
209+
var ref, digest string
210+
// first of all, remove the @sha256 suffix to make the parsing easier
211+
withDigest := strings.SplitN(reference, "@sha256:", 2)
212+
213+
if len(withDigest) > 1 {
214+
// it has digest
215+
ref = withDigest[0]
216+
digest = withDigest[1]
217+
} else {
218+
ref = reference
219+
}
220+
209221
parts := strings.SplitN(ref, "://", 2)
210222
var pn []string
211223
if len(parts) == 1 {
@@ -232,6 +244,11 @@ func ProviderParts(ref string) *ProviderRef {
232244
name = scoped[1]
233245
}
234246

247+
// return the digest back
248+
if digest != "" {
249+
name = fmt.Sprintf("%s@sha256:%s", name, digest)
250+
}
251+
235252
return &ProviderRef{
236253
Provider: provider,
237254
Name: name,

pkg/policies/policies_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ func (s *testSuite) TestVerifyAttestations() {
249249

250250
func (s *testSuite) TestProviderParts() {
251251
testCases := []struct {
252-
ref string
253-
prov string
254-
name string
255-
org string
252+
ref string
253+
prov string
254+
name string
255+
org string
256+
digest string
256257
}{
257258
{
258259
ref: "chainloop://cyclonedx-freshness",
@@ -302,6 +303,29 @@ func (s *testSuite) TestProviderParts() {
302303
org: "myorg",
303304
name: "cyclonedx-freshness",
304305
},
306+
{
307+
ref: "myorg/cyclonedx-freshness@sha256:123123123",
308+
org: "myorg",
309+
name: "cyclonedx-freshness@sha256:123123123",
310+
},
311+
{
312+
ref: "builtin:myorg/cyclonedx-freshness@sha256:123123123",
313+
prov: "builtin",
314+
org: "myorg",
315+
name: "cyclonedx-freshness@sha256:123123123",
316+
},
317+
{
318+
ref: "chainloop://builtin:myorg/cyclonedx-freshness@sha256:123123123",
319+
prov: "builtin",
320+
org: "myorg",
321+
name: "cyclonedx-freshness@sha256:123123123",
322+
},
323+
{
324+
ref: "chainloop://myorg/cyclonedx-freshness@sha256:123123123",
325+
prov: "",
326+
org: "myorg",
327+
name: "cyclonedx-freshness@sha256:123123123",
328+
},
305329
}
306330

307331
for _, tc := range testCases {

0 commit comments

Comments
 (0)