From d1cfabf793bc669d62494e6a18cc1e482ef75f6b Mon Sep 17 00:00:00 2001 From: LWJ Date: Mon, 29 Mar 2021 18:15:57 +0100 Subject: [PATCH] Fix nil pointer dereference and minor refactor Signed-off-by: LWJ --- api/v1alpha1/imageupdateautomation_types.go | 2 +- api/v1alpha1/zz_generated.deepcopy.go | 9 +--- .../imageupdateautomation_controller.go | 4 +- controllers/update_test.go | 47 ++++++++++--------- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/api/v1alpha1/imageupdateautomation_types.go b/api/v1alpha1/imageupdateautomation_types.go index b54aae9..4e33d22 100644 --- a/api/v1alpha1/imageupdateautomation_types.go +++ b/api/v1alpha1/imageupdateautomation_types.go @@ -152,7 +152,7 @@ type SigningKey struct { // keypair as the value. It must be in the same namespace as the // ImageUpdateAutomation. // +required - SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"` + SecretRef meta.LocalObjectReference `json:"secretRef,omitempty"` } const ( diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 0382958..5e4d4bd 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -21,7 +21,6 @@ limitations under the License. package v1alpha1 import ( - "github.com/fluxcd/pkg/apis/meta" "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -32,7 +31,7 @@ func (in *CommitSpec) DeepCopyInto(out *CommitSpec) { if in.SigningKey != nil { in, out := &in.SigningKey, &out.SigningKey *out = new(SigningKey) - (*in).DeepCopyInto(*out) + **out = **in } } @@ -198,11 +197,7 @@ func (in *PushSpec) DeepCopy() *PushSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SigningKey) DeepCopyInto(out *SigningKey) { *out = *in - if in.SecretRef != nil { - in, out := &in.SecretRef, &out.SecretRef - *out = new(meta.LocalObjectReference) - **out = **in - } + out.SecretRef = in.SecretRef } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SigningKey. diff --git a/controllers/imageupdateautomation_controller.go b/controllers/imageupdateautomation_controller.go index c280f99..d18e4b7 100644 --- a/controllers/imageupdateautomation_controller.go +++ b/controllers/imageupdateautomation_controller.go @@ -72,6 +72,8 @@ const defaultMessageTemplate = `Update from image update automation` const repoRefKey = ".spec.gitRepository" const imagePolicyKey = ".spec.update.imagePolicy" +const signingSecretKey = "git.asc" + // TemplateData is the type of the value given to the commit message // template. type TemplateData struct { @@ -502,7 +504,7 @@ func (r *ImageUpdateAutomationReconciler) getSigningEntity(ctx context.Context, } // get data from secret - data, ok := secret.Data["git.asc"] + data, ok := secret.Data[signingSecretKey] if !ok { return nil, fmt.Errorf("signing key secret '%s' does not contain a 'git.asc' key", secretName) } diff --git a/controllers/update_test.go b/controllers/update_test.go index 7b7cd2b..c69b50c 100644 --- a/controllers/update_test.go +++ b/controllers/update_test.go @@ -463,6 +463,27 @@ Images: // made by automation. waitForNewHead(localRepo, branch) + // configure OpenPGP armor encoder + b := bytes.NewBuffer(nil) + w, err := armor.Encode(b, openpgp.PrivateKeyType, nil) + Expect(err).ToNot(HaveOccurred()) + + // serialize private key + err = pgpEntity.SerializePrivate(w, nil) + Expect(err).ToNot(HaveOccurred()) + err = w.Close() + Expect(err).ToNot(HaveOccurred()) + + // create the secret containing signing key + sec := &corev1.Secret{ + Data: map[string][]byte{ + "git.asc": b.Bytes(), + }, + } + sec.Name = "signing-key-secret-" + randStringRunes(5) + sec.Namespace = namespace.Name + Expect(k8sClient.Create(context.Background(), sec)).To(Succeed()) + // now create the automation object, and let it (one // hopes!) make a commit itself. updateKey := types.NamespacedName{ @@ -486,33 +507,13 @@ Images: Strategy: imagev1.UpdateStrategySetters, }, Commit: imagev1.CommitSpec{ - SigningKey: &imagev1.SigningKey{}, + SigningKey: &imagev1.SigningKey{ + SecretRef: meta.LocalObjectReference{Name: sec.Name}, + }, }, }, } - // configure OpenPGP armor encoder - b := bytes.NewBuffer(nil) - w, err := armor.Encode(b, openpgp.PrivateKeyType, nil) - Expect(err).ToNot(HaveOccurred()) - - // serialize private key - err = pgpEntity.SerializePrivate(w, nil) - Expect(err).ToNot(HaveOccurred()) - err = w.Close() - Expect(err).ToNot(HaveOccurred()) - - // create the secret containing signing key - sec := &corev1.Secret{ - Data: map[string][]byte{ - "git.asc": b.Bytes(), - }, - } - sec.Name = "signing-key-secret-" + randStringRunes(5) - sec.Namespace = namespace.Name - Expect(k8sClient.Create(context.Background(), sec)).To(Succeed()) - updateBySetters.Spec.Commit.SigningKey.SecretRef = &meta.LocalObjectReference{Name: sec.Name} - Expect(k8sClient.Create(context.Background(), updateBySetters)).To(Succeed()) // wait for a new commit to be made by the controller waitForNewHead(localRepo, branch)