2015-06-17 16:39:49 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-18 00:33:03 +00:00
|
|
|
"fmt"
|
2015-06-26 01:47:32 +00:00
|
|
|
"net"
|
2015-08-13 21:18:30 +00:00
|
|
|
"strings"
|
2015-08-13 00:10:35 +00:00
|
|
|
"time"
|
2015-06-17 16:39:49 +00:00
|
|
|
|
2015-07-22 18:15:19 +00:00
|
|
|
"github.com/hashicorp/vault/helper/uuid"
|
2015-06-17 16:39:49 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-07-29 18:21:36 +00:00
|
|
|
type sshOTP struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
}
|
|
|
|
|
2015-07-24 16:13:26 +00:00
|
|
|
func pathCredsCreate(b *backend) *framework.Path {
|
2015-06-17 16:39:49 +00:00
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("role"),
|
2015-06-17 16:39:49 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2015-08-13 00:36:27 +00:00
|
|
|
"role": &framework.FieldSchema{
|
2015-06-24 22:13:12 +00:00
|
|
|
Type: framework.TypeString,
|
2015-08-14 19:41:26 +00:00
|
|
|
Description: "[Required] Name of the role",
|
2015-06-24 22:13:12 +00:00
|
|
|
},
|
2015-06-17 16:39:49 +00:00
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-08-14 19:41:26 +00:00
|
|
|
Description: "[Optional] Username in remote host",
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
2015-06-24 22:13:12 +00:00
|
|
|
"ip": &framework.FieldSchema{
|
2015-06-17 16:39:49 +00:00
|
|
|
Type: framework.TypeString,
|
2015-08-14 19:41:26 +00:00
|
|
|
Description: "[Required] IP of the remote host",
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2015-07-27 17:02:31 +00:00
|
|
|
logical.WriteOperation: b.pathCredsCreateWrite,
|
2015-06-17 16:39:49 +00:00
|
|
|
},
|
2015-07-27 17:02:31 +00:00
|
|
|
HelpSynopsis: pathCredsCreateHelpSyn,
|
|
|
|
HelpDescription: pathCredsCreateHelpDesc,
|
2015-06-17 16:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:02:31 +00:00
|
|
|
func (b *backend) pathCredsCreateWrite(
|
2015-06-17 16:39:49 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-08-13 00:36:27 +00:00
|
|
|
roleName := d.Get("role").(string)
|
2015-06-30 20:30:13 +00:00
|
|
|
if roleName == "" {
|
2015-08-13 00:36:27 +00:00
|
|
|
return logical.ErrorResponse("Missing role"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
2015-07-27 17:02:31 +00:00
|
|
|
|
|
|
|
ipRaw := d.Get("ip").(string)
|
2015-06-30 20:30:13 +00:00
|
|
|
if ipRaw == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing ip"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
2015-06-18 00:33:03 +00:00
|
|
|
|
2015-08-13 18:12:30 +00:00
|
|
|
role, err := b.getRole(req.Storage, roleName)
|
2015-06-19 00:48:41 +00:00
|
|
|
if err != nil {
|
2015-06-24 22:13:12 +00:00
|
|
|
return nil, fmt.Errorf("error retrieving role: %s", err)
|
|
|
|
}
|
2015-08-13 18:12:30 +00:00
|
|
|
if role == nil {
|
2015-06-24 22:13:12 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Role '%s' not found", roleName)), nil
|
|
|
|
}
|
2015-06-19 00:48:41 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// username is an optional parameter.
|
2015-08-13 21:18:30 +00:00
|
|
|
username := d.Get("username").(string)
|
2015-08-18 01:22:03 +00:00
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Set the default username
|
2015-06-30 20:30:13 +00:00
|
|
|
if username == "" {
|
2015-07-27 19:03:10 +00:00
|
|
|
if role.DefaultUser == "" {
|
|
|
|
return logical.ErrorResponse("No default username registered. Use 'username' option"), nil
|
|
|
|
}
|
2015-06-30 20:30:13 +00:00
|
|
|
username = role.DefaultUser
|
|
|
|
}
|
|
|
|
|
2015-08-13 21:18:30 +00:00
|
|
|
if role.AllowedUsers != "" {
|
|
|
|
// Check if the username is present in allowed users list.
|
|
|
|
err := validateUsername(username, role.AllowedUsers)
|
|
|
|
|
|
|
|
// If username is not present in allowed users list, check if it
|
|
|
|
// is the default username in the role. If neither is true, then
|
|
|
|
// that username is not allowed to generate a credential.
|
|
|
|
if err != nil && username != role.DefaultUser {
|
|
|
|
return logical.ErrorResponse("Username is not present is allowed users list."), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Validate the IP address
|
2015-06-26 01:47:32 +00:00
|
|
|
ipAddr := net.ParseIP(ipRaw)
|
|
|
|
if ipAddr == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid IP '%s'", ipRaw)), nil
|
|
|
|
}
|
2015-08-13 21:18:30 +00:00
|
|
|
|
|
|
|
// Check if the IP belongs to the registered list of CIDR blocks under the role
|
2015-06-26 01:47:32 +00:00
|
|
|
ip := ipAddr.String()
|
2015-08-13 15:46:55 +00:00
|
|
|
ipMatched, err := cidrContainsIP(ip, role.CIDRList)
|
2015-07-02 21:23:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Error validating IP: %s", err)), nil
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
|
|
|
if !ipMatched {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("IP[%s] does not belong to role[%s]", ip, roleName)), nil
|
|
|
|
}
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-07-22 18:15:19 +00:00
|
|
|
var result *logical.Response
|
|
|
|
if role.KeyType == KeyTypeOTP {
|
2015-08-18 01:22:03 +00:00
|
|
|
// Generate an OTP
|
2015-07-29 18:21:36 +00:00
|
|
|
otp, err := b.GenerateOTPCredential(req, username, ip)
|
2015-07-22 18:15:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// Return the information relevant to user of OTP type and save
|
|
|
|
// the data required for later use in the internal section of secret.
|
|
|
|
// In this case, saving just the OTP is sufficient since there is
|
|
|
|
// no need to establish connection with the remote host.
|
2015-07-22 18:15:19 +00:00
|
|
|
result = b.Secret(SecretOTPType).Response(map[string]interface{}{
|
2015-07-23 21:20:28 +00:00
|
|
|
"key_type": role.KeyType,
|
|
|
|
"key": otp,
|
2015-08-13 23:55:47 +00:00
|
|
|
"username": username,
|
|
|
|
"ip": ip,
|
|
|
|
"port": role.Port,
|
2015-07-22 18:15:19 +00:00
|
|
|
}, map[string]interface{}{
|
|
|
|
"otp": otp,
|
|
|
|
})
|
|
|
|
} else if role.KeyType == KeyTypeDynamic {
|
2015-08-18 01:22:03 +00:00
|
|
|
// Generate an RSA key pair. This also installs the newly generated
|
|
|
|
// public key in the remote host.
|
2015-08-13 18:12:30 +00:00
|
|
|
dynamicPublicKey, dynamicPrivateKey, err := b.GenerateDynamicCredential(req, role, username, ip)
|
2015-07-22 18:15:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// Return the information relevant to user of dynamic type and save
|
|
|
|
// information required for later use in internal section of secret.
|
2015-07-22 18:15:19 +00:00
|
|
|
result = b.Secret(SecretDynamicKeyType).Response(map[string]interface{}{
|
2015-07-23 21:20:28 +00:00
|
|
|
"key": dynamicPrivateKey,
|
|
|
|
"key_type": role.KeyType,
|
2015-08-13 23:55:47 +00:00
|
|
|
"username": username,
|
|
|
|
"ip": ip,
|
|
|
|
"port": role.Port,
|
2015-07-22 18:15:19 +00:00
|
|
|
}, map[string]interface{}{
|
2015-07-27 19:03:10 +00:00
|
|
|
"admin_user": role.AdminUser,
|
2015-07-22 18:15:19 +00:00
|
|
|
"username": username,
|
|
|
|
"ip": ip,
|
|
|
|
"host_key_name": role.KeyName,
|
|
|
|
"dynamic_public_key": dynamicPublicKey,
|
|
|
|
"port": role.Port,
|
2015-08-06 19:50:12 +00:00
|
|
|
"install_script": role.InstallScript,
|
2015-07-22 18:15:19 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("key type unknown")
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
2015-06-29 15:49:34 +00:00
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Change the lease information to reflect user's choice
|
2015-07-01 00:21:41 +00:00
|
|
|
lease, _ := b.Lease(req.Storage)
|
2015-07-31 17:24:23 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// If the lease information is set, update it in secret.
|
2015-07-01 00:21:41 +00:00
|
|
|
if lease != nil {
|
2015-08-21 00:47:17 +00:00
|
|
|
result.Secret.TTL = lease.Lease
|
|
|
|
result.Secret.GracePeriod = lease.LeaseMax
|
2015-07-01 00:21:41 +00:00
|
|
|
}
|
2015-07-31 17:24:23 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// If lease information is not set, set it to 10 minutes.
|
2015-08-13 00:10:35 +00:00
|
|
|
if lease == nil {
|
2015-08-21 00:47:17 +00:00
|
|
|
result.Secret.TTL = 10 * time.Minute
|
|
|
|
result.Secret.GracePeriod = 2 * time.Minute
|
2015-08-13 00:10:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 00:21:41 +00:00
|
|
|
return result, nil
|
2015-06-26 01:47:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 18:21:36 +00:00
|
|
|
// Generates a RSA key pair and installs it in the remote target
|
|
|
|
func (b *backend) GenerateDynamicCredential(req *logical.Request, role *sshRole, username, ip string) (string, string, error) {
|
|
|
|
// Fetch the host key to be used for dynamic key installation
|
|
|
|
keyEntry, err := req.Storage.Get(fmt.Sprintf("keys/%s", role.KeyName))
|
|
|
|
if err != nil {
|
2015-08-18 01:22:03 +00:00
|
|
|
return "", "", fmt.Errorf("key '%s' not found. err:%s", role.KeyName, err)
|
2015-07-29 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if keyEntry == nil {
|
2015-08-18 01:22:03 +00:00
|
|
|
return "", "", fmt.Errorf("key '%s' not found", role.KeyName)
|
2015-07-29 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hostKey sshHostKey
|
|
|
|
if err := keyEntry.DecodeJSON(&hostKey); err != nil {
|
|
|
|
return "", "", fmt.Errorf("error reading the host key: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Generate a new RSA key pair with the given key length.
|
2015-08-13 17:36:31 +00:00
|
|
|
dynamicPublicKey, dynamicPrivateKey, err := generateRSAKeys(role.KeyBits)
|
2015-07-29 18:21:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error generating key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the public key to authorized_keys file in target machine
|
2015-08-18 01:22:03 +00:00
|
|
|
err = b.installPublicKeyInTarget(role.AdminUser, username, ip, role.Port, hostKey.Key, dynamicPublicKey, role.InstallScript, true)
|
2015-07-29 18:21:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error adding public key to authorized_keys file in target")
|
|
|
|
}
|
|
|
|
return dynamicPublicKey, dynamicPrivateKey, nil
|
2015-07-22 18:15:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 18:12:30 +00:00
|
|
|
// Generates a UUID OTP and its salted value based on the salt of the backend.
|
|
|
|
func (b *backend) GenerateSaltedOTP() (string, string) {
|
|
|
|
str := uuid.GenerateUUID()
|
|
|
|
return str, b.salt.SaltID(str)
|
|
|
|
}
|
|
|
|
|
2015-08-13 21:18:30 +00:00
|
|
|
// Generates an UUID OTP and creates an entry for the same in storage backend with its salted string.
|
2015-07-29 18:21:36 +00:00
|
|
|
func (b *backend) GenerateOTPCredential(req *logical.Request, username, ip string) (string, error) {
|
2015-08-13 18:12:30 +00:00
|
|
|
otp, otpSalted := b.GenerateSaltedOTP()
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// Check if there is an entry already created for the newly generated OTP.
|
2015-08-13 21:18:30 +00:00
|
|
|
entry, err := b.getOTP(req.Storage, otpSalted)
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// If entry already exists for the OTP, make sure that new OTP is not
|
|
|
|
// replacing an existing one by recreating new ones until an unused
|
|
|
|
// OTP is generated. It is very unlikely that this is the case and this
|
|
|
|
// code is just for safety.
|
2015-07-29 18:21:36 +00:00
|
|
|
for err == nil && entry != nil {
|
2015-08-13 18:12:30 +00:00
|
|
|
otp, otpSalted = b.GenerateSaltedOTP()
|
2015-08-13 21:18:30 +00:00
|
|
|
entry, err = b.getOTP(req.Storage, otpSalted)
|
2015-07-29 18:21:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 01:22:03 +00:00
|
|
|
|
|
|
|
// Store an entry for the salt of OTP.
|
2015-08-13 21:18:30 +00:00
|
|
|
newEntry, err := logical.StorageEntryJSON("otp/"+otpSalted, sshOTP{
|
2015-07-29 18:21:36 +00:00
|
|
|
Username: username,
|
|
|
|
IP: ip,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-13 21:18:30 +00:00
|
|
|
if err := req.Storage.Put(newEntry); err != nil {
|
2015-07-29 18:21:36 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return otp, nil
|
2015-06-17 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Checks if the username supplied by the user is present in the list of
|
|
|
|
// allowed users registered which creation of role.
|
|
|
|
func validateUsername(username, allowedUsers string) error {
|
|
|
|
userList := strings.Split(allowedUsers, ",")
|
|
|
|
for _, user := range userList {
|
|
|
|
if user == username {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("username not in allowed users list")
|
|
|
|
}
|
|
|
|
|
2015-07-27 17:02:31 +00:00
|
|
|
const pathCredsCreateHelpSyn = `
|
2015-08-13 00:36:27 +00:00
|
|
|
Creates a credential for establishing SSH connection with the remote host.
|
2015-06-17 16:39:49 +00:00
|
|
|
`
|
|
|
|
|
2015-07-27 17:02:31 +00:00
|
|
|
const pathCredsCreateHelpDesc = `
|
|
|
|
This path will generate a new key for establishing SSH session with
|
|
|
|
target host. The key can either be a long lived dynamic key or a One
|
2015-08-21 00:47:17 +00:00
|
|
|
Time Password (OTP), using 'key_type' parameter being 'dynamic' or
|
2015-07-27 17:02:31 +00:00
|
|
|
'otp' respectively. For dynamic keys, a named key should be supplied.
|
|
|
|
Create named key using the 'keys/' endpoint, and this represents the
|
|
|
|
shared SSH key of target host. If this backend is mounted at 'ssh',
|
|
|
|
then "ssh/creds/web" would generate a key for 'web' role.
|
|
|
|
|
|
|
|
Keys will have a lease associated with them. The access keys can be
|
|
|
|
revoked by using the lease ID.
|
2015-06-17 16:39:49 +00:00
|
|
|
`
|