2 * Copyright 2018 Intel Corporation, Inc
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.
35 pkgerrors "github.com/pkg/errors"
38 //DataJSON stores a list of domains from JSON file
39 type DataJSON struct {
40 //Support single domain: {} structure in JSON
41 Domain SecretDomainJSON `json:"domain,omitempty"`
42 //Support plural domains: [{}] structure in JSON
43 Domains []SecretDomainJSON `json:"domains,omitempty"`
46 //SecretDomainJSON stores a name for the Domain and a list of Secrets
47 type SecretDomainJSON struct {
48 Name string `json:"name"`
49 Secrets []SecretJSON `json:"secrets"`
52 //SecretJSON stores a name for the Secret and a list of Values
53 type SecretJSON struct {
54 Name string `json:"name"`
55 Values map[string]interface{} `json:"values"`
58 //Processes the JSON file and returns a DataJSON struct
59 func processJSONFile(name string) (DataJSON, error) {
61 data, err := ioutil.ReadFile(name)
63 return DataJSON{}, pkgerrors.Cause(err)
67 err = json.Unmarshal(data, &d)
69 return DataJSON{}, pkgerrors.Cause(err)
75 type smsClient struct {
81 httpClient *http.Client
84 func (c *smsClient) init() error {
87 caCert, err := ioutil.ReadFile(c.CaCertPath)
89 fmt.Println(pkgerrors.Cause(err))
90 fmt.Println("Using Insecure Server Verification")
94 tlsConfig := &tls.Config{
95 MinVersion: tls.VersionTLS12,
98 tlsConfig.InsecureSkipVerify = skipVerify
100 // Add cert information when skipVerify is false
101 if skipVerify == false {
102 caCertPool := x509.NewCertPool()
103 caCertPool.AppendCertsFromPEM(caCert)
104 tlsConfig.RootCAs = caCertPool
107 tr := &http.Transport{
108 TLSClientConfig: tlsConfig,
111 c.httpClient = &http.Client{
113 Timeout: time.Duration(c.Timeout) * time.Second,
119 func (c *smsClient) sendPostRequest(relURL string, message map[string]interface{}) error {
121 rel, err := url.Parse(relURL)
123 return pkgerrors.Cause(err)
125 u := c.BaseURL.ResolveReference(rel)
127 body, err := json.Marshal(message)
129 return pkgerrors.Cause(err)
132 resp, err := c.httpClient.Post(u.String(), "application/json", bytes.NewBuffer(body))
134 return pkgerrors.Cause(err)
137 if resp.StatusCode >= 400 && resp.StatusCode < 600 {
139 errText, _ := ioutil.ReadAll(resp.Body)
140 return pkgerrors.Errorf("Request Failed with: %s and Error: %s",
141 resp.Status, string(errText))
147 func (c *smsClient) createDomain(domain string) error {
149 message := map[string]interface{}{
152 url := "/v1/sms/domain"
153 err := c.sendPostRequest(url, message)
155 return pkgerrors.Cause(err)
160 func (c *smsClient) createSecret(domain string, secret string,
162 values map[string]interface{}) error {
163 message := map[string]interface{}{
168 url := "/v1/sms/domain/" + strings.TrimSpace(domain) + "/secret"
169 err := c.sendPostRequest(url, message)
171 return pkgerrors.Cause(err)
177 //uploadToSMS reads through the domain or domains and uploads
178 //their corresponding secrets to SMS service
179 func (c *smsClient) uploadToSMS(data DataJSON) error {
181 var ldata []SecretDomainJSON
183 //Check if Domain is empty
184 if strings.TrimSpace(data.Domain.Name) != "" {
185 ldata = append(ldata, data.Domain)
186 } else if len(data.Domains) != 0 {
187 //Check if plural Domains are empty
188 ldata = append(ldata, data.Domains...)
190 return pkgerrors.New("Invalid JSON Data. No domain or domains found")
193 for _, d := range ldata {
194 err := c.createDomain(d.Name)
196 return pkgerrors.Cause(err)
199 for _, s := range d.Secrets {
200 err = c.createSecret(d.Name, s.Name, s.Values)
202 return pkgerrors.Cause(err)
212 cacert := flag.String("cacert", "/sms/certs/aaf_root_ca.cer",
213 "Path to the CA Certificate file")
214 serviceurl := flag.String("serviceurl", "https://aaf-sms.onap",
215 "Url for the SMS Service")
216 serviceport := flag.Int("serviceport", 10443,
217 "Service port if its different than the default")
218 jsondir := flag.String("jsondir", ".",
219 "Folder containing json files to upload")
223 files, err := ioutil.ReadDir(*jsondir)
225 log.Fatal(pkgerrors.Cause(err))
228 serviceURL, err := url.Parse(*serviceurl + ":" + strconv.Itoa(*serviceport))
230 log.Fatal(pkgerrors.Cause(err))
233 client := &smsClient{
240 for _, file := range files {
241 if filepath.Ext(file.Name()) == ".json" {
242 fmt.Println("Processing ", file.Name())
243 d, err := processJSONFile(file.Name())
245 log.Printf("Error Reading %s : %s", file.Name(), pkgerrors.Cause(err))
249 err = client.uploadToSMS(d)
251 log.Printf("Error Uploading %s : %s", file.Name(), pkgerrors.Cause(err))