Skip to content

Commit b0fb5e9

Browse files
authored
chore(policies): support file:// to reference filesystem policies (#1267)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent dce6791 commit b0fb5e9

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

docs/docs/reference/policies.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ materials:
4545
type: CONTAINER_IMAGE
4646
policies:
4747
materials: # policies applied to materials
48-
- ref: cyclonedx-licenses.yaml # (1)
48+
- ref: file://cyclonedx-licenses.yaml # (1)
4949
attestation: # policies applied to the whole attestation
5050
- ref: https://github.com/chainloop/chainloop-dev/blob/main/docs/examples/policies/chainloop-commit.yaml # (2)
5151
```
@@ -56,7 +56,7 @@ Here we can see that:
5656
```yaml
5757
policies:
5858
materials:
59-
- ref: cyclonedx-licenses.yaml
59+
- ref: file://cyclonedx-licenses.yaml
6060
selector: # (3)
6161
name: sbom
6262
```
@@ -68,11 +68,11 @@ Finally, note that material policies are evaluated during `chainloop attestation
6868

6969
### Embedding or referencing policies
7070
There are two ways to attach a policy to a contract:
71-
* **By referencing it**, as it can be seen in the examples above. `ref` property admits a local (filesystem) or remote reference (HTTPS). For example:
71+
* **By referencing it**, as it can be seen in the examples above. `ref` property admits a local `file://`` (filesystem) or remote reference `https://`. For example:
7272
```yaml
7373
policies:
7474
materials:
75-
- ref: cyclonedx-licenses.yaml # local reference
75+
- ref: file://cyclonedx-licenses.yaml # local reference
7676
```
7777
and
7878
```yaml

pkg/policies/loader.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package policies
1818
import (
1919
"context"
2020
"fmt"
21+
"os"
2122
"path/filepath"
2223
"strings"
2324
"sync"
@@ -45,13 +46,28 @@ func (e *EmbeddedLoader) Load(_ context.Context, attachment *v1.PolicyAttachment
4546
type BlobLoader struct{}
4647

4748
func (l *BlobLoader) Load(_ context.Context, attachment *v1.PolicyAttachment) (*v1.Policy, error) {
49+
var (
50+
rawData []byte
51+
err error
52+
)
53+
4854
reference := attachment.GetRef()
4955

50-
// look for the referenced policy spec (note: loading by `name` is not supported yet)
51-
// this method understands env, http and https schemes, and defaults to file system.
52-
rawData, err := blob.LoadFileOrURL(reference)
53-
if err != nil {
54-
return nil, fmt.Errorf("loading policy spec: %w", err)
56+
// Support file:// references
57+
parts := strings.SplitAfterN(reference, "://", 2)
58+
if len(parts) == 2 && parts[0] == "file://" {
59+
rawData, err = os.ReadFile(filepath.Clean(parts[1]))
60+
if err != nil {
61+
return nil, fmt.Errorf("loading policy spec: %w", err)
62+
}
63+
}
64+
65+
// this method understands env, http and https schemes, and defaults to file system (without scheme).
66+
if rawData == nil {
67+
rawData, err = blob.LoadFileOrURL(reference)
68+
if err != nil {
69+
return nil, fmt.Errorf("loading policy spec: %w", err)
70+
}
5571
}
5672

5773
jsonContent, err := materials.LoadJSONBytes(rawData, filepath.Ext(reference))

pkg/policies/policies_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,17 @@ func (s *testSuite) TestLoadPolicySpec() {
492492
expectedDesc: "This policy checks that the SPDX SBOM was created with syft",
493493
expectedCategory: "SBOM",
494494
},
495+
{
496+
name: "by file ref",
497+
attachment: &v12.PolicyAttachment{
498+
Policy: &v12.PolicyAttachment_Ref{
499+
Ref: "file://testdata/sbom_syft.yaml",
500+
},
501+
},
502+
expectedName: "made-with-syft",
503+
expectedDesc: "This policy checks that the SPDX SBOM was created with syft",
504+
expectedCategory: "SBOM",
505+
},
495506
{
496507
name: "embedded invalid",
497508
attachment: &v12.PolicyAttachment{
@@ -557,6 +568,11 @@ func (s *testSuite) TestLoader() {
557568
ref: "local-policy.yaml",
558569
expected: &BlobLoader{},
559570
},
571+
{
572+
name: "file ref",
573+
ref: "file://local-policy.yaml",
574+
expected: &BlobLoader{},
575+
},
560576
{
561577
name: "http ref",
562578
ref: "https://myhost/policy.yaml",

0 commit comments

Comments
 (0)