2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (c) 2019 Smallstep Labs, Inc.
6 * Modifications copyright (C) 2020 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
37 certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
38 "k8s.io/apimachinery/pkg/types"
39 ctrl "sigs.k8s.io/controller-runtime"
41 "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
44 var collection = new(sync.Map)
46 type CertServiceCA struct {
55 func New(cmpv2Issuer *cmpv2api.CMPv2Issuer, key []byte, cert []byte, cacert []byte) (*CertServiceCA, error) {
58 ca.name = cmpv2Issuer.Name
59 ca.url = cmpv2Issuer.Spec.URL
60 ca.caName = cmpv2Issuer.Spec.CaName
65 log := ctrl.Log.WithName("cmpv2-provisioner")
66 log.Info("Configuring CA: ", "name", ca.name, "url", ca.url, "caName", ca.caName, "key", ca.key,
67 "cert", ca.cert, "cacert", ca.cacert)
72 func Load(namespacedName types.NamespacedName) (*CertServiceCA, bool) {
73 provisioner, ok := collection.Load(namespacedName)
77 certServiceCAprovisioner, ok := provisioner.(*CertServiceCA)
78 return certServiceCAprovisioner, ok
81 func Store(namespacedName types.NamespacedName, provisioner *CertServiceCA) {
82 collection.Store(namespacedName, provisioner)
85 func (ca *CertServiceCA) Sign(ctx context.Context, certificateRequest *certmanager.CertificateRequest) ([]byte, []byte, error) {
86 log := ctrl.Log.WithName("certservice-provisioner")
87 log.Info("Signing certificate: ", "cert-name", certificateRequest.Name)
89 key, _ := base64.RawStdEncoding.DecodeString(string(ca.key))
90 log.Info("CA: ", "name", ca.name, "url", ca.url, "key", key)
92 crPEM := certificateRequest.Spec.Request
94 log.Info("Csr PEM: ", "bytes", csrBase64)
96 csr, err := decodeCSR(crPEM)
101 cert := x509.Certificate{}
105 // write here code which will call CertServiceCA and sign CSR
108 encodedPEM, err := encodeX509(&cert)
113 signedPEM := encodedPEM
114 trustedCA := encodedPEM
116 log.Info("Successfully signed: ", "cert-name", certificateRequest.Name)
117 log.Info("Signed cert PEM: ", "bytes", signedPEM)
118 log.Info("Trusted CA PEM: ", "bytes", trustedCA)
120 return signedPEM, trustedCA, nil
123 // TODO JM utility methods - will be used in "real" implementation
125 // decodeCSR decodes a certificate request in PEM format and returns the
126 func decodeCSR(data []byte) (*x509.CertificateRequest, error) {
127 block, rest := pem.Decode(data)
128 if block == nil || len(rest) > 0 {
129 return nil, fmt.Errorf("unexpected CSR PEM on sign request")
131 if block.Type != "CERTIFICATE REQUEST" {
132 return nil, fmt.Errorf("PEM is not a certificate request")
134 csr, err := x509.ParseCertificateRequest(block.Bytes)
136 return nil, fmt.Errorf("error parsing certificate request: %v", err)
138 if err := csr.CheckSignature(); err != nil {
139 return nil, fmt.Errorf("error checking certificate request signature: %v", err)
144 // encodeX509 will encode a *x509.Certificate into PEM format.
145 func encodeX509(cert *x509.Certificate) ([]byte, error) {
146 caPem := bytes.NewBuffer([]byte{})
147 err := pem.Encode(caPem, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
151 return caPem.Bytes(), nil
154 // generateSubject returns the first SAN that is not 127.0.0.1 or localhost. The
155 // CSRs generated by the Certificate resource have always those SANs. If no SANs
156 // are available `certservice-issuer-certificate` will be used as a subject is always
158 func generateSubject(sans []string) string {
160 return "certservice-issuer-certificate"
162 for _, s := range sans {
163 if s != "127.0.0.1" && s != "localhost" {
170 func decode(cert string) []byte {
171 bytes, _ := base64.RawStdEncoding.DecodeString(cert)