2017-03-16 18:55:21 +00:00
|
|
|
package pluginutil
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2017-03-16 18:55:21 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-03-20 18:54:03 +00:00
|
|
|
squarejwt "gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
|
2017-03-16 18:55:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/api"
|
2017-05-04 00:37:34 +00:00
|
|
|
"github.com/hashicorp/vault/helper/certutil"
|
2017-03-16 18:55:21 +00:00
|
|
|
)
|
|
|
|
|
2017-03-16 21:14:49 +00:00
|
|
|
var (
|
2017-03-16 21:17:44 +00:00
|
|
|
// PluginUnwrapTokenEnv is the ENV name used to pass unwrap tokens to the
|
|
|
|
// plugin.
|
|
|
|
PluginUnwrapTokenEnv = "VAULT_UNWRAP_TOKEN"
|
2017-07-31 15:28:06 +00:00
|
|
|
|
|
|
|
// PluginCACertPEMEnv is an ENV name used for holding a CA PEM-encoded
|
|
|
|
// string. Used for testing.
|
|
|
|
PluginCACertPEMEnv = "VAULT_TESTING_PLUGIN_CA_PEM"
|
2017-03-16 21:14:49 +00:00
|
|
|
)
|
|
|
|
|
2017-05-04 00:37:34 +00:00
|
|
|
// generateCert is used internally to create certificates for the plugin
|
|
|
|
// client and server.
|
|
|
|
func generateCert() ([]byte, *ecdsa.PrivateKey, error) {
|
2017-03-16 18:55:21 +00:00
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
|
|
|
if err != nil {
|
2017-04-19 22:46:07 +00:00
|
|
|
return nil, nil, err
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
host, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
2017-04-19 22:46:07 +00:00
|
|
|
return nil, nil, err
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:37:34 +00:00
|
|
|
sn, err := certutil.GenerateSerialNumber()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-16 18:55:21 +00:00
|
|
|
template := &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: host,
|
|
|
|
},
|
|
|
|
DNSNames: []string{host},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,
|
2017-05-04 00:37:34 +00:00
|
|
|
SerialNumber: sn,
|
2017-03-16 18:55:21 +00:00
|
|
|
NotBefore: time.Now().Add(-30 * time.Second),
|
|
|
|
NotAfter: time.Now().Add(262980 * time.Hour),
|
2017-04-19 22:46:07 +00:00
|
|
|
IsCA: true,
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 22:46:07 +00:00
|
|
|
certBytes, err := x509.CreateCertificate(rand.Reader, template, template, key.Public(), key)
|
2017-03-16 18:55:21 +00:00
|
|
|
if err != nil {
|
2017-04-19 22:46:07 +00:00
|
|
|
return nil, nil, errwrap.Wrapf("unable to generate client certificate: {{err}}", err)
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 22:46:07 +00:00
|
|
|
return certBytes, key, nil
|
2017-03-16 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:37:34 +00:00
|
|
|
// createClientTLSConfig creates a signed certificate and returns a configured
|
2017-03-16 21:14:49 +00:00
|
|
|
// TLS config.
|
2017-05-04 00:37:34 +00:00
|
|
|
func createClientTLSConfig(certBytes []byte, key *ecdsa.PrivateKey) (*tls.Config, error) {
|
2017-04-19 22:46:07 +00:00
|
|
|
clientCert, err := x509.ParseCertificate(certBytes)
|
2017-03-16 18:55:21 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing generated plugin certificate: {{err}}", err)
|
2017-03-16 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert := tls.Certificate{
|
2017-04-19 22:46:07 +00:00
|
|
|
Certificate: [][]byte{certBytes},
|
|
|
|
PrivateKey: key,
|
2017-03-16 21:14:49 +00:00
|
|
|
Leaf: clientCert,
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:14:49 +00:00
|
|
|
clientCertPool := x509.NewCertPool()
|
2017-04-19 22:46:07 +00:00
|
|
|
clientCertPool.AddCert(clientCert)
|
2017-03-16 21:14:49 +00:00
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: clientCertPool,
|
2018-01-18 21:49:20 +00:00
|
|
|
ClientCAs: clientCertPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
2017-04-19 22:46:07 +00:00
|
|
|
ServerName: clientCert.Subject.CommonName,
|
2017-03-16 21:14:49 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig.BuildNameToCertificate()
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|
|
|
|
|
2017-05-04 00:37:34 +00:00
|
|
|
// wrapServerConfig is used to create a server certificate and private key, then
|
2017-03-16 21:14:49 +00:00
|
|
|
// wrap them in an unwrap token for later retrieval by the plugin.
|
2018-01-19 06:44:44 +00:00
|
|
|
func wrapServerConfig(ctx context.Context, sys RunnerUtil, certBytes []byte, key *ecdsa.PrivateKey) (string, error) {
|
2017-04-19 22:46:07 +00:00
|
|
|
rawKey, err := x509.MarshalECPrivateKey(key)
|
2017-03-16 21:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
wrapInfo, err := sys.ResponseWrapData(ctx, map[string]interface{}{
|
2017-04-19 22:46:07 +00:00
|
|
|
"ServerCert": certBytes,
|
2017-03-16 21:14:49 +00:00
|
|
|
"ServerKey": rawKey,
|
2017-05-09 13:50:07 +00:00
|
|
|
}, time.Second*60, true)
|
2017-04-24 19:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-03-16 21:14:49 +00:00
|
|
|
|
2017-04-24 19:15:01 +00:00
|
|
|
return wrapInfo.Token, nil
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// VaultPluginTLSProvider is run inside a plugin and retrieves the response
|
2017-03-16 21:14:49 +00:00
|
|
|
// wrapped TLS certificate from vault. It returns a configured TLS Config.
|
2017-05-02 21:40:11 +00:00
|
|
|
func VaultPluginTLSProvider(apiTLSConfig *api.TLSConfig) func() (*tls.Config, error) {
|
2018-03-20 18:54:10 +00:00
|
|
|
if os.Getenv(PluginMetadataModeEnv) == "true" {
|
2017-09-01 05:02:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:40:11 +00:00
|
|
|
return func() (*tls.Config, error) {
|
|
|
|
unwrapToken := os.Getenv(PluginUnwrapTokenEnv)
|
|
|
|
|
2019-03-20 18:54:03 +00:00
|
|
|
parsedJWT, err := squarejwt.ParseSigned(unwrapToken)
|
2017-05-02 21:40:11 +00:00
|
|
|
if err != nil {
|
2019-03-20 18:54:03 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing wrapping token: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
2019-03-20 18:54:03 +00:00
|
|
|
|
|
|
|
var allClaims = make(map[string]interface{})
|
|
|
|
if err = parsedJWT.UnsafeClaimsWithoutVerification(&allClaims); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error parsing claims from wrapping token: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 18:54:03 +00:00
|
|
|
addrClaimRaw, ok := allClaims["addr"]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not validate addr claim")
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
2019-03-20 18:54:03 +00:00
|
|
|
vaultAddr, ok := addrClaimRaw.(string)
|
2017-05-02 21:40:11 +00:00
|
|
|
if !ok {
|
2019-03-20 18:54:03 +00:00
|
|
|
return nil, errors.New("could not parse addr claim")
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
if vaultAddr == "" {
|
2017-12-05 17:01:35 +00:00
|
|
|
return nil, errors.New(`no vault api_addr found`)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check the value
|
|
|
|
if _, err := url.Parse(vaultAddr); err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing the vault api_addr: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap the token
|
|
|
|
clientConf := api.DefaultConfig()
|
|
|
|
clientConf.Address = vaultAddr
|
|
|
|
if apiTLSConfig != nil {
|
2017-09-01 05:02:03 +00:00
|
|
|
err := clientConf.ConfigureTLS(apiTLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error configuring api client {{err}}", err)
|
|
|
|
}
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
client, err := api.NewClient(clientConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error during api client creation: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().Unwrap(unwrapToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("error during token unwrap request: {{err}}", err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
return nil, errors.New("error during token unwrap request: secret is nil")
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve and parse the server's certificate
|
|
|
|
serverCertBytesRaw, ok := secret.Data["ServerCert"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("error unmarshalling certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
serverCertBytes, err := base64.StdEncoding.DecodeString(serverCertBytesRaw)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serverCert, err := x509.ParseCertificate(serverCertBytes)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve and parse the server's private key
|
|
|
|
serverKeyB64, ok := secret.Data["ServerKey"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("error unmarshalling certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
serverKeyRaw, err := base64.StdEncoding.DecodeString(serverKeyB64)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serverKey, err := x509.ParseECPrivateKey(serverKeyRaw)
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add CA cert to the cert pool
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
caCertPool.AddCert(serverCert)
|
|
|
|
|
|
|
|
// Build a certificate object out of the server's cert and private key.
|
|
|
|
cert := tls.Certificate{
|
|
|
|
Certificate: [][]byte{serverCertBytes},
|
|
|
|
PrivateKey: serverKey,
|
|
|
|
Leaf: serverCert,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup TLS config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ClientCAs: caCertPool,
|
|
|
|
RootCAs: caCertPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
// TLS 1.2 minimum
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2018-01-18 21:49:20 +00:00
|
|
|
ServerName: serverCert.Subject.CommonName,
|
2017-05-02 21:40:11 +00:00
|
|
|
}
|
|
|
|
tlsConfig.BuildNameToCertificate()
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
2017-03-16 18:55:21 +00:00
|
|
|
}
|
|
|
|
}
|