aboutsummaryrefslogtreecommitdiffstats
path: root/certServiceK8sExternalProvider/src/cmpv2controller/cmpv2_issuer_controller.go
blob: 9bc41e7fd561b0f97fb8f3a3ccdcbd3736af4846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * ============LICENSE_START=======================================================
 * oom-certservice-k8s-external-provider
 * ================================================================================
 * Copyright (c) 2019 Smallstep Labs, Inc.
 * Modifications copyright (C) 2020 Nokia. All rights reserved.
 * ================================================================================
 * This source code was copied from the following git repository:
 * https://github.com/smallstep/step-issuer
 * The source code was modified for usage in the ONAP project.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package cmpv2controller

import (
	"context"
	"fmt"

	"github.com/go-logr/logr"
	core "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/tools/record"
	"k8s.io/utils/clock"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"

	"onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
	provisioners "onap.org/oom-certservice/k8s-external-provider/src/cmpv2provisioner"
)

// CMPv2IssuerController reconciles a CMPv2Issuer object
type CMPv2IssuerController struct {
	client.Client
	Log      logr.Logger
	Clock    clock.Clock
	Recorder record.EventRecorder
	ProvisionerFactory provisioners.ProvisionerFactory
}

// Reconcile will read and validate the CMPv2Issuer resources, it will set the
// status condition ready to true if everything is right.
func (controller *CMPv2IssuerController) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	ctx := context.Background()
	log := controller.Log.WithValues("cmpv2-issuer-controller", req.NamespacedName)

	// 1. Load CMPv2Issuer
	issuer := new(cmpv2api.CMPv2Issuer)
	if err := controller.loadResource(ctx, req.NamespacedName, issuer); err != nil {
		handleErrorLoadingCMPv2Issuer(log, err)
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}
	log.Info("CMPv2Issuer loaded: ", "issuer", issuer)

	// 2. Validate CMPv2Issuer
	statusUpdater := newStatusUpdater(controller, issuer, log)
	if err := validateCMPv2IssuerSpec(issuer.Spec, log); err != nil {
		handleErrorCMPv2IssuerValidation(ctx, log, err, statusUpdater)
		return ctrl.Result{}, err
	}

	// 3. Load keystore and truststore information form k8s secret
	var secret core.Secret
	secretNamespaceName := types.NamespacedName{
		Namespace: req.Namespace,
		Name:      issuer.Spec.CertSecretRef.Name,
	}
	if err := controller.loadResource(ctx, secretNamespaceName, &secret); err != nil {
		handleErrorInvalidSecret(ctx, log, err, statusUpdater, secretNamespaceName)
		return ctrl.Result{}, err
	}

	// 4. Create CMPv2 provisioner
	provisioner, err := controller.ProvisionerFactory.CreateProvisioner(issuer, secret)
	if err != nil {
		log.Error(err, "failed to initialize provisioner")
		statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, "Error", "Failed to initialize provisioner: %v", err)
		handleErrorProvisionerInitialization(ctx, log, err, statusUpdater)
		return ctrl.Result{}, err
	}

	// 5. Check health of the provisioner and store the instance for further use
	if err := provisioner.CheckHealth(); err != nil {
		return ctrl.Result{}, err
	}
	provisioners.Store(req.NamespacedName, provisioner)

	// 6. Update the status of CMPv2Issuer to 'Validated'
	if err := updateCMPv2IssuerStatusToVerified(statusUpdater, ctx, log); err != nil {
		handleErrorUpdatingCMPv2IssuerStatus(log, err)
		return ctrl.Result{}, err
	}

	return ctrl.Result{}, nil
}

func (controller *CMPv2IssuerController) SetupWithManager(manager ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(manager).
		For(&cmpv2api.CMPv2Issuer{}).
		Complete(controller)
}

func (controller *CMPv2IssuerController) loadResource(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
	return controller.Client.Get(ctx, key, obj)
}

func validateCMPv2IssuerSpec(issuerSpec cmpv2api.CMPv2IssuerSpec, log logr.Logger) error {
	switch {
	case issuerSpec.URL == "":
		return fmt.Errorf("spec.url cannot be empty")
	case issuerSpec.CaName == "":
		return fmt.Errorf("spec.caName cannot be empty")
	case issuerSpec.CertSecretRef.Name == "":
		return fmt.Errorf("spec.certSecretRef.name cannot be empty")
	case issuerSpec.CertSecretRef.KeyRef == "":
		return fmt.Errorf("spec.certSecretRef.keyRef cannot be empty")
	case issuerSpec.CertSecretRef.CertRef == "":
		return fmt.Errorf("spec.certSecretRef.certRef cannot be empty")
	case issuerSpec.CertSecretRef.CacertRef == "":
		return fmt.Errorf("spec.certSecretRef.cacertRef cannot be empty")
	default:
		return nil
	}
}

func updateCMPv2IssuerStatusToVerified(statusUpdater *CMPv2IssuerStatusUpdater, ctx context.Context, log logr.Logger) error {
	log.Info("CMPv2 provisioner created -> updating status to of CMPv2Issuer resource to: Verified")
	return statusUpdater.Update(ctx, cmpv2api.ConditionTrue, Verified, "CMPv2Issuer verified and ready to sign certificates")
}

// Error handling

func handleErrorUpdatingCMPv2IssuerStatus(log logr.Logger, err error) {
	log.Error(err, "Failed to update CMPv2Issuer status")
}

func handleErrorLoadingCMPv2Issuer(log logr.Logger, err error) {
	log.Error(err, "Failed to retrieve CMPv2Issuer resource")
}

func handleErrorProvisionerInitialization(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater) {
	log.Error(err, "Failed to initialize provisioner")
	statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, Error, "Failed to initialize provisioner: %v", err)
}

func handleErrorCMPv2IssuerValidation(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater) {
	log.Error(err, "Failed to validate CMPv2Issuer resource")
	statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, ValidationFailed, "Failed to validate resource: %v", err)
}

func handleErrorInvalidSecret(ctx context.Context, log logr.Logger, err error, statusUpdater *CMPv2IssuerStatusUpdater, secretNamespaceName types.NamespacedName) {
	log.Error(err, "Failed to retrieve CMPv2Issuer provisioner secret", "namespace", secretNamespaceName.Namespace, "name", secretNamespaceName.Name)
	if apierrors.IsNotFound(err) {
		statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, NotFound, "Failed to retrieve provisioner secret: %v", err)
	} else {
		statusUpdater.UpdateNoError(ctx, cmpv2api.ConditionFalse, Error, "Failed to retrieve provisioner secret: %v", err)
	}
}