This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_client.go
184 lines (147 loc) · 6.37 KB
/
http_client.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
package registry
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
const httpClientLogTag = "RegistryHTTPClient"
const httpClientMaxAttemps = 5
const httpClientRetryDelay = 5
// HTTPClient represents a BOSH Registry Client.
type HTTPClient struct {
options ClientOptions
logger boshlog.Logger
}
// NewHTTPClient creates a new BOSH Registry Client.
func NewHTTPClient(
options ClientOptions,
logger boshlog.Logger,
) HTTPClient {
return HTTPClient{
options: options,
logger: logger,
}
}
// Delete deletes the instance settings for a given instance ID.
func (c HTTPClient) Delete(instanceID string) error {
endpoint := fmt.Sprintf("%s/instances/%s/settings", c.options.EndpointWithCredentials(), instanceID)
c.logger.Debug(httpClientLogTag, "Deleting agent settings from registry endpoint '%s'", endpoint)
request, err := http.NewRequest("DELETE", endpoint, nil)
if err != nil {
return bosherr.WrapErrorf(err, "Creating DELETE request for registry endpoint '%s'", endpoint)
}
httpResponse, err := c.doRequest(request)
if err != nil {
return bosherr.WrapErrorf(err, "Deleting agent settings from registry endpoint '%s'", endpoint)
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != http.StatusOK {
return bosherr.Errorf("Received status code '%d' when deleting agent settings from registry endpoint '%s'", httpResponse.StatusCode, endpoint)
}
c.logger.Debug(httpClientLogTag, "Deleted agent settings from registry endpoint '%s'", endpoint)
return nil
}
// Fetch gets the agent settings for a given instance ID.
func (c HTTPClient) Fetch(instanceID string) (AgentSettings, error) {
endpoint := fmt.Sprintf("%s/instances/%s/settings", c.options.EndpointWithCredentials(), instanceID)
c.logger.Debug(httpClientLogTag, "Fetching agent settings from registry endpoint '%s'", endpoint)
request, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return AgentSettings{}, bosherr.WrapErrorf(err, "Creating GET request for registry endpoint '%s'", endpoint)
}
httpResponse, err := c.doRequest(request)
if err != nil {
return AgentSettings{}, bosherr.WrapErrorf(err, "Fetching agent settings from registry endpoint '%s'", endpoint)
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != http.StatusOK {
return AgentSettings{}, bosherr.Errorf("Received status code '%d' when fetching agent settings from registry endpoint '%s'", httpResponse.StatusCode, endpoint)
}
httpBody, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return AgentSettings{}, bosherr.WrapErrorf(err, "Reading agent settings response from registry endpoint '%s'", endpoint)
}
var settingsResponse agentSettingsResponse
if err = json.Unmarshal(httpBody, &settingsResponse); err != nil {
return AgentSettings{}, bosherr.WrapErrorf(err, "Unmarshalling agent settings response from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
}
var agentSettings AgentSettings
if err = json.Unmarshal([]byte(settingsResponse.Settings), &agentSettings); err != nil {
return AgentSettings{}, bosherr.WrapErrorf(err, "Unmarshalling agent settings response from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
}
c.logger.Debug(httpClientLogTag, "Received agent settings from registry endpoint '%s', contents: '%s'", endpoint, httpBody)
return agentSettings, nil
}
// Update updates the agent settings for a given instance ID. If there are not already agent settings for the instance, it will create ones.
func (c HTTPClient) Update(instanceID string, agentSettings AgentSettings) error {
settingsJSON, err := json.Marshal(agentSettings)
if err != nil {
return bosherr.WrapErrorf(err, "Marshalling agent settings, contents: '%#v", agentSettings)
}
endpoint := fmt.Sprintf("%s/instances/%s/settings", c.options.EndpointWithCredentials(), instanceID)
c.logger.Debug(httpClientLogTag, "Updating registry endpoint '%s' with agent settings '%s'", endpoint, settingsJSON)
putPayload := bytes.NewReader(settingsJSON)
request, err := http.NewRequest("PUT", endpoint, putPayload)
if err != nil {
return bosherr.WrapErrorf(err, "Creating PUT request for registry endpoint '%s' with agent settings '%s'", endpoint, settingsJSON)
}
httpResponse, err := c.doRequest(request)
if err != nil {
return bosherr.WrapErrorf(err, "Updating registry endpoint '%s' with agent settings: '%s'", endpoint, settingsJSON)
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != http.StatusOK && httpResponse.StatusCode != http.StatusCreated {
return bosherr.Errorf("Received status code '%d' when updating registry endpoint '%s' with agent settings: '%s'", httpResponse.StatusCode, endpoint, settingsJSON)
}
c.logger.Debug(httpClientLogTag, "Updated registry endpoint '%s' with agent settings '%s'", endpoint, settingsJSON)
return nil
}
func (c HTTPClient) doRequest(request *http.Request) (httpResponse *http.Response, err error) {
httpClient, err := c.httpClient()
if err != nil {
return nil, bosherr.WrapErrorf(err, "Creating HTTP Client")
}
retryDelay := time.Duration(httpClientRetryDelay) * time.Second
for attempt := 0; attempt < httpClientMaxAttemps; attempt++ {
httpResponse, err = httpClient.Do(request)
if err == nil {
return httpResponse, nil
}
c.logger.Debug(httpClientLogTag, "Performing registry HTTP call #%d got error '%v'", attempt, err)
time.Sleep(retryDelay)
}
return nil, err
}
func (c HTTPClient) httpClient() (http.Client, error) {
httpClient := http.Client{}
if c.options.Protocol == "https" {
certificates, err := tls.LoadX509KeyPair(c.options.TLS.CertFile, c.options.TLS.KeyFile)
if err != nil {
return httpClient, bosherr.WrapError(err, "Loading X509 Key Pair")
}
certPool := x509.NewCertPool()
if c.options.TLS.CACertFile != "" {
caCert, err := ioutil.ReadFile(c.options.TLS.CACertFile)
if err != nil {
return httpClient, bosherr.WrapError(err, "Loading CA certificate")
}
if !certPool.AppendCertsFromPEM(caCert) {
return httpClient, bosherr.WrapError(err, "Invalid CA Certificate")
}
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{certificates},
InsecureSkipVerify: c.options.TLS.InsecureSkipVerify,
RootCAs: certPool,
}
httpClient.Transport = &http.Transport{TLSClientConfig: tlsConfig}
}
return httpClient, nil
}