state/remote: more canonical Go for skip TLS verify
/cc @LeftyBC - Hope this helps! Some basic point: * Idiomatic Go is to use thisCasingStyle and not this_casing_style * Less repetitive to just create an http.Client once and re-use, also more flexible for later. * The empty `if ok {}` was kind of awkward, replace with proper check
This commit is contained in:
parent
6649658d62
commit
76d920f504
|
@ -26,41 +26,38 @@ func httpFactory(conf map[string]string) (Client, error) {
|
||||||
return nil, fmt.Errorf("address must be HTTP or HTTPS")
|
return nil, fmt.Errorf("address must be HTTP or HTTPS")
|
||||||
}
|
}
|
||||||
|
|
||||||
skip_cert_verification := false
|
client := &http.Client{}
|
||||||
skip_cert_config_string, ok := conf["skip_cert_verification"]
|
if skipRaw, ok := conf["skip_cert_verification"]; ok {
|
||||||
if !ok {
|
skip, err := strconv.ParseBool(skipRaw)
|
||||||
// config wasn't specified
|
|
||||||
// use the default - check cert validity
|
|
||||||
} else {
|
|
||||||
skip_cert_verification, err = strconv.ParseBool(skip_cert_config_string)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("skip_cert_verification must be boolean (true/false)")
|
return nil, fmt.Errorf("skip_cert_verification must be boolean")
|
||||||
|
}
|
||||||
|
if skip {
|
||||||
|
// Replace the client with one that ignores TLS verification
|
||||||
|
client = &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// skip_cert_verification should now be set to true or false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HTTPClient{
|
return &HTTPClient{
|
||||||
URL: url,
|
URL: url,
|
||||||
skipCertVerify: skip_cert_verification,
|
Client: client,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPClient is a remote client that stores data in Consul or HTTP REST.
|
// HTTPClient is a remote client that stores data in Consul or HTTP REST.
|
||||||
type HTTPClient struct {
|
type HTTPClient struct {
|
||||||
URL *url.URL
|
URL *url.URL
|
||||||
skipCertVerify bool
|
Client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HTTPClient) Get() (*Payload, error) {
|
func (c *HTTPClient) Get() (*Payload, error) {
|
||||||
|
resp, err := c.Client.Get(c.URL.String())
|
||||||
// Build the HTTP client
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.skipCertVerify},
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{Transport: tr}
|
|
||||||
|
|
||||||
resp, err := client.Get(c.URL.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -135,13 +132,6 @@ func (c *HTTPClient) Put(data []byte) error {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Build the HTTP client and request
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.skipCertVerify},
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{Transport: tr}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", base.String(), bytes.NewReader(data))
|
req, err := http.NewRequest("POST", base.String(), bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to make HTTP request: %s", err)
|
return fmt.Errorf("Failed to make HTTP request: %s", err)
|
||||||
|
@ -153,7 +143,7 @@ func (c *HTTPClient) Put(data []byte) error {
|
||||||
req.ContentLength = int64(len(data))
|
req.ContentLength = int64(len(data))
|
||||||
|
|
||||||
// Make the request
|
// Make the request
|
||||||
resp, err := client.Do(req)
|
resp, err := c.Client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to upload state: %v", err)
|
return fmt.Errorf("Failed to upload state: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -169,19 +159,13 @@ func (c *HTTPClient) Put(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HTTPClient) Delete() error {
|
func (c *HTTPClient) Delete() error {
|
||||||
// Build the HTTP request
|
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.skipCertVerify},
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{Transport: tr}
|
|
||||||
req, err := http.NewRequest("DELETE", c.URL.String(), nil)
|
req, err := http.NewRequest("DELETE", c.URL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to make HTTP request: %s", err)
|
return fmt.Errorf("Failed to make HTTP request: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the request
|
// Make the request
|
||||||
resp, err := client.Do(req)
|
resp, err := c.Client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to delete state: %s", err)
|
return fmt.Errorf("Failed to delete state: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ func TestHTTPClient(t *testing.T) {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &HTTPClient{URL: url}
|
client := &HTTPClient{URL: url, Client: http.DefaultClient}
|
||||||
testClient(t, client)
|
testClient(t, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue