2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (c) 2019 Smallstep Labs, Inc.
6 * Copyright (C) 2020-2021 Nokia. All rights reserved.
7 * ================================================================================
8 * This source code was copied from the following git repository:
9 * https://github.com/smallstep/step-issuer
10 * The source code was modified for usage in the ONAP project.
11 * ================================================================================
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package cmpv2provisioner
32 "k8s.io/apimachinery/pkg/types"
34 "onap.org/oom-certservice/k8s-external-provider/src/certserviceclient"
35 "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
36 "onap.org/oom-certservice/k8s-external-provider/src/cmpv2provisioner/csr"
37 "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
38 "onap.org/oom-certservice/k8s-external-provider/src/model"
41 var collection = new(sync.Map)
43 type CertServiceCA struct {
49 certServiceClient certserviceclient.CertServiceClient
52 func New(cmpv2Issuer *cmpv2api.CMPv2Issuer, certServiceClient certserviceclient.CertServiceClient) (*CertServiceCA, error) {
55 ca.name = cmpv2Issuer.Name
56 ca.url = cmpv2Issuer.Spec.URL
57 ca.caName = cmpv2Issuer.Spec.CaName
58 ca.healthEndpoint = cmpv2Issuer.Spec.HealthEndpoint
59 ca.certEndpoint = cmpv2Issuer.Spec.CertEndpoint
60 ca.certServiceClient = certServiceClient
62 log := leveledlogger.GetLoggerWithName("cmpv2-provisioner")
63 log.Info("Configuring CA: ", "name", ca.name, "url", ca.url, "caName", ca.caName, "healthEndpoint", ca.healthEndpoint, "certEndpoint", ca.certEndpoint)
68 func (ca *CertServiceCA) CheckHealth() error {
69 log := leveledlogger.GetLoggerWithName("cmpv2-provisioner")
70 log.Info("Checking health of CMPv2 issuer: ", "name", ca.name)
71 return ca.certServiceClient.CheckHealth()
74 func Load(namespacedName types.NamespacedName) (*CertServiceCA, bool) {
75 provisioner, ok := collection.Load(namespacedName)
79 certServiceCAprovisioner, ok := provisioner.(*CertServiceCA)
80 return certServiceCAprovisioner, ok
83 func Store(namespacedName types.NamespacedName, provisioner *CertServiceCA) {
84 collection.Store(namespacedName, provisioner)
87 func (ca *CertServiceCA) Sign(
89 signCertificateModel model.SignCertificateModel,
90 ) (signedCertificateChain []byte, trustedCertificates []byte, err error) {
91 log := leveledlogger.GetLoggerWithName("certservice-provisioner")
93 if signCertificateModel.IsUpdateRevision {
94 log.Debug("Certificate will be updated.", "old-certificate", signCertificateModel.OldCertificate,
95 "old-private-key", signCertificateModel.OldPrivateKey)
98 certificateRequest := signCertificateModel.CertificateRequest
99 privateKeyBytes := signCertificateModel.PrivateKeyBytes
100 log.Info("Signing certificate: ", "cert-name", certificateRequest.Name)
102 log.Info("CA: ", "name", ca.name, "url", ca.url)
104 csrBytes := certificateRequest.Spec.Request
105 log.Debug("Original CSR PEM: ", "bytes", csrBytes)
107 filteredCsrBytes, err := csr.FilterFieldsFromCSR(csrBytes, privateKeyBytes)
111 log.Debug("Filtered out CSR PEM: ", "bytes", filteredCsrBytes)
113 var response *certserviceclient.CertificatesResponse
116 if signCertificateModel.IsUpdateRevision {
117 log.Info("Attempt to send certificate update request")
118 response, errAPI = ca.certServiceClient.UpdateCertificate(filteredCsrBytes, privateKeyBytes, signCertificateModel)
120 log.Info("Attempt to send certificate request")
121 response, errAPI = ca.certServiceClient.GetCertificates(filteredCsrBytes, privateKeyBytes)
125 return nil, nil, errAPI
127 log.Info("Successfully received response from CertService API")
128 log.Debug("Certificate Chain", "cert-chain", response.CertificateChain)
129 log.Debug("Trusted Certificates", "trust-certs", response.TrustedCertificates)
131 log.Info("Start parsing response")
132 signedCertificateChain, trustedCertificates, signErr := parseResponseToBytes(response)
135 log.Error(signErr, "Cannot parse response from CertService API")
136 return nil, nil, signErr
139 log.Info("Successfully signed: ", "cert-name", certificateRequest.Name)
141 log.Debug("Signed cert PEM: ", "bytes", signedCertificateChain)
142 log.Debug("Trusted CA PEM: ", "bytes", trustedCertificates)
144 return signedCertificateChain, trustedCertificates, nil