Skip to content

Commit

Permalink
Add backwards compat support for API env vars (#7135)
Browse files Browse the repository at this point in the history
Several env vars got renamed in
#6306. This re-adds support for
those.

Indirectly addresses
hashicorp/consul-template#1233 although they
should still update to the new values.
  • Loading branch information
jefferai authored Jul 17, 2019
1 parent f9268b8 commit 61a1729
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const EnvVaultToken = "VAULT_TOKEN"
const EnvVaultMFA = "VAULT_MFA"
const EnvRateLimit = "VAULT_RATE_LIMIT"

// Deprecated values
const EnvVaultAgentAddress = "VAULT_AGENT_ADDR"
const EnvVaultInsecure = "VAULT_SKIP_VERIFY"

// WrappingLookupFunc is a function that, given an HTTP verb and a path,
// returns an optional string duration to be used for response wrapping (e.g.
// "15s", or simply "15"). The path will not begin with "/v1/" or "v1/" or "/",
Expand Down Expand Up @@ -246,6 +250,8 @@ func (c *Config) ReadEnvironment() error {
}
if v := os.Getenv(EnvVaultAgentAddr); v != "" {
envAgentAddress = v
} else if v := os.Getenv(EnvVaultAgentAddress); v != "" {
envAgentAddress = v
}
if v := os.Getenv(EnvVaultMaxRetries); v != "" {
maxRetries, err := strconv.ParseUint(v, 10, 32)
Expand Down Expand Up @@ -286,7 +292,14 @@ func (c *Config) ReadEnvironment() error {
if err != nil {
return fmt.Errorf("could not parse VAULT_SKIP_VERIFY")
}
} else if v := os.Getenv(EnvVaultInsecure); v != "" {
var err error
envInsecure, err = strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("could not parse VAULT_INSECURE")
}
}

if v := os.Getenv(EnvVaultTLSServerName); v != "" {
envTLSServerName = v
}
Expand Down
19 changes: 18 additions & 1 deletion api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package api

import (
"bytes"
"github.com/hashicorp/vault/sdk/helper/consts"
"io"
"net/http"
"os"
"strings"
"testing"

"github.com/hashicorp/vault/sdk/helper/consts"
)

func init() {
Expand Down Expand Up @@ -196,6 +197,22 @@ func TestClientEnvSettings(t *testing.T) {
}
}

func TestClientDeprecatedEnvSettings(t *testing.T) {
oldInsecure := os.Getenv(EnvVaultInsecure)
os.Setenv(EnvVaultInsecure, "true")
defer os.Setenv(EnvVaultInsecure, oldInsecure)

config := DefaultConfig()
if err := config.ReadEnvironment(); err != nil {
t.Fatalf("error reading environment: %v", err)
}

tlsConfig := config.HttpClient.Transport.(*http.Transport).TLSClientConfig
if tlsConfig.InsecureSkipVerify != true {
t.Fatalf("bad: %v", tlsConfig.InsecureSkipVerify)
}
}

func TestClientEnvNamespace(t *testing.T) {
var seenNamespace string
handler := func(w http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 61a1729

Please sign in to comment.