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
|
|
|
"time"
|
|
|
|
|
2016-04-28 01:29:32 +00:00
|
|
|
"github.com/fatih/structs"
|
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 pathIdentityWhitelist(b *backend) *framework.Path {
|
2016-04-06 00:42:26 +00:00
|
|
|
return &framework.Path{
|
2016-05-05 17:34:50 +00:00
|
|
|
Pattern: "identity-whitelist/" + framework.GenericNameRegex("instance_id"),
|
2016-04-06 00:42:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"instance_id": &framework.FieldSchema{
|
2016-05-12 11:19:29 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `EC2 instance ID. A successful login operation from an EC2 instance
|
|
|
|
gets cached in this whitelist, keyed off of instance ID.`,
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-05-12 11:19:29 +00:00
|
|
|
logical.ReadOperation: b.pathIdentityWhitelistRead,
|
|
|
|
logical.DeleteOperation: b.pathIdentityWhitelistDelete,
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
HelpSynopsis: pathIdentityWhitelistSyn,
|
|
|
|
HelpDescription: pathIdentityWhitelistDesc,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
func pathListIdentityWhitelist(b *backend) *framework.Path {
|
2016-04-06 00:42:26 +00:00
|
|
|
return &framework.Path{
|
2016-05-05 17:34:50 +00:00
|
|
|
Pattern: "identity-whitelist/?",
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathWhitelistIdentitiesList,
|
|
|
|
},
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
HelpSynopsis: pathListIdentityWhitelistHelpSyn,
|
|
|
|
HelpDescription: pathListIdentityWhitelistHelpDesc,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathWhitelistIdentitiesList is used to list all the instance IDs that are present
|
|
|
|
// in the identity whitelist. This will list both valid and expired entries.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathWhitelistIdentitiesList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-04-06 00:42:26 +00:00
|
|
|
identities, err := req.Storage.List("whitelist/identity/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return logical.ListResponse(identities), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch an item from the whitelist given an instance ID.
|
|
|
|
func whitelistIdentityEntry(s logical.Storage, instanceID string) (*whitelistIdentity, error) {
|
|
|
|
entry, err := s.Get("whitelist/identity/" + instanceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result whitelistIdentity
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stores an instance ID and the information required to validate further login/renewal attempts from
|
|
|
|
// the same instance ID.
|
|
|
|
func setWhitelistIdentityEntry(s logical.Storage, instanceID string, identity *whitelistIdentity) error {
|
|
|
|
entry, err := logical.StorageEntryJSON("whitelist/identity/"+instanceID, identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Put(entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
// pathIdentityWhitelistDelete is used to delete an entry from the identity whitelist given an instance ID.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathIdentityWhitelistDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-04-06 00:42:26 +00:00
|
|
|
instanceID := data.Get("instance_id").(string)
|
|
|
|
if instanceID == "" {
|
|
|
|
return logical.ErrorResponse("missing instance_id"), nil
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:35:49 +00:00
|
|
|
return nil, req.Storage.Delete("whitelist/identity/" + instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
// pathIdentityWhitelistRead is used to view an entry in the identity whitelist given an instance ID.
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathIdentityWhitelistRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-04-06 00:42:26 +00:00
|
|
|
instanceID := data.Get("instance_id").(string)
|
|
|
|
if instanceID == "" {
|
|
|
|
return logical.ErrorResponse("missing instance_id"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := whitelistIdentityEntry(req.Storage, instanceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:32:07 +00:00
|
|
|
resp := &logical.Response{
|
2016-04-28 01:29:32 +00:00
|
|
|
Data: structs.New(entry).Map(),
|
2016-09-23 16:32:07 +00:00
|
|
|
}
|
|
|
|
resp.Data["creation_time"] = entry.CreationTime.Format(time.RFC3339Nano)
|
|
|
|
resp.Data["expiration_time"] = entry.ExpirationTime.Format(time.RFC3339Nano)
|
|
|
|
resp.Data["last_updated_time"] = entry.LastUpdatedTime.Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
return resp, nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Struct to represent each item in the identity whitelist.
|
|
|
|
type whitelistIdentity struct {
|
2016-05-13 18:31:13 +00:00
|
|
|
Role string `json:"role" structs:"role" mapstructure:"role"`
|
2016-04-07 22:42:29 +00:00
|
|
|
ClientNonce string `json:"client_nonce" structs:"client_nonce" mapstructure:"client_nonce"`
|
|
|
|
CreationTime time.Time `json:"creation_time" structs:"creation_time" mapstructure:"creation_time"`
|
2016-04-28 01:29:32 +00:00
|
|
|
DisallowReauthentication bool `json:"disallow_reauthentication" structs:"disallow_reauthentication" mapstructure:"disallow_reauthentication"`
|
|
|
|
PendingTime string `json:"pending_time" structs:"pending_time" mapstructure:"pending_time"`
|
2016-04-07 22:42:29 +00:00
|
|
|
ExpirationTime time.Time `json:"expiration_time" structs:"expiration_time" mapstructure:"expiration_time"`
|
2016-04-28 01:29:32 +00:00
|
|
|
LastUpdatedTime time.Time `json:"last_updated_time" structs:"last_updated_time" mapstructure:"last_updated_time"`
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathIdentityWhitelistSyn = `
|
2016-04-06 00:42:26 +00:00
|
|
|
Read or delete entries in the identity whitelist.
|
|
|
|
`
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathIdentityWhitelistDesc = `
|
2016-04-06 00:42:26 +00:00
|
|
|
Each login from an EC2 instance creates/updates an entry in the identity whitelist.
|
|
|
|
|
|
|
|
Entries in this list can be viewed or deleted using this endpoint.
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
By default, a cron task will periodically look for expired entries in the whitelist
|
|
|
|
and deletes them. The duration to periodically run this, is one hour by default.
|
2016-04-28 01:29:32 +00:00
|
|
|
However, this can be configured using the 'config/tidy/identities' endpoint. This tidy
|
|
|
|
action can be triggered via the API as well, using the 'tidy/identities' endpoint.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathListIdentityWhitelistHelpSyn = `
|
|
|
|
Lists the items present in the identity whitelist.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
const pathListIdentityWhitelistHelpDesc = `
|
2016-04-06 00:42:26 +00:00
|
|
|
The entries in the identity whitelist is keyed off of the EC2 instance IDs.
|
|
|
|
This endpoint lists all the entries present in the identity whitelist, both
|
2016-04-28 01:29:32 +00:00
|
|
|
expired and un-expired entries. Use 'tidy/identities' endpoint to clean-up
|
|
|
|
the whitelist of identities.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|