-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
282 lines (239 loc) · 6.69 KB
/
client.go
File metadata and controls
282 lines (239 loc) · 6.69 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (C) 2016 Eneo Tecnologia S.L.
// Diego Fernández Barrera <bigomby@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/sirupsen/logrus"
)
const (
registerRequest = "register"
verifyRequest = "verify"
claimedResponse = "claimed"
registeredResponse = "registered"
)
// APIClient is an objet that can communicate with the API to perform a
// registration. It has the necessary methods to interact with the API.
type APIClient struct {
status string // Current status of the registrtation
cert string // Client certificate
nodename string // Name of the node received along with the cert
config APIClientConfig
}
// NewAPIClient creates a new instance of an ApiClient object
func NewAPIClient(config APIClientConfig) *APIClient {
c := &APIClient{
config: config,
status: "registering",
}
if c.config.Logger == nil {
c.config.Logger = logrus.NewEntry(logrus.New())
c.config.Logger.Logger.Out = ioutil.Discard
} else {
c.config.Logger = c.config.Logger.WithFields(logrus.Fields{
"component": "api_client",
})
}
logger := c.config.Logger
// Check if the configuration is ok
if len(c.config.URL) == 0 {
logger.Warnf("Url not provided")
return nil
}
if len(c.config.Hash) == 0 {
logger.Warnf("Hash not provided")
return nil
}
if c.config.Cpus == 0 {
logger.Warnf("CPU number not provided")
return nil
}
if c.config.Memory == 0 {
logger.Warnf("Memory not provided")
return nil
}
if c.config.DeviceType == 0 {
logger.Warnf("Device type not provided")
return nil
}
if c.config.HTTPClient == nil {
if c.config.Insecure {
c.config.HTTPClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
} else {
c.config.HTTPClient = &http.Client{}
}
}
return c
}
// Register send a POST request with some fields to the remote API. It expects
// a UUID from the API.
func (c *APIClient) Register() (uuid string, err error) {
logger := c.config.Logger
if c.status == registeredResponse {
return "", errors.New("This device is already registered")
}
// request structure for register method
type Request struct {
Order string `json:"order"`
Cpus int `json:"cpus"`
Memory uint64 `json:"memory"`
DeviceType int `json:"type"`
Hash string `json:"hash"`
}
// response structure for register method
type Response struct {
Status string `json:"status"`
Hash string `json:"hash"`
UUID string `json:"uuid"`
}
// Build the request
req := Request{
Order: "register",
Cpus: c.config.Cpus,
Memory: c.config.Memory,
DeviceType: c.config.DeviceType,
Hash: c.config.Hash,
}
// Generate a JSON message with the request
marshalledReq, err := json.Marshal(&req)
if err != nil {
return "", err
}
// Send request
logger.Debugf("Register request: %v", req)
bufferReq := bytes.NewBuffer(marshalledReq)
httpReq, err := http.NewRequest("POST", c.config.URL, bufferReq)
if err != nil {
return "", err
}
httpReq.Header.Set("Content-Type", "application/json")
rawResponse, err := c.config.HTTPClient.Do(httpReq)
if err != nil {
return "", err
}
defer rawResponse.Body.Close()
if rawResponse.StatusCode >= 400 {
return "", errors.New("Got status code: " + rawResponse.Status)
}
// Read response to a buffer
bufferResponse, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return "", err
}
// Unmarshall the response
res := Response{}
err = json.Unmarshal(bufferResponse, &res)
if err != nil {
return "", err
}
logger.Debugf("Register response: %v", res)
// Check response
if res.Status == registeredResponse {
c.status = res.Status
}
return res.UUID, nil
}
// Verify send the UUID along with the HASH to the API and expect to receive
// a client certificate
func (c *APIClient) Verify(uuid string) error {
logger := c.config.Logger
if c.status == claimedResponse {
return errors.New("This device is already claimed")
}
// request structure for register method
type request struct {
Order string `json:"order"`
Hash string `json:"hash"`
UUID string `json:"uuid"`
}
// response structure for register method
type response struct {
Status string `json:"status"`
Cert string `json:"cert"`
Nodename string `json:"nodename"`
}
// Build the request
req := request{
Order: "verify",
Hash: c.config.Hash,
UUID: uuid,
}
// Generate a JSON message with the request
marshalledReq, err := json.Marshal(req)
if err != nil {
return err
}
// Send request
logger.Debugf("Verify request: %v", req)
bufferReq := bytes.NewBuffer(marshalledReq)
httpReq, err := http.NewRequest("POST", c.config.URL, bufferReq)
if err != nil {
return err
}
httpReq.Header.Set("Content-Type", "application/json")
rawResponse, err := c.config.HTTPClient.Do(httpReq)
if err != nil {
return err
}
defer rawResponse.Body.Close()
if rawResponse.StatusCode >= 400 {
return errors.New("Got status code: " + rawResponse.Status)
}
// Read response to a buffer
bufferResponse, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return err
}
// Unmarshall the response
res := response{}
err = json.Unmarshal(bufferResponse, &res)
if err != nil {
return err
}
logger.Debugf("Claimed response: %v", res)
if res.Status == registeredResponse {
return nil
}
if res.Status == claimedResponse {
c.nodename = res.Nodename
c.status = res.Status
c.cert = res.Cert
return nil
}
return errors.New("Unknow status: " + res.Status)
}
// IsRegistered check if the client has been registered previously
func (c *APIClient) IsRegistered() bool {
return c.status == registeredResponse
}
// IsClaimed check if the client has been claimed previously
func (c *APIClient) IsClaimed() bool {
return c.status == claimedResponse
}
// GetCertificate returns the certificate if the device is claimed
func (c *APIClient) GetCertificate() string {
return c.cert
}
// GetNodename return the certificate if the device is claimed
func (c *APIClient) GetNodename() string {
return c.nodename
}