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=========================================================
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"
39 revisionAnnotation = "cert-manager.io/certificate-revision"
40 certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
41 oldCertificateSecretKey = "tls.crt"
42 oldPrivateKeySecretKey = "tls.key"
45 func RetrieveOldCertificateAndPkForCertificateUpdate(
46 k8sClient client.Client,
47 certificateRequest *cmapi.CertificateRequest,
50 if !IsUpdateCertificateRevision(certificateRequest) {
51 return []byte{}, []byte{}
53 return RetrieveOldCertificateAndPk(k8sClient, certificateRequest, ctx)
56 func IsUpdateCertificateRevision(certificateRequest *cmapi.CertificateRequest) bool {
57 revision, err := strconv.Atoi(certificateRequest.ObjectMeta.Annotations[revisionAnnotation])
64 func RetrieveOldCertificateAndPk(
65 k8sClient client.Client,
66 certificateRequest *cmapi.CertificateRequest,
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{}
74 oldCertificateSecretName := certificateConfig.Spec.SecretName
75 oldCertificateSecretNamespacedName := types.NamespacedName{
76 Namespace: certificateConfig.Namespace,
77 Name: oldCertificateSecretName,
79 var oldCertificateSecret core.Secret
80 if err := k8sClient.Get(ctx, oldCertificateSecretNamespacedName, &oldCertificateSecret); err != nil {
81 return []byte{}, []byte{}
83 return oldCertificateSecret.Data[oldCertificateSecretKey], oldCertificateSecret.Data[oldPrivateKeySecretKey]