aboutsummaryrefslogtreecommitdiffstats
path: root/certServiceK8sExternalProvider/src/cmpv2controller/util/certificate_update_util.go
blob: 93746b821389c7392a36e4af8239d820445ca8d0 (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
/*
 * ============LICENSE_START=======================================================
 * oom-certservice-k8s-external-provider
 * ================================================================================
 * Copyright (C) 2021 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 util

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"strconv"

	cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
	core "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
	revisionAnnotation                 = "cert-manager.io/certificate-revision"
	certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
	oldCertificateSecretKey            = "tls.crt"
	oldPrivateKeySecretKey             = "tls.key"
)

func CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk(
	k8sClient client.Client,
	certificateRequest *cmapi.CertificateRequest,
	ctx context.Context,
) (bool, string, string) {
	if !IsUpdateCertificateRevision(certificateRequest) {
		return false, "", ""
	}
	certificate, privateKey := RetrieveOldCertificateAndPk(k8sClient, certificateRequest, ctx)
	areCertAndPkPresent := certificate != "" && privateKey != ""
	return areCertAndPkPresent, certificate, privateKey
}

func IsUpdateCertificateRevision(certificateRequest *cmapi.CertificateRequest) bool {
	revision, err := strconv.Atoi(certificateRequest.ObjectMeta.Annotations[revisionAnnotation])
	if err != nil {
		return false
	}
	return revision > 1
}

func RetrieveOldCertificateAndPk(
	k8sClient client.Client,
	certificateRequest *cmapi.CertificateRequest,
	ctx context.Context,
) (string, string) {
	certificateConfigString := certificateRequest.ObjectMeta.Annotations[certificateConfigurationAnnotation]
	var certificateConfig cmapi.Certificate
	if err := json.Unmarshal([]byte(certificateConfigString), &certificateConfig); err != nil {
		return "", ""
	}
	oldCertificateSecretName := certificateConfig.Spec.SecretName
	oldCertificateSecretNamespacedName := types.NamespacedName{
		Namespace: certificateConfig.Namespace,
		Name:      oldCertificateSecretName,
	}
	var oldCertificateSecret core.Secret
	if err := k8sClient.Get(ctx, oldCertificateSecretNamespacedName, &oldCertificateSecret); err != nil {
		return "", ""
	}
	oldCertificateString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldCertificateSecretKey])
	oldPrivateKeyString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldPrivateKeySecretKey])
	return oldCertificateString, oldPrivateKeyString
}