[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add client for CertService API
[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, caName string, keyPemBase64 []byte, certPemBase64 []byte, cacertPemBase64 []byte) (*CertServiceClientImpl, error) {
33         cert, err := tls.X509KeyPair(certPemBase64, keyPemBase64)
34         if err != nil {
35                 return nil, err
36         }
37         x509.NewCertPool()
38         caCertPool := x509.NewCertPool()
39         ok := caCertPool.AppendCertsFromPEM(cacertPemBase64)
40         if !ok {
41                 return nil, fmt.Errorf("couldn't certs from cacert")
42         }
43         httpClient := &http.Client{
44                 Transport: &http.Transport{
45                         TLSClientConfig: &tls.Config{
46                                 RootCAs:      caCertPool,
47                                 Certificates: []tls.Certificate{cert},
48                         },
49                 },
50         }
51         certificationUrl, err := parseUrl(baseUrl, caName)
52         if err != nil {
53                 return nil, err
54         }
55         client := CertServiceClientImpl{
56                 certificationUrl: certificationUrl.String(),
57                 httpClient:       httpClient,
58         }
59
60         return &client, nil
61 }
62
63 func parseUrl(baseUrl string, caName string) (*url.URL, error) {
64         parsedUrl, err := url.Parse(baseUrl)
65         if err != nil {
66                 return nil, err
67         }
68         if caName == "" {
69                 return nil, fmt.Errorf("caName cannot be empty")
70         }
71
72         parsedUrl.Path = path.Join(parsedUrl.Path, caName)
73         return parsedUrl, nil
74 }