Skip to content

Commit 27c4671

Browse files
Merge pull request #279 from smarterclayton/config_map
Refactor the verify package to not have dependencies on CVO
2 parents b89aa75 + 1d13e88 commit 27c4671

File tree

200 files changed

+15547
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+15547
-340
lines changed

Gopkg.lock

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cvo/cvo.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212

1313
"github.com/blang/semver"
1414
"github.com/google/uuid"
15+
"github.com/pkg/errors"
1516
corev1 "k8s.io/api/core/v1"
1617
apierrors "k8s.io/apimachinery/pkg/api/errors"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1820
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1921
"k8s.io/apimachinery/pkg/util/wait"
2022
informerscorev1 "k8s.io/client-go/informers/core/v1"
@@ -44,6 +46,7 @@ import (
4446
"github.com/openshift/cluster-version-operator/pkg/payload/precondition"
4547
preconditioncv "github.com/openshift/cluster-version-operator/pkg/payload/precondition/clusterversion"
4648
"github.com/openshift/cluster-version-operator/pkg/verify"
49+
"github.com/openshift/cluster-version-operator/pkg/verify/verifyconfigmap"
4750
)
4851

4952
const (
@@ -134,6 +137,9 @@ type Operator struct {
134137
// verifier, if provided, will be used to check an update before it is executed.
135138
// Any error will prevent an update payload from being accessed.
136139
verifier verify.Interface
140+
// signatureStore, if set, will be used to periodically persist signatures to
141+
// the cluster as a config map
142+
signatureStore *verify.StorePersister
137143

138144
configSync ConfigSyncWorker
139145
// statusInterval is how often the configSync worker is allowed to retrigger
@@ -236,17 +242,21 @@ func (optr *Operator) InitializeFromPayload(restConfig *rest.Config, burstRestCo
236242
}
237243
// XXX: set this to the cincinnati version in preference
238244
if _, err := semver.Parse(update.ImageRef.Name); err != nil {
239-
return fmt.Errorf("The local release contents name %q is not a valid semantic version - no current version will be reported: %v", update.ImageRef.Name, err)
245+
return fmt.Errorf("the local release contents name %q is not a valid semantic version - no current version will be reported: %v", update.ImageRef.Name, err)
240246
}
241247

242248
optr.releaseCreated = update.ImageRef.CreationTimestamp.Time
243249
optr.releaseVersion = update.ImageRef.Name
244250

245251
// Wraps operator's HTTPClient method to allow releaseVerifier to create http client with up-to-date config.
246252
clientBuilder := &verifyClientBuilder{builder: optr.HTTPClient}
253+
configClient, err := coreclientsetv1.NewForConfig(restConfig)
254+
if err != nil {
255+
return fmt.Errorf("unable to create a configuration client: %v", err)
256+
}
247257

248258
// attempt to load a verifier as defined in the payload
249-
verifier, err := verify.LoadFromPayload(update, clientBuilder)
259+
verifier, signatureStore, err := loadConfigMapVerifierDataFromUpdate(update, clientBuilder, configClient)
250260
if err != nil {
251261
return err
252262
}
@@ -257,6 +267,7 @@ func (optr *Operator) InitializeFromPayload(restConfig *rest.Config, burstRestCo
257267
verifier = verify.Reject
258268
}
259269
optr.verifier = verifier
270+
optr.signatureStore = signatureStore
260271

261272
// after the verifier has been loaded, initialize the sync worker with a payload retriever
262273
// which will consume the verifier
@@ -276,6 +287,39 @@ func (optr *Operator) InitializeFromPayload(restConfig *rest.Config, burstRestCo
276287
return nil
277288
}
278289

290+
// loadConfigMapVerifierDataFromUpdate fetches the first config map in the payload with the correct annotation.
291+
// It returns an error if the data is not valid, or no verifier if no config map is found. See the verify
292+
// package for more details on the algorithm for verification. If the annotation is set, a verifier or error
293+
// is always returned.
294+
func loadConfigMapVerifierDataFromUpdate(update *payload.Update, clientBuilder verify.ClientBuilder, configMapClient coreclientsetv1.ConfigMapsGetter) (verify.Interface, *verify.StorePersister, error) {
295+
configMapGVK := corev1.SchemeGroupVersion.WithKind("ConfigMap")
296+
for _, manifest := range update.Manifests {
297+
if manifest.GVK != configMapGVK {
298+
continue
299+
}
300+
if _, ok := manifest.Obj.GetAnnotations()[verify.ReleaseAnnotationConfigMapVerifier]; !ok {
301+
continue
302+
}
303+
src := fmt.Sprintf("the config map %s/%s", manifest.Obj.GetNamespace(), manifest.Obj.GetName())
304+
data, _, err := unstructured.NestedStringMap(manifest.Obj.Object, "data")
305+
if err != nil {
306+
return nil, nil, errors.Wrapf(err, "%s is not valid: %v", src, err)
307+
}
308+
verifier, err := verify.NewFromConfigMapData(src, data, clientBuilder)
309+
if err != nil {
310+
return nil, nil, err
311+
}
312+
313+
// allow the verifier to consult the cluster for signature data, and also configure
314+
// a process that writes signatures back to that store
315+
signatureStore := verifyconfigmap.NewStore(configMapClient, nil)
316+
verifier = verifier.WithStores(signatureStore)
317+
persister := verify.NewSignatureStorePersister(signatureStore, verifier)
318+
return verifier, persister, nil
319+
}
320+
return nil, nil, nil
321+
}
322+
279323
// Run runs the cluster version operator until stopCh is completed. Workers is ignored for now.
280324
func (optr *Operator) Run(ctx context.Context, workers int) {
281325
defer utilruntime.HandleCrash()
@@ -310,6 +354,9 @@ func (optr *Operator) Run(ctx context.Context, workers int) {
310354
utilruntime.HandleError(fmt.Errorf("unable to perform final sync: %v", err))
311355
}
312356
}, time.Second, stopCh)
357+
if optr.signatureStore != nil {
358+
go optr.signatureStore.Run(ctx, optr.minimumUpdateCheckInterval*2)
359+
}
313360

314361
<-stopCh
315362

0 commit comments

Comments
 (0)