2015-06-26 18:08:03 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-08-06 18:48:19 +00:00
|
|
|
"bytes"
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-06-26 18:08:03 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2015-07-02 01:26:42 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
2015-08-06 18:48:19 +00:00
|
|
|
"time"
|
2015-07-02 01:26:42 +00:00
|
|
|
|
2021-10-21 13:32:03 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-06-26 18:08:03 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Creates a new RSA key pair with the given key length. The private key will be
|
|
|
|
// of pem format and the public key will be of OpenSSH format.
|
2015-07-29 18:21:36 +00:00
|
|
|
func generateRSAKeys(keyBits int) (publicKeyRsa string, privateKeyRsa string, err error) {
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, keyBits)
|
2015-06-26 18:08:03 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return "", "", fmt.Errorf("error generating RSA key-pair: %w", err)
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
privateKeyRsa = string(pem.EncodeToMemory(&pem.Block{
|
2015-06-26 18:08:03 +00:00
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
|
|
|
}))
|
|
|
|
|
|
|
|
sshPublicKey, err := ssh.NewPublicKey(privateKey.Public())
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return "", "", fmt.Errorf("error generating RSA key-pair: %w", err)
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
2015-06-30 20:30:13 +00:00
|
|
|
publicKeyRsa = "ssh-rsa " + base64.StdEncoding.EncodeToString(sshPublicKey.Marshal())
|
|
|
|
return
|
2015-06-26 18:08:03 +00:00
|
|
|
}
|
2015-07-02 01:26:42 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Public key and the script to install the key are uploaded to remote machine.
|
|
|
|
// Public key is either added or removed from authorized_keys file using the
|
|
|
|
// script. Default script is for a Linux machine and hence the path of the
|
|
|
|
// authorized_keys file is hard coded to resemble Linux.
|
|
|
|
//
|
|
|
|
// The last param 'install' if false, uninstalls the key.
|
2018-03-08 19:21:11 +00:00
|
|
|
func (b *backend) installPublicKeyInTarget(ctx context.Context, adminUser, username, ip string, port int, hostkey, dynamicPublicKey, installScript string, install bool) error {
|
2015-08-18 01:22:03 +00:00
|
|
|
// Transfer the newly generated public key to remote host under a random
|
|
|
|
// file name. This is to avoid name collisions from other requests.
|
2018-03-08 19:21:11 +00:00
|
|
|
_, publicKeyFileName, err := b.GenerateSaltedOTP(ctx)
|
2016-01-13 18:40:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
comm, err := createSSHComm(b.Logger(), adminUser, ip, port, hostkey)
|
2016-01-19 06:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer comm.Close()
|
|
|
|
|
|
|
|
err = comm.Upload(publicKeyFileName, bytes.NewBufferString(dynamicPublicKey), nil)
|
2015-08-18 01:22:03 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error uploading public key: %w", err)
|
2015-08-18 01:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer the script required to install or uninstall the key to the remote
|
|
|
|
// host under a random file name as well. This is to avoid name collisions
|
|
|
|
// from other requests.
|
|
|
|
scriptFileName := fmt.Sprintf("%s.sh", publicKeyFileName)
|
2016-01-19 06:59:08 +00:00
|
|
|
err = comm.Upload(scriptFileName, bytes.NewBufferString(installScript), nil)
|
2015-08-18 01:22:03 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error uploading install script: %w", err)
|
2015-08-18 01:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a session to run remote command that triggers the script to install
|
|
|
|
// or uninstall the key.
|
2016-01-19 06:59:08 +00:00
|
|
|
session, err := comm.NewSession()
|
2015-07-02 21:23:09 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("unable to create SSH Session using public keys: %w", err)
|
2015-07-02 21:23:09 +00:00
|
|
|
}
|
|
|
|
if session == nil {
|
|
|
|
return fmt.Errorf("invalid session object")
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
|
2015-07-10 22:18:02 +00:00
|
|
|
authKeysFileName := fmt.Sprintf("/home/%s/.ssh/authorized_keys", username)
|
2015-07-02 21:23:09 +00:00
|
|
|
|
2015-08-13 17:36:31 +00:00
|
|
|
var installOption string
|
|
|
|
if install {
|
|
|
|
installOption = "install"
|
|
|
|
} else {
|
|
|
|
installOption = "uninstall"
|
2015-07-02 21:23:09 +00:00
|
|
|
}
|
2015-08-18 01:22:03 +00:00
|
|
|
|
2015-08-06 19:50:12 +00:00
|
|
|
// Give execute permissions to install script, run and delete it.
|
|
|
|
chmodCmd := fmt.Sprintf("chmod +x %s", scriptFileName)
|
2015-08-13 17:36:31 +00:00
|
|
|
scriptCmd := fmt.Sprintf("./%s %s %s %s", scriptFileName, installOption, publicKeyFileName, authKeysFileName)
|
2015-08-06 19:50:12 +00:00
|
|
|
rmCmd := fmt.Sprintf("rm -f %s", scriptFileName)
|
|
|
|
targetCmd := fmt.Sprintf("%s;%s;%s", chmodCmd, scriptCmd, rmCmd)
|
|
|
|
|
2020-06-11 12:10:13 +00:00
|
|
|
return session.Run(targetCmd)
|
2015-07-02 21:23:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Takes an IP address and role name and checks if the IP is part
|
|
|
|
// of CIDR blocks belonging to the role.
|
2018-01-19 06:44:44 +00:00
|
|
|
func roleContainsIP(ctx context.Context, s logical.Storage, roleName string, ip string) (bool, error) {
|
2015-07-02 01:26:42 +00:00
|
|
|
if roleName == "" {
|
|
|
|
return false, fmt.Errorf("missing role name")
|
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2015-07-02 01:26:42 +00:00
|
|
|
if ip == "" {
|
|
|
|
return false, fmt.Errorf("missing ip")
|
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
roleEntry, err := s.Get(ctx, fmt.Sprintf("roles/%s", roleName))
|
2015-07-02 01:26:42 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return false, fmt.Errorf("error retrieving role %w", err)
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
|
|
|
if roleEntry == nil {
|
2016-10-18 16:46:54 +00:00
|
|
|
return false, fmt.Errorf("role %q not found", roleName)
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
2015-07-27 20:42:03 +00:00
|
|
|
|
2015-07-02 01:26:42 +00:00
|
|
|
var role sshRole
|
|
|
|
if err := roleEntry.DecodeJSON(&role); err != nil {
|
2016-10-18 16:46:54 +00:00
|
|
|
return false, fmt.Errorf("error decoding role %q", roleName)
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
|
2015-08-28 03:19:55 +00:00
|
|
|
if matched, err := cidrListContainsIP(ip, role.CIDRList); err != nil {
|
2015-07-02 21:23:09 +00:00
|
|
|
return false, err
|
|
|
|
} else {
|
|
|
|
return matched, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 20:42:03 +00:00
|
|
|
// Returns true if the IP supplied by the user is part of the comma
|
|
|
|
// separated CIDR blocks
|
2015-08-28 03:19:55 +00:00
|
|
|
func cidrListContainsIP(ip, cidrList string) (bool, error) {
|
2015-08-29 19:24:15 +00:00
|
|
|
if len(cidrList) == 0 {
|
|
|
|
return false, fmt.Errorf("IP does not belong to role")
|
|
|
|
}
|
2015-08-13 15:46:55 +00:00
|
|
|
for _, item := range strings.Split(cidrList, ",") {
|
2015-07-02 01:26:42 +00:00
|
|
|
_, cidrIPNet, err := net.ParseCIDR(item)
|
|
|
|
if err != nil {
|
2016-10-18 16:46:54 +00:00
|
|
|
return false, fmt.Errorf("invalid CIDR entry %q", item)
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
if cidrIPNet.Contains(net.ParseIP(ip)) {
|
|
|
|
return true, nil
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
return false, nil
|
2015-07-02 01:26:42 +00:00
|
|
|
}
|
2015-08-06 18:48:19 +00:00
|
|
|
|
2020-06-24 12:40:19 +00:00
|
|
|
func insecureIgnoreHostWarning(logger log.Logger) ssh.HostKeyCallback {
|
|
|
|
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
|
|
logger.Warn("cannot verify server key: host key validation disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:45:17 +00:00
|
|
|
func createSSHComm(logger log.Logger, username, ip string, port int, hostkey string) (*comm, error) {
|
2015-08-06 18:48:19 +00:00
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(hostkey))
|
2016-01-19 06:59:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:48:19 +00:00
|
|
|
clientConfig := &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(signer),
|
|
|
|
},
|
2020-06-24 12:40:19 +00:00
|
|
|
HostKeyCallback: insecureIgnoreHostWarning(logger),
|
2022-05-16 16:36:47 +00:00
|
|
|
Timeout: 1 * time.Minute,
|
2015-08-06 18:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connfunc := func() (net.Conn, error) {
|
2015-08-13 15:46:55 +00:00
|
|
|
c, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), 15*time.Second)
|
2015-08-06 18:48:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcpConn, ok := c.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
tcpConn.SetKeepAlivePeriod(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
2015-08-13 15:46:55 +00:00
|
|
|
config := &SSHCommConfig{
|
2015-08-06 18:48:19 +00:00
|
|
|
SSHConfig: clientConfig,
|
|
|
|
Connection: connfunc,
|
|
|
|
Pty: false,
|
|
|
|
DisableAgent: true,
|
2016-08-19 20:45:17 +00:00
|
|
|
Logger: logger,
|
2015-08-06 18:48:19 +00:00
|
|
|
}
|
2016-01-19 06:59:08 +00:00
|
|
|
|
|
|
|
return SSHCommNew(fmt.Sprintf("%s:%d", ip, port), config)
|
2015-08-06 18:48:19 +00:00
|
|
|
}
|
2016-12-26 14:03:27 +00:00
|
|
|
|
2017-02-28 22:08:10 +00:00
|
|
|
func parsePublicSSHKey(key string) (ssh.PublicKey, error) {
|
|
|
|
keyParts := strings.Split(key, " ")
|
|
|
|
if len(keyParts) > 1 {
|
|
|
|
// Someone has sent the 'full' public key rather than just the base64 encoded part that the ssh library wants
|
|
|
|
key = keyParts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.StdEncoding.DecodeString(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssh.ParsePublicKey([]byte(decodedKey))
|
|
|
|
}
|
|
|
|
|
2016-12-26 14:03:27 +00:00
|
|
|
func convertMapToStringValue(initial map[string]interface{}) map[string]string {
|
|
|
|
result := map[string]string{}
|
|
|
|
for key, value := range initial {
|
|
|
|
result[key] = fmt.Sprintf("%v", value)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2017-06-29 03:05:06 +00:00
|
|
|
|
2022-02-17 20:36:56 +00:00
|
|
|
func convertMapToIntSlice(initial map[string]interface{}) (map[string][]int, error) {
|
|
|
|
result := map[string][]int{}
|
|
|
|
|
2019-02-11 18:03:26 +00:00
|
|
|
for key, value := range initial {
|
2022-02-17 20:36:56 +00:00
|
|
|
sliced, err := parseutil.ParseIntSlice(value)
|
2019-02-11 18:03:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-17 20:36:56 +00:00
|
|
|
|
|
|
|
result[key] = make([]int, 0, len(sliced))
|
|
|
|
for _, value := range sliced {
|
|
|
|
result[key] = append(result[key], int(value))
|
|
|
|
}
|
2019-02-11 18:03:26 +00:00
|
|
|
}
|
2022-02-17 20:36:56 +00:00
|
|
|
|
2019-02-11 18:03:26 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 03:05:06 +00:00
|
|
|
// Serve a template processor for custom format inputs
|
|
|
|
func substQuery(tpl string, data map[string]string) string {
|
|
|
|
for k, v := range data {
|
|
|
|
tpl = strings.Replace(tpl, fmt.Sprintf("{{%s}}", k), v, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpl
|
|
|
|
}
|