-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 != "" { | ||
req.SetBasicAuth(c.Username, c.Password) | ||
} | ||
|
||
// Make the request | ||
resp, err := c.Client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -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 != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 andpassword
is not set? Possible to add in extra verification for this case?There was a problem hiding this comment.
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.