[OOM-K8S-CERT-EXTERNAL-PROVIDER] Extend SANs support
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / logger / certificate_request_logger.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020 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 logger
22
23 import (
24         x509 "crypto/x509"
25         "net"
26         "net/url"
27         "strconv"
28
29         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
30
31         "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
32 )
33
34 const (
35         CertServiceName = "Cert Service API"
36         CMPv2ServerName = "CMPv2 Server"
37 )
38
39 func LogCertRequestProperties(log leveledlogger.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) {
40         logSupportedProperties(log, csr)
41         logPropertiesNotSupportedByCertService(log, request, csr)
42         logPropertiesOverriddenByCMPv2Server(log, request)
43 }
44
45 func logSupportedProperties(log leveledlogger.Logger, csr *x509.CertificateRequest) {
46         logSupportedSingleValueProperty(log, csr.Subject.CommonName, "common name")
47         logSupportedMultiValueProperty(log, csr.Subject.Organization, "organization")
48         logSupportedMultiValueProperty(log, csr.Subject.OrganizationalUnit, "organization unit")
49         logSupportedMultiValueProperty(log, csr.Subject.Country, "country")
50         logSupportedMultiValueProperty(log, csr.Subject.Province, "state")
51         logSupportedMultiValueProperty(log, csr.Subject.Locality, "location")
52         logSupportedMultiValueProperty(log, csr.DNSNames, "dns names")
53         logSupportedMultiValueProperty(log, csr.EmailAddresses, "email addresses")
54         logSupportedMultiValueProperty(log, mapIpAddressesToText(csr.IPAddresses), "ipAddresses")
55         logSupportedMultiValueProperty(log, mapUrisToText(csr.URIs), "uris")
56 }
57
58 func logSupportedMultiValueProperty(log leveledlogger.Logger, values []string, propertyName string) {
59         if len(values) > 0 {
60                 log.Info(getSupportedMessage(propertyName, extractStringArray(values)))
61         }
62 }
63
64 func logSupportedSingleValueProperty(log leveledlogger.Logger, value string, propertyName string) {
65         log.Info(getSupportedMessage(propertyName, value))
66 }
67
68 func logPropertiesOverriddenByCMPv2Server(log leveledlogger.Logger, request *cmapi.CertificateRequest) {
69         if request.Spec.Duration != nil && len(request.Spec.Duration.String()) > 0 {
70                 log.Info(getOverriddenMessage("duration", request.Spec.Duration.Duration.String()))
71         }
72         if request.Spec.Usages != nil && len(request.Spec.Usages) > 0 {
73                 log.Info(getOverriddenMessage("usages", extractUsages(request.Spec.Usages)))
74         }
75 }
76
77 func extractUsages(usages []cmapi.KeyUsage) string {
78         values := ""
79         for _, usage := range usages {
80                 values = values + string(usage) + ", "
81         }
82         return values
83 }
84
85 func logPropertiesNotSupportedByCertService(log leveledlogger.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) {
86         if request.Spec.IsCA == true {
87                 log.Warning(getNotSupportedMessage("isCA", strconv.FormatBool(request.Spec.IsCA)))
88         }
89
90         if len(csr.Subject.StreetAddress) > 0 {
91                 log.Warning(getNotSupportedMessage("subject.streetAddress", extractStringArray(csr.Subject.StreetAddress)))
92         }
93
94         if len(csr.Subject.PostalCode) > 0 {
95                 log.Warning(getNotSupportedMessage("subject.postalCodes", extractStringArray(csr.Subject.PostalCode)))
96         }
97
98         if len(csr.Subject.SerialNumber) > 0 {
99                 log.Warning(getNotSupportedMessage("subject.serialNumber", csr.Subject.SerialNumber))
100         }
101
102 }
103
104 func extractStringArray(strArray []string) string {
105         values := ""
106         for _, val := range strArray {
107                 values = values + val + ", "
108         }
109         return values
110 }
111
112 func mapUrisToText(uris []*url.URL) []string {
113         urisAsText := make([]string, len(uris))
114         for i, ipAddress := range uris {
115                 urisAsText[i] = ipAddress.String()
116         }
117         return urisAsText
118 }
119
120 func mapIpAddressesToText(addresses []net.IP) []string {
121         ipsAsText := make([]string, len(addresses))
122         for i, ipAddress := range addresses {
123                 ipsAsText[i] = ipAddress.String()
124         }
125         return ipsAsText
126 }
127
128 func getSupportedMessage(property string, value string) string {
129         return "+ property '" + property + "' with value '" + value + "' will be sent in certificate signing request to " + CMPv2ServerName
130 }
131
132 func getNotSupportedMessage(property string, value string) string {
133         return "- property '" + property + "' with value '" + value + "' is not supported by " + CertServiceName
134 }
135
136 func getOverriddenMessage(property string, values string) string {
137         return "* property '" + property + "' with value '" + values + "' will be overridden by " + CMPv2ServerName
138 }