/
start.go
195 lines (157 loc) · 3.97 KB
/
start.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
package webhook
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/spf13/viper"
)
type token string
type StartConfig struct {
// maximum time allowed to wait for data to be retrieved
Duration time.Duration `json:"duration"`
// path to query for current hooks
ApiPath string `json:"apiPath"`
AuthHeader string `json:"authHeader"`
// sat configuration data for requesting token
Sat struct {
// url path
Path string `json:"path"`
// client id
Id string `json:"id"`
// client secret
Secret string `json:"secret"`
// client capabilities
Capabilities string `json:"capabilities"`
// the obtained sat token
Token token
} `json:"sat"`
// client is here for testing purposes
client http.Client
}
type Result struct {
Hooks []W
Error error
}
type satReqResp struct {
expires int `json:"expires_in"`
Token token `json:"serviceAccessToken"`
}
func NewStartFactory(v *viper.Viper) (sc *StartConfig) {
if v == nil {
v = viper.New()
v.SetDefault("duration", 1000000000)
v.SetDefault("apiPath", "http://111.2.3.44:5555/api")
v.SetDefault("sat.path", "http://111.22.33.4.7777/sat")
v.SetDefault("sat.id", "myidisthisstring")
v.SetDefault("sat.secret", "donottellsecrets")
v.SetDefault("sat.capabilities", "capabilitiesgohere")
}
v.Unmarshal(&sc)
sc.client = http.Client{}
return sc
}
func (sc *StartConfig) getAuthorization() (err error) {
u, err := url.Parse(sc.Sat.Path)
if err != nil {
return
}
if sc.Duration > 0 {
u.RawQuery = fmt.Sprintf("ttl=%d&capabilities=%s", int(sc.Duration.Seconds()), sc.Sat.Capabilities)
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Client-Id", sc.Sat.Id)
req.Header.Set("X-Client-Secret", sc.Sat.Secret)
resp, err := sc.client.Do(req)
if err != nil {
return
}
body, err := getPayload(resp)
if err != nil {
return
}
var srr satReqResp
err = json.Unmarshal(body, &srr)
if err != nil {
return
}
sc.Sat.Token = srr.Token
return
}
func getPayload(resp *http.Response) (body []byte, err error) {
if resp == nil {
return body, errors.New("Response was nil")
} else if resp.StatusCode >= 400 {
return body, errors.New(fmt.Sprintf("Response status code: %d", resp.StatusCode))
} else {
body, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return body, errors.New(fmt.Sprintf("Response body read failed. %v", err))
}
return
}
}
func (sc *StartConfig) makeRequest() (resp *http.Response, err error) {
req, err := http.NewRequest("GET", sc.ApiPath, nil)
if err != nil {
return
}
req.Header.Set("content-type", "application/json")
if len(sc.Sat.Token) < 1 && len(sc.AuthHeader) > 0 {
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", sc.AuthHeader))
} else {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", sc.Sat.Token))
}
resp, err = sc.client.Do(req)
if err != nil {
return
}
return
}
func (sc *StartConfig) GetCurrentSystemsHooks(rc chan Result) {
var hooks []W
if sc.Sat.Token == "" {
err := sc.getAuthorization()
if err != nil {
rc <- Result{hooks, err}
return
}
}
fn := func(sc *StartConfig, rChan chan Result) {
resp, err := sc.makeRequest()
body, err := getPayload(resp)
err = json.Unmarshal(body, &hooks)
// temporary fix to convert old webhook struct to new.
if err != nil && strings.HasPrefix(err.Error(), "parsing time") {
hooks, err = convertOldHooksToNewHooks(body)
}
rChan <- Result{hooks, err}
}
getHooksChan := make(chan Result, 1)
timeout := time.After(sc.Duration)
fn(sc, getHooksChan)
for {
select {
case r := <-getHooksChan:
if r.Error != nil || len(r.Hooks) <= 0 {
time.Sleep(time.Second * 2) // wait a moment between queries
fn(sc, getHooksChan)
} else {
rc <- Result{r.Hooks, r.Error}
return
}
case <-timeout:
rc <- Result{hooks, errors.New("Unable to obtain hook list in allotted time.")}
return
}
}
}