2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-12-14 18:28:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-03-21 16:19:37 +00:00
|
|
|
)
|
|
|
|
|
2015-05-26 00:33:31 +00:00
|
|
|
const (
|
2016-01-29 22:44:09 +00:00
|
|
|
SecretTokenType = "token"
|
2015-05-26 00:33:31 +00:00
|
|
|
)
|
2015-03-21 16:19:37 +00:00
|
|
|
|
2016-01-29 22:44:09 +00:00
|
|
|
func secretToken(b *backend) *framework.Secret {
|
2015-03-21 16:19:37 +00:00
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretTokenType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"token": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Request token",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-01-29 22:44:09 +00:00
|
|
|
Renew: b.secretTokenRenew,
|
2018-11-02 14:44:12 +00:00
|
|
|
Revoke: b.secretTokenRevoke,
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretTokenRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
2017-12-14 18:28:19 +00:00
|
|
|
roleRaw, ok := req.Secret.InternalData["role"]
|
2018-11-02 14:44:12 +00:00
|
|
|
if !ok {
|
2018-04-03 16:20:20 +00:00
|
|
|
return resp, nil
|
2017-12-14 18:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
role, ok := roleRaw.(string)
|
|
|
|
if !ok {
|
2018-04-03 16:20:20 +00:00
|
|
|
return resp, nil
|
2017-12-14 18:28:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := req.Storage.Get(ctx, "policy/"+role)
|
2017-12-14 18:28:19 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error retrieving role: {{err}}", err)
|
2017-12-14 18:28:19 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf("issuing role %q not found", role)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result roleConfig
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-02 14:44:12 +00:00
|
|
|
resp.Secret.TTL = result.TTL
|
|
|
|
resp.Secret.MaxTTL = result.MaxTTL
|
2018-04-03 16:20:20 +00:00
|
|
|
return resp, nil
|
2016-01-29 22:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) secretTokenRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
c, userErr, intErr := b.client(ctx, req.Storage)
|
2016-05-24 18:23:27 +00:00
|
|
|
if intErr != nil {
|
|
|
|
return nil, intErr
|
|
|
|
}
|
|
|
|
if userErr != nil {
|
|
|
|
// Returning logical.ErrorResponse from revocation function is risky
|
|
|
|
return nil, userErr
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 03:24:10 +00:00
|
|
|
tokenRaw, ok := req.Secret.InternalData["token"]
|
|
|
|
if !ok {
|
2016-05-27 17:09:52 +00:00
|
|
|
// We return nil here because this is a pre-0.5.3 problem and there is
|
|
|
|
// nothing we can do about it. We already can't revoke the lease
|
|
|
|
// properly if it has been renewed and this is documented pre-0.5.3
|
|
|
|
// behavior with a security bulletin about it.
|
|
|
|
return nil, nil
|
2016-05-26 03:24:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
var version string
|
|
|
|
versionRaw, ok := req.Secret.InternalData["version"]
|
|
|
|
if ok {
|
|
|
|
version = versionRaw.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch version {
|
|
|
|
case "":
|
|
|
|
// Pre 1.4 tokens
|
|
|
|
_, err := c.ACL().Destroy(tokenRaw.(string), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case tokenPolicyType:
|
|
|
|
_, err := c.ACL().TokenDelete(tokenRaw.(string), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid version string in data: %s", version)
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|