2 * Copyright 2018 TechMahindra
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
32 //This application checks the backend status and
33 //calls necessary initialization endpoints on the
36 //Struct to read json configuration file
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"`
45 //Load the config File for reading
46 vcf, err := os.Open("config.json")
48 log.Fatalf("Error reading config file %v", err)
52 decoder := json.NewDecoder(vcf)
53 err = decoder.Decode(&cfg)
55 log.Fatalf("Error while parsing config file %v", err)
58 duration, _ := time.ParseDuration(cfg.TimeOut)
59 ticker := time.NewTicker(duration)
61 for _ = range ticker.C {
63 caCert, err := ioutil.ReadFile(cfg.CAFile)
65 log.Fatalf("Error while reading CA file %v ", err)
67 caCertPool := x509.NewCertPool()
68 caCertPool.AppendCertsFromPEM(caCert)
69 cert, err := tls.LoadX509KeyPair(cfg.ClientCert, cfg.ClientKey)
71 log.Fatalf("Error while loading key pair %v ", err)
74 client := &http.Client{
75 Transport: &http.Transport{
76 TLSClientConfig: &tls.Config{
78 Certificates: []tls.Certificate{cert},
82 //URL and Port is configured in config file
83 response, err := client.Get(cfg.BackEndURL + "v1/sms/status")
85 log.Fatalf("Error while connecting to SMS webservice %v", err)
88 responseData, err := ioutil.ReadAll(response.Body)
90 log.Fatalf("Error while reading response %v", err)
92 var data map[string]interface{}
93 json.Unmarshal(responseData, &data)
94 sealed := data["sealed"].(bool)
95 // Unseal the vault 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)
102 log.Fatalf("Error while unsealing %v", err)