2015-08-12 17:48:58 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-08-12 19:52:21 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2015-08-12 17:48:58 +00:00
|
|
|
"fmt"
|
2015-08-12 19:52:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
2015-08-12 17:48:58 +00:00
|
|
|
|
2015-08-12 19:52:21 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2015-08-12 17:48:58 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2015-08-12 20:22:48 +00:00
|
|
|
const (
|
2015-08-18 01:22:03 +00:00
|
|
|
// Default path at which SSH backend will be mounted in Vault server
|
2015-08-12 20:22:48 +00:00
|
|
|
SSHAgentDefaultMountPoint = "ssh"
|
|
|
|
|
|
|
|
// Echo request message sent as OTP by the agent
|
|
|
|
VerifyEchoRequest = "verify-echo-request"
|
|
|
|
|
|
|
|
// Echo response message sent as a response to OTP matching echo request
|
|
|
|
VerifyEchoResponse = "verify-echo-response"
|
|
|
|
)
|
2015-08-12 17:48:58 +00:00
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// SSHAgent is a structure representing an SSH agent 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.
|
|
|
|
type SSHAgent struct {
|
|
|
|
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 {
|
2015-08-18 01:22:03 +00:00
|
|
|
// 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"`
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// IP associated with the OTP
|
|
|
|
IP string `mapstructure:"ip"`
|
2015-08-12 17:48:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// SSHAgentConfig is a structure which represents the entries from the agent's configuration file.
|
2015-08-12 19:52:21 +00:00
|
|
|
type SSHAgentConfig struct {
|
|
|
|
VaultAddr string `hcl:"vault_addr"`
|
|
|
|
SSHMountPoint string `hcl:"ssh_mount_point"`
|
|
|
|
CACert string `hcl:"ca_cert"`
|
|
|
|
CAPath string `hcl:"ca_path"`
|
|
|
|
TLSSkipVerify bool `hcl:"tls_skip_verify"`
|
|
|
|
AllowedCidrList string `hcl:"allowed_cidr_list"`
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// TLSClient returns a HTTP client that uses TLS verification (TLS 1.2) for a given
|
2015-08-12 19:52:21 +00:00
|
|
|
// certificate pool.
|
2015-10-15 20:09:45 +00:00
|
|
|
func (c *SSHAgentConfig) SetTLSParameters(clientConfig *Config, certPool *x509.CertPool) {
|
2015-08-12 19:52:21 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: c.TLSSkipVerify,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
RootCAs: certPool,
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:09:45 +00:00
|
|
|
clientConfig.HttpClient.Transport = &http.Transport{
|
2015-08-12 19:52:21 +00:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// NewClient returns a new client for the configuration. This client will be used by the
|
2015-08-13 00:10:35 +00:00
|
|
|
// SSH agent to communicate with Vault server and verify the OTP entered by user.
|
2015-08-12 20:09:32 +00:00
|
|
|
// If the configuration supplies Vault SSL certificates, then the client will
|
2015-08-13 00:10:35 +00:00
|
|
|
// have TLS configured in its transport.
|
2015-08-12 20:09:32 +00:00
|
|
|
func (c *SSHAgentConfig) 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
|
|
|
|
|
2015-08-13 00:10:35 +00:00
|
|
|
// Check if certificates are provided via config file.
|
2015-08-12 20:09:32 +00:00
|
|
|
if c.CACert != "" || c.CAPath != "" || c.TLSSkipVerify {
|
|
|
|
var certPool *x509.CertPool
|
|
|
|
var err error
|
|
|
|
if c.CACert != "" {
|
2015-11-03 19:21:14 +00:00
|
|
|
certPool, err = LoadCACert(c.CACert)
|
2015-08-12 20:09:32 +00:00
|
|
|
} else if c.CAPath != "" {
|
2015-11-03 19:21:14 +00:00
|
|
|
certPool, err = LoadCAPath(c.CAPath)
|
2015-08-12 20:09:32 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-13 00:10:35 +00:00
|
|
|
|
2015-10-15 20:09:45 +00:00
|
|
|
// Enable TLS on the HTTP client information
|
|
|
|
c.SetTLSParameters(clientConfig, certPool)
|
2015-08-12 20:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creating the client object for the given configuration
|
|
|
|
client, err := NewClient(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-13 00:10:35 +00:00
|
|
|
|
2015-08-12 20:09:32 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// LoadSSHAgentConfig loads agent's configuration from the file and populates the corresponding
|
2015-08-18 01:22:03 +00:00
|
|
|
// in-memory structure.
|
|
|
|
//
|
|
|
|
// Vault address is a required parameter.
|
|
|
|
// Mount point defaults to "ssh".
|
2015-08-12 19:52:21 +00:00
|
|
|
func LoadSSHAgentConfig(path string) (*SSHAgentConfig, error) {
|
|
|
|
var config SSHAgentConfig
|
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
obj, err := hcl.Parse(string(contents))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hcl.DecodeObject(&config, obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-13 00:10:35 +00:00
|
|
|
|
|
|
|
if config.VaultAddr == "" {
|
|
|
|
return nil, fmt.Errorf("config missing vault_addr")
|
|
|
|
}
|
|
|
|
if config.SSHMountPoint == "" {
|
2015-08-18 01:22:03 +00:00
|
|
|
config.SSHMountPoint = SSHAgentDefaultMountPoint
|
2015-08-13 00:10:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:52:21 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// SSHAgent creates an SSHAgent object which can talk to Vault server with SSH backend
|
2015-08-12 17:48:58 +00:00
|
|
|
// mounted at default path ("ssh").
|
|
|
|
func (c *Client) SSHAgent() *SSHAgent {
|
|
|
|
return c.SSHAgentWithMountPoint(SSHAgentDefaultMountPoint)
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// SSHAgentWithMountPoint creates an SSHAgent object which can talk to Vault server with SSH backend
|
2015-08-12 17:48:58 +00:00
|
|
|
// mounted at a specific mount point.
|
|
|
|
func (c *Client) SSHAgentWithMountPoint(mountPoint string) *SSHAgent {
|
|
|
|
return &SSHAgent{
|
|
|
|
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
|
2015-08-18 01:22:03 +00:00
|
|
|
// 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,
|
|
|
|
// an echo response message is returned. This feature is used by agent to verify if
|
|
|
|
// its configured correctly.
|
2015-08-12 17:48:58 +00:00
|
|
|
func (c *SSHAgent) Verify(otp string) (*SSHVerifyResponse, error) {
|
|
|
|
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
|
|
|
|
}
|