-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
266 lines (229 loc) · 6.24 KB
/
configuration.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package control
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
"runtime"
"strings"
"github.com/ghchinoy/rwctl/version"
"time"
)
// Configuration provides a simple struct to hold login info
type Configuration struct {
URL string `json:"url" mapstructure:"url"`
Email string `json:"email" mapstructure:"email"`
Password string `json:"password" mapstructure:"password"`
Theme string `json:"theme" mapstructure:"theme"`
ConsoleUsername string `json:"consoleUsername" mapstructure:"console-username"`
LoginDomainID string `json:"loginDomainID"`
}
// UserInfo is the logged-in user's information
type UserInfo struct {
UserName string `json:"userName"`
Status string `json:"status"`
AvatarURL string `json:"avatarURL"`
UserFDN string `json:"userFDN"`
LoginState string `json:"loginState"`
AuthTokenValidUntil string `json:"authTokenValidUntil"`
LoginDomainID string `json:"loginDomainId"`
PendingNotifications int `json:"pendingNotifications"`
}
// ViperToConfiguration Takes in viper, returns Configuration object
func ViperToConfiguration(configmap map[string]interface{}, debug bool) (Configuration, error) {
var config Configuration
keys := make([]string, len(configmap))
for k := range configmap {
keys = append(keys,k)
if k == "url" {
config.URL = configmap["url"].(string)
} else if k == "email" {
config.Email = configmap["email"].(string)
} else if k == "password" {
config.Password = configmap["password"].(string)
} else if k == "theme" {
config.Theme = configmap["theme"].(string)
} else if k == "console-username" {
config.ConsoleUsername = configmap["console-username"].(string)
} else if k == "loginDomainID" {
config.ConsoleUsername = configmap["loginDomainID"].(string)
}
}
if debug {
log.Println("Config keys", strings.Join(keys, ", "))
}
return config, nil
}
// InitializeConfiguration reads config (or local.conf)
func InitializeConfiguration(configLocation string, debug bool) (Configuration, error) {
var config Configuration
// if not specified, try ./local.conf, then try ~/.akana/local.conf
if configLocation == "" || configLocation == "<nil>" {
cwd, err := os.Getwd()
if err != nil {
if debug {
fmt.Println("Cant't get working directory,")
}
return config, err
}
configLocation = cwd + "/local.conf"
config, err := tryConfig(configLocation)
if err != nil {
if debug {
fmt.Println("Couldn't find config at ./local.conf")
}
} else {
return config, nil
}
configLocation = userHomeDir() + "/.akana/local.conf"
config, err = tryConfig(configLocation)
if err != nil {
if debug {
fmt.Println("Couldn't find config at ~/.akana/local.conf")
}
return config, err
}
return config, nil
}
config, err := tryConfig(configLocation)
if err != nil {
return config, err
}
if len(config.Password) < 1 {
fmt.Printf("Missing or blank password.")
return config, err
}
if debug {
log.Println("Config file contents:", config)
}
return config, nil
}
// extracting to make repeatable
// for _, v := range pathstocheckarray {
// config, err := tryConfig(v)
// if err == nil {
// return config
// }
//}
func tryConfig(configLocation string) (Configuration, error) {
var config Configuration
configBytes, err := ioutil.ReadFile(configLocation)
if err != nil {
//fmt.Printf("Error opening config file: %s\n", err)
return config, err
}
//var config Configuration
err = json.Unmarshal(configBytes, &config)
if err != nil {
//fmt.Printf("Unable to parse configuration file: %s\n", err)
return config, err
}
return config, nil
}
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
// Auth is another simple struct
type Auth struct {
Email string `json:"email"`
Password string `json:"password"`
}
// LoginToCM logs in to the API Platform
func LoginToCM(config Configuration, debug bool) (*http.Client, UserInfo, error) {
var u UserInfo
// Login
if debug {
log.Println("Logging in...")
}
transport := &http.Transport{
TLSHandshakeTimeout: 60 * time.Second,
}
client := &http.Client{Transport: transport}
var err error
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatalln(err)
return client, u, err
}
client.Jar = jar
if debug {
log.Println(config)
}
loginURI := config.URL + "/api/login"
auth := Auth{config.Email, config.Password}
buf, err := json.Marshal(auth)
if err != nil {
log.Fatalln(err)
return client, u, err
}
req, err := http.NewRequest("POST", loginURI, bytes.NewReader(buf))
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
return client, u, err
}
if resp.StatusCode != 200 || debug {
log.Printf("Login %s", resp.Status)
}
// assign logindomainid to UserInfo
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
if debug {
log.Println("Can't get UserInfo")
}
}
err = json.Unmarshal(bodyBytes, &u)
if err != nil {
if debug {
log.Println("Cant unmarshal UserInfo")
}
}
// debug
if debug {
DebugResponseHeader(resp)
}
return client, u, nil
}
// AddCsrfHeader checks to see if cookie jar has Csrf and adds it as a header
func AddCsrfHeader(req *http.Request, client *http.Client) *http.Request {
for _, v := range client.Jar.Cookies(req.URL) {
if strings.HasPrefix(v.Name, "Csrf-Token") {
req.Header.Add("X-"+v.Name, v.Value)
}
}
req.Header.Add("rwctl", version.Version())
return req
}
// DebugResponseHeader outputs to the log the headers of an http.Response struct
func DebugResponseHeader(resp *http.Response) {
log.Println(">>> DEBUG >>>")
for k, v := range resp.Header {
for _, h := range v {
log.Printf("%s : %s", k, h)
}
}
log.Println("<<< DEBUG <<<")
}
// DebugRequestHeader outputs headers of an http.Request struct to the log
func DebugRequestHeader(req *http.Request) {
log.Println(">>> DEBUG >>>")
for k, v := range req.Header {
for _, h := range v {
log.Printf("%s : %s", k, h)
}
}
log.Println("<<< DEBUG <<<")
}