2015-03-04 21:10:10 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-11-03 19:21:14 +00:00
|
|
|
"crypto/tls"
|
2015-04-20 18:30:35 +00:00
|
|
|
"fmt"
|
2015-03-04 21:10:10 +00:00
|
|
|
"net/http"
|
2015-03-09 18:38:50 +00:00
|
|
|
"net/url"
|
2015-04-23 15:43:20 +00:00
|
|
|
"os"
|
2017-05-19 12:34:17 +00:00
|
|
|
"path"
|
2015-11-03 19:21:14 +00:00
|
|
|
"strconv"
|
2015-05-02 20:08:35 +00:00
|
|
|
"strings"
|
2015-09-03 17:34:45 +00:00
|
|
|
"sync"
|
2015-10-08 07:44:00 +00:00
|
|
|
"time"
|
2015-10-22 18:37:12 +00:00
|
|
|
|
2017-02-27 17:49:35 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
|
2015-10-22 18:37:12 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2016-05-03 16:23:25 +00:00
|
|
|
"github.com/hashicorp/go-rootcerts"
|
2017-07-18 13:48:31 +00:00
|
|
|
"github.com/hashicorp/vault/helper/parseutil"
|
2016-07-11 21:37:46 +00:00
|
|
|
"github.com/sethgrid/pester"
|
2015-03-04 21:10:10 +00:00
|
|
|
)
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
const EnvVaultAddress = "VAULT_ADDR"
|
|
|
|
const EnvVaultCACert = "VAULT_CACERT"
|
|
|
|
const EnvVaultCAPath = "VAULT_CAPATH"
|
|
|
|
const EnvVaultClientCert = "VAULT_CLIENT_CERT"
|
|
|
|
const EnvVaultClientKey = "VAULT_CLIENT_KEY"
|
2017-07-18 13:48:31 +00:00
|
|
|
const EnvVaultClientTimeout = "VAULT_CLIENT_TIMEOUT"
|
2015-11-03 19:21:14 +00:00
|
|
|
const EnvVaultInsecure = "VAULT_SKIP_VERIFY"
|
2016-02-24 15:50:10 +00:00
|
|
|
const EnvVaultTLSServerName = "VAULT_TLS_SERVER_NAME"
|
2016-05-02 05:58:58 +00:00
|
|
|
const EnvVaultWrapTTL = "VAULT_WRAP_TTL"
|
2016-07-11 21:37:46 +00:00
|
|
|
const EnvVaultMaxRetries = "VAULT_MAX_RETRIES"
|
2017-02-27 23:36:21 +00:00
|
|
|
const EnvVaultToken = "VAULT_TOKEN"
|
2015-11-03 19:21:14 +00:00
|
|
|
|
2016-05-19 15:47:18 +00:00
|
|
|
// 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 "/",
|
|
|
|
// however, end-of-path forward slashes are not trimmed, so must match your
|
|
|
|
// called path precisely.
|
2016-05-16 20:11:33 +00:00
|
|
|
type WrappingLookupFunc func(operation, path string) string
|
|
|
|
|
2015-03-04 21:10:10 +00:00
|
|
|
// Config is used to configure the creation of the client.
|
|
|
|
type Config struct {
|
|
|
|
// Address is the address of the Vault server. This should be a complete
|
|
|
|
// URL such as "http://vault.example.com". If you need a custom SSL
|
|
|
|
// cert or want to enable insecure mode, you need to specify a custom
|
|
|
|
// HttpClient.
|
|
|
|
Address string
|
|
|
|
|
2015-09-03 17:47:20 +00:00
|
|
|
// HttpClient is the HTTP client to use, which will currently always have the
|
|
|
|
// same values as http.DefaultClient. This is used to control redirect behavior.
|
2015-03-04 21:10:10 +00:00
|
|
|
HttpClient *http.Client
|
2015-10-15 20:09:45 +00:00
|
|
|
|
|
|
|
redirectSetup sync.Once
|
2016-07-06 20:42:34 +00:00
|
|
|
|
2016-07-11 21:37:46 +00:00
|
|
|
// MaxRetries controls the maximum number of times to retry when a 5xx error
|
2016-11-16 19:08:09 +00:00
|
|
|
// occurs. Set to 0 or less to disable retrying. Defaults to 0.
|
2016-07-11 21:37:46 +00:00
|
|
|
MaxRetries int
|
2017-07-18 13:48:31 +00:00
|
|
|
|
|
|
|
// Timeout is for setting custom timeout parameter in the HttpClient
|
|
|
|
Timeout time.Duration
|
2015-03-04 21:10:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:17:45 +00:00
|
|
|
// TLSConfig contains the parameters needed to configure TLS on the HTTP client
|
|
|
|
// used to communicate with Vault.
|
|
|
|
type TLSConfig struct {
|
|
|
|
// CACert is the path to a PEM-encoded CA cert file to use to verify the
|
|
|
|
// Vault server SSL certificate.
|
|
|
|
CACert string
|
|
|
|
|
|
|
|
// CAPath is the path to a directory of PEM-encoded CA cert files to verify
|
|
|
|
// the Vault server SSL certificate.
|
|
|
|
CAPath string
|
|
|
|
|
|
|
|
// ClientCert is the path to the certificate for Vault communication
|
|
|
|
ClientCert string
|
|
|
|
|
|
|
|
// ClientKey is the path to the private key for Vault communication
|
|
|
|
ClientKey string
|
|
|
|
|
|
|
|
// TLSServerName, if set, is used to set the SNI host when connecting via
|
|
|
|
// TLS.
|
|
|
|
TLSServerName string
|
|
|
|
|
|
|
|
// Insecure enables or disables SSL verification
|
|
|
|
Insecure bool
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:10:10 +00:00
|
|
|
// DefaultConfig returns a default configuration for the client. It is
|
|
|
|
// safe to modify the return value of this function.
|
2015-04-23 15:45:37 +00:00
|
|
|
//
|
|
|
|
// The default Address is https://127.0.0.1:8200, but this can be overridden by
|
2015-04-23 15:46:22 +00:00
|
|
|
// setting the `VAULT_ADDR` environment variable.
|
2015-04-23 15:13:52 +00:00
|
|
|
func DefaultConfig() *Config {
|
|
|
|
config := &Config{
|
2017-02-27 17:49:35 +00:00
|
|
|
Address: "https://127.0.0.1:8200",
|
2015-10-22 18:37:12 +00:00
|
|
|
HttpClient: cleanhttp.DefaultClient(),
|
2015-08-22 00:36:19 +00:00
|
|
|
}
|
2015-10-22 18:37:12 +00:00
|
|
|
config.HttpClient.Timeout = time.Second * 60
|
2015-11-03 19:21:14 +00:00
|
|
|
transport := config.HttpClient.Transport.(*http.Transport)
|
|
|
|
transport.TLSHandshakeTimeout = 10 * time.Second
|
|
|
|
transport.TLSClientConfig = &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2015-08-22 00:36:19 +00:00
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
if v := os.Getenv(EnvVaultAddress); v != "" {
|
|
|
|
config.Address = v
|
2015-04-23 15:43:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 21:10:10 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:17:45 +00:00
|
|
|
// ConfigureTLS takes a set of TLS configurations and applies those to the the HTTP client.
|
|
|
|
func (c *Config) ConfigureTLS(t *TLSConfig) error {
|
2016-07-28 00:26:26 +00:00
|
|
|
if c.HttpClient == nil {
|
2017-02-06 22:47:56 +00:00
|
|
|
c.HttpClient = DefaultConfig().HttpClient
|
2016-07-28 00:26:26 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:17:45 +00:00
|
|
|
var clientCert tls.Certificate
|
2016-07-28 00:26:26 +00:00
|
|
|
foundClientCert := false
|
2016-08-02 20:17:45 +00:00
|
|
|
if t.CACert != "" || t.CAPath != "" || t.ClientCert != "" || t.ClientKey != "" || t.Insecure {
|
|
|
|
if t.ClientCert != "" && t.ClientKey != "" {
|
2016-07-28 00:26:26 +00:00
|
|
|
var err error
|
2016-08-02 20:17:45 +00:00
|
|
|
clientCert, err = tls.LoadX509KeyPair(t.ClientCert, t.ClientKey)
|
2016-07-28 00:26:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
foundClientCert = true
|
2016-08-02 20:17:45 +00:00
|
|
|
} else if t.ClientCert != "" || t.ClientKey != "" {
|
2016-07-28 00:26:26 +00:00
|
|
|
return fmt.Errorf("Both client cert and client key must be provided")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clientTLSConfig := c.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
|
|
|
rootConfig := &rootcerts.Config{
|
2016-08-02 20:17:45 +00:00
|
|
|
CAFile: t.CACert,
|
|
|
|
CAPath: t.CAPath,
|
2016-07-28 00:26:26 +00:00
|
|
|
}
|
|
|
|
if err := rootcerts.ConfigureTLS(clientTLSConfig, rootConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:17:45 +00:00
|
|
|
clientTLSConfig.InsecureSkipVerify = t.Insecure
|
2016-07-28 00:26:26 +00:00
|
|
|
|
|
|
|
if foundClientCert {
|
2016-08-02 20:17:45 +00:00
|
|
|
clientTLSConfig.Certificates = []tls.Certificate{clientCert}
|
2016-07-28 00:26:26 +00:00
|
|
|
}
|
2016-08-02 20:17:45 +00:00
|
|
|
if t.TLSServerName != "" {
|
|
|
|
clientTLSConfig.ServerName = t.TLSServerName
|
2016-07-28 00:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
// ReadEnvironment reads configuration information from the
|
|
|
|
// environment. If there is an error, no configuration value
|
|
|
|
// is updated.
|
|
|
|
func (c *Config) ReadEnvironment() error {
|
|
|
|
var envAddress string
|
|
|
|
var envCACert string
|
|
|
|
var envCAPath string
|
|
|
|
var envClientCert string
|
|
|
|
var envClientKey string
|
2017-07-18 13:48:31 +00:00
|
|
|
var envClientTimeout time.Duration
|
2016-08-02 20:17:45 +00:00
|
|
|
var envInsecure bool
|
2016-02-24 15:50:10 +00:00
|
|
|
var envTLSServerName string
|
2016-07-11 21:37:46 +00:00
|
|
|
var envMaxRetries *uint64
|
2016-07-06 20:42:34 +00:00
|
|
|
|
2016-07-28 00:26:26 +00:00
|
|
|
// Parse the environment variables
|
2015-11-03 19:21:14 +00:00
|
|
|
if v := os.Getenv(EnvVaultAddress); v != "" {
|
|
|
|
envAddress = v
|
|
|
|
}
|
2016-07-11 21:37:46 +00:00
|
|
|
if v := os.Getenv(EnvVaultMaxRetries); v != "" {
|
|
|
|
maxRetries, err := strconv.ParseUint(v, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-07-06 20:42:34 +00:00
|
|
|
}
|
2016-07-11 21:37:46 +00:00
|
|
|
envMaxRetries = &maxRetries
|
2016-07-06 20:42:34 +00:00
|
|
|
}
|
2015-11-03 19:21:14 +00:00
|
|
|
if v := os.Getenv(EnvVaultCACert); v != "" {
|
|
|
|
envCACert = v
|
|
|
|
}
|
|
|
|
if v := os.Getenv(EnvVaultCAPath); v != "" {
|
|
|
|
envCAPath = v
|
|
|
|
}
|
|
|
|
if v := os.Getenv(EnvVaultClientCert); v != "" {
|
|
|
|
envClientCert = v
|
|
|
|
}
|
|
|
|
if v := os.Getenv(EnvVaultClientKey); v != "" {
|
|
|
|
envClientKey = v
|
|
|
|
}
|
2017-07-18 13:48:31 +00:00
|
|
|
if t := os.Getenv(EnvVaultClientTimeout); t != "" {
|
|
|
|
clientTimeout, err := parseutil.ParseDurationSecond(t)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not parse %s", EnvVaultClientTimeout)
|
|
|
|
}
|
|
|
|
envClientTimeout = clientTimeout
|
|
|
|
}
|
2015-11-03 19:21:14 +00:00
|
|
|
if v := os.Getenv(EnvVaultInsecure); v != "" {
|
|
|
|
var err error
|
|
|
|
envInsecure, err = strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not parse VAULT_SKIP_VERIFY")
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 15:50:10 +00:00
|
|
|
if v := os.Getenv(EnvVaultTLSServerName); v != "" {
|
|
|
|
envTLSServerName = v
|
|
|
|
}
|
2015-11-03 19:21:14 +00:00
|
|
|
|
2016-07-28 00:26:26 +00:00
|
|
|
// Configure the HTTP clients TLS configuration.
|
2016-08-02 20:17:45 +00:00
|
|
|
t := &TLSConfig{
|
|
|
|
CACert: envCACert,
|
|
|
|
CAPath: envCAPath,
|
|
|
|
ClientCert: envClientCert,
|
|
|
|
ClientKey: envClientKey,
|
|
|
|
TLSServerName: envTLSServerName,
|
|
|
|
Insecure: envInsecure,
|
|
|
|
}
|
|
|
|
if err := c.ConfigureTLS(t); err != nil {
|
2016-07-06 20:42:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
if envAddress != "" {
|
|
|
|
c.Address = envAddress
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:37:46 +00:00
|
|
|
if envMaxRetries != nil {
|
2016-07-11 21:57:14 +00:00
|
|
|
c.MaxRetries = int(*envMaxRetries) + 1
|
2016-07-06 20:42:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 13:48:31 +00:00
|
|
|
if envClientTimeout != 0 {
|
|
|
|
c.Timeout = envClientTimeout
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:21:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:10:10 +00:00
|
|
|
// Client is the client to the Vault API. Create a client with
|
|
|
|
// NewClient.
|
|
|
|
type Client struct {
|
2016-05-16 20:11:33 +00:00
|
|
|
addr *url.URL
|
|
|
|
config *Config
|
|
|
|
token string
|
|
|
|
wrappingLookupFunc WrappingLookupFunc
|
2015-03-04 21:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new client for the given configuration.
|
2015-04-23 15:45:37 +00:00
|
|
|
//
|
|
|
|
// If the environment variable `VAULT_TOKEN` is present, the token will be
|
|
|
|
// automatically added to the client. Otherwise, you must manually call
|
|
|
|
// `SetToken()`.
|
2015-04-23 15:13:52 +00:00
|
|
|
func NewClient(c *Config) (*Client, error) {
|
2016-08-12 15:37:13 +00:00
|
|
|
if c == nil {
|
|
|
|
c = DefaultConfig()
|
|
|
|
if err := c.ReadEnvironment(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading environment: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:38:50 +00:00
|
|
|
u, err := url.Parse(c.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:09:45 +00:00
|
|
|
if c.HttpClient == nil {
|
|
|
|
c.HttpClient = DefaultConfig().HttpClient
|
2015-04-20 18:30:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 17:49:35 +00:00
|
|
|
tp := c.HttpClient.Transport.(*http.Transport)
|
|
|
|
if err := http2.ConfigureTransport(tp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:09:45 +00:00
|
|
|
redirFunc := func() {
|
|
|
|
// Ensure redirects are not automatically followed
|
|
|
|
// Note that this is sane for the API client as it has its own
|
|
|
|
// redirect handling logic (and thus also for command/meta),
|
|
|
|
// but in e.g. http_test actual redirect handling is necessary
|
|
|
|
c.HttpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
2016-08-12 19:13:42 +00:00
|
|
|
// Returning this value causes the Go net library to not close the
|
2017-03-01 17:23:54 +00:00
|
|
|
// response body and to nil out the error. Otherwise pester tries
|
2016-08-12 19:13:42 +00:00
|
|
|
// three times on every redirect because it sees an error from this
|
2017-03-01 17:23:54 +00:00
|
|
|
// function (to prevent redirects) passing through to it.
|
2016-08-12 19:13:42 +00:00
|
|
|
return http.ErrUseLastResponse
|
2015-10-15 20:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.redirectSetup.Do(redirFunc)
|
|
|
|
|
2015-04-23 15:43:20 +00:00
|
|
|
client := &Client{
|
2015-03-09 18:38:50 +00:00
|
|
|
addr: u,
|
2015-03-04 21:10:10 +00:00
|
|
|
config: c,
|
2015-04-23 15:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 23:36:21 +00:00
|
|
|
if token := os.Getenv(EnvVaultToken); token != "" {
|
2015-04-23 15:43:20 +00:00
|
|
|
client.SetToken(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
2015-03-04 21:10:10 +00:00
|
|
|
}
|
2015-03-09 18:38:50 +00:00
|
|
|
|
2016-08-12 15:37:13 +00:00
|
|
|
// Sets the address of Vault in the client. The format of address should be
|
|
|
|
// "<Scheme>://<Host>:<Port>". Setting this on a client will override the
|
|
|
|
// value of VAULT_ADDR environment variable.
|
|
|
|
func (c *Client) SetAddress(addr string) error {
|
|
|
|
var err error
|
|
|
|
if c.addr, err = url.Parse(addr); err != nil {
|
|
|
|
return fmt.Errorf("failed to set address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:24:57 +00:00
|
|
|
// Address returns the Vault URL the client is configured to connect to
|
|
|
|
func (c *Client) Address() string {
|
|
|
|
return c.addr.String()
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:23:54 +00:00
|
|
|
// SetMaxRetries sets the number of retries that will be used in the case of certain errors
|
|
|
|
func (c *Client) SetMaxRetries(retries int) {
|
|
|
|
c.config.MaxRetries = retries
|
|
|
|
}
|
|
|
|
|
2017-07-18 13:48:31 +00:00
|
|
|
// SetClientTimeout sets the client request timeout
|
|
|
|
func (c *Client) SetClientTimeout(timeout time.Duration) {
|
|
|
|
c.config.Timeout = timeout
|
|
|
|
}
|
|
|
|
|
2016-05-16 20:11:33 +00:00
|
|
|
// SetWrappingLookupFunc sets a lookup function that returns desired wrap TTLs
|
|
|
|
// for a given operation and path
|
|
|
|
func (c *Client) SetWrappingLookupFunc(lookupFunc WrappingLookupFunc) {
|
|
|
|
c.wrappingLookupFunc = lookupFunc
|
|
|
|
}
|
|
|
|
|
2015-03-11 22:42:08 +00:00
|
|
|
// Token returns the access token being used by this client. It will
|
|
|
|
// return the empty string if there is no token set.
|
|
|
|
func (c *Client) Token() string {
|
2015-08-22 00:36:19 +00:00
|
|
|
return c.token
|
2015-03-11 22:42:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 04:20:23 +00:00
|
|
|
// SetToken sets the token directly. This won't perform any auth
|
2015-09-03 14:36:59 +00:00
|
|
|
// verification, it simply sets the token properly for future requests.
|
2015-03-31 04:20:23 +00:00
|
|
|
func (c *Client) SetToken(v string) {
|
2015-08-22 00:36:19 +00:00
|
|
|
c.token = v
|
2015-03-31 04:20:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 14:36:59 +00:00
|
|
|
// ClearToken deletes the token if it is set or does nothing otherwise.
|
2015-03-11 22:42:08 +00:00
|
|
|
func (c *Client) ClearToken() {
|
2015-08-22 00:36:19 +00:00
|
|
|
c.token = ""
|
2015-03-11 22:42:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 03:08:15 +00:00
|
|
|
// Clone creates a copy of this client.
|
|
|
|
func (c *Client) Clone() (*Client, error) {
|
|
|
|
return NewClient(c.config)
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:38:50 +00:00
|
|
|
// NewRequest creates a new raw request object to query the Vault server
|
|
|
|
// configured for this client. This is an advanced method and generally
|
|
|
|
// doesn't need to be called externally.
|
2017-04-13 18:06:38 +00:00
|
|
|
func (c *Client) NewRequest(method, requestPath string) *Request {
|
2015-08-22 00:36:19 +00:00
|
|
|
req := &Request{
|
2015-03-09 18:38:50 +00:00
|
|
|
Method: method,
|
|
|
|
URL: &url.URL{
|
2017-03-10 12:19:23 +00:00
|
|
|
User: c.addr.User,
|
2015-03-09 18:38:50 +00:00
|
|
|
Scheme: c.addr.Scheme,
|
|
|
|
Host: c.addr.Host,
|
2017-04-13 18:06:38 +00:00
|
|
|
Path: path.Join(c.addr.Path, requestPath),
|
2015-03-09 18:38:50 +00:00
|
|
|
},
|
2015-08-22 00:36:19 +00:00
|
|
|
ClientToken: c.token,
|
|
|
|
Params: make(map[string][]string),
|
2015-03-09 18:38:50 +00:00
|
|
|
}
|
2015-08-22 00:36:19 +00:00
|
|
|
|
2016-09-29 04:01:28 +00:00
|
|
|
var lookupPath string
|
|
|
|
switch {
|
2017-04-13 18:06:38 +00:00
|
|
|
case strings.HasPrefix(requestPath, "/v1/"):
|
|
|
|
lookupPath = strings.TrimPrefix(requestPath, "/v1/")
|
|
|
|
case strings.HasPrefix(requestPath, "v1/"):
|
|
|
|
lookupPath = strings.TrimPrefix(requestPath, "v1/")
|
2016-09-29 04:01:28 +00:00
|
|
|
default:
|
2017-04-13 18:06:38 +00:00
|
|
|
lookupPath = requestPath
|
2016-09-29 04:01:28 +00:00
|
|
|
}
|
2016-05-16 20:11:33 +00:00
|
|
|
if c.wrappingLookupFunc != nil {
|
2016-05-19 15:47:18 +00:00
|
|
|
req.WrapTTL = c.wrappingLookupFunc(method, lookupPath)
|
2016-09-29 04:01:28 +00:00
|
|
|
} else {
|
|
|
|
req.WrapTTL = DefaultWrappingLookupFunc(method, lookupPath)
|
2016-05-16 20:11:33 +00:00
|
|
|
}
|
2017-07-18 13:48:31 +00:00
|
|
|
if c.config.Timeout != 0 {
|
|
|
|
c.config.HttpClient.Timeout = c.config.Timeout
|
|
|
|
}
|
2016-05-16 20:11:33 +00:00
|
|
|
|
2015-08-22 00:36:19 +00:00
|
|
|
return req
|
2015-03-09 18:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RawRequest performs the raw request given. This request may be against
|
|
|
|
// a Vault server not configured with this client. This is an advanced operation
|
|
|
|
// that generally won't need to be called externally.
|
2015-03-11 18:33:20 +00:00
|
|
|
func (c *Client) RawRequest(r *Request) (*Response, error) {
|
2015-04-20 18:30:35 +00:00
|
|
|
redirectCount := 0
|
|
|
|
START:
|
2015-03-09 18:38:50 +00:00
|
|
|
req, err := r.ToHTTP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:37:46 +00:00
|
|
|
client := pester.NewExtendedClient(c.config.HttpClient)
|
|
|
|
client.Backoff = pester.LinearJitterBackoff
|
|
|
|
client.MaxRetries = c.config.MaxRetries
|
2016-07-06 20:42:34 +00:00
|
|
|
|
2015-04-07 18:15:20 +00:00
|
|
|
var result *Response
|
2016-07-06 20:42:34 +00:00
|
|
|
resp, err := client.Do(req)
|
2015-04-07 18:15:20 +00:00
|
|
|
if resp != nil {
|
|
|
|
result = &Response{Response: resp}
|
|
|
|
}
|
2015-04-20 18:30:35 +00:00
|
|
|
if err != nil {
|
2016-08-12 19:13:42 +00:00
|
|
|
if strings.Contains(err.Error(), "tls: oversized") {
|
2015-05-02 20:08:35 +00:00
|
|
|
err = fmt.Errorf(
|
|
|
|
"%s\n\n"+
|
|
|
|
"This error usually means that the server is running with TLS disabled\n"+
|
|
|
|
"but the client is configured to use TLS. Please either enable TLS\n"+
|
2015-05-14 14:33:38 +00:00
|
|
|
"on the server or run the client with -address set to an address\n"+
|
|
|
|
"that uses the http protocol:\n\n"+
|
|
|
|
" vault <command> -address http://<address>\n\n"+
|
|
|
|
"You can also set the VAULT_ADDR environment variable:\n\n\n"+
|
|
|
|
" VAULT_ADDR=http://<address> vault <command>\n\n"+
|
|
|
|
"where <address> is replaced by the actual address to the server.",
|
2015-05-02 20:08:35 +00:00
|
|
|
err)
|
2015-04-20 18:30:35 +00:00
|
|
|
}
|
2015-04-07 18:15:20 +00:00
|
|
|
return result, err
|
2015-03-09 18:38:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 18:30:35 +00:00
|
|
|
// Check for a redirect, only allowing for a single redirect
|
2015-10-09 21:11:31 +00:00
|
|
|
if (resp.StatusCode == 301 || resp.StatusCode == 302 || resp.StatusCode == 307) && redirectCount == 0 {
|
2015-04-20 18:30:35 +00:00
|
|
|
// Parse the updated location
|
|
|
|
respLoc, err := resp.Location()
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a protocol downgrade doesn't happen
|
|
|
|
if req.URL.Scheme == "https" && respLoc.Scheme != "https" {
|
|
|
|
return result, fmt.Errorf("redirect would cause protocol downgrade")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the request
|
|
|
|
r.URL = respLoc
|
|
|
|
|
|
|
|
// Reset the request body if any
|
|
|
|
if err := r.ResetJSONBody(); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry the request
|
|
|
|
redirectCount++
|
|
|
|
goto START
|
|
|
|
}
|
|
|
|
|
2015-03-11 18:46:07 +00:00
|
|
|
if err := result.Error(); err != nil {
|
2015-04-07 18:15:20 +00:00
|
|
|
return result, err
|
2015-03-11 18:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2015-03-09 18:38:50 +00:00
|
|
|
}
|