[OOM-K8S-CERT-EXTERNAL-PROVIDER] Format golang code
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certserviceclient / cert_service_client_factory.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 certserviceclient
22
23 import (
24         "crypto/tls"
25         "crypto/x509"
26         "fmt"
27         "net/http"
28         "net/url"
29         "path"
30 )
31
32 func CreateCertServiceClient(baseUrl string, healthEndpoint string, certEndpoint string, caName string,
33         keyPemBase64 []byte, certPemBase64 []byte, cacertPemBase64 []byte) (*CertServiceClientImpl, error) {
34         cert, err := tls.X509KeyPair(certPemBase64, keyPemBase64)
35         if err != nil {
36                 return nil, err
37         }
38         x509.NewCertPool()
39         caCertPool := x509.NewCertPool()
40         ok := caCertPool.AppendCertsFromPEM(cacertPemBase64)
41         if !ok {
42                 return nil, fmt.Errorf("couldn't certs from cacert")
43         }
44         httpClient := &http.Client{
45                 Transport: &http.Transport{
46                         TLSClientConfig: &tls.Config{
47                                 RootCAs:      caCertPool,
48                                 Certificates: []tls.Certificate{cert},
49                         },
50                 },
51         }
52         healthUrl, certificationUrl, err := validateAndParseUrls(baseUrl, healthEndpoint, certEndpoint, caName)
53         if err != nil {
54                 return nil, err
55         }
56         client := CertServiceClientImpl{
57                 healthUrl:        healthUrl,
58                 certificationUrl: certificationUrl,
59                 httpClient:       httpClient,
60         }
61
62         return &client, nil
63 }
64
65 func validateAndParseUrls(baseUrl string, healthEndpoint string, certEndpoint string, caName string) (string, string, error) {
66         if err := validateUrls(baseUrl, healthEndpoint, certEndpoint, caName); err != nil {
67                 return "", "", err
68         }
69
70         certUrl, _ := url.Parse(baseUrl)
71         healthUrl, _ := url.Parse(baseUrl)
72
73         certUrl.Path = path.Join(certEndpoint, caName)
74         healthUrl.Path = path.Join(healthEndpoint)
75
76         return healthUrl.String(), certUrl.String(), nil
77 }
78
79 func validateUrls(baseUrl string, healthEndpoint string, certEndpoint string, caName string) error {
80         if _, err := url.Parse(baseUrl); err != nil {
81                 return err
82         }
83         if caName == "" {
84                 return fmt.Errorf("caName cannot be empty")
85         }
86         if _, err := url.Parse(caName); err != nil {
87                 return err
88         }
89         if _, err := url.Parse(healthEndpoint); err != nil {
90                 return err
91         }
92         if _, err := url.Parse(certEndpoint); err != nil {
93                 return err
94         }
95         return nil
96 }