-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
234 lines (191 loc) · 5.08 KB
/
Copy pathclient.go
File metadata and controls
234 lines (191 loc) · 5.08 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/tactycal/agent/packageLookup"
)
const (
DefaultClientTimeout = time.Second * 3
ErrorCodeExpiredToken = "TOKEN_EXPIRED"
ErrorCodeInvalidToken = "TOKEN_INVALID"
apiVersionPrefix = "v2"
)
type Client struct {
token string
host *Host
uri string
proxyUrl *url.URL
state *State
timeout time.Duration
}
type SendPackagesRequestBody struct {
*Host
Package []*packageLookup.Package `json:"packages"`
}
type ResponseErrorCode struct {
Error string `json:"error"`
}
type Token struct {
Token string `json:"token"`
}
func NewClient(cfg *Config, host *Host, state *State, timeout time.Duration) *Client {
// copy labels from config to host
host.Labels = cfg.Labels
// compose the client
return &Client{
token: cfg.Token,
host: host,
uri: cfg.Uri,
proxyUrl: cfg.Proxy,
state: state,
timeout: timeout,
}
}
func (c *Client) Authenticate() (string, error) {
// create a request
rsp, err := c.apiRequest("POST", "/agent/auth", fmt.Sprintf("Token %s", c.token), &c.host)
if err != nil {
return "", err
}
defer rsp.Body.Close()
log.Debugf("Got a %d response", rsp.StatusCode)
// validate the response code
if rsp.StatusCode != 200 {
return "", fmt.Errorf("API returned status code %d, expected 200", rsp.StatusCode)
}
// decode the response
var rspData Token
decoder := json.NewDecoder(rsp.Body)
err = decoder.Decode(&rspData)
if err != nil {
return "", err
}
return rspData.Token, nil
}
func (c *Client) SendPackageList(packages []*packageLookup.Package) error {
token, err := c.getToken()
if err != nil {
return err
}
body := &SendPackagesRequestBody{
Host: c.host,
Package: packages,
}
// create a request
rsp, err := c.apiRequest("POST", "/agent/submit", fmt.Sprintf("JWT %s", token), body)
if err != nil {
return err
}
defer rsp.Body.Close()
log.Debugf("Got a %d response", rsp.StatusCode)
if rsp.StatusCode == http.StatusNoContent {
// check if a new token (header X-Token) was returned by the API; if so,
// update the state
if h := rsp.Header.Get("X-Token"); h != "" {
log.Debug("Received a new token from the API; updating state")
c.state.SetToken(h)
}
return nil
}
// handle invalid or expired token response
if rsp.StatusCode == http.StatusUnauthorized {
// check error code
errCode := &ResponseErrorCode{}
if err := json.NewDecoder(rsp.Body).Decode(errCode); err != nil {
return err
}
if errCode.Error == ErrorCodeInvalidToken {
if err := c.state.Reset(); err != nil {
return err
}
return fmt.Errorf("Token was reported as invalid. Perhaps the host was deleted. New host ID will be assigned to this machine.")
}
// renew a token
if errCode.Error == ErrorCodeExpiredToken {
err := c.renewToken(token)
if err == nil {
err = fmt.Errorf("Token was reported as expired. It has been renewed")
}
return err
}
}
return fmt.Errorf("API returned status code %d, expected 204", rsp.StatusCode)
}
func (c *Client) getToken() (string, error) {
// try to read token from state
token, err := c.state.GetToken()
if err == nil && token != "" {
return token, nil
}
// get a new token from API
log.Debug("Agent not authenticated yet.")
token, err = c.Authenticate()
if err != nil {
return "", err
}
// write the token new token to state
if err := c.state.SetToken(token); err != nil {
return "", err
}
return token, nil
}
func (c *Client) renewToken(token string) error {
// create a request
rsp, err := c.apiRequest("POST", "/agent/renew", fmt.Sprintf("Token %s", c.token), &Token{token})
if err != nil {
return err
}
defer rsp.Body.Close()
log.Debugf("Got a %d response", rsp.StatusCode)
if rsp.StatusCode == 401 {
c.state.Reset()
return fmt.Errorf("Token was reported as invalid. Perhaps the host was deleted. New host ID will be assigned to this machine.")
}
if rsp.StatusCode != 200 {
return fmt.Errorf("Token could not be renewed")
}
var rspData Token
decoder := json.NewDecoder(rsp.Body)
err = decoder.Decode(&rspData)
if err != nil {
return err
}
// write the new token to state
err = c.state.SetToken(rspData.Token)
return err
}
func (c *Client) apiRequest(method, endpoint, authorization string, input interface{}) (*http.Response, error) {
// encode body
body := bytes.NewBuffer(nil)
if input != nil {
enc := json.NewEncoder(body)
if err := enc.Encode(input); err != nil {
return nil, err
}
}
// strip slashes from the beginning of the endpoint
endpoint = strings.TrimLeft(endpoint, "/")
// build the request
uri := fmt.Sprintf("%s/%s/%s", c.uri, apiVersionPrefix, endpoint)
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", authorization)
req.Header.Set("Content-Type", "application/json")
req.Close = true
// execute the request
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(c.proxyUrl),
},
Timeout: c.timeout,
}
log.Debugf("Sending a %s request to %s", req.Method, req.URL)
return client.Do(req)
}