-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgotral.go
154 lines (126 loc) · 3.55 KB
/
gotral.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* github.com/codenoid - Developer
* code source : - https://github.com/codenoid/GoTral
* - https://golang.org/pkg/net/http/
*/
package gotral
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
import "github.com/mervick/aes-everywhere/go/aes256"
// GoTral : enabling uses of basic auth
type GoTral struct {
Url string
Passphrase string
BasicAuth bool
Username string
Password string
}
// config : a data structure that come from GoTral server
// usually just {"key": "what", "value": "somethingsecret"}
type config struct {
Key string `json:"key"`
Value string `json:"value"`
}
// confret : return config data as string map
// easy to use/get/understand
type confret map[string]string
// Get : returns a value or an error for a key
func (config confret) Get(key string) (string, error) {
if len(config) == 0 {
return "", fmt.Errorf("The config is empty")
}
if val, ok := config[key]; ok {
return val, nil
}
return "", fmt.Errorf("The key doesn't exist")
}
// DirectLoad : directly load config from given url and decrypt with given passphrase
// usually this function only called on app boot time
func DirectLoad(url string, passphrase string) (confret, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
if resp.StatusCode == 401 {
return nil, fmt.Errorf("Unauthorized, username & password for basic auth needed")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
decrypt := aes256.Decrypt(string(body), passphrase)
// initialize slices of config to receive data from api
var decoded []config
err = json.Unmarshal([]byte(decrypt), &decoded)
if err != nil {
return nil, err
}
// initialize empty map[string]string
result := make(map[string]string)
// iterate and put all data into result
for _, value := range decoded {
// break and return error if there is duplicate key
// that come from api
if _, ok := result[value.Key]; ok {
return nil, fmt.Errorf("error: duplicate config key : %v", value.Key)
}
result[value.Key] = value.Value
}
return result, nil
}
// LoadConfig : basic auth version support
func (r GoTral) LoadConfig() (confret, error) {
if r.BasicAuth == false {
val, err := DirectLoad(r.Url, r.Passphrase)
if err != nil {
return nil, err
}
return val, nil
}
// initialize http client with/out option
client := &http.Client{}
// create new request by given url
req, err := http.NewRequest("GET", r.Url, nil)
if err != nil {
return nil, err
}
// pass auth for BasicAuth
req.SetBasicAuth(r.Username, r.Password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
if resp.StatusCode == 401 {
return nil, fmt.Errorf("Unauthorized, wrong username/password")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
decrypt := aes256.Decrypt(string(body), r.Passphrase)
// initialize slices of config to receive data from api
var decoded []config
err = json.Unmarshal([]byte(decrypt), &decoded)
if err != nil {
return nil, err
}
// initialize empty map[string]string
result := make(map[string]string)
// iterate and put all data into result
for _, value := range decoded {
// break and return error if there is duplicate key
// that come from api
if _, ok := result[value.Key]; ok {
return nil, fmt.Errorf("error: duplicate config key : %v", value.Key)
}
result[value.Key] = value.Value
}
return result, nil
}