open-vault/api/ssh_agent.go

242 lines
6.9 KiB
Go
Raw Normal View History

2015-08-12 17:48:58 +00:00
package api
import (
"crypto/tls"
"crypto/x509"
2015-08-12 17:48:58 +00:00
"fmt"
"io/ioutil"
"os"
2015-08-12 17:48:58 +00:00
"github.com/hashicorp/go-cleanhttp"
2016-03-10 21:47:46 +00:00
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
2016-03-10 21:47:46 +00:00
"github.com/hashicorp/hcl/hcl/ast"
2015-08-12 17:48:58 +00:00
"github.com/mitchellh/mapstructure"
)
const (
2016-03-10 21:47:46 +00:00
// SSHHelperDefaultMountPoint is the default path at which SSH backend will be
// mounted in the Vault server.
2016-02-23 05:08:21 +00:00
SSHHelperDefaultMountPoint = "ssh"
2016-03-10 21:47:46 +00:00
// VerifyEchoRequest is the echo request message sent as OTP by the helper.
VerifyEchoRequest = "verify-echo-request"
2016-03-10 21:47:46 +00:00
// VerifyEchoResponse is the echo response message sent as a response to OTP
// matching echo request.
VerifyEchoResponse = "verify-echo-response"
)
2015-08-12 17:48:58 +00:00
// SSHHelper is a structure representing a vault-ssh-helper which can talk to vault server
2015-08-12 17:48:58 +00:00
// in order to verify the OTP entered by the user. It contains the path at which
// SSH backend is mounted at the server.
2016-02-23 05:08:21 +00:00
type SSHHelper struct {
2015-08-12 17:48:58 +00:00
c *Client
MountPoint string
}
2015-09-29 07:35:16 +00:00
// SSHVerifyResponse is a structure representing the fields in Vault server's
2015-08-12 17:48:58 +00:00
// response.
type SSHVerifyResponse struct {
// Usually empty. If the request OTP is echo request message, this will
// be set to the corresponding echo response message.
Message string `mapstructure:"message"`
// Username associated with the OTP
2015-08-12 17:48:58 +00:00
Username string `mapstructure:"username"`
// IP associated with the OTP
IP string `mapstructure:"ip"`
2015-08-12 17:48:58 +00:00
}
// SSHHelperConfig is a structure which represents the entries from the vault-ssh-helper's configuration file.
2016-02-23 05:08:21 +00:00
type SSHHelperConfig struct {
VaultAddr string `hcl:"vault_addr"`
SSHMountPoint string `hcl:"ssh_mount_point"`
CACert string `hcl:"ca_cert"`
CAPath string `hcl:"ca_path"`
AllowedCidrList string `hcl:"allowed_cidr_list"`
TLSSkipVerify bool `hcl:"tls_skip_verify"`
}
2016-03-10 21:47:46 +00:00
// SetTLSParameters sets the TLS parameters for this SSH agent.
2016-02-23 05:08:21 +00:00
func (c *SSHHelperConfig) SetTLSParameters(clientConfig *Config, certPool *x509.CertPool) {
tlsConfig := &tls.Config{
InsecureSkipVerify: c.TLSSkipVerify,
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
}
transport := cleanhttp.DefaultTransport()
transport.TLSClientConfig = tlsConfig
clientConfig.HttpClient.Transport = transport
}
2015-09-29 07:35:16 +00:00
// NewClient returns a new client for the configuration. This client will be used by the
// vault-ssh-helper to communicate with Vault server and verify the OTP entered by user.
// If the configuration supplies Vault SSL certificates, then the client will
// have TLS configured in its transport.
2016-02-23 05:08:21 +00:00
func (c *SSHHelperConfig) NewClient() (*Client, error) {
// Creating a default client configuration for communicating with vault server.
clientConfig := DefaultConfig()
// Pointing the client to the actual address of vault server.
clientConfig.Address = c.VaultAddr
// Check if certificates are provided via config file.
if c.CACert != "" || c.CAPath != "" || c.TLSSkipVerify {
var certPool *x509.CertPool
var err error
if c.CACert != "" {
certPool, err = LoadCACert(c.CACert)
} else if c.CAPath != "" {
certPool, err = LoadCAPath(c.CAPath)
}
if err != nil {
return nil, err
}
// Enable TLS on the HTTP client information
c.SetTLSParameters(clientConfig, certPool)
}
// Creating the client object for the given configuration
client, err := NewClient(clientConfig)
if err != nil {
return nil, err
}
return client, nil
}
2016-02-23 05:08:21 +00:00
// LoadSSHHelperConfig loads ssh-helper's configuration from the file and populates the corresponding
// in-memory structure.
//
// Vault address is a required parameter.
// Mount point defaults to "ssh".
2016-02-23 05:08:21 +00:00
func LoadSSHHelperConfig(path string) (*SSHHelperConfig, error) {
contents, err := ioutil.ReadFile(path)
2016-03-10 21:47:46 +00:00
if err != nil && !os.IsNotExist(err) {
return nil, multierror.Prefix(err, "ssh_helper:")
}
return ParseSSHHelperConfig(string(contents))
}
2016-03-10 21:47:46 +00:00
// ParseSSHHelperConfig parses the given contents as a string for the SSHHelper
// configuration.
func ParseSSHHelperConfig(contents string) (*SSHHelperConfig, error) {
root, err := hcl.Parse(string(contents))
if err != nil {
return nil, fmt.Errorf("ssh_helper: error parsing config: %s", err)
}
2016-03-10 21:47:46 +00:00
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("ssh_helper: error parsing config: file doesn't contain a root object")
}
2016-03-10 21:47:46 +00:00
valid := []string{
"vault_addr",
"ssh_mount_point",
"ca_cert",
"ca_path",
"allowed_cidr_list",
"tls_skip_verify",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, multierror.Prefix(err, "ssh_helper:")
}
2016-03-10 21:47:46 +00:00
var c SSHHelperConfig
c.SSHMountPoint = SSHHelperDefaultMountPoint
if err := hcl.DecodeObject(&c, list); err != nil {
return nil, multierror.Prefix(err, "ssh_helper:")
}
if c.VaultAddr == "" {
return nil, fmt.Errorf("ssh_helper: missing config 'vault_addr'")
}
return &c, nil
}
2016-02-23 05:08:21 +00:00
// SSHHelper creates an SSHHelper object which can talk to Vault server with SSH backend
2015-08-12 17:48:58 +00:00
// mounted at default path ("ssh").
2016-02-23 05:08:21 +00:00
func (c *Client) SSHHelper() *SSHHelper {
return c.SSHHelperWithMountPoint(SSHHelperDefaultMountPoint)
2015-08-12 17:48:58 +00:00
}
2016-02-23 05:08:21 +00:00
// SSHHelperWithMountPoint creates an SSHHelper object which can talk to Vault server with SSH backend
2015-08-12 17:48:58 +00:00
// mounted at a specific mount point.
2016-02-23 05:08:21 +00:00
func (c *Client) SSHHelperWithMountPoint(mountPoint string) *SSHHelper {
return &SSHHelper{
2015-08-12 17:48:58 +00:00
c: c,
MountPoint: mountPoint,
}
}
2015-09-29 07:35:16 +00:00
// Verify verifies if the key provided by user is present in Vault server. The response
// will contain the IP address and username associated with the OTP. In case the
// OTP matches the echo request message, instead of searching an entry for the OTP,
2016-02-23 05:08:21 +00:00
// an echo response message is returned. This feature is used by ssh-helper to verify if
// its configured correctly.
2016-02-23 05:08:21 +00:00
func (c *SSHHelper) Verify(otp string) (*SSHVerifyResponse, error) {
2015-08-12 17:48:58 +00:00
data := map[string]interface{}{
"otp": otp,
}
verifyPath := fmt.Sprintf("/v1/%s/verify", c.MountPoint)
r := c.c.NewRequest("PUT", verifyPath)
if err := r.SetJSONBody(data); err != nil {
return nil, err
}
resp, err := c.c.RawRequest(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret.Data == nil {
return nil, nil
}
var verifyResp SSHVerifyResponse
err = mapstructure.Decode(secret.Data, &verifyResp)
if err != nil {
return nil, err
}
return &verifyResp, nil
}
2016-03-10 21:47:46 +00:00
func checkHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key '%s' on line %d", key, item.Assign.Line))
}
}
return result
}