2015-03-04 21:10:10 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-11-03 19:21:14 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2015-04-20 18:30:35 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-11-03 19:21:14 +00:00
|
|
|
"io/ioutil"
|
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"
|
2015-11-03 19:21:14 +00:00
|
|
|
"path/filepath"
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
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"
|
|
|
|
const EnvVaultInsecure = "VAULT_SKIP_VERIFY"
|
|
|
|
|
2015-04-20 18:30:35 +00:00
|
|
|
var (
|
2015-10-15 20:09:45 +00:00
|
|
|
errRedirect = errors.New("redirect")
|
2015-04-20 18:30:35 +00:00
|
|
|
)
|
|
|
|
|
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
|
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{
|
2015-10-15 20:09:45 +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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
var envInsecure bool
|
|
|
|
var foundInsecure bool
|
|
|
|
|
|
|
|
var newCertPool *x509.CertPool
|
|
|
|
var clientCert tls.Certificate
|
|
|
|
var foundClientCert bool
|
|
|
|
|
|
|
|
if v := os.Getenv(EnvVaultAddress); v != "" {
|
|
|
|
envAddress = v
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
foundInsecure = true
|
|
|
|
}
|
|
|
|
// If we need custom TLS configuration, then set it
|
|
|
|
if envCACert != "" || envCAPath != "" || envClientCert != "" || envClientKey != "" || envInsecure {
|
|
|
|
var err error
|
|
|
|
if envCACert != "" {
|
|
|
|
newCertPool, err = LoadCACert(envCACert)
|
|
|
|
} else if envCAPath != "" {
|
|
|
|
newCertPool, err = LoadCAPath(envCAPath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error setting up CA path: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if envClientCert != "" && envClientKey != "" {
|
|
|
|
clientCert, err = tls.LoadX509KeyPair(envClientCert, envClientKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
foundClientCert = true
|
|
|
|
} else if envClientCert != "" || envClientKey != "" {
|
|
|
|
return fmt.Errorf("Both client cert and client key must be provided")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if envAddress != "" {
|
|
|
|
c.Address = envAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
clientTLSConfig := c.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
|
|
|
if foundInsecure {
|
|
|
|
clientTLSConfig.InsecureSkipVerify = envInsecure
|
|
|
|
}
|
|
|
|
if newCertPool != nil {
|
|
|
|
clientTLSConfig.RootCAs = newCertPool
|
|
|
|
}
|
|
|
|
if foundClientCert {
|
|
|
|
clientTLSConfig.Certificates = []tls.Certificate{clientCert}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-03-09 18:38:50 +00:00
|
|
|
addr *url.URL
|
2015-04-23 15:13:52 +00:00
|
|
|
config *Config
|
2015-08-22 00:36:19 +00:00
|
|
|
token string
|
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) {
|
2015-11-03 19:21:14 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return errRedirect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if token := os.Getenv("VAULT_TOKEN"); token != "" {
|
|
|
|
client.SetToken(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
2015-03-04 21:10:10 +00:00
|
|
|
}
|
2015-03-09 18:38:50 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (c *Client) NewRequest(method, path 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{
|
|
|
|
Scheme: c.addr.Scheme,
|
|
|
|
Host: c.addr.Host,
|
|
|
|
Path: path,
|
|
|
|
},
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:15:20 +00:00
|
|
|
var result *Response
|
2015-03-09 18:38:50 +00:00
|
|
|
resp, err := c.config.HttpClient.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 {
|
2015-05-02 20:08:35 +00:00
|
|
|
if urlErr, ok := err.(*url.Error); ok && urlErr.Err == errRedirect {
|
2015-04-20 18:30:35 +00:00
|
|
|
err = nil
|
2015-05-02 20:08:35 +00:00
|
|
|
} else if strings.Contains(err.Error(), "tls: oversized") {
|
|
|
|
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-03-09 18:38:50 +00:00
|
|
|
if err != nil {
|
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
|
|
|
}
|
2015-11-03 19:21:14 +00:00
|
|
|
|
|
|
|
// Loads the certificate from given path and creates a certificate pool from it.
|
|
|
|
func LoadCACert(path string) (*x509.CertPool, error) {
|
|
|
|
certs, err := loadCertFromPEM(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := x509.NewCertPool()
|
|
|
|
for _, cert := range certs {
|
|
|
|
result.AddCert(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads the certificates present in the given directory and creates a
|
|
|
|
// certificate pool from it.
|
|
|
|
func LoadCAPath(path string) (*x509.CertPool, error) {
|
|
|
|
result := x509.NewCertPool()
|
|
|
|
fn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
certs, err := loadCertFromPEM(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cert := range certs {
|
|
|
|
result.AddCert(cert)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, filepath.Walk(path, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a certificate from the given path
|
|
|
|
func loadCertFromPEM(path string) ([]*x509.Certificate, error) {
|
|
|
|
pemCerts, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := make([]*x509.Certificate, 0, 5)
|
|
|
|
for len(pemCerts) > 0 {
|
|
|
|
var block *pem.Block
|
|
|
|
block, pemCerts = pem.Decode(pemCerts)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certs = append(certs, cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
return certs, nil
|
|
|
|
}
|