2017-04-24 19:15:50 +00:00
|
|
|
package awsauth
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-04-06 00:42:26 +00:00
|
|
|
"fmt"
|
2016-05-05 18:28:46 +00:00
|
|
|
"sync/atomic"
|
2016-04-06 00:42:26 +00:00
|
|
|
"time"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
func pathTidyIdentityWhitelist(b *backend) *framework.Path {
|
2016-04-06 00:42:26 +00:00
|
|
|
return &framework.Path{
|
2016-05-12 11:19:29 +00:00
|
|
|
Pattern: "tidy/identity-whitelist$",
|
2016-04-06 00:42:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"safety_buffer": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Default: 259200,
|
|
|
|
Description: `The amount of extra time that must have passed beyond the identity's
|
|
|
|
expiration, before it is removed from the backend storage.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-05-12 11:19:29 +00:00
|
|
|
logical.UpdateOperation: b.pathTidyIdentityWhitelistUpdate,
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
HelpSynopsis: pathTidyIdentityWhitelistSyn,
|
|
|
|
HelpDescription: pathTidyIdentityWhitelistDesc,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
// tidyWhitelistIdentity is used to delete entries in the whitelist that are expired.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) tidyWhitelistIdentity(ctx context.Context, s logical.Storage, safety_buffer int) error {
|
2018-06-09 19:35:22 +00:00
|
|
|
grabbed := atomic.CompareAndSwapUint32(b.tidyWhitelistCASGuard, 0, 1)
|
2016-05-05 18:28:46 +00:00
|
|
|
if grabbed {
|
2018-06-09 19:35:22 +00:00
|
|
|
defer atomic.StoreUint32(b.tidyWhitelistCASGuard, 0)
|
2016-05-05 18:28:46 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("identity whitelist tidy operation already running")
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:42:26 +00:00
|
|
|
bufferDuration := time.Duration(safety_buffer) * time.Second
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
identities, err := s.List(ctx, "whitelist/identity/")
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return err
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, instanceID := range identities {
|
2018-01-19 06:44:44 +00:00
|
|
|
identityEntry, err := s.Get(ctx, "whitelist/identity/"+instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error fetching identity of instanceID %q: {{err}}", instanceID), err)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry == nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return fmt.Errorf("identity entry for instanceID %q is nil", instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry.Value == nil || len(identityEntry.Value) == 0 {
|
2018-04-05 15:49:21 +00:00
|
|
|
return fmt.Errorf("found identity entry for instanceID %q but actual identity is empty", instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result whitelistIdentity
|
|
|
|
if err := identityEntry.DecodeJSON(&result); err != nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return err
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 21:44:14 +00:00
|
|
|
if time.Now().After(result.ExpirationTime.Add(bufferDuration)) {
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := s.Delete(ctx, "whitelist/identity"+instanceID); err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting identity of instanceID %q from storage: {{err}}", instanceID), err)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
// pathTidyIdentityWhitelistUpdate is used to delete entries in the whitelist that are expired.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathTidyIdentityWhitelistUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
return nil, b.tidyWhitelistIdentity(ctx, req.Storage, data.Get("safety_buffer").(int))
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathTidyIdentityWhitelistSyn = `
|
2016-05-03 16:14:07 +00:00
|
|
|
Clean-up the whitelist instance identity entries.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathTidyIdentityWhitelistDesc = `
|
2016-04-06 00:42:26 +00:00
|
|
|
When an instance identity is whitelisted, the expiration time of the whitelist
|
2016-05-12 11:19:29 +00:00
|
|
|
entry is set based on the maximum 'max_ttl' value set on: the role, the role tag
|
2016-04-28 02:03:11 +00:00
|
|
|
and the backend's mount.
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2016-04-28 02:03:11 +00:00
|
|
|
When this endpoint is invoked, all the entries that are expired will be deleted.
|
2016-04-06 00:42:26 +00:00
|
|
|
A 'safety_buffer' (duration in seconds) can be provided, to ensure deletion of
|
|
|
|
only those entries that are expired before 'safety_buffer' seconds.
|
|
|
|
`
|