Skip to content

Commit

Permalink
Merge pull request #1610 from kyhavlov/master
Browse files Browse the repository at this point in the history
Added NOMAD_HTTP_AUTH env var for basic auth
  • Loading branch information
dadgar authored Aug 17, 2016
2 parents 400e459 + 145312c commit dfe9357
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-cleanhttp"
Expand Down Expand Up @@ -74,6 +75,15 @@ type WriteMeta struct {
RequestTime time.Duration
}

// HttpBasicAuth is used to authenticate http client with HTTP Basic Authentication
type HttpBasicAuth struct {
// Username to use for HTTP Basic Authentication
Username string

// Password to use for HTTP Basic Authentication
Password string
}

// Config is used to configure the creation of a client
type Config struct {
// Address is the address of the Nomad agent
Expand All @@ -86,6 +96,9 @@ type Config struct {
// used if not provided.
HttpClient *http.Client

// HttpAuth is the auth info to use for http access.
HttpAuth *HttpBasicAuth

// WaitTime limits how long a Watch will block. If not provided,
// the agent default values will be used.
WaitTime time.Duration
Expand All @@ -100,6 +113,21 @@ func DefaultConfig() *Config {
if addr := os.Getenv("NOMAD_ADDR"); addr != "" {
config.Address = addr
}
if auth := os.Getenv("NOMAD_HTTP_AUTH"); auth != "" {
var username, password string
if strings.Contains(auth, ":") {
split := strings.SplitN(auth, ":", 2)
username = split[0]
password = split[1]
} else {
username = auth
}

config.HttpAuth = &HttpBasicAuth{
Username: username,
Password: password,
}
}
return config
}

Expand Down Expand Up @@ -211,6 +239,8 @@ func (r *request) toHTTP() (*http.Request, error) {
username := r.url.User.Username()
password, _ := r.url.User.Password()
req.SetBasicAuth(username, password)
} else if r.config.HttpAuth != nil {
req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password)
}

req.Header.Add("Accept-Encoding", "gzip")
Expand Down
13 changes: 13 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -90,15 +91,27 @@ func TestRequestTime(t *testing.T) {

func TestDefaultConfig_env(t *testing.T) {
url := "http://1.2.3.4:5678"
auth := []string{"nomaduser", "12345"}

os.Setenv("NOMAD_ADDR", url)
defer os.Setenv("NOMAD_ADDR", "")

os.Setenv("NOMAD_HTTP_AUTH", strings.Join(auth, ":"))
defer os.Setenv("NOMAD_HTTP_AUTH", "")

config := DefaultConfig()

if config.Address != url {
t.Errorf("expected %q to be %q", config.Address, url)
}

if config.HttpAuth.Username != auth[0] {
t.Errorf("expected %q to be %q", config.HttpAuth.Username, auth[0])
}

if config.HttpAuth.Password != auth[1] {
t.Errorf("expected %q to be %q", config.HttpAuth.Password, auth[1])
}
}

func TestSetQueryOptions(t *testing.T) {
Expand Down

0 comments on commit dfe9357

Please sign in to comment.