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
15 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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"
40 revisionAnnotation = "cert-manager.io/certificate-revision"
41 certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
42 oldCertificateSecretKey = "tls.crt"
43 oldPrivateKeySecretKey = "tls.key"
46 func CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk(
47 k8sClient client.Client,
48 certificateRequest *cmapi.CertificateRequest,
50 ) (bool, string, string) {
51 if !IsUpdateCertificateRevision(certificateRequest) {
54 certificate, privateKey := RetrieveOldCertificateAndPk(k8sClient, certificateRequest, ctx)
55 areCertAndPkPresent := certificate != "" && privateKey != ""
56 return areCertAndPkPresent, certificate, privateKey
59 func IsUpdateCertificateRevision(certificateRequest *cmapi.CertificateRequest) bool {
60 revision, err := strconv.Atoi(certificateRequest.ObjectMeta.Annotations[revisionAnnotation])
67 func RetrieveOldCertificateAndPk(
68 k8sClient client.Client,
69 certificateRequest *cmapi.CertificateRequest,
72 certificateConfigString := certificateRequest.ObjectMeta.Annotations[certificateConfigurationAnnotation]
73 var certificateConfig cmapi.Certificate
74 if err := json.Unmarshal([]byte(certificateConfigString), &certificateConfig); err != nil {
77 oldCertificateSecretName := certificateConfig.Spec.SecretName
78 oldCertificateSecretNamespacedName := types.NamespacedName{
79 Namespace: certificateConfig.Namespace,
80 Name: oldCertificateSecretName,
82 var oldCertificateSecret core.Secret
83 if err := k8sClient.Get(ctx, oldCertificateSecretNamespacedName, &oldCertificateSecret); err != nil {
86 oldCertificateString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldCertificateSecretKey])
87 oldPrivateKeyString := base64.StdEncoding.EncodeToString(oldCertificateSecret.Data[oldPrivateKeySecretKey])
88 return oldCertificateString, oldPrivateKeyString