2019-05-06 14:39:28 +00:00
|
|
|
package cert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-01-09 22:56:34 +00:00
|
|
|
"net/http"
|
2019-05-06 14:39:28 +00:00
|
|
|
|
2020-01-09 22:56:34 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-05-06 14:39:28 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/command/agent/auth"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certMethod struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
mountPath string
|
|
|
|
name string
|
2020-08-25 21:26:06 +00:00
|
|
|
|
|
|
|
caCert string
|
|
|
|
clientCert string
|
|
|
|
clientKey string
|
|
|
|
|
|
|
|
// Client is the cached client to use if cert info was provided.
|
|
|
|
client *api.Client
|
2019-05-06 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:26:06 +00:00
|
|
|
var _ auth.AuthMethodWithClient = &certMethod{}
|
|
|
|
|
2019-05-06 14:39:28 +00:00
|
|
|
func NewCertAuthMethod(conf *auth.AuthConfig) (auth.AuthMethod, error) {
|
|
|
|
if conf == nil {
|
|
|
|
return nil, errors.New("empty config")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not concerned if the conf.Config is empty as the 'name'
|
|
|
|
// parameter is optional when using TLS Auth
|
|
|
|
|
|
|
|
c := &certMethod{
|
|
|
|
logger: conf.Logger,
|
|
|
|
mountPath: conf.MountPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Config != nil {
|
|
|
|
nameRaw, ok := conf.Config["name"]
|
|
|
|
if !ok {
|
|
|
|
nameRaw = ""
|
|
|
|
}
|
|
|
|
c.name, ok = nameRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not convert 'name' config value to string")
|
|
|
|
}
|
2020-08-25 21:26:06 +00:00
|
|
|
|
|
|
|
caCertRaw, ok := conf.Config["ca_cert"]
|
|
|
|
if ok {
|
|
|
|
c.caCert, ok = caCertRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not convert 'ca_cert' config value to string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCertRaw, ok := conf.Config["client_cert"]
|
|
|
|
if ok {
|
|
|
|
c.clientCert, ok = clientCertRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not convert 'cert_file' config value to string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clientKeyRaw, ok := conf.Config["client_key"]
|
|
|
|
if ok {
|
|
|
|
c.clientKey, ok = clientKeyRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not convert 'cert_key' config value to string")
|
|
|
|
}
|
|
|
|
}
|
2019-05-06 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 22:56:34 +00:00
|
|
|
func (c *certMethod) Authenticate(_ context.Context, client *api.Client) (string, http.Header, map[string]interface{}, error) {
|
2019-05-06 14:39:28 +00:00
|
|
|
c.logger.Trace("beginning authentication")
|
|
|
|
|
|
|
|
authMap := map[string]interface{}{}
|
|
|
|
|
|
|
|
if c.name != "" {
|
|
|
|
authMap["name"] = c.name
|
|
|
|
}
|
|
|
|
|
2020-01-09 22:56:34 +00:00
|
|
|
return fmt.Sprintf("%s/login", c.mountPath), nil, authMap, nil
|
2019-05-06 14:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *certMethod) NewCreds() chan struct{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *certMethod) CredSuccess() {}
|
|
|
|
|
|
|
|
func (c *certMethod) Shutdown() {}
|
2020-08-25 21:26:06 +00:00
|
|
|
|
|
|
|
// AuthClient uses the existing client's address and returns a new client with
|
|
|
|
// the auto-auth method's certificate information if that's provided in its
|
|
|
|
// config map.
|
|
|
|
func (c *certMethod) AuthClient(client *api.Client) (*api.Client, error) {
|
|
|
|
c.logger.Trace("deriving auth client to use")
|
|
|
|
|
|
|
|
clientToAuth := client
|
|
|
|
|
|
|
|
if c.caCert != "" || (c.clientKey != "" && c.clientCert != "") {
|
|
|
|
// Return cached client if present
|
|
|
|
if c.client != nil {
|
2021-05-12 21:59:07 +00:00
|
|
|
return c.client, nil
|
2020-08-25 21:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
if config.Error != nil {
|
|
|
|
return nil, config.Error
|
|
|
|
}
|
|
|
|
config.Address = client.Address()
|
|
|
|
|
|
|
|
t := &api.TLSConfig{
|
|
|
|
CACert: c.caCert,
|
|
|
|
ClientCert: c.clientCert,
|
|
|
|
ClientKey: c.clientKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup TLS config
|
|
|
|
if err := config.ConfigureTLS(t); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
clientToAuth, err = api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the client for future use
|
|
|
|
c.client = clientToAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientToAuth, nil
|
|
|
|
}
|