2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-12 20:48:58 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-07-25 01:49:55 +03:00
|
|
|
"context"
|
2015-08-12 22:52:21 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2015-08-12 20:48:58 +03:00
|
|
|
"fmt"
|
2015-08-12 22:52:21 +03:00
|
|
|
"io/ioutil"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2015-08-12 22:52:21 +03:00
|
|
|
"os"
|
2015-08-12 20:48:58 +03:00
|
|
|
|
2018-04-05 18:49:21 +03:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-01-09 03:48:57 +03:00
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
rootcerts "github.com/hashicorp/go-rootcerts"
|
2015-08-12 22:52:21 +03:00
|
|
|
"github.com/hashicorp/hcl"
|
2016-03-11 00:47:46 +03:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2015-08-12 20:48:58 +03:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2015-08-12 23:22:48 +03:00
|
|
|
const (
|
2016-03-11 00:47:46 +03:00
|
|
|
// SSHHelperDefaultMountPoint is the default path at which SSH backend will be
|
|
|
|
// mounted in the Vault server.
|
2016-02-23 08:08:21 +03:00
|
|
|
SSHHelperDefaultMountPoint = "ssh"
|
2015-08-12 23:22:48 +03:00
|
|
|
|
2016-03-11 00:47:46 +03:00
|
|
|
// VerifyEchoRequest is the echo request message sent as OTP by the helper.
|
2015-08-12 23:22:48 +03:00
|
|
|
VerifyEchoRequest = "verify-echo-request"
|
|
|
|
|
2016-03-11 00:47:46 +03:00
|
|
|
// VerifyEchoResponse is the echo response message sent as a response to OTP
|
|
|
|
// matching echo request.
|
2015-08-12 23:22:48 +03:00
|
|
|
VerifyEchoResponse = "verify-echo-response"
|
|
|
|
)
|
2015-08-12 20:48:58 +03:00
|
|
|
|
2016-02-24 01:32:49 +03:00
|
|
|
// SSHHelper is a structure representing a vault-ssh-helper which can talk to vault server
|
2015-08-12 20:48:58 +03: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 08:08:21 +03:00
|
|
|
type SSHHelper struct {
|
2015-08-12 20:48:58 +03:00
|
|
|
c *Client
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2015-09-29 10:35:16 +03:00
|
|
|
// SSHVerifyResponse is a structure representing the fields in Vault server's
|
2015-08-12 20:48:58 +03:00
|
|
|
// response.
|
|
|
|
type SSHVerifyResponse struct {
|
2015-08-18 04:22:03 +03:00
|
|
|
// Usually empty. If the request OTP is echo request message, this will
|
|
|
|
// be set to the corresponding echo response message.
|
2018-04-10 05:23:56 +03:00
|
|
|
Message string `json:"message" mapstructure:"message"`
|
2015-08-18 04:22:03 +03:00
|
|
|
|
|
|
|
// Username associated with the OTP
|
2018-04-10 05:23:56 +03:00
|
|
|
Username string `json:"username" mapstructure:"username"`
|
2015-08-18 04:22:03 +03:00
|
|
|
|
|
|
|
// IP associated with the OTP
|
2018-04-10 05:23:56 +03:00
|
|
|
IP string `json:"ip" mapstructure:"ip"`
|
2016-07-01 21:31:37 +03:00
|
|
|
|
|
|
|
// Name of the role against which the OTP was issued
|
2018-04-10 05:23:56 +03:00
|
|
|
RoleName string `json:"role_name" mapstructure:"role_name"`
|
2015-08-12 20:48:58 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 01:32:49 +03:00
|
|
|
// SSHHelperConfig is a structure which represents the entries from the vault-ssh-helper's configuration file.
|
2016-02-23 08:08:21 +03:00
|
|
|
type SSHHelperConfig struct {
|
2015-08-12 22:52:21 +03:00
|
|
|
VaultAddr string `hcl:"vault_addr"`
|
|
|
|
SSHMountPoint string `hcl:"ssh_mount_point"`
|
2020-06-08 18:16:03 +03:00
|
|
|
Namespace string `hcl:"namespace"`
|
2015-08-12 22:52:21 +03:00
|
|
|
CACert string `hcl:"ca_cert"`
|
|
|
|
CAPath string `hcl:"ca_path"`
|
|
|
|
AllowedCidrList string `hcl:"allowed_cidr_list"`
|
2016-07-01 21:31:37 +03:00
|
|
|
AllowedRoles string `hcl:"allowed_roles"`
|
2016-02-24 01:32:49 +03:00
|
|
|
TLSSkipVerify bool `hcl:"tls_skip_verify"`
|
2016-08-24 05:06:56 +03:00
|
|
|
TLSServerName string `hcl:"tls_server_name"`
|
2015-08-12 22:52:21 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 00:47:46 +03:00
|
|
|
// SetTLSParameters sets the TLS parameters for this SSH agent.
|
2016-02-23 08:08:21 +03:00
|
|
|
func (c *SSHHelperConfig) SetTLSParameters(clientConfig *Config, certPool *x509.CertPool) {
|
2015-08-12 22:52:21 +03:00
|
|
|
tlsConfig := &tls.Config{
|
2016-02-24 01:32:49 +03:00
|
|
|
InsecureSkipVerify: c.TLSSkipVerify,
|
2015-08-12 22:52:21 +03:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
RootCAs: certPool,
|
2016-08-24 05:06:56 +03:00
|
|
|
ServerName: c.TLSServerName,
|
2015-08-12 22:52:21 +03:00
|
|
|
}
|
|
|
|
|
2015-12-17 23:23:13 +03:00
|
|
|
transport := cleanhttp.DefaultTransport()
|
|
|
|
transport.TLSClientConfig = tlsConfig
|
|
|
|
clientConfig.HttpClient.Transport = transport
|
2015-08-12 22:52:21 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 05:06:56 +03:00
|
|
|
// Returns true if any of the following conditions are true:
|
2022-09-08 03:31:20 +03:00
|
|
|
// - CA cert is configured
|
|
|
|
// - CA path is configured
|
|
|
|
// - configured to skip certificate verification
|
|
|
|
// - TLS server name is configured
|
2016-08-24 05:06:56 +03:00
|
|
|
func (c *SSHHelperConfig) shouldSetTLSParameters() bool {
|
|
|
|
return c.CACert != "" || c.CAPath != "" || c.TLSServerName != "" || c.TLSSkipVerify
|
|
|
|
}
|
|
|
|
|
2015-09-29 10:35:16 +03:00
|
|
|
// NewClient returns a new client for the configuration. This client will be used by the
|
2016-02-24 01:32:49 +03:00
|
|
|
// vault-ssh-helper to communicate with Vault server and verify the OTP entered by user.
|
2015-08-12 23:09:32 +03:00
|
|
|
// If the configuration supplies Vault SSL certificates, then the client will
|
2015-08-13 03:10:35 +03:00
|
|
|
// have TLS configured in its transport.
|
2016-02-23 08:08:21 +03:00
|
|
|
func (c *SSHHelperConfig) NewClient() (*Client, error) {
|
2015-08-12 23:09:32 +03:00
|
|
|
// 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 03:10:35 +03:00
|
|
|
// Check if certificates are provided via config file.
|
2016-08-24 05:06:56 +03:00
|
|
|
if c.shouldSetTLSParameters() {
|
2016-05-03 19:23:25 +03:00
|
|
|
rootConfig := &rootcerts.Config{
|
|
|
|
CAFile: c.CACert,
|
|
|
|
CAPath: c.CAPath,
|
2015-08-12 23:09:32 +03:00
|
|
|
}
|
2016-05-03 19:23:25 +03:00
|
|
|
certPool, err := rootcerts.LoadCACerts(rootConfig)
|
2015-08-12 23:09:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-15 23:09:45 +03:00
|
|
|
// Enable TLS on the HTTP client information
|
|
|
|
c.SetTLSParameters(clientConfig, certPool)
|
2015-08-12 23:09:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creating the client object for the given configuration
|
|
|
|
client, err := NewClient(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-13 03:10:35 +03:00
|
|
|
|
2020-06-08 18:16:03 +03:00
|
|
|
// Configure namespace
|
|
|
|
if c.Namespace != "" {
|
|
|
|
client.SetNamespace(c.Namespace)
|
|
|
|
}
|
|
|
|
|
2015-08-12 23:09:32 +03:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 08:08:21 +03:00
|
|
|
// LoadSSHHelperConfig loads ssh-helper's configuration from the file and populates the corresponding
|
2015-08-18 04:22:03 +03:00
|
|
|
// in-memory structure.
|
|
|
|
//
|
|
|
|
// Vault address is a required parameter.
|
|
|
|
// Mount point defaults to "ssh".
|
2016-02-23 08:08:21 +03:00
|
|
|
func LoadSSHHelperConfig(path string) (*SSHHelperConfig, error) {
|
2015-08-12 22:52:21 +03:00
|
|
|
contents, err := ioutil.ReadFile(path)
|
2016-03-11 00:47:46 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, multierror.Prefix(err, "ssh_helper:")
|
|
|
|
}
|
|
|
|
return ParseSSHHelperConfig(string(contents))
|
|
|
|
}
|
2015-08-12 22:52:21 +03:00
|
|
|
|
2016-03-11 00:47:46 +03: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 {
|
2018-04-05 18:49:21 +03:00
|
|
|
return nil, errwrap.Wrapf("error parsing config: {{err}}", err)
|
2015-08-12 22:52:21 +03:00
|
|
|
}
|
2015-08-13 03:10:35 +03:00
|
|
|
|
2016-03-11 00:47:46 +03:00
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
2018-04-05 18:49:21 +03:00
|
|
|
return nil, fmt.Errorf("error parsing config: file doesn't contain a root object")
|
2015-08-13 03:10:35 +03:00
|
|
|
}
|
2016-03-11 00:47:46 +03:00
|
|
|
|
|
|
|
valid := []string{
|
|
|
|
"vault_addr",
|
|
|
|
"ssh_mount_point",
|
2020-06-08 18:16:03 +03:00
|
|
|
"namespace",
|
2016-03-11 00:47:46 +03:00
|
|
|
"ca_cert",
|
|
|
|
"ca_path",
|
|
|
|
"allowed_cidr_list",
|
2016-07-01 21:31:37 +03:00
|
|
|
"allowed_roles",
|
2016-03-11 00:47:46 +03:00
|
|
|
"tls_skip_verify",
|
2016-08-24 05:06:56 +03:00
|
|
|
"tls_server_name",
|
2016-03-11 00:47:46 +03:00
|
|
|
}
|
2023-02-06 17:41:56 +03:00
|
|
|
if err := CheckHCLKeys(list, valid); err != nil {
|
2016-03-11 00:47:46 +03:00
|
|
|
return nil, multierror.Prefix(err, "ssh_helper:")
|
2015-08-13 03:10:35 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 00:47:46 +03: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 == "" {
|
2018-04-06 03:43:29 +03:00
|
|
|
return nil, fmt.Errorf(`missing config "vault_addr"`)
|
2016-03-11 00:47:46 +03:00
|
|
|
}
|
|
|
|
return &c, nil
|
2015-08-12 22:52:21 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 17:41:56 +03: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 %q on line %d", key, item.Assign.Line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-02-23 08:08:21 +03:00
|
|
|
// SSHHelper creates an SSHHelper object which can talk to Vault server with SSH backend
|
2015-08-12 20:48:58 +03:00
|
|
|
// mounted at default path ("ssh").
|
2016-02-23 08:08:21 +03:00
|
|
|
func (c *Client) SSHHelper() *SSHHelper {
|
|
|
|
return c.SSHHelperWithMountPoint(SSHHelperDefaultMountPoint)
|
2015-08-12 20:48:58 +03:00
|
|
|
}
|
|
|
|
|
2016-02-23 08:08:21 +03:00
|
|
|
// SSHHelperWithMountPoint creates an SSHHelper object which can talk to Vault server with SSH backend
|
2015-08-12 20:48:58 +03:00
|
|
|
// mounted at a specific mount point.
|
2016-02-23 08:08:21 +03:00
|
|
|
func (c *Client) SSHHelperWithMountPoint(mountPoint string) *SSHHelper {
|
|
|
|
return &SSHHelper{
|
2015-08-12 20:48:58 +03:00
|
|
|
c: c,
|
|
|
|
MountPoint: mountPoint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 10:35:16 +03:00
|
|
|
// Verify verifies if the key provided by user is present in Vault server. The response
|
2015-08-18 04:22:03 +03: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,
|
2016-02-23 08:08:21 +03:00
|
|
|
// an echo response message is returned. This feature is used by ssh-helper to verify if
|
2015-08-18 04:22:03 +03:00
|
|
|
// its configured correctly.
|
2016-02-23 08:08:21 +03:00
|
|
|
func (c *SSHHelper) Verify(otp string) (*SSHVerifyResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.VerifyWithContext(context.Background(), otp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyWithContext the same as Verify but with a custom context.
|
|
|
|
func (c *SSHHelper) VerifyWithContext(ctx context.Context, otp string) (*SSHVerifyResponse, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2015-08-12 20:48:58 +03:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"otp": otp,
|
|
|
|
}
|
|
|
|
verifyPath := fmt.Sprintf("/v1/%s/verify", c.MountPoint)
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, verifyPath)
|
2015-08-12 20:48:58 +03:00
|
|
|
if err := r.SetJSONBody(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-08-12 20:48:58 +03:00
|
|
|
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
|
|
|
|
}
|