mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Added NOMAD_HTTP_AUTH env var for basic auth
This commit is contained in:
30
api/api.go
30
api/api.go
@@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/go-cleanhttp"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user