|
| 1 | +// Package customsignaturestore implements a signature store as configured by ClusterVersion. |
| 2 | +package customsignaturestore |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + configv1listers "github.com/openshift/client-go/config/listers/config/v1" |
| 13 | + "github.com/openshift/library-go/pkg/verify/store" |
| 14 | + "github.com/openshift/library-go/pkg/verify/store/parallel" |
| 15 | + "github.com/openshift/library-go/pkg/verify/store/sigstore" |
| 16 | +) |
| 17 | + |
| 18 | +type Store struct { |
| 19 | + // Name is the name of the ClusterVersion object that configures this store. |
| 20 | + Name string |
| 21 | + |
| 22 | + // Lister allows the store to fetch the current ClusterVersion configuration. |
| 23 | + Lister configv1listers.ClusterVersionLister |
| 24 | + |
| 25 | + // HTTPClient is called once for each Signatures call to ensure |
| 26 | + // requests are made with the currently-recommended parameters. |
| 27 | + HTTPClient sigstore.HTTPClient |
| 28 | + |
| 29 | + // lock allows the store to be locked while mutating or accessing internal state. |
| 30 | + lock sync.Mutex |
| 31 | + |
| 32 | + // customURIs tracks the most-recently retrieved ClusterVersion configuration. |
| 33 | + customURIs []*url.URL |
| 34 | +} |
| 35 | + |
| 36 | +// Signatures fetches signatures for the provided digest. |
| 37 | +func (s *Store) Signatures(ctx context.Context, name string, digest string, fn store.Callback) error { |
| 38 | + uris, err := s.refreshConfiguration(ctx) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + if uris == nil { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + if len(uris) == 0 { |
| 48 | + return errors.New("ClusterVersion spec.signatureStores is an empty array. Unset signatureStores entirely if you want to to enable the default signature stores.") |
| 49 | + } |
| 50 | + |
| 51 | + allDone := false |
| 52 | + |
| 53 | + wrapper := func(ctx context.Context, signature []byte, errIn error) (done bool, err error) { |
| 54 | + done, err = fn(ctx, signature, errIn) |
| 55 | + if done { |
| 56 | + allDone = true |
| 57 | + } |
| 58 | + return done, err |
| 59 | + } |
| 60 | + |
| 61 | + stores := make([]store.Store, 0, len(uris)) |
| 62 | + for i := range uris { |
| 63 | + uri := *uris[i] |
| 64 | + stores = append(stores, &sigstore.Store{ |
| 65 | + URI: &uri, |
| 66 | + HTTPClient: s.HTTPClient, |
| 67 | + }) |
| 68 | + } |
| 69 | + store := ¶llel.Store{Stores: stores} |
| 70 | + if err := store.Signatures(ctx, name, digest, wrapper); err != nil || allDone { |
| 71 | + return err |
| 72 | + } |
| 73 | + return errors.New("ClusterVersion spec.signatureStores exhausted without finding a valid signature.") |
| 74 | +} |
| 75 | + |
| 76 | +func (s *Store) refreshConfiguration(ctx context.Context) ([]*url.URL, error) { |
| 77 | + var uris []*url.URL |
| 78 | + config, err := s.Lister.Get(s.Name) |
| 79 | + if err != nil { |
| 80 | + return uris, err |
| 81 | + } |
| 82 | + |
| 83 | + if config.Spec.SignatureStores != nil { |
| 84 | + uris = make([]*url.URL, 0, len(config.Spec.SignatureStores)) |
| 85 | + for _, store := range config.Spec.SignatureStores { |
| 86 | + uri, err := url.Parse(store.URL) |
| 87 | + if err != nil { |
| 88 | + return uris, err |
| 89 | + } |
| 90 | + |
| 91 | + uris = append(uris, uri) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + s.lock.Lock() |
| 96 | + defer s.lock.Unlock() |
| 97 | + s.customURIs = uris |
| 98 | + return uris, err |
| 99 | +} |
| 100 | + |
| 101 | +// String returns a description of where this store finds |
| 102 | +// signatures. |
| 103 | +func (s *Store) String() string { |
| 104 | + s.lock.Lock() |
| 105 | + defer s.lock.Unlock() |
| 106 | + |
| 107 | + if s.customURIs == nil { |
| 108 | + return "ClusterVersion signatureStores unset, falling back to default stores" |
| 109 | + } else if len(s.customURIs) == 0 { |
| 110 | + return "0 ClusterVersion signatureStores" |
| 111 | + } |
| 112 | + uris := make([]string, 0, len(s.customURIs)) |
| 113 | + for _, uri := range s.customURIs { |
| 114 | + uris = append(uris, uri.String()) |
| 115 | + } |
| 116 | + return fmt.Sprintf("ClusterVersion signatureStores: %s", strings.Join(uris, ", ")) |
| 117 | +} |
0 commit comments