2017-04-24 19:15:50 +00:00
|
|
|
package awsauth
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-07 14:27:11 +00:00
|
|
|
"fmt"
|
2016-04-07 18:13:19 +00:00
|
|
|
"sync"
|
2016-04-19 18:21:27 +00:00
|
|
|
"time"
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2017-06-07 14:27:11 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/endpoints"
|
2016-04-07 18:13:19 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-09-23 16:47:35 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2017-08-30 21:51:48 +00:00
|
|
|
"github.com/patrickmn/go-cache"
|
2016-04-06 00:42:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
b, err := Backend(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
if err := b.Setup(conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 16:14:07 +00:00
|
|
|
type backend struct {
|
|
|
|
*framework.Backend
|
|
|
|
|
|
|
|
// Lock to make changes to any of the backend's configuration endpoints.
|
|
|
|
configMutex sync.RWMutex
|
|
|
|
|
2016-05-05 18:51:09 +00:00
|
|
|
// Lock to make changes to role entries
|
|
|
|
roleMutex sync.RWMutex
|
|
|
|
|
2016-05-05 18:12:22 +00:00
|
|
|
// Lock to make changes to the blacklist entries
|
|
|
|
blacklistMutex sync.RWMutex
|
|
|
|
|
2016-05-05 18:28:46 +00:00
|
|
|
// Guards the blacklist/whitelist tidy functions
|
|
|
|
tidyBlacklistCASGuard uint32
|
|
|
|
tidyWhitelistCASGuard uint32
|
|
|
|
|
2016-05-03 16:14:07 +00:00
|
|
|
// Duration after which the periodic function of the backend needs to
|
|
|
|
// tidy the blacklist and whitelist entries.
|
|
|
|
tidyCooldownPeriod time.Duration
|
|
|
|
|
|
|
|
// nextTidyTime holds the time at which the periodic func should initiatite
|
|
|
|
// the tidy operations. This is set by the periodicFunc based on the value
|
|
|
|
// of tidyCooldownPeriod.
|
|
|
|
nextTidyTime time.Time
|
|
|
|
|
2017-02-01 19:16:03 +00:00
|
|
|
// Map to hold the EC2 client objects indexed by region and STS role.
|
|
|
|
// This avoids the overhead of creating a client object for every login request.
|
|
|
|
// When the credentials are modified or deleted, all the cached client objects
|
|
|
|
// will be flushed. The empty STS role signifies the master account
|
|
|
|
EC2ClientsMap map[string]map[string]*ec2.EC2
|
|
|
|
|
|
|
|
// Map to hold the IAM client objects indexed by region and STS role.
|
|
|
|
// This avoids the overhead of creating a client object for every login request.
|
|
|
|
// When the credentials are modified or deleted, all the cached client objects
|
|
|
|
// will be flushed. The empty STS role signifies the master account
|
|
|
|
IAMClientsMap map[string]map[string]*iam.IAM
|
2017-06-07 14:27:11 +00:00
|
|
|
|
2017-08-30 21:51:48 +00:00
|
|
|
// Map of AWS unique IDs to the full ARN corresponding to that unique ID
|
|
|
|
// This avoids the overhead of an AWS API hit for every login request
|
|
|
|
// using the IAM auth method when bound_iam_principal_arn contains a wildcard
|
|
|
|
iamUserIdToArnCache *cache.Cache
|
|
|
|
|
2017-06-07 14:27:11 +00:00
|
|
|
// AWS Account ID of the "default" AWS credentials
|
|
|
|
// This cache avoids the need to call GetCallerIdentity repeatedly to learn it
|
|
|
|
// We can't store this because, in certain pathological cases, it could change
|
|
|
|
// out from under us, such as a standby and active Vault server in different AWS
|
|
|
|
// accounts using their IAM instance profile to get their credentials.
|
|
|
|
defaultAWSAccountID string
|
|
|
|
|
|
|
|
resolveArnToUniqueIDFunc func(logical.Storage, string) (string, error)
|
2016-05-03 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:17:47 +00:00
|
|
|
func Backend(conf *logical.BackendConfig) (*backend, error) {
|
2016-04-19 18:21:27 +00:00
|
|
|
b := &backend{
|
2016-04-28 00:01:39 +00:00
|
|
|
// Setting the periodic func to be run once in an hour.
|
|
|
|
// If there is a real need, this can be made configurable.
|
2017-08-30 21:51:48 +00:00
|
|
|
tidyCooldownPeriod: time.Hour,
|
|
|
|
EC2ClientsMap: make(map[string]map[string]*ec2.EC2),
|
|
|
|
IAMClientsMap: make(map[string]map[string]*iam.IAM),
|
|
|
|
iamUserIdToArnCache: cache.New(7*24*time.Hour, 24*time.Hour),
|
2016-04-19 18:21:27 +00:00
|
|
|
}
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2017-06-07 14:27:11 +00:00
|
|
|
b.resolveArnToUniqueIDFunc = b.resolveArnToRealUniqueId
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
b.Backend = &framework.Backend{
|
|
|
|
PeriodicFunc: b.periodicFunc,
|
|
|
|
AuthRenew: b.pathLoginRenew,
|
|
|
|
Help: backendHelp,
|
2016-04-06 00:42:26 +00:00
|
|
|
PathsSpecial: &logical.Paths{
|
|
|
|
Unauthenticated: []string{
|
|
|
|
"login",
|
|
|
|
},
|
2017-02-16 21:29:30 +00:00
|
|
|
LocalStorage: []string{
|
|
|
|
"whitelist/identity/",
|
|
|
|
},
|
2017-11-03 15:43:31 +00:00
|
|
|
SealWrapStorage: []string{
|
|
|
|
"config/client",
|
|
|
|
},
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
2016-04-30 07:20:21 +00:00
|
|
|
Paths: []*framework.Path{
|
2016-04-19 18:21:27 +00:00
|
|
|
pathLogin(b),
|
2016-05-05 18:51:09 +00:00
|
|
|
pathListRole(b),
|
2016-05-03 16:14:07 +00:00
|
|
|
pathListRoles(b),
|
2016-05-05 18:51:09 +00:00
|
|
|
pathRole(b),
|
2016-05-03 16:14:07 +00:00
|
|
|
pathRoleTag(b),
|
2016-04-19 18:21:27 +00:00
|
|
|
pathConfigClient(b),
|
|
|
|
pathConfigCertificate(b),
|
2017-02-01 19:16:03 +00:00
|
|
|
pathConfigSts(b),
|
|
|
|
pathListSts(b),
|
2016-05-12 11:19:29 +00:00
|
|
|
pathConfigTidyRoletagBlacklist(b),
|
|
|
|
pathConfigTidyIdentityWhitelist(b),
|
2016-04-19 18:21:27 +00:00
|
|
|
pathListCertificates(b),
|
2016-05-12 11:19:29 +00:00
|
|
|
pathListRoletagBlacklist(b),
|
|
|
|
pathRoletagBlacklist(b),
|
|
|
|
pathTidyRoletagBlacklist(b),
|
|
|
|
pathListIdentityWhitelist(b),
|
|
|
|
pathIdentityWhitelist(b),
|
|
|
|
pathTidyIdentityWhitelist(b),
|
2016-04-30 07:20:21 +00:00
|
|
|
},
|
2017-07-28 18:04:46 +00:00
|
|
|
Invalidate: b.invalidate,
|
|
|
|
BackendType: logical.TypeCredential,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:17:47 +00:00
|
|
|
return b, nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
// periodicFunc performs the tasks that the backend wishes to do periodically.
|
|
|
|
// Currently this will be triggered once in a minute by the RollbackManager.
|
2016-04-28 00:01:39 +00:00
|
|
|
//
|
2016-05-03 16:14:07 +00:00
|
|
|
// The tasks being done currently by this function are to cleanup the expired
|
|
|
|
// entries of both blacklist role tags and whitelist identities. Tidying is done
|
|
|
|
// not once in a minute, but once in an hour, controlled by 'tidyCooldownPeriod'.
|
2016-04-28 00:01:39 +00:00
|
|
|
// Tidying of blacklist and whitelist are by default enabled. This can be
|
|
|
|
// changed using `config/tidy/roletags` and `config/tidy/identities` endpoints.
|
2016-04-19 18:21:27 +00:00
|
|
|
func (b *backend) periodicFunc(req *logical.Request) error {
|
2016-05-03 16:14:07 +00:00
|
|
|
// Run the tidy operations for the first time. Then run it when current
|
|
|
|
// time matches the nextTidyTime.
|
2016-07-07 21:44:14 +00:00
|
|
|
if b.nextTidyTime.IsZero() || !time.Now().Before(b.nextTidyTime) {
|
2016-05-05 18:12:22 +00:00
|
|
|
// safety_buffer defaults to 180 days for roletag blacklist
|
|
|
|
safety_buffer := 15552000
|
2016-05-18 00:39:24 +00:00
|
|
|
tidyBlacklistConfigEntry, err := b.lockedConfigTidyRoleTags(req.Storage)
|
2016-04-19 18:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
skipBlacklistTidy := false
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if tidying of role tags was configured
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyBlacklistConfigEntry != nil {
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if periodic tidying of role tags was disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyBlacklistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipBlacklistTidy = true
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// overwrite the default safety_buffer with the configured value
|
2016-04-19 18:21:27 +00:00
|
|
|
safety_buffer = tidyBlacklistConfigEntry.SafetyBuffer
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// tidy role tags if explicitly not disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if !skipBlacklistTidy {
|
2016-05-05 18:28:46 +00:00
|
|
|
b.tidyBlacklistRoleTag(req.Storage, safety_buffer)
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
// reset the safety_buffer to 72h
|
|
|
|
safety_buffer = 259200
|
2016-05-18 00:39:24 +00:00
|
|
|
tidyWhitelistConfigEntry, err := b.lockedConfigTidyIdentities(req.Storage)
|
2016-04-19 18:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
2016-04-19 18:21:27 +00:00
|
|
|
skipWhitelistTidy := false
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if tidying of identities was configured
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyWhitelistConfigEntry != nil {
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if periodic tidying of identities was disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyWhitelistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipWhitelistTidy = true
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// overwrite the default safety_buffer with the configured value
|
2016-04-19 18:21:27 +00:00
|
|
|
safety_buffer = tidyWhitelistConfigEntry.SafetyBuffer
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// tidy identities if explicitly not disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if !skipWhitelistTidy {
|
2016-05-05 18:28:46 +00:00
|
|
|
b.tidyWhitelistIdentity(req.Storage, safety_buffer)
|
2016-04-19 18:21:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 16:14:07 +00:00
|
|
|
// Update the time at which to run the tidy functions again.
|
2016-07-07 21:44:14 +00:00
|
|
|
b.nextTidyTime = time.Now().Add(b.tidyCooldownPeriod)
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 21:29:30 +00:00
|
|
|
func (b *backend) invalidate(key string) {
|
|
|
|
switch key {
|
|
|
|
case "config/client":
|
|
|
|
b.configMutex.Lock()
|
|
|
|
defer b.configMutex.Unlock()
|
|
|
|
b.flushCachedEC2Clients()
|
|
|
|
b.flushCachedIAMClients()
|
2017-06-07 14:27:11 +00:00
|
|
|
b.defaultAWSAccountID = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Putting this here so we can inject a fake resolver into the backend for unit testing
|
|
|
|
// purposes
|
|
|
|
func (b *backend) resolveArnToRealUniqueId(s logical.Storage, arn string) (string, error) {
|
|
|
|
entity, err := parseIamArn(arn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// This odd-looking code is here because IAM is an inherently global service. IAM and STS ARNs
|
|
|
|
// don't have regions in them, and there is only a single global endpoint for IAM; see
|
|
|
|
// http://docs.aws.amazon.com/general/latest/gr/rande.html#iam_region
|
|
|
|
// However, the ARNs do have a partition in them, because the GovCloud and China partitions DO
|
|
|
|
// have their own separate endpoints, and the partition is encoded in the ARN. If Amazon's Go SDK
|
|
|
|
// would allow us to pass a partition back to the IAM client, it would be much simpler. But it
|
|
|
|
// doesn't appear that's possible, so in order to properly support GovCloud and China, we do a
|
|
|
|
// circular dance of extracting the partition from the ARN, finding any arbitrary region in the
|
|
|
|
// partition, and passing that region back back to the SDK, so that the SDK can figure out the
|
|
|
|
// proper partition from the arbitrary region we passed in to look up the endpoint.
|
|
|
|
// Sigh
|
|
|
|
region := getAnyRegionForAwsPartition(entity.Partition)
|
|
|
|
if region == nil {
|
|
|
|
return "", fmt.Errorf("Unable to resolve partition %q to a region", entity.Partition)
|
|
|
|
}
|
|
|
|
iamClient, err := b.clientIAM(s, region.ID(), entity.AccountNumber)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch entity.Type {
|
|
|
|
case "user":
|
|
|
|
userInfo, err := iamClient.GetUser(&iam.GetUserInput{UserName: &entity.FriendlyName})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if userInfo == nil {
|
|
|
|
return "", fmt.Errorf("got nil result from GetUser")
|
|
|
|
}
|
|
|
|
return *userInfo.User.UserId, nil
|
|
|
|
case "role":
|
|
|
|
roleInfo, err := iamClient.GetRole(&iam.GetRoleInput{RoleName: &entity.FriendlyName})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if roleInfo == nil {
|
|
|
|
return "", fmt.Errorf("got nil result from GetRole")
|
|
|
|
}
|
|
|
|
return *roleInfo.Role.RoleId, nil
|
|
|
|
case "instance-profile":
|
|
|
|
profileInfo, err := iamClient.GetInstanceProfile(&iam.GetInstanceProfileInput{InstanceProfileName: &entity.FriendlyName})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if profileInfo == nil {
|
|
|
|
return "", fmt.Errorf("got nil result from GetInstanceProfile")
|
|
|
|
}
|
|
|
|
return *profileInfo.InstanceProfile.InstanceProfileId, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unrecognized error type %#v", entity.Type)
|
2017-02-16 21:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 14:27:11 +00:00
|
|
|
// Adapted from https://docs.aws.amazon.com/sdk-for-go/api/aws/endpoints/
|
|
|
|
// the "Enumerating Regions and Endpoint Metadata" section
|
|
|
|
func getAnyRegionForAwsPartition(partitionId string) *endpoints.Region {
|
|
|
|
resolver := endpoints.DefaultResolver()
|
|
|
|
partitions := resolver.(endpoints.EnumPartitions).Partitions()
|
|
|
|
|
|
|
|
for _, p := range partitions {
|
|
|
|
if p.ID() == partitionId {
|
|
|
|
for _, r := range p.Regions() {
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:42:26 +00:00
|
|
|
const backendHelp = `
|
2016-06-01 14:36:58 +00:00
|
|
|
aws-ec2 auth backend takes in PKCS#7 signature of an AWS EC2 instance and a client
|
2016-04-28 00:01:39 +00:00
|
|
|
created nonce to authenticates the EC2 instance with Vault.
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2016-05-03 16:14:07 +00:00
|
|
|
Authentication is backed by a preconfigured role in the backend. The role
|
|
|
|
represents the authorization of resources by containing Vault's policies.
|
2016-05-13 18:31:13 +00:00
|
|
|
Role can be created using 'role/<role>' endpoint.
|
2016-04-28 00:01:39 +00:00
|
|
|
|
2016-05-12 11:19:29 +00:00
|
|
|
If there is need to further restrict the capabilities of the role on the instance
|
|
|
|
that is using the role, 'role_tag' option can be enabled on the role, and a tag
|
2016-05-13 18:31:13 +00:00
|
|
|
can be generated using 'role/<role>/tag' endpoint. This tag represents the
|
2016-05-12 11:19:29 +00:00
|
|
|
subset of capabilities set on the role. When the 'role_tag' option is enabled on
|
|
|
|
the role, the login operation requires that a respective role tag is attached to
|
|
|
|
the EC2 instance which performs the login.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|