[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
[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/json"
30         "strconv"
31
32         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
33         core "k8s.io/api/core/v1"
34         "k8s.io/apimachinery/pkg/types"
35         "sigs.k8s.io/controller-runtime/pkg/client"
36 )
37
38 const (
39         revisionAnnotation                 = "cert-manager.io/certificate-revision"
40         certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
41         oldCertificateSecretKey            = "tls.crt"
42         oldPrivateKeySecretKey             = "tls.key"
43 )
44
45 func RetrieveOldCertificateAndPkForCertificateUpdate(
46         k8sClient client.Client,
47         certificateRequest *cmapi.CertificateRequest,
48         ctx context.Context,
49 ) ([]byte, []byte) {
50         if !IsUpdateCertificateRevision(certificateRequest) {
51                 return []byte{}, []byte{}
52         }
53         return RetrieveOldCertificateAndPk(k8sClient, certificateRequest, ctx)
54 }
55
56 func IsUpdateCertificateRevision(certificateRequest *cmapi.CertificateRequest) bool {
57         revision, err := strconv.Atoi(certificateRequest.ObjectMeta.Annotations[revisionAnnotation])
58         if err != nil {
59                 return false
60         }
61         return revision > 1
62 }
63
64 func RetrieveOldCertificateAndPk(
65         k8sClient client.Client,
66         certificateRequest *cmapi.CertificateRequest,
67         ctx context.Context,
68 ) ([]byte, []byte) {
69         certificateConfigString := certificateRequest.ObjectMeta.Annotations[certificateConfigurationAnnotation]
70         var certificateConfig cmapi.Certificate
71         if err := json.Unmarshal([]byte(certificateConfigString), &certificateConfig); err != nil {
72                 return []byte{}, []byte{}
73         }
74         oldCertificateSecretName := certificateConfig.Spec.SecretName
75         oldCertificateSecretNamespacedName := types.NamespacedName{
76                 Namespace: certificateConfig.Namespace,
77                 Name:      oldCertificateSecretName,
78         }
79         var oldCertificateSecret core.Secret
80         if err := k8sClient.Get(ctx, oldCertificateSecretNamespacedName, &oldCertificateSecret); err != nil {
81                 return []byte{}, []byte{}
82         }
83         return oldCertificateSecret.Data[oldCertificateSecretKey], oldCertificateSecret.Data[oldPrivateKeySecretKey]
84 }