moving the docker build portion
[aaf/sms.git] / sms-service / src / quorumclient / quorumclient.go
1 /*
2 * Copyright 2018 TechMahindra
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15  */
16
17 package main
18
19 import (
20         "crypto/tls"
21         "crypto/x509"
22         "encoding/base64"
23         "encoding/json"
24         "io/ioutil"
25         "log"
26         "net/http"
27         "os"
28         "strings"
29         "time"
30 )
31
32 //This application checks the backend status and
33 //calls necessary initialization endpoints on the
34 //SMS webservice
35 func main() {
36         //Struct to read json configuration file
37         type config struct {
38                 BackEndURL string `json:"url"`
39                 CAFile     string `json:"cafile"`
40                 ClientCert string `json:"clientcert"`
41                 ClientKey  string `json:"clientkey"`
42                 B64Key     string `json:"key"`
43                 TimeOut    string `json:"timeout"`
44         }
45         //Load the config File for reading
46         vcf, err := os.Open("config.json")
47         if err != nil {
48                 log.Fatalf("Error reading config file %v", err)
49         }
50
51         cfg := config{}
52         decoder := json.NewDecoder(vcf)
53         err = decoder.Decode(&cfg)
54         if err != nil {
55                 log.Fatalf("Error while parsing config file %v", err)
56         }
57
58         duration, _ := time.ParseDuration(cfg.TimeOut)
59         ticker := time.NewTicker(duration)
60
61         for _ = range ticker.C {
62
63                 caCert, err := ioutil.ReadFile(cfg.CAFile)
64                 if err != nil {
65                         log.Fatalf("Error while reading CA file %v ", err)
66                 }
67                 caCertPool := x509.NewCertPool()
68                 caCertPool.AppendCertsFromPEM(caCert)
69                 cert, err := tls.LoadX509KeyPair(cfg.ClientCert, cfg.ClientKey)
70                 if err != nil {
71                         log.Fatalf("Error while loading key pair %v ", err)
72                 }
73
74                 client := &http.Client{
75                         Transport: &http.Transport{
76                                 TLSClientConfig: &tls.Config{
77                                         RootCAs:      caCertPool,
78                                         Certificates: []tls.Certificate{cert},
79                                 },
80                         },
81                 }
82                 //URL and Port is configured in config file
83                 response, err := client.Get(cfg.BackEndURL + "v1/sms/status")
84                 if err != nil {
85                         log.Fatalf("Error while connecting to SMS webservice %v", err)
86                 }
87
88                 responseData, err := ioutil.ReadAll(response.Body)
89                 if err != nil {
90                         log.Fatalf("Error while reading response %v", err)
91                 }
92                 var data map[string]interface{}
93                 json.Unmarshal(responseData, &data)
94                 sealed := data["sealed"].(bool)
95                 // Unseal the vault if sealed
96                 if sealed {
97                         decdB64Key, _ := base64.StdEncoding.DecodeString(cfg.B64Key)
98                         body := strings.NewReader(`{"key":"` + string(decdB64Key) + `"}`)
99                         //URL and PORT is configured via config file
100                         response, err = client.Post(cfg.BackEndURL+"v1/sms/unseal", "application/json", body)
101                         if err != nil {
102                                 log.Fatalf("Error while unsealing %v", err)
103                         }
104                 }
105         }
106 }