2015-04-19 05:25:37 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-04-19 05:25:37 +00:00
|
|
|
"fmt"
|
|
|
|
"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"
|
2015-04-19 05:25:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathConfigLease(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/lease",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"lease": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Default lease for roles.",
|
|
|
|
},
|
|
|
|
|
|
|
|
"lease_max": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Maximum time a credential is valid for.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-08-19 20:48:32 +00:00
|
|
|
logical.ReadOperation: b.pathLeaseRead,
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathLeaseWrite,
|
2015-04-19 05:25:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigLeaseHelpSyn,
|
|
|
|
HelpDescription: pathConfigLeaseHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lease returns the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) Lease(ctx context.Context, s logical.Storage) (*configLease, error) {
|
|
|
|
entry, err := s.Get(ctx, "config/lease")
|
2015-04-19 05:25:37 +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
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathLeaseWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-19 05:25:37 +00:00
|
|
|
leaseRaw := d.Get("lease").(string)
|
2015-08-13 19:32:40 +00:00
|
|
|
leaseMaxRaw := d.Get("lease_max").(string)
|
2015-04-19 05:25:37 +00:00
|
|
|
|
2015-08-26 16:04:47 +00:00
|
|
|
if len(leaseRaw) == 0 {
|
|
|
|
return logical.ErrorResponse("'lease' is a required parameter"), nil
|
|
|
|
}
|
|
|
|
if len(leaseMaxRaw) == 0 {
|
|
|
|
return logical.ErrorResponse("'lease_max' is a required parameter"), nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:25:37 +00:00
|
|
|
lease, err := time.ParseDuration(leaseRaw)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Invalid lease: %s", err)), nil
|
|
|
|
}
|
|
|
|
leaseMax, err := time.ParseDuration(leaseMaxRaw)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
2015-08-26 16:04:47 +00:00
|
|
|
"Invalid lease_max: %s", err)), nil
|
2015-04-19 05:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
|
|
|
entry, err := logical.StorageEntryJSON("config/lease", &configLease{
|
|
|
|
Lease: lease,
|
|
|
|
LeaseMax: leaseMax,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-04-19 05:25:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
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.Lease(ctx, req.Storage)
|
2015-08-13 19:32:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lease == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"lease": lease.Lease.String(),
|
|
|
|
"lease_max": lease.LeaseMax.String(),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:25:37 +00:00
|
|
|
type configLease struct {
|
|
|
|
Lease time.Duration
|
|
|
|
LeaseMax time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpSyn = `
|
|
|
|
Configure the default lease information for generated credentials.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigLeaseHelpDesc = `
|
|
|
|
This configures the default lease information used for credentials
|
|
|
|
generated by this backend. The lease specifies the duration that a
|
|
|
|
credential will be valid for, as well as the maximum session for
|
|
|
|
a set of credentials.
|
|
|
|
|
|
|
|
The format for the lease is "1h" or integer and then unit. The longest
|
|
|
|
unit is hour.
|
|
|
|
`
|