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

feat(authentication): add basic authentication via address and env-vars (#1415) #1416

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 32 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ type Config struct {
// WaitTime limits how long a Watch will block. If not provided,
// the agent default values will be used.
WaitTime time.Duration

// basic authentication user and password
bauser string
bapass string
}

// DefaultConfig returns a default configuration for the client
Expand All @@ -97,6 +101,12 @@ func DefaultConfig() *Config {
if addr := os.Getenv("NOMAD_ADDR"); addr != "" {
config.Address = addr
}
if bauser := os.Getenv("NOMAD_BASIC_USER"); bauser != "" {
config.bauser = bauser
}
if bapass := os.Getenv("NOMAD_BASIC_PASS"); bapass != "" {
config.bapass = bapass
}
return config
}

Expand Down Expand Up @@ -133,12 +143,15 @@ func (c *Client) SetRegion(region string) {

// request is used to help build up a request
type request struct {
config *Config
method string
url *url.URL
params url.Values
body io.Reader
obj interface{}
config *Config
method string
url *url.URL
params url.Values
body io.Reader
obj interface{}
Userinfo *url.Userinfo
bauser string
bapass string
}

// setQueryOptions is used to annotate the request with
Expand Down Expand Up @@ -200,6 +213,15 @@ func (r *request) toHTTP() (*http.Request, error) {
return nil, err
}

if r.bauser != "" && r.bapass != "" {
req.SetBasicAuth(r.bauser, r.bapass)
}

if r.Userinfo != nil {
pwd, _ := r.Userinfo.Password()
req.SetBasicAuth(r.Userinfo.Username(), pwd)
}

req.Header.Add("Accept-Encoding", "gzip")
req.URL.Host = r.url.Host
req.URL.Scheme = r.url.Scheme
Expand All @@ -219,7 +241,10 @@ func (c *Client) newRequest(method, path string) *request {
Host: base.Host,
Path: u.Path,
},
params: make(map[string][]string),
Userinfo: base.User,
params: make(map[string][]string),
bauser: c.config.bauser,
bapass: c.config.bapass,
}
if c.config.Region != "" {
r.params.Set("region", c.config.Region)
Expand Down