2015-06-16 20:58:54 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-30 20:30:13 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2015-06-16 20:58:54 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-07-02 01:05:52 +00:00
|
|
|
Pattern: "roles/(?P<name>[-\\w]+)",
|
2015-06-16 20:58:54 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role",
|
|
|
|
},
|
2015-06-24 22:13:12 +00:00
|
|
|
"key": &framework.FieldSchema{
|
2015-06-16 20:58:54 +00:00
|
|
|
Type: framework.TypeString,
|
2015-06-24 22:13:12 +00:00
|
|
|
Description: "Named key in Vault",
|
|
|
|
},
|
|
|
|
"admin_user": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Admin user at target address",
|
|
|
|
},
|
|
|
|
"default_user": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Default user to whom the dynamic key is installed",
|
|
|
|
},
|
|
|
|
"cidr": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "CIDR blocks and IP addresses",
|
2015-06-16 20:58:54 +00:00
|
|
|
},
|
2015-07-06 20:56:45 +00:00
|
|
|
"port": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Port number for SSH connection",
|
|
|
|
},
|
2015-07-22 18:15:19 +00:00
|
|
|
"key_type": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "one-time-password or dynamic-key",
|
|
|
|
},
|
2015-06-16 20:58:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathRoleRead,
|
|
|
|
logical.WriteOperation: b.pathRoleWrite,
|
|
|
|
logical.DeleteOperation: b.pathRoleDelete,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:15:19 +00:00
|
|
|
func createOTPRole(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
adminUser := d.Get("admin_user").(string)
|
|
|
|
defaultUser := d.Get("default_user").(string)
|
|
|
|
cidr := d.Get("cidr").(string)
|
|
|
|
port := d.Get("port").(string)
|
|
|
|
if roleName == "" {
|
|
|
|
return logical.ErrorResponse("Missing role name"), nil
|
|
|
|
}
|
|
|
|
if adminUser == "" {
|
|
|
|
return logical.ErrorResponse("Missing admin username"), nil
|
|
|
|
}
|
|
|
|
if cidr == "" {
|
|
|
|
return logical.ErrorResponse("Missing cidr blocks"), nil
|
|
|
|
}
|
|
|
|
for _, item := range strings.Split(cidr, ",") {
|
|
|
|
_, _, err := net.ParseCIDR(item)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid cidr entry '%s'", item)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if defaultUser == "" {
|
|
|
|
defaultUser = adminUser
|
|
|
|
}
|
|
|
|
if port == "" {
|
|
|
|
port = "22"
|
|
|
|
}
|
|
|
|
entry, err := logical.StorageEntryJSON(fmt.Sprintf("policy/%s", roleName), sshRole{
|
|
|
|
AdminUser: adminUser,
|
|
|
|
DefaultUser: defaultUser,
|
|
|
|
CIDR: cidr,
|
|
|
|
Port: port,
|
|
|
|
KeyType: KeyTypeOTP,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDynamicKeyRole(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-24 22:13:12 +00:00
|
|
|
roleName := d.Get("name").(string)
|
|
|
|
keyName := d.Get("key").(string)
|
|
|
|
adminUser := d.Get("admin_user").(string)
|
|
|
|
defaultUser := d.Get("default_user").(string)
|
|
|
|
cidr := d.Get("cidr").(string)
|
2015-07-06 20:56:45 +00:00
|
|
|
port := d.Get("port").(string)
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-07-02 21:23:09 +00:00
|
|
|
// Input validations
|
2015-06-30 20:30:13 +00:00
|
|
|
if roleName == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing role name"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
|
|
|
if keyName == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing key name"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
|
|
|
if adminUser == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing admin username"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
|
|
|
if cidr == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing cidr blocks"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
2015-07-06 20:56:45 +00:00
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
for _, item := range strings.Split(cidr, ",") {
|
|
|
|
_, _, err := net.ParseCIDR(item)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid cidr entry '%s'", item)), nil
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-07-02 00:35:11 +00:00
|
|
|
keyEntry, err := req.Storage.Get(fmt.Sprintf("keys/%s", keyName))
|
2015-07-01 15:58:49 +00:00
|
|
|
if err != nil || keyEntry == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Invalid 'key': '%s'", keyName)), nil
|
|
|
|
}
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
if defaultUser == "" {
|
|
|
|
defaultUser = adminUser
|
|
|
|
}
|
2015-07-06 20:56:45 +00:00
|
|
|
if port == "" {
|
|
|
|
port = "22"
|
|
|
|
}
|
2015-06-30 20:30:13 +00:00
|
|
|
|
2015-07-02 00:35:11 +00:00
|
|
|
entry, err := logical.StorageEntryJSON(fmt.Sprintf("policy/%s", roleName), sshRole{
|
2015-06-24 22:13:12 +00:00
|
|
|
KeyName: keyName,
|
|
|
|
AdminUser: adminUser,
|
|
|
|
DefaultUser: defaultUser,
|
|
|
|
CIDR: cidr,
|
2015-07-06 20:56:45 +00:00
|
|
|
Port: port,
|
2015-07-22 18:15:19 +00:00
|
|
|
KeyType: KeyTypeDynamic,
|
2015-06-24 22:13:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 18:15:19 +00:00
|
|
|
func (b *backend) pathRoleWrite(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
keyType := d.Get("key_type").(string)
|
|
|
|
if keyType == "" {
|
|
|
|
return logical.ErrorResponse("Missing key type"), nil
|
|
|
|
}
|
|
|
|
keyType = strings.ToLower(keyType)
|
|
|
|
if keyType == KeyTypeOTP {
|
|
|
|
return createOTPRole(req, d)
|
|
|
|
} else if keyType == KeyTypeDynamic {
|
|
|
|
return createDynamicKeyRole(req, d)
|
|
|
|
} else {
|
|
|
|
return logical.ErrorResponse("Invalid key type"), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:48:41 +00:00
|
|
|
func (b *backend) pathRoleRead(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-24 22:13:12 +00:00
|
|
|
roleName := d.Get("name").(string)
|
2015-07-02 21:23:09 +00:00
|
|
|
roleEntry, err := req.Storage.Get(fmt.Sprintf("policy/%s", roleName))
|
2015-06-19 00:48:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
if roleEntry == nil {
|
2015-06-19 00:48:41 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-07-02 21:23:09 +00:00
|
|
|
var role sshRole
|
|
|
|
if err := roleEntry.DecodeJSON(&role); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-19 00:48:41 +00:00
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2015-07-02 21:23:09 +00:00
|
|
|
"key": role.KeyName,
|
|
|
|
"admin_user": role.AdminUser,
|
|
|
|
"default_user": role.DefaultUser,
|
|
|
|
"cidr": role.CIDR,
|
2015-07-06 20:56:45 +00:00
|
|
|
"port": role.Port,
|
2015-07-22 18:15:19 +00:00
|
|
|
"key_type": role.KeyType,
|
2015-06-19 00:48:41 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2015-06-16 20:58:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:48:41 +00:00
|
|
|
func (b *backend) pathRoleDelete(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-06-24 22:13:12 +00:00
|
|
|
roleName := d.Get("name").(string)
|
2015-07-02 00:35:11 +00:00
|
|
|
err := req.Storage.Delete(fmt.Sprintf("policy/%s", roleName))
|
2015-06-19 00:48:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-16 20:58:54 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
type sshRole struct {
|
2015-07-22 18:15:19 +00:00
|
|
|
KeyType string `json:"key_type"`
|
2015-06-24 22:13:12 +00:00
|
|
|
KeyName string `json:"key"`
|
|
|
|
AdminUser string `json:"admin_user"`
|
|
|
|
DefaultUser string `json:"default_user"`
|
2015-07-22 18:15:19 +00:00
|
|
|
CIDR string `json:"cidr"`
|
|
|
|
Port string `json:"port"`
|
2015-06-24 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 18:15:19 +00:00
|
|
|
const KeyTypeOTP = "otp"
|
|
|
|
const KeyTypeDynamic = "dynamic"
|
|
|
|
|
2015-06-16 20:58:54 +00:00
|
|
|
const pathRoleHelpSyn = `
|
2015-06-30 20:30:13 +00:00
|
|
|
Manage the 'roles' that can be created with this backend.
|
2015-06-16 20:58:54 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathRoleHelpDesc = `
|
2015-07-02 01:26:42 +00:00
|
|
|
This path allows you to manage the roles that are used to create
|
|
|
|
dynamic keys. These roles will be having privileged access to all
|
|
|
|
the hosts mentioned by CIDR blocks. For example, if the backend
|
|
|
|
is mounted at "ssh" and the role is created at "ssh/roles/web",
|
|
|
|
then a user could request for a new key at "ssh/creds/web" for the
|
|
|
|
supplied username and IP address.
|
|
|
|
|
|
|
|
The 'cidr' field takes comma seperated CIDR blocks. The 'admin_user'
|
|
|
|
should have root access in all the hosts represented by the 'cidr'
|
|
|
|
field. When the user requests key for an IP, the key will be installed
|
|
|
|
for the user mentioned by 'default_user' field. The 'key' field takes
|
|
|
|
a named key which can be configured by 'ssh/keys/' endpoint.
|
2015-06-16 20:58:54 +00:00
|
|
|
`
|