Skip to content

Commit

Permalink
fix(policies): fix provider parsing for pinned policies (chainloop-de…
Browse files Browse the repository at this point in the history
…v#1410)

Signed-off-by: Jose I. Paris <[email protected]>
  • Loading branch information
jiparis authored Oct 17, 2024
1 parent cdd749a commit 0c6836b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/controlplane/api/workflowcontract/v1/crafting_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,22 @@ func TestValidateRefs(t *testing.T) {
ref: "foobar:policy_name@foobar",
wantErrString: "invalid digest",
},
{
name: "valid digest",
ref: "foobar:policy-name@sha256:133d39edc0f0d32780dd9c940951df0910ef53e6fd64942801ba6fb76494bbf9",
},
{
name: "chainloop provider with valid digest",
ref: "foobar:policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
},
{
name: "custom policy with valid digest",
ref: "readonly-demo/policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
},
{
name: "builtin policy with valid digest",
ref: "policy-name@sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
},
{
name: "unsupported protocol",
ref: "unsupported://foobar/policy_name",
Expand Down
19 changes: 18 additions & 1 deletion pkg/policies/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,19 @@ type ProviderRef struct {
}

// ProviderParts returns the provider information for a given reference
func ProviderParts(ref string) *ProviderRef {
func ProviderParts(reference string) *ProviderRef {
var ref, digest string
// first of all, remove the @sha256 suffix to make the parsing easier
withDigest := strings.SplitN(reference, "@sha256:", 2)

if len(withDigest) > 1 {
// it has digest
ref = withDigest[0]
digest = withDigest[1]
} else {
ref = reference
}

parts := strings.SplitN(ref, "://", 2)
var pn []string
if len(parts) == 1 {
Expand All @@ -232,6 +244,11 @@ func ProviderParts(ref string) *ProviderRef {
name = scoped[1]
}

// return the digest back
if digest != "" {
name = fmt.Sprintf("%s@sha256:%s", name, digest)
}

return &ProviderRef{
Provider: provider,
Name: name,
Expand Down
32 changes: 28 additions & 4 deletions pkg/policies/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ func (s *testSuite) TestVerifyAttestations() {

func (s *testSuite) TestProviderParts() {
testCases := []struct {
ref string
prov string
name string
org string
ref string
prov string
name string
org string
digest string
}{
{
ref: "chainloop://cyclonedx-freshness",
Expand Down Expand Up @@ -302,6 +303,29 @@ func (s *testSuite) TestProviderParts() {
org: "myorg",
name: "cyclonedx-freshness",
},
{
ref: "myorg/cyclonedx-freshness@sha256:123123123",
org: "myorg",
name: "cyclonedx-freshness@sha256:123123123",
},
{
ref: "builtin:myorg/cyclonedx-freshness@sha256:123123123",
prov: "builtin",
org: "myorg",
name: "cyclonedx-freshness@sha256:123123123",
},
{
ref: "chainloop://builtin:myorg/cyclonedx-freshness@sha256:123123123",
prov: "builtin",
org: "myorg",
name: "cyclonedx-freshness@sha256:123123123",
},
{
ref: "chainloop://myorg/cyclonedx-freshness@sha256:123123123",
prov: "",
org: "myorg",
name: "cyclonedx-freshness@sha256:123123123",
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 0c6836b

Please sign in to comment.