|
| 1 | +package signature |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/containers/image/docker" |
| 10 | + "github.com/golang/glog" |
| 11 | + |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/util/wait" |
| 15 | + "k8s.io/client-go/tools/cache" |
| 16 | + "k8s.io/client-go/util/workqueue" |
| 17 | + kapi "k8s.io/kubernetes/pkg/api" |
| 18 | + "k8s.io/kubernetes/pkg/controller" |
| 19 | + |
| 20 | + imageapi "github.com/openshift/origin/pkg/image/apis/image" |
| 21 | + informers "github.com/openshift/origin/pkg/image/generated/informers/internalversion/image/internalversion" |
| 22 | + imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset" |
| 23 | + imagelister "github.com/openshift/origin/pkg/image/generated/listers/image/internalversion" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + // SignatureManagedAnnotation marks signatures that were imported by this |
| 28 | + // controller. |
| 29 | + SignatureManagedAnnotation = "image.openshift.io/managed-signature" |
| 30 | +) |
| 31 | + |
| 32 | +type SignatureImportController struct { |
| 33 | + imageClient imageclient.Interface |
| 34 | + imageLister imagelister.ImageLister |
| 35 | + |
| 36 | + imageHasSynced cache.InformerSynced |
| 37 | + |
| 38 | + queue workqueue.RateLimitingInterface |
| 39 | + |
| 40 | + ctx context.Context |
| 41 | + fetchTimeout time.Duration |
| 42 | +} |
| 43 | + |
| 44 | +func NewSignatureImportController(ctx context.Context, imageClient imageclient.Interface, imageInformer informers.ImageInformer, resyncInterval, fetchTimeout time.Duration) *SignatureImportController { |
| 45 | + controller := &SignatureImportController{ |
| 46 | + queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), |
| 47 | + ctx: ctx, |
| 48 | + fetchTimeout: fetchTimeout, |
| 49 | + imageClient: imageClient, |
| 50 | + imageLister: imageInformer.Lister(), |
| 51 | + imageHasSynced: imageInformer.Informer().HasSynced, |
| 52 | + } |
| 53 | + |
| 54 | + imageInformer.Informer().AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{ |
| 55 | + AddFunc: func(obj interface{}) { |
| 56 | + image := obj.(*imageapi.Image) |
| 57 | + glog.V(4).Infof("Adding image %s", image.Name) |
| 58 | + controller.enqueueImage(obj) |
| 59 | + }, |
| 60 | + UpdateFunc: func(old, cur interface{}) { |
| 61 | + image := cur.(*imageapi.Image) |
| 62 | + glog.V(4).Infof("Updating image %s", image.Name) |
| 63 | + controller.enqueueImage(cur) |
| 64 | + }, |
| 65 | + }, resyncInterval) |
| 66 | + |
| 67 | + return controller |
| 68 | +} |
| 69 | + |
| 70 | +func (s *SignatureImportController) Run(workers int, stopCh <-chan struct{}) { |
| 71 | + defer utilruntime.HandleCrash() |
| 72 | + defer s.queue.ShutDown() |
| 73 | + |
| 74 | + if !cache.WaitForCacheSync(stopCh, s.imageHasSynced) { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + glog.V(5).Infof("Starting workers") |
| 79 | + for i := 0; i < workers; i++ { |
| 80 | + go wait.Until(s.worker, time.Second, stopCh) |
| 81 | + } |
| 82 | + <-stopCh |
| 83 | + glog.V(1).Infof("Shutting down") |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +func (s *SignatureImportController) worker() { |
| 88 | + for { |
| 89 | + if !s.work() { |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// work returns true if the worker thread should continue |
| 96 | +func (s *SignatureImportController) work() bool { |
| 97 | + key, quit := s.queue.Get() |
| 98 | + if quit { |
| 99 | + return false |
| 100 | + } |
| 101 | + defer s.queue.Done(key) |
| 102 | + |
| 103 | + err := s.syncImageSignatures(key.(string)) |
| 104 | + if err != nil { |
| 105 | + utilruntime.HandleError(fmt.Errorf("error syncing image %s, it will be retried: %v", key.(string), err)) |
| 106 | + s.queue.AddRateLimited(key) |
| 107 | + return true |
| 108 | + } |
| 109 | + |
| 110 | + s.queue.Forget(key) |
| 111 | + return true |
| 112 | +} |
| 113 | + |
| 114 | +func (s *SignatureImportController) enqueueImage(obj interface{}) { |
| 115 | + _, ok := obj.(*imageapi.Image) |
| 116 | + if !ok { |
| 117 | + return |
| 118 | + } |
| 119 | + key, err := controller.KeyFunc(obj) |
| 120 | + if err != nil { |
| 121 | + glog.Errorf("Couldn't get key for object %+v: %v", obj, err) |
| 122 | + return |
| 123 | + } |
| 124 | + s.queue.Add(key) |
| 125 | +} |
| 126 | + |
| 127 | +func (s *SignatureImportController) syncImageSignatures(key string) error { |
| 128 | + glog.V(4).Infof("Initiating download of signatures for %s", key) |
| 129 | + image, err := s.imageLister.Get(key) |
| 130 | + if err != nil { |
| 131 | + glog.V(4).Infof("Unable to get image %v: %v", key, err) |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + currentSignatures, err := s.fetchImageSignatures(image) |
| 136 | + if err != nil { |
| 137 | + glog.V(4).Infof("Failed to fetch image %s signatures: %v", image.Name, err) |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + // Having no signatures means no-op (we don't remove stored signatures when |
| 142 | + // the sig-store no longer have them). |
| 143 | + if len(currentSignatures) == 0 { |
| 144 | + glog.V(4).Infof("No signatures dowloaded for %s", image.Name) |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + t, err := kapi.Scheme.DeepCopy(image) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + newImage := t.(*imageapi.Image) |
| 153 | + |
| 154 | + shouldUpdate := false |
| 155 | + |
| 156 | + // Only add new signatures, do not override existing stored signatures as that |
| 157 | + // can void their verification status. |
| 158 | + for _, c := range currentSignatures { |
| 159 | + found := false |
| 160 | + for _, s := range newImage.Signatures { |
| 161 | + if s.Name == c.Name { |
| 162 | + found = true |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + if !found { |
| 167 | + newImage.Signatures = append(newImage.Signatures, c) |
| 168 | + shouldUpdate = true |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Avoid unnecessary updates to images. |
| 173 | + if !shouldUpdate { |
| 174 | + return nil |
| 175 | + } |
| 176 | + glog.V(4).Infof("Image %s now has %d signatures", newImage.Name, len(newImage.Signatures)) |
| 177 | + |
| 178 | + if _, err := s.imageClient.Image().Images().Update(newImage); err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func (s *SignatureImportController) fetchImageSignatures(image *imageapi.Image) ([]imageapi.ImageSignature, error) { |
| 186 | + reference, err := docker.ParseReference("//" + image.DockerImageReference) |
| 187 | + if err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + source, err := reference.NewImageSource(nil, nil) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + defer source.Close() |
| 195 | + |
| 196 | + ctx, cancel := context.WithTimeout(s.ctx, s.fetchTimeout) |
| 197 | + defer cancel() |
| 198 | + |
| 199 | + signatures, err := source.GetSignatures(ctx) |
| 200 | + if err != nil { |
| 201 | + return nil, err |
| 202 | + } |
| 203 | + |
| 204 | + ret := []imageapi.ImageSignature{} |
| 205 | + for _, blob := range signatures { |
| 206 | + sig := imageapi.ImageSignature{Type: imageapi.ImageSignatureTypeAtomicImageV1} |
| 207 | + // This will use the name of the image (sha256:xxxx) and the SHA256 of the |
| 208 | + // signature itself as the signature name has to be unique for each |
| 209 | + // signature. |
| 210 | + sig.Name = image.Name + "@" + fmt.Sprintf("%x", sha256.Sum256(blob)) |
| 211 | + sig.Content = blob |
| 212 | + sig.Annotations = map[string]string{ |
| 213 | + SignatureManagedAnnotation: "true", |
| 214 | + } |
| 215 | + sig.CreationTimestamp = metav1.Now() |
| 216 | + ret = append(ret, sig) |
| 217 | + } |
| 218 | + return ret, nil |
| 219 | +} |
0 commit comments