mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
Read environment variables for VAULT_HTTP_ADDR and VAULT_TOKEN
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
vaultHttp "github.com/hashicorp/vault/http"
|
||||
@@ -39,6 +40,10 @@ func DefaultConfig() *Config {
|
||||
HttpClient: &http.Client{},
|
||||
}
|
||||
|
||||
if addr := os.Getenv("VAULT_HTTP_ADDR"); addr != "" {
|
||||
config.Address = addr
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
@@ -75,10 +80,16 @@ func NewClient(c *Config) (*Client, error) {
|
||||
return errRedirect
|
||||
}
|
||||
|
||||
return &Client{
|
||||
client := &Client{
|
||||
addr: u,
|
||||
config: c,
|
||||
}, nil
|
||||
}
|
||||
|
||||
if token := os.Getenv("VAULT_TOKEN"); token != "" {
|
||||
client.SetToken(token)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Token returns the access token being used by this client. It will
|
||||
|
||||
@@ -4,12 +4,41 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
vaultHttp "github.com/hashicorp/vault/http"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Ensure our special envvars are not present
|
||||
os.Setenv("VAULT_HTTP_ADDR", "")
|
||||
os.Setenv("VAULT_TOKEN", "")
|
||||
}
|
||||
|
||||
func TestDefaultConfig_envvar(t *testing.T) {
|
||||
os.Setenv("VAULT_HTTP_ADDR", "https://vault.mycompany.com")
|
||||
defer os.Setenv("VAULT_HTTP_ADDR", "")
|
||||
|
||||
config := DefaultConfig()
|
||||
if config.Address != "https://vault.mycompany.com" {
|
||||
t.Fatalf("bad: %s", config.Address)
|
||||
}
|
||||
|
||||
os.Setenv("VAULT_TOKEN", "testing")
|
||||
defer os.Setenv("VAULT_TOKEN", "")
|
||||
|
||||
client, err := NewClient(config)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if token := client.Token(); token != "testing" {
|
||||
t.Fatalf("bad: %s", token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientToken(t *testing.T) {
|
||||
tokenValue := "foo"
|
||||
handler := func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user