2016-12-26 14:03:27 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2017-03-02 21:22:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2016-12-26 14:03:27 +00:00
|
|
|
"fmt"
|
2017-03-02 21:22:06 +00:00
|
|
|
|
2016-12-26 14:03:27 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathConfigCA(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/ca",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"private_key": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Private half of the SSH key that will be used to sign certificates.`,
|
|
|
|
},
|
|
|
|
"public_key": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Public half of the SSH key that will be used to sign certificates.`,
|
|
|
|
},
|
2017-03-02 09:32:50 +00:00
|
|
|
"generate_signing_key": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: `Generate SSH key pair internally rather than use the private_key and public_key fields.`,
|
2017-03-02 21:22:06 +00:00
|
|
|
Default: true,
|
2017-03-02 09:32:50 +00:00
|
|
|
},
|
2016-12-26 14:03:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2017-03-03 15:19:45 +00:00
|
|
|
logical.UpdateOperation: b.pathConfigCAUpdate,
|
|
|
|
logical.DeleteOperation: b.pathConfigCADelete,
|
2016-12-26 14:03:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: `Set the SSH private key used for signing certificates.`,
|
|
|
|
HelpDescription: `This sets the CA information used for certificates generated by this
|
|
|
|
by this mount. The fields must be in the standard private and public SSH format.
|
|
|
|
|
|
|
|
For security reasons, the private key cannot be retrieved later.`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
func (b *backend) pathConfigCADelete(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
if err := req.Storage.Delete("config/ca_bundle"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Delete("config/ca_public_key"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathConfigCAUpdate(req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2017-03-02 09:32:50 +00:00
|
|
|
var err error
|
2017-03-02 21:22:06 +00:00
|
|
|
publicKey := data.Get("public_key").(string)
|
|
|
|
privateKey := data.Get("private_key").(string)
|
|
|
|
|
2017-03-02 21:37:03 +00:00
|
|
|
var generateSigningKey bool
|
|
|
|
|
2017-03-02 21:22:06 +00:00
|
|
|
generateSigningKeyRaw, ok := data.GetOk("generate_signing_key")
|
2017-03-02 21:37:03 +00:00
|
|
|
switch {
|
|
|
|
// explicitly set true
|
|
|
|
case ok && generateSigningKeyRaw.(bool):
|
|
|
|
if publicKey != "" || privateKey != "" {
|
|
|
|
return logical.ErrorResponse("public_key and private_key must not be set when generate_signing_key is set to true"), nil
|
2017-03-02 21:22:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 21:37:03 +00:00
|
|
|
generateSigningKey = true
|
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
// explicitly set to false, or not set and we have both a public and private key
|
2017-03-02 21:37:03 +00:00
|
|
|
case ok, publicKey != "" && privateKey != "":
|
2017-03-02 21:22:06 +00:00
|
|
|
if publicKey == "" {
|
|
|
|
return logical.ErrorResponse("missing public_key"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if privateKey == "" {
|
|
|
|
return logical.ErrorResponse("missing private_key"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ssh.ParsePrivateKey([]byte(privateKey))
|
2017-03-02 09:32:50 +00:00
|
|
|
if err != nil {
|
2017-03-02 21:22:06 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unable to parse private_key as an SSH private key: %v", err)), nil
|
2017-03-02 09:32:50 +00:00
|
|
|
}
|
2017-02-28 22:08:10 +00:00
|
|
|
|
2017-03-02 21:22:06 +00:00
|
|
|
_, err = parsePublicSSHKey(publicKey)
|
2017-03-02 09:32:50 +00:00
|
|
|
if err != nil {
|
2017-03-02 21:22:06 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unable to parse public_key as an SSH public key: %v", err)), nil
|
2017-03-02 09:32:50 +00:00
|
|
|
}
|
2017-03-02 21:37:03 +00:00
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
// not set and no public/private key provided so generate
|
2017-03-02 21:37:03 +00:00
|
|
|
case publicKey == "" && privateKey == "":
|
|
|
|
generateSigningKey = true
|
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
// not set, but one or the other supplied
|
|
|
|
default:
|
2017-03-02 21:37:03 +00:00
|
|
|
return logical.ErrorResponse("only one of public_key and private_key set; both must be set to use, or both must be blank to auto-generate"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if generateSigningKey {
|
|
|
|
publicKey, privateKey, err = generateSSHKeyPair()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-28 22:08:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 21:22:06 +00:00
|
|
|
if publicKey == "" || privateKey == "" {
|
|
|
|
return nil, fmt.Errorf("failed to generate or parse the keys")
|
|
|
|
}
|
|
|
|
|
2017-03-03 15:19:45 +00:00
|
|
|
publicKeyEntry, err := req.Storage.Get("config/ca_public_key")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed while reading ca_public_key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeyEntry, err := req.Storage.Get("config/ca_bundle")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed while reading ca_bundle: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if publicKeyEntry != nil || privateKeyEntry != nil {
|
|
|
|
return nil, fmt.Errorf("keys are already configured; delete them before reconfiguring")
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:08:10 +00:00
|
|
|
err = req.Storage.Put(&logical.StorageEntry{
|
2017-03-03 15:19:45 +00:00
|
|
|
Key: "config/ca_public_key",
|
2017-02-28 22:08:10 +00:00
|
|
|
Value: []byte(publicKey),
|
2016-12-26 14:03:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle := signingBundle{
|
2017-02-28 22:08:10 +00:00
|
|
|
Certificate: privateKey,
|
2016-12-26 14:03:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 22:08:10 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/ca_bundle", bundle)
|
2016-12-26 14:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = req.Storage.Put(entry)
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-02 09:32:50 +00:00
|
|
|
|
|
|
|
func generateSSHKeyPair() (string, string, error) {
|
|
|
|
privateSeed, err := rsa.GenerateKey(rand.Reader, 4096)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateBlock := &pem.Block{
|
2017-03-02 21:22:06 +00:00
|
|
|
Type: "RSA PRIVATE KEY",
|
2017-03-02 09:32:50 +00:00
|
|
|
Headers: nil,
|
2017-03-02 21:22:06 +00:00
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateSeed),
|
2017-03-02 09:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public, err := ssh.NewPublicKey(&privateSeed.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(ssh.MarshalAuthorizedKey(public)), string(pem.EncodeToMemory(privateBlock)), nil
|
|
|
|
}
|