|
| 1 | +package archivista |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + |
| 11 | + archivistaClient "github.com/in-toto/archivista/pkg/http-client" |
| 12 | + "github.com/in-toto/go-witness/dsse" |
| 13 | + "github.com/tektoncd/chains/pkg/chains/objects" |
| 14 | + "github.com/tektoncd/chains/pkg/config" // if needed |
| 15 | + "knative.dev/pkg/logging" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + StorageBackendArchivista = "archivista" |
| 20 | +) |
| 21 | + |
| 22 | +type Backend struct { |
| 23 | + client *archivistaClient.ArchivistaClient |
| 24 | + url string |
| 25 | + cfg config.ArchivistaStorageConfig |
| 26 | +} |
| 27 | + |
| 28 | +// NewStorageBackend returns a new Archivista StorageBackend that can store Payloaders that are signed |
| 29 | +// and wrapped in a DSSE envelope |
| 30 | +func NewStorageBackend(cfg config.Config) (*Backend, error) { |
| 31 | + archCfg := cfg.Storage.Archivista |
| 32 | + if strings.TrimSpace(archCfg.URL) == "" { |
| 33 | + return nil, fmt.Errorf("missing archivista URL in storage configuration") |
| 34 | + } |
| 35 | + |
| 36 | + client, err := archivistaClient.CreateArchivistaClient(&http.Client{}, archCfg.URL) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to create Archivista client: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + return &Backend{ |
| 42 | + client: client, |
| 43 | + url: archCfg.URL, |
| 44 | + cfg: archCfg, |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +// StorePayload attempts to parse `signature` as a DSSE envelope, and if successful |
| 49 | +// sends it to an Archivista server for storage. |
| 50 | +func (a *Backend) StorePayload(ctx context.Context, obj objects.TektonObject, rawPayload []byte, signature string, opts config.StorageOpts) error { |
| 51 | + logger := logging.FromContext(ctx) |
| 52 | + var env dsse.Envelope |
| 53 | + if err := json.Unmarshal([]byte(signature), &env); err != nil { |
| 54 | + logger.Errorf("Failed to parse DSSE envelope: %w", err) |
| 55 | + return errors.Join(errors.New("Failed to parse DSSE envelope"), err) |
| 56 | + } |
| 57 | + |
| 58 | + uploadResp, err := a.client.Store(ctx, env) |
| 59 | + if err != nil { |
| 60 | + logger.Errorw("Failed to upload DSSE envelope to Archivista", "error", err) |
| 61 | + return err |
| 62 | + } |
| 63 | + logger.Infof("Successfully uploaded DSSE envelope to Archivista, response: %+v", uploadResp) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// RetrievePayload is not implemented for Archivista. |
| 68 | +func (a *Backend) RetrievePayload(ctx context.Context, key string) ([]byte, []byte, error) { |
| 69 | + return nil, nil, fmt.Errorf("RetrievePayload not implemented for Archivista") |
| 70 | +} |
| 71 | + |
| 72 | +// RetrievePayloads is not implemented for Archivista. |
| 73 | +func (a *Backend) RetrievePayloads(ctx context.Context, obj objects.TektonObject, opts config.StorageOpts) (map[string]string, error) { |
| 74 | + return nil, fmt.Errorf("RetrievePayloads not implemented for Archivista") |
| 75 | +} |
| 76 | + |
| 77 | +// RetrieveSignatures is not implemented for Archivista. |
| 78 | +func (a *Backend) RetrieveSignatures(ctx context.Context, obj objects.TektonObject, opts config.StorageOpts) (map[string][]string, error) { |
| 79 | + return nil, fmt.Errorf("RetrieveSignatures not implemented for Archivista") |
| 80 | +} |
| 81 | + |
| 82 | +func (a *Backend) Type() string { |
| 83 | + return StorageBackendArchivista |
| 84 | +} |
0 commit comments