[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add logging of supported CSR properties
[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         "crypto/x509"
31         "sync"
32
33         certmanager "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
34         "k8s.io/apimachinery/pkg/types"
35         ctrl "sigs.k8s.io/controller-runtime"
36
37         "onap.org/oom-certservice/k8s-external-provider/src/certserviceclient"
38         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
39         x509utils "onap.org/oom-certservice/k8s-external-provider/src/x509"
40 )
41
42 var collection = new(sync.Map)
43
44 type CertServiceCA struct {
45         name              string
46         url               string
47         healthEndpoint    string
48         certEndpoint      string
49         caName            string
50         certServiceClient certserviceclient.CertServiceClient
51 }
52
53 func New(cmpv2Issuer *cmpv2api.CMPv2Issuer, certServiceClient certserviceclient.CertServiceClient) (*CertServiceCA, error) {
54
55         ca := CertServiceCA{}
56         ca.name = cmpv2Issuer.Name
57         ca.url = cmpv2Issuer.Spec.URL
58         ca.caName = cmpv2Issuer.Spec.CaName
59         ca.healthEndpoint = cmpv2Issuer.Spec.HealthEndpoint
60         ca.certEndpoint = cmpv2Issuer.Spec.CertEndpoint
61         ca.certServiceClient = certServiceClient
62
63         log := ctrl.Log.WithName("cmpv2-provisioner")
64         log.Info("Configuring CA: ", "name", ca.name, "url", ca.url, "caName", ca.caName, "healthEndpoint", ca.healthEndpoint, "certEndpoint", ca.certEndpoint)
65
66         return &ca, nil
67 }
68
69 func (ca *CertServiceCA) CheckHealth() error {
70         log := ctrl.Log.WithName("cmpv2-provisioner")
71         log.Info("Checking health of CMPv2 issuer: ", "name", ca.name)
72         return ca.certServiceClient.CheckHealth()
73 }
74
75 func Load(namespacedName types.NamespacedName) (*CertServiceCA, bool) {
76         provisioner, ok := collection.Load(namespacedName)
77         if !ok {
78                 return nil, ok
79         }
80         certServiceCAprovisioner, ok := provisioner.(*CertServiceCA)
81         return certServiceCAprovisioner, ok
82 }
83
84 func Store(namespacedName types.NamespacedName, provisioner *CertServiceCA) {
85         collection.Store(namespacedName, provisioner)
86 }
87
88 func (ca *CertServiceCA) Sign(ctx context.Context, certificateRequest *certmanager.CertificateRequest, privateKeyBytes []byte) ([]byte, []byte, error) {
89         log := ctrl.Log.WithName("certservice-provisioner")
90         log.Info("Signing certificate: ", "cert-name", certificateRequest.Name)
91
92         log.Info("CA: ", "name", ca.name, "url", ca.url)
93
94         csrBytes := certificateRequest.Spec.Request
95         log.Info("Csr PEM: ", "bytes", csrBytes)
96
97         csr, err := x509utils.DecodeCSR(csrBytes)
98         if err != nil {
99                 return nil, nil, err
100         }
101
102         response, err := ca.certServiceClient.GetCertificates(csrBytes, privateKeyBytes)
103         if err != nil {
104                 return nil, nil, err
105         }
106         log.Info("Certificate Chain", "cert-chain", response.CertificateChain)
107         log.Info("Trusted Certificates", "trust-certs", response.TrustedCertificates)
108
109
110         // TODO
111         // stored response as PEM
112         cert := x509.Certificate{}
113         cert.Raw = csr.Raw
114         encodedPEM, err := x509utils.EncodeX509(&cert)
115         if err != nil {
116                 return nil, nil, err
117         }
118         // END
119
120         signedPEM := encodedPEM
121         trustedCA := encodedPEM
122
123         log.Info("Signed cert PEM: ", "bytes", signedPEM)
124         log.Info("Trusted CA  PEM: ", "bytes", trustedCA)
125         log.Info("Successfully signed: ", "cert-name", certificateRequest.Name)
126
127         return signedPEM, trustedCA, nil
128 }