open-vault/builtin/logical/mongodb/path_config_lease.go

108 lines
2.6 KiB
Go
Raw Normal View History

2016-05-13 20:42:09 +00:00
package mongodb
import (
"fmt"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathConfigLease(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/lease",
Fields: map[string]*framework.FieldSchema{
"ttl": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Default ttl for credentials.",
},
"max_ttl": &framework.FieldSchema{
2016-05-13 20:42:09 +00:00
Type: framework.TypeString,
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,
}
}
func (b *backend) pathConfigLeaseWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
ttlRaw := d.Get("ttl").(string)
maxTTLRaw := d.Get("max_ttl").(string)
2016-05-13 20:42:09 +00:00
ttl, err := time.ParseDuration(ttlRaw)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Invalid ttl: %s", err)), nil
}
maxTTL, err := time.ParseDuration(maxTTLRaw)
2016-05-13 20:42:09 +00:00
if err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Invalid max_ttl: %s", err)), nil
2016-05-13 20:42:09 +00:00
}
// Store it
entry, err := logical.StorageEntryJSON("config/lease", &configLease{
TTL: ttl,
MaxTTL: maxTTL,
2016-05-13 20:42:09 +00:00
})
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathConfigLeaseRead(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
leaseConfig, err := b.LeaseConfig(req.Storage)
if err != nil {
return nil, err
}
if leaseConfig == nil {
return nil, nil
}
return &logical.Response{
Data: map[string]interface{}{
"ttl": leaseConfig.TTL.String(),
"max_ttl": leaseConfig.MaxTTL.String(),
2016-05-13 20:42:09 +00:00
},
}, nil
}
type configLease struct {
TTL time.Duration
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
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.
`