|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package archivista |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "strings" |
| 23 | + |
| 24 | + archivistaClient "github.com/in-toto/archivista/pkg/http-client" |
| 25 | + "github.com/in-toto/go-witness/dsse" |
| 26 | + "github.com/tektoncd/chains/pkg/chains/objects" |
| 27 | + "github.com/tektoncd/chains/pkg/config" |
| 28 | + "knative.dev/pkg/logging" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + // StorageBackendArchivista is the name of the Archivista storage backend |
| 33 | + StorageBackendArchivista = "archivista" |
| 34 | +) |
| 35 | + |
| 36 | +// Backend is a storage backend that is capable of storing Payloaders that are signed and wrapped |
| 37 | +// with a DSSE envelope. Archivista is an in-toto attestation storage service. |
| 38 | +type Backend struct { |
| 39 | + client *archivistaClient.ArchivistaClient |
| 40 | + url string |
| 41 | + cfg config.ArchivistaStorageConfig |
| 42 | +} |
| 43 | + |
| 44 | +// NewStorageBackend returns a new Archivista StorageBackend that can store Payloaders that are signed |
| 45 | +// and wrapped in a DSSE envelope |
| 46 | +func NewStorageBackend(cfg config.Config) (*Backend, error) { |
| 47 | + archCfg := cfg.Storage.Archivista |
| 48 | + if strings.TrimSpace(archCfg.URL) == "" { |
| 49 | + return nil, fmt.Errorf("missing archivista URL in storage configuration") |
| 50 | + } |
| 51 | + |
| 52 | + client, err := archivistaClient.CreateArchivistaClient(&http.Client{}, archCfg.URL) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to create Archivista client: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + return &Backend{ |
| 58 | + client: client, |
| 59 | + url: archCfg.URL, |
| 60 | + cfg: archCfg, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +// StorePayload attempts to parse `signature` as a DSSE envelope, and if successful |
| 65 | +// sends it to an Archivista server for storage. |
| 66 | +func (b *Backend) StorePayload(ctx context.Context, _ objects.TektonObject, _ []byte, signature string, _ config.StorageOpts) error { |
| 67 | + logger := logging.FromContext(ctx) |
| 68 | + var env dsse.Envelope |
| 69 | + if err := json.Unmarshal([]byte(signature), &env); err != nil { |
| 70 | + logger.Errorf("Failed to parse DSSE envelope: %w", err) |
| 71 | + return errors.Join(errors.New("Failed to parse DSSE envelope"), err) |
| 72 | + } |
| 73 | + |
| 74 | + uploadResp, err := b.client.Store(ctx, env) |
| 75 | + if err != nil { |
| 76 | + logger.Errorw("Failed to upload DSSE envelope to Archivista", "error", err) |
| 77 | + return err |
| 78 | + } |
| 79 | + logger.Infof("Successfully uploaded DSSE envelope to Archivista, response: %+v", uploadResp) |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// RetrievePayload is not implemented for Archivista. |
| 84 | +func (b *Backend) RetrievePayload(_ context.Context, _ string) ([]byte, []byte, error) { |
| 85 | + return nil, nil, fmt.Errorf("RetrievePayload not implemented for Archivista") |
| 86 | +} |
| 87 | + |
| 88 | +// RetrievePayloads is not implemented for Archivista. |
| 89 | +func (b *Backend) RetrievePayloads(_ context.Context, _ objects.TektonObject, _ config.StorageOpts) (map[string]string, error) { |
| 90 | + return nil, fmt.Errorf("RetrievePayloads not implemented for Archivista") |
| 91 | +} |
| 92 | + |
| 93 | +// RetrieveSignatures is not implemented for Archivista. |
| 94 | +func (b *Backend) RetrieveSignatures(_ context.Context, _ objects.TektonObject, _ config.StorageOpts) (map[string][]string, error) { |
| 95 | + return nil, fmt.Errorf("RetrieveSignatures not implemented for Archivista") |
| 96 | +} |
| 97 | + |
| 98 | +// Type returns the name of the storage backend |
| 99 | +func (b *Backend) Type() string { |
| 100 | + return StorageBackendArchivista |
| 101 | +} |
0 commit comments