60610d32b26d293b47cb8b91ba7bae0a2d93d87c
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2provisioner / cmpv2_provisioner.go
1 /*
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
15  *
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  *
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=========================================================
24  */
25
26 package cmpv2provisioner
27
28 import (
29         "context"
30         "sync"
31
32         certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
33         "k8s.io/apimachinery/pkg/types"
34         ctrl "sigs.k8s.io/controller-runtime"
35
36         "onap.org/oom-certservice/k8s-external-provider/src/certserviceclient"
37         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
38 )
39
40 var collection = new(sync.Map)
41
42 type CertServiceCA struct {
43         name              string
44         url               string
45         healthEndpoint    string
46         certEndpoint      string
47         caName            string
48         certServiceClient certserviceclient.CertServiceClient
49 }
50
51 func New(cmpv2Issuer *cmpv2api.CMPv2Issuer, certServiceClient certserviceclient.CertServiceClient) (*CertServiceCA, error) {
52
53         ca := CertServiceCA{}
54         ca.name = cmpv2Issuer.Name
55         ca.url = cmpv2Issuer.Spec.URL
56         ca.caName = cmpv2Issuer.Spec.CaName
57         ca.healthEndpoint = cmpv2Issuer.Spec.HealthEndpoint
58         ca.certEndpoint = cmpv2Issuer.Spec.CertEndpoint
59         ca.certServiceClient = certServiceClient
60
61         log := ctrl.Log.WithName("cmpv2-provisioner")
62         log.Info("Configuring CA: ", "name", ca.name, "url", ca.url, "caName", ca.caName, "healthEndpoint", ca.healthEndpoint, "certEndpoint", ca.certEndpoint)
63
64         return &ca, nil
65 }
66
67 func (ca *CertServiceCA) CheckHealth() error {
68         log := ctrl.Log.WithName("cmpv2-provisioner")
69         log.Info("Checking health of CMPv2 issuer: ", "name", ca.name)
70         return ca.certServiceClient.CheckHealth()
71 }
72
73 func Load(namespacedName types.NamespacedName) (*CertServiceCA, bool) {
74         provisioner, ok := collection.Load(namespacedName)
75         if !ok {
76                 return nil, ok
77         }
78         certServiceCAprovisioner, ok := provisioner.(*CertServiceCA)
79         return certServiceCAprovisioner, ok
80 }
81
82 func Store(namespacedName types.NamespacedName, provisioner *CertServiceCA) {
83         collection.Store(namespacedName, provisioner)
84 }
85
86 func (ca *CertServiceCA) Sign(ctx context.Context, certificateRequest *certmanager.CertificateRequest, privateKeyBytes []byte) ([]byte, []byte, error) {
87         log := ctrl.Log.WithName("certservice-provisioner")
88         log.Info("Signing certificate: ", "cert-name", certificateRequest.Name)
89
90         log.Info("CA: ", "name", ca.name, "url", ca.url)
91
92         csrBytes := certificateRequest.Spec.Request
93         log.Info("Csr PEM: ", "bytes", csrBytes)
94
95         response, err := ca.certServiceClient.GetCertificates(csrBytes, privateKeyBytes)
96         if err != nil {
97                 return nil, nil, err
98         }
99         log.Info("Successfully received response from CertService API")
100         log.Info("Certificate Chain", "cert-chain", response.CertificateChain)
101         log.Info("Trusted Certificates", "trust-certs", response.TrustedCertificates)
102
103         log.Info("Start parsing response")
104         signedCertificateChain, trustedCertificates, signErr := parseResponseToBytes(response)
105
106         if signErr != nil {
107                 log.Error(signErr, "Cannot parse response from CertService API")
108                 return nil, nil, signErr
109         }
110
111         log.Info("Successfully signed: ", "cert-name", certificateRequest.Name)
112
113         //TODO Debug level or skip
114         log.Info("Signed cert PEM: ", "bytes", signedCertificateChain)
115         log.Info("Trusted CA  PEM: ", "bytes", trustedCertificates)
116
117         return signedCertificateChain, trustedCertificates, nil
118 }