2016-05-13 20:42:09 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-05-13 20:42:09 +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"
|
2016-05-13 20:42:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathConfigLease(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/lease",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2016-07-19 16:35:14 +00:00
|
|
|
"ttl": {
|
2016-07-08 01:48:44 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2016-05-13 20:42:09 +00:00
|
|
|
Description: "Default ttl for credentials.",
|
|
|
|
},
|
|
|
|
|
2016-07-19 16:35:14 +00:00
|
|
|
"max_ttl": {
|
2016-07-08 01:48:44 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
2016-05-13 20:42:09 +00:00
|
|
|
Description: "Maximum time a set of credentials can be valid for.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathConfigLeaseRead,
|
|
|
|
logical.UpdateOperation: b.pathConfigLeaseWrite,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigLeaseHelpSyn,
|
|
|
|
HelpDescription: pathConfigLeaseHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigLeaseWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-05-13 20:42:09 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/lease", &configLease{
|
2016-07-08 01:48:44 +00:00
|
|
|
TTL: time.Second * time.Duration(d.Get("ttl").(int)),
|
|
|
|
MaxTTL: time.Second * time.Duration(d.Get("max_ttl").(int)),
|
2016-05-13 20:42:09 +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 {
|
2016-05-13 20:42:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigLeaseRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
|
2016-05-13 20:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if leaseConfig == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2016-07-19 15:23:56 +00:00
|
|
|
"ttl": leaseConfig.TTL.Seconds(),
|
|
|
|
"max_ttl": leaseConfig.MaxTTL.Seconds(),
|
2016-05-13 20:42:09 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type configLease struct {
|
|
|
|
TTL time.Duration
|
2016-06-30 13:57:43 +00:00
|
|
|
MaxTTL time.Duration
|
2016-05-13 20:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpSyn = `
|
|
|
|
Configure the default lease TTL settings for credentials
|
|
|
|
generated by the mongodb backend.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpDesc = `
|
|
|
|
This configures the default lease TTL settings used for
|
|
|
|
credentials generated by this backend. The ttl specifies the
|
|
|
|
duration that a set of credentials will be valid for before
|
|
|
|
the lease must be renewed (if it is renewable), while the
|
2016-06-30 13:57:43 +00:00
|
|
|
max_ttl specifies the overall maximum duration that the
|
2016-05-13 20:42:09 +00:00
|
|
|
credentials will be valid regardless of lease renewals.
|
|
|
|
|
|
|
|
The format for the TTL values is an integer and then unit. For
|
|
|
|
example, the value "1h" specifies a 1-hour TTL. The longest
|
|
|
|
supported unit is hours.
|
|
|
|
`
|