2016-12-26 14:03:27 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-12-26 14:03:27 +00:00
|
|
|
"fmt"
|
2017-03-01 20:50:23 +00:00
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-12-26 14:03:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathSign(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2020-01-21 10:46:29 +00:00
|
|
|
Pattern: "sign/" + framework.GenericNameWithAtRegex("role"),
|
2016-12-26 14:03:27 +00:00
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.UpdateOperation: b.pathSign,
|
|
|
|
},
|
|
|
|
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"role": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `The desired role with configuration for this request.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"ttl": {
|
2017-10-30 20:05:47 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2016-12-26 14:03:27 +00:00
|
|
|
Description: `The requested Time To Live for the SSH certificate;
|
|
|
|
sets the expiration date. If not specified
|
|
|
|
the role default, backend default, or system
|
|
|
|
default TTL is used, in that order. Cannot
|
|
|
|
be later than the role max TTL.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"public_key": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `SSH public key that should be signed.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"valid_principals": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Valid principals, either usernames or hostnames, that the certificate should be signed for.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"cert_type": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Type of certificate to be created; either "user" or "host".`,
|
|
|
|
Default: "user",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"key_id": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Key id that the created certificate should have. If not specified, the display name of the token will be used.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"critical_options": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeMap,
|
|
|
|
Description: `Critical options that the certificate should be signed for.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"extensions": {
|
2016-12-26 14:03:27 +00:00
|
|
|
Type: framework.TypeMap,
|
|
|
|
Description: `Extensions that the certificate should be signed for.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: `Request signing an SSH key using a certain role with the provided details.`,
|
|
|
|
HelpDescription: `This path allows SSH keys to be signed according to the policy of the given role.`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathSign(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-12-26 14:03:27 +00:00
|
|
|
roleName := data.Get("role").(string)
|
|
|
|
|
|
|
|
// Get the role
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.getRole(ctx, req.Storage, roleName)
|
2016-12-26 14:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("Unknown role: %s", roleName)), nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return b.pathSignCertificate(ctx, req, data, role)
|
2016-12-26 14:03:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) pathSignCertificate(ctx context.Context, req *logical.Request, data *framework.FieldData, role *sshRole) (*logical.Response, error) {
|
2016-12-26 14:03:27 +00:00
|
|
|
publicKey := data.Get("public_key").(string)
|
|
|
|
if publicKey == "" {
|
2017-03-02 20:38:34 +00:00
|
|
|
return logical.ErrorResponse("missing public_key"), nil
|
2016-12-26 14:03:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 22:08:10 +00:00
|
|
|
userPublicKey, err := parsePublicSSHKey(publicKey)
|
2016-12-26 14:03:27 +00:00
|
|
|
if err != nil {
|
2017-03-08 22:36:21 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("failed to parse public_key as SSH key: %s", err)), nil
|
2016-12-26 14:03:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 18:03:26 +00:00
|
|
|
err = b.validateSignedKeyRequirements(userPublicKey, role)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("public_key failed to meet the key requirements: %s", err)), nil
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:21:29 +00:00
|
|
|
return b.pathSignIssueCertificateHelper(ctx, req, data, role, userPublicKey)
|
2016-12-26 14:03:27 +00:00
|
|
|
}
|