93746b821389c7392a36e4af8239d820445ca8d0
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / util / certificate_update_util.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * ================================================================================
7  * This source code was copied from the following git repository:
8  * https://github.com/smallstep/step-issuer
9  * The source code was modified for usage in the ONAP project.
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package util
26
27 import (
28         "context"
29         "encoding/base64"
30         "encoding/json"
31         "strconv"
32
33         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
34         core "k8s.io/api/core/v1"
35         "k8s.io/apimachinery/pkg/types"
36         "sigs.k8s.io/controller-runtime/pkg/client"
37 )
38
39 const (
40         revisionAnnotation                 = "cert-manager.io/certificate-revision"
41         certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
42         oldCertificateSecretKey            = "tls.crt"
43         oldPrivateKeySecretKey             = "tls.key"
44 )
45
46 func CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk(
47         k8sClient client.Client,
48         certificateRequest *cmapi.CertificateRequest,
49         ctx context.Context,
50 ) (bool, string, string) {
51         if !IsUpdateCertificateRevision(certificateRequest) {
52                 return false, "", ""
53         }
54         certificate, privateKey := RetrieveOldCertificateAndPk(k8sClient, certificateRequest, ctx)
55         areCertAndPkPresent := certificate != "" && privateKey != ""
56         return areCertAndPkPresent, certificate, privateKey
57 }
58
59 func IsUpdateCertificateRevision(certificateRequest *cmapi.CertificateRequest) bool {
60         revision, err := strconv.Atoi(certificateRequest.ObjectMeta.Annotations[revisionAnnotation])
61         if err != nil {
62                 return false
63         }
64         return revision > 1
65 }
66
67 func RetrieveOldCertificateAndPk(
68         k8sClient client.Client,
69         certificateRequest *cmapi.CertificateRequest,
70         ctx context.Context,
71 ) (string, string) {
72         certificateConfigString := certificateRequest.ObjectMeta.Annotations[certificateConfigurationAnnotation]
73         var certificateConfig cmapi.Certificate
74         if err := json.Unmarshal([]byte(certificateConfigString), &certificateConfig); err != nil {
75                 return "", ""
76         }
77         oldCertificateSecretName := certificateConfig.Spec.SecretName
78         oldCertificateSecretNamespacedName := types.NamespacedName{
79                 Namespace: certificateConfig.Namespace,
80                 Name:      oldCertificateSecretName,
81         }
82         var oldCertificateSecret core.Secret
83         if err := k8sClient.Get(ctx, oldCertificateSecretNamespacedName, &oldCertificateSecret); err != nil {
84                 return "", ""
85         }
86         oldCertificateString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldCertificateSecretKey])
87         oldPrivateKeyString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldPrivateKeySecretKey])
88         return oldCertificateString, oldPrivateKeyString
89 }