2015-06-24 22:13:12 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-06-30 20:30:13 +00:00
|
|
|
"fmt"
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-07-29 18:21:36 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-07-29 18:21:36 +00:00
|
|
|
type sshHostKey struct {
|
2015-08-03 20:18:14 +00:00
|
|
|
Key string `json:"key"`
|
2015-07-29 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
func pathKeys(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "keys/" + framework.GenericNameRegex("key_name"),
|
2015-06-24 22:13:12 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2015-08-13 00:36:27 +00:00
|
|
|
"key_name": &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 key",
|
2015-06-24 22:13:12 +00:00
|
|
|
},
|
|
|
|
"key": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-08-14 19:41:26 +00:00
|
|
|
Description: "[Required] SSH private key with super user privileges in host",
|
2015-06-24 22:13:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-08-19 20:48:32 +00:00
|
|
|
logical.UpdateOperation: b.pathKeysWrite,
|
2015-06-24 22:13:12 +00:00
|
|
|
logical.DeleteOperation: b.pathKeysDelete,
|
|
|
|
},
|
2015-06-30 20:30:13 +00:00
|
|
|
HelpSynopsis: pathKeysSyn,
|
|
|
|
HelpDescription: pathKeysDesc,
|
2015-06-24 22:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) getKey(ctx context.Context, s logical.Storage, n string) (*sshHostKey, error) {
|
|
|
|
entry, err := s.Get(ctx, "keys/"+n)
|
2015-06-24 22:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 20:18:14 +00:00
|
|
|
var result sshHostKey
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-13 21:18:30 +00:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathKeysDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-08-13 00:36:27 +00:00
|
|
|
keyName := d.Get("key_name").(string)
|
2015-07-02 00:35:11 +00:00
|
|
|
keyPath := fmt.Sprintf("keys/%s", keyName)
|
2018-01-19 06:44:44 +00:00
|
|
|
err := req.Storage.Delete(ctx, keyPath)
|
2015-06-24 22:13:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathKeysWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-08-13 00:36:27 +00:00
|
|
|
keyName := d.Get("key_name").(string)
|
2015-08-14 19:41:26 +00:00
|
|
|
if keyName == "" {
|
|
|
|
return logical.ErrorResponse("Missing key_name"), nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 22:13:12 +00:00
|
|
|
keyString := d.Get("key").(string)
|
2015-06-30 20:30:13 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Check if the key provided is infact a private key
|
2015-07-29 18:21:36 +00:00
|
|
|
signer, err := ssh.ParsePrivateKey([]byte(keyString))
|
|
|
|
if err != nil || signer == nil {
|
|
|
|
return logical.ErrorResponse("Invalid key"), nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
if keyString == "" {
|
2015-07-02 01:26:42 +00:00
|
|
|
return logical.ErrorResponse("Missing key"), nil
|
2015-06-30 20:30:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 00:35:11 +00:00
|
|
|
keyPath := fmt.Sprintf("keys/%s", keyName)
|
2015-06-24 22:13:12 +00:00
|
|
|
|
2015-08-18 01:22:03 +00:00
|
|
|
// Store the key
|
2015-08-03 20:18:14 +00:00
|
|
|
entry, err := logical.StorageEntryJSON(keyPath, map[string]interface{}{
|
|
|
|
"key": keyString,
|
2015-06-24 22:13:12 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-06-24 22:13:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
const pathKeysSyn = `
|
2015-08-18 01:22:03 +00:00
|
|
|
Register a shared private key with Vault.
|
2015-06-24 22:13:12 +00:00
|
|
|
`
|
|
|
|
|
2015-06-30 20:30:13 +00:00
|
|
|
const pathKeysDesc = `
|
2015-08-18 01:22:03 +00:00
|
|
|
Vault uses this key to install and uninstall dynamic keys in remote hosts. This
|
|
|
|
key should have sudoer privileges in remote hosts. This enables installing keys
|
2015-07-27 17:02:31 +00:00
|
|
|
for unprivileged usernames.
|
|
|
|
|
2015-09-21 20:12:38 +00:00
|
|
|
If this backend is mounted as "ssh", then the endpoint for registering shared
|
|
|
|
key is "ssh/keys/<name>". The name given here can be associated with any number
|
|
|
|
of roles via the endpoint "ssh/roles/".
|
2015-06-24 22:13:12 +00:00
|
|
|
`
|