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

Added NOMAD_HTTP_AUTH env var for basic auth #1610

Merged
merged 1 commit into from
Aug 17, 2016
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
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