2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-11-06 15:09:56 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-11-06 15:09:56 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
2017-11-06 15:09:56 +00:00
|
|
|
)
|
|
|
|
|
2017-12-15 22:06:56 +00:00
|
|
|
const leaseConfigKey = "config/lease"
|
|
|
|
|
2017-11-06 15:09:56 +00:00
|
|
|
func pathConfigLease(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/lease",
|
2023-04-07 17:21:21 +00:00
|
|
|
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationPrefix: operationPrefixNomad,
|
|
|
|
},
|
|
|
|
|
2017-11-06 15:09:56 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"ttl": {
|
2017-11-06 15:09:56 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Description: "Duration before which the issued token needs renewal",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"max_ttl": {
|
2017-11-06 15:09:56 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Description: `Duration after which the issued token should not be allowed to be renewed`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-04-07 17:21:21 +00:00
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.ReadOperation: &framework.PathOperation{
|
|
|
|
Callback: b.pathLeaseRead,
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "read",
|
|
|
|
OperationSuffix: "lease-configuration",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
|
|
|
Callback: b.pathLeaseUpdate,
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "configure",
|
|
|
|
OperationSuffix: "lease",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
logical.DeleteOperation: &framework.PathOperation{
|
|
|
|
Callback: b.pathLeaseDelete,
|
|
|
|
DisplayAttrs: &framework.DisplayAttributes{
|
|
|
|
OperationVerb: "delete",
|
|
|
|
OperationSuffix: "lease-configuration",
|
|
|
|
},
|
|
|
|
},
|
2017-11-06 15:09:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigLeaseHelpSyn,
|
|
|
|
HelpDescription: pathConfigLeaseHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the lease configuration parameters
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathLeaseUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2017-11-06 15:09:56 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/lease", &configLease{
|
|
|
|
TTL: time.Second * time.Duration(d.Get("ttl").(int)),
|
|
|
|
MaxTTL: time.Second * time.Duration(d.Get("max_ttl").(int)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2017-11-06 15:09:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathLeaseDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Delete(ctx, leaseConfigKey); err != nil {
|
2017-12-15 22:06:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 15:09:56 +00:00
|
|
|
// Returns the lease configuration parameters
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathLeaseRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
lease, err := b.LeaseConfig(ctx, req.Storage)
|
2017-11-06 15:09:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lease == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
2017-11-06 21:34:20 +00:00
|
|
|
Data: map[string]interface{}{
|
2017-12-15 22:06:56 +00:00
|
|
|
"ttl": int64(lease.TTL.Seconds()),
|
|
|
|
"max_ttl": int64(lease.MaxTTL.Seconds()),
|
2017-11-06 21:34:20 +00:00
|
|
|
},
|
2017-11-06 15:09:56 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-29 11:07:17 +00:00
|
|
|
// Lease returns the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) LeaseConfig(ctx context.Context, s logical.Storage) (*configLease, error) {
|
|
|
|
entry, err := s.Get(ctx, leaseConfigKey)
|
2017-11-29 11:07:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result configLease
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 15:09:56 +00:00
|
|
|
// Lease configuration information for the secrets issued by this backend
|
|
|
|
type configLease struct {
|
2017-11-06 21:34:20 +00:00
|
|
|
TTL time.Duration `json:"ttl" mapstructure:"ttl"`
|
|
|
|
MaxTTL time.Duration `json:"max_ttl" mapstructure:"max_ttl"`
|
2017-11-06 15:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pathConfigLeaseHelpSyn = "Configure the lease parameters for generated tokens"
|
|
|
|
|
|
|
|
var pathConfigLeaseHelpDesc = `
|
|
|
|
Sets the ttl and max_ttl values for the secrets to be issued by this backend.
|
|
|
|
Both ttl and max_ttl takes in an integer number of seconds as input as well as
|
|
|
|
inputs like "1h".
|
|
|
|
`
|