[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / model / sign_certificate_model_factory.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package model
22
23 import (
24         "context"
25
26         "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
27         "sigs.k8s.io/controller-runtime/pkg/client"
28
29         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2controller/util"
30         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2provisioner/csr"
31         "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
32 )
33
34 func CreateSignCertificateModel(client client.Client, certificateRequest *v1.CertificateRequest, ctx context.Context, privateKeyBytes []byte) (SignCertificateModel, error) {
35         log := leveledlogger.GetLoggerWithName("certservice-certificate-model")
36         oldCertificateBytes, oldPrivateKeyBytes := util.RetrieveOldCertificateAndPkForCertificateUpdate(
37                 client, certificateRequest, ctx)
38
39         csrBytes := certificateRequest.Spec.Request
40         log.Debug("Original CSR PEM: ", "bytes", csrBytes)
41
42         filteredCsrBytes, err := csr.FilterFieldsFromCSR(csrBytes, privateKeyBytes)
43         if err != nil {
44                 return SignCertificateModel{}, err
45         }
46         log.Debug("Filtered out CSR PEM: ", "bytes", filteredCsrBytes)
47
48         signCertificateModel := SignCertificateModel{
49                 CertificateRequest:  certificateRequest,
50                 FilteredCsr:         filteredCsrBytes,
51                 PrivateKeyBytes:     privateKeyBytes,
52                 OldCertificateBytes: oldCertificateBytes,
53                 OldPrivateKeyBytes:  oldPrivateKeyBytes,
54         }
55         return signCertificateModel, nil
56 }