Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic authentication for HTTP remote state backend #11301

Merged
merged 1 commit into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions state/remote/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,40 @@ func httpFactory(conf map[string]string) (Client, error) {
}
}

return &HTTPClient{
ret := &HTTPClient{
URL: url,
Client: client,
}, nil
}
if username, ok := conf["username"]; ok && username != "" {
ret.Username = username
}
if password, ok := conf["password"]; ok && password != "" {
ret.Password = password
}
return ret, nil
}

// HTTPClient is a remote client that stores data in Consul or HTTP REST.
type HTTPClient struct {
URL *url.URL
Client *http.Client
URL *url.URL
Client *http.Client
Username string
Password string
}

func (c *HTTPClient) Get() (*Payload, error) {
resp, err := c.Client.Get(c.URL.String())
req, err := http.NewRequest("GET", c.URL.String(), nil)
if err != nil {
return nil, err
}

// Prepare the request
if c.Username != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if username is set and password is not set? Possible to add in extra verification for this case?

Copy link
Contributor Author

@syskill syskill Jan 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary. The empty string is the zero value, so in this case the HTTP client will just use an empty password, which is totally legit for HTTP basic auth.

req.SetBasicAuth(c.Username, c.Password)
}

// Make the request
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,6 +161,9 @@ func (c *HTTPClient) Put(data []byte) error {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-MD5", b64)
req.ContentLength = int64(len(data))
if c.Username != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

req.SetBasicAuth(c.Username, c.Password)
}

// Make the request
resp, err := c.Client.Do(req)
Expand All @@ -164,6 +187,11 @@ func (c *HTTPClient) Delete() error {
return fmt.Errorf("Failed to make HTTP request: %s", err)
}

// Prepare the request
if c.Username != "" {
req.SetBasicAuth(c.Username, c.Password)
}

// Make the request
resp, err := c.Client.Do(req)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/state/remote/http.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ data "terraform_remote_state" "foo" {
The following configuration options are supported:

* `address` - (Required) The address of the REST endpoint
* `username` - (Optional) The username for HTTP basic authentication
* `password` - (Optional) The password for HTTP basic authentication
* `skip_cert_verification` - (Optional) Whether to skip TLS verification.
Defaults to `false`.