|
| 1 | +// |
| 2 | +// Copyright 2024 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package policies |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "sync" |
| 24 | + |
| 25 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 26 | + v1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" |
| 27 | + "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/materials" |
| 28 | + "github.com/sigstore/cosign/v2/pkg/blob" |
| 29 | + "google.golang.org/protobuf/encoding/protojson" |
| 30 | +) |
| 31 | + |
| 32 | +// Loader defines the interface for policy loaders from contract attachments |
| 33 | +type Loader interface { |
| 34 | + Load(context.Context, *v1.PolicyAttachment) (*v1.Policy, error) |
| 35 | +} |
| 36 | + |
| 37 | +// EmbeddedLoader returns embedded policies |
| 38 | +type EmbeddedLoader struct{} |
| 39 | + |
| 40 | +func (e *EmbeddedLoader) Load(_ context.Context, attachment *v1.PolicyAttachment) (*v1.Policy, error) { |
| 41 | + return attachment.GetEmbedded(), nil |
| 42 | +} |
| 43 | + |
| 44 | +// URLLoader loader loads policies from filesystem and HTTPS references |
| 45 | +type URLLoader struct{} |
| 46 | + |
| 47 | +func (l *URLLoader) Load(_ context.Context, attachment *v1.PolicyAttachment) (*v1.Policy, error) { |
| 48 | + reference := attachment.GetRef() |
| 49 | + |
| 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) |
| 55 | + } |
| 56 | + |
| 57 | + jsonContent, err := materials.LoadJSONBytes(rawData, filepath.Ext(reference)) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("loading policy spec: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + var spec v1.Policy |
| 63 | + if err := protojson.Unmarshal(jsonContent, &spec); err != nil { |
| 64 | + return nil, fmt.Errorf("unmarshalling policy spec: %w", err) |
| 65 | + } |
| 66 | + return &spec, nil |
| 67 | +} |
| 68 | + |
| 69 | +const chainloopScheme = "chainloop" |
| 70 | + |
| 71 | +// ChainloopLoader loads policies referenced with chainloop://provider/name URLs |
| 72 | +type ChainloopLoader struct { |
| 73 | + Client pb.AttestationServiceClient |
| 74 | + |
| 75 | + cacheMutex sync.Mutex |
| 76 | +} |
| 77 | + |
| 78 | +var remotePolicyCache = make(map[string]*v1.Policy) |
| 79 | + |
| 80 | +func NewChainloopLoader(client pb.AttestationServiceClient) *ChainloopLoader { |
| 81 | + return &ChainloopLoader{Client: client} |
| 82 | +} |
| 83 | + |
| 84 | +func (c *ChainloopLoader) Load(ctx context.Context, attachment *v1.PolicyAttachment) (*v1.Policy, error) { |
| 85 | + ref := attachment.GetRef() |
| 86 | + |
| 87 | + c.cacheMutex.Lock() |
| 88 | + defer c.cacheMutex.Unlock() |
| 89 | + |
| 90 | + if remotePolicyCache[ref] != nil { |
| 91 | + return remotePolicyCache[ref], nil |
| 92 | + } |
| 93 | + |
| 94 | + parts := strings.SplitN(ref, "://", 2) |
| 95 | + if len(parts) != 2 || parts[0] != chainloopScheme { |
| 96 | + return nil, fmt.Errorf("invalid policy reference %q", ref) |
| 97 | + } |
| 98 | + |
| 99 | + pn := strings.SplitN(parts[1], "/", 2) |
| 100 | + var ( |
| 101 | + name = pn[0] |
| 102 | + provider string |
| 103 | + ) |
| 104 | + if len(pn) == 2 { |
| 105 | + provider = pn[0] |
| 106 | + name = pn[1] |
| 107 | + } |
| 108 | + |
| 109 | + resp, err := c.Client.GetPolicy(ctx, &pb.AttestationServiceGetPolicyRequest{ |
| 110 | + Provider: provider, |
| 111 | + PolicyName: name, |
| 112 | + }) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("loading policy: %w", err) |
| 115 | + } |
| 116 | + |
| 117 | + // cache result |
| 118 | + remotePolicyCache[ref] = resp.GetPolicy() |
| 119 | + |
| 120 | + return resp.GetPolicy(), nil |
| 121 | +} |
0 commit comments