2016-03-03 14:19:17 +00:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
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{
|
2016-03-11 02:15:18 +00:00
|
|
|
"ttl": &framework.FieldSchema{
|
2016-03-03 14:19:17 +00:00
|
|
|
Type: framework.TypeString,
|
2016-03-11 02:15:18 +00:00
|
|
|
Description: "Default ttl for roles.",
|
2016-03-03 14:19:17 +00:00
|
|
|
},
|
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
"ttl_max": &framework.FieldSchema{
|
2016-09-20 16:39:02 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Deprecated: use "max_ttl" instead. Maximum
|
|
|
|
time a credential is valid for.`,
|
|
|
|
},
|
|
|
|
|
|
|
|
"max_ttl": &framework.FieldSchema{
|
2016-03-03 14:19:17 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Maximum time a credential is valid for.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-03-11 02:15:18 +00:00
|
|
|
logical.ReadOperation: b.pathConfigLeaseRead,
|
|
|
|
logical.UpdateOperation: b.pathConfigLeaseWrite,
|
2016-03-03 14:19:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigLeaseHelpSyn,
|
|
|
|
HelpDescription: pathConfigLeaseHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
func (b *backend) pathConfigLeaseWrite(
|
2016-03-03 14:19:17 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-03-11 02:15:18 +00:00
|
|
|
ttlRaw := d.Get("ttl").(string)
|
2016-09-20 16:39:02 +00:00
|
|
|
ttlMaxRaw := d.Get("max_ttl").(string)
|
|
|
|
if len(ttlMaxRaw) == 0 {
|
|
|
|
ttlMaxRaw = d.Get("ttl_max").(string)
|
|
|
|
}
|
2016-03-03 14:19:17 +00:00
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
ttl, err := time.ParseDuration(ttlRaw)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
2016-03-11 02:15:18 +00:00
|
|
|
"Invalid ttl: %s", err)), nil
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
2016-03-11 02:15:18 +00:00
|
|
|
ttlMax, err := time.ParseDuration(ttlMaxRaw)
|
2016-03-03 14:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
2016-09-20 16:39:02 +00:00
|
|
|
"Invalid max_ttl: %s", err)), nil
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
|
|
|
entry, err := logical.StorageEntryJSON("config/lease", &configLease{
|
2016-03-11 02:15:18 +00:00
|
|
|
TTL: ttl,
|
|
|
|
TTLMax: ttlMax,
|
2016-03-03 14:19:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
func (b *backend) pathConfigLeaseRead(
|
2016-03-03 14:19:17 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-03-11 02:15:18 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(req.Storage)
|
2016-03-03 14:19:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-11 02:15:18 +00:00
|
|
|
if leaseConfig == nil {
|
2016-03-03 14:19:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-09-20 16:39:02 +00:00
|
|
|
resp := &logical.Response{
|
2016-03-03 14:19:17 +00:00
|
|
|
Data: map[string]interface{}{
|
2016-03-11 02:15:18 +00:00
|
|
|
"ttl": leaseConfig.TTL.String(),
|
|
|
|
"ttl_max": leaseConfig.TTLMax.String(),
|
2016-09-20 16:39:02 +00:00
|
|
|
"max_ttl": leaseConfig.TTLMax.String(),
|
2016-03-03 14:19:17 +00:00
|
|
|
},
|
2016-09-20 16:39:02 +00:00
|
|
|
}
|
|
|
|
resp.AddWarning("The field ttl_max is deprecated and will be removed in a future release. Use max_ttl instead.")
|
|
|
|
|
|
|
|
return resp, nil
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type configLease struct {
|
2016-03-11 02:15:18 +00:00
|
|
|
TTL time.Duration
|
|
|
|
TTLMax time.Duration
|
2016-03-03 14:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpSyn = `
|
2016-03-11 02:15:18 +00:00
|
|
|
Configure the default lease ttl for generated credentials.
|
2016-03-03 14:19:17 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpDesc = `
|
2016-03-11 02:15:18 +00:00
|
|
|
This configures the default lease ttl used for credentials
|
|
|
|
generated by this backend. The ttl specifies the duration that a
|
2016-03-03 14:19:17 +00:00
|
|
|
credential will be valid for, as well as the maximum session for
|
|
|
|
a set of credentials.
|
|
|
|
|
2016-03-11 02:15:18 +00:00
|
|
|
The format for the ttl is "1h" or integer and then unit. The longest
|
2016-03-03 14:19:17 +00:00
|
|
|
unit is hour.
|
|
|
|
`
|