2015-03-20 16:59:48 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-03-20 16:59:48 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-08-06 16:26:41 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-03-20 16:59:48 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2015-03-21 10:18:46 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-03-20 16:59:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathUser(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
2015-08-21 07:56:13 +00:00
|
|
|
Pattern: "creds/" + framework.GenericNameRegex("name"),
|
2015-03-20 16:59:48 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-04-27 21:20:28 +00:00
|
|
|
Description: "Name of the role",
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathUserRead,
|
|
|
|
},
|
2015-04-04 04:10:54 +00:00
|
|
|
|
|
|
|
HelpSynopsis: pathUserHelpSyn,
|
|
|
|
HelpDescription: pathUserHelpDesc,
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathUserRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-03-21 10:49:56 +00:00
|
|
|
policyName := d.Get("name").(string)
|
2015-03-20 16:59:48 +00:00
|
|
|
|
|
|
|
// Read the policy
|
2018-01-19 06:44:44 +00:00
|
|
|
policy, err := req.Storage.Get(ctx, "policy/"+policyName)
|
2015-03-20 16:59:48 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("error retrieving role: {{err}}", err)
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
|
|
|
if policy == nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
2015-04-27 21:20:28 +00:00
|
|
|
"Role '%s' not found", policyName)), nil
|
2015-03-21 10:18:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 10:49:56 +00:00
|
|
|
// Use the helper to create the secret
|
|
|
|
return b.secretAccessKeysCreate(
|
2018-01-19 06:44:44 +00:00
|
|
|
ctx, req.Storage, req.DisplayName, policyName, string(policy.Value))
|
2015-03-20 16:59:48 +00:00
|
|
|
}
|
2015-03-21 10:18:46 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func pathUserRollback(ctx context.Context, req *logical.Request, _kind string, data interface{}) error {
|
2015-03-21 10:18:46 +00:00
|
|
|
var entry walUser
|
|
|
|
if err := mapstructure.Decode(data, &entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
username := entry.UserName
|
|
|
|
|
|
|
|
// Get the client
|
2018-01-19 06:44:44 +00:00
|
|
|
client, err := clientIAM(ctx, req.Storage)
|
2015-03-21 10:18:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get information about this user
|
2015-08-06 16:37:08 +00:00
|
|
|
groupsResp, err := client.ListGroupsForUser(&iam.ListGroupsForUserInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
2015-08-06 16:37:08 +00:00
|
|
|
MaxItems: aws.Int64(1000),
|
2015-03-21 10:18:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groups := groupsResp.Groups
|
|
|
|
|
2015-12-30 18:05:54 +00:00
|
|
|
// Inline (user) policies
|
2015-08-06 16:37:08 +00:00
|
|
|
policiesResp, err := client.ListUserPolicies(&iam.ListUserPoliciesInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
2015-08-06 16:37:08 +00:00
|
|
|
MaxItems: aws.Int64(1000),
|
2015-03-21 10:18:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
policies := policiesResp.PolicyNames
|
|
|
|
|
2015-12-30 18:05:54 +00:00
|
|
|
// Attached managed policies
|
|
|
|
manPoliciesResp, err := client.ListAttachedUserPolicies(&iam.ListAttachedUserPoliciesInput{
|
|
|
|
UserName: aws.String(username),
|
|
|
|
MaxItems: aws.Int64(1000),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
manPolicies := manPoliciesResp.AttachedPolicies
|
|
|
|
|
2015-08-06 16:37:08 +00:00
|
|
|
keysResp, err := client.ListAccessKeys(&iam.ListAccessKeysInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
2015-08-06 16:37:08 +00:00
|
|
|
MaxItems: aws.Int64(1000),
|
2015-03-21 10:18:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keys := keysResp.AccessKeyMetadata
|
|
|
|
|
|
|
|
// Revoke all keys
|
|
|
|
for _, k := range keys {
|
2015-08-06 16:37:08 +00:00
|
|
|
_, err = client.DeleteAccessKey(&iam.DeleteAccessKeyInput{
|
2015-08-19 01:12:51 +00:00
|
|
|
AccessKeyId: k.AccessKeyId,
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:05:54 +00:00
|
|
|
// Detach managed policies
|
|
|
|
for _, p := range manPolicies {
|
|
|
|
_, err = client.DetachUserPolicy(&iam.DetachUserPolicyInput{
|
|
|
|
UserName: aws.String(username),
|
|
|
|
PolicyArn: p.PolicyArn,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete any inline (user) policies
|
2015-03-21 10:18:46 +00:00
|
|
|
for _, p := range policies {
|
2015-08-06 16:37:08 +00:00
|
|
|
_, err = client.DeleteUserPolicy(&iam.DeleteUserPolicyInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
2015-08-06 16:37:08 +00:00
|
|
|
PolicyName: p,
|
2015-03-21 10:18:46 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the user from all their groups
|
|
|
|
for _, g := range groups {
|
2015-08-06 16:37:08 +00:00
|
|
|
_, err = client.RemoveUserFromGroup(&iam.RemoveUserFromGroupInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
GroupName: g.GroupName,
|
|
|
|
UserName: aws.String(username),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the user
|
2015-08-06 16:37:08 +00:00
|
|
|
_, err = client.DeleteUser(&iam.DeleteUserInput{
|
2015-03-21 10:18:46 +00:00
|
|
|
UserName: aws.String(username),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type walUser struct {
|
|
|
|
UserName string
|
|
|
|
}
|
2015-04-04 04:10:54 +00:00
|
|
|
|
|
|
|
const pathUserHelpSyn = `
|
2015-04-27 21:20:28 +00:00
|
|
|
Generate an access key pair for a specific role.
|
2015-04-04 04:10:54 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathUserHelpDesc = `
|
|
|
|
This path will generate a new, never before used key pair for
|
|
|
|
accessing AWS. The IAM policy used to back this key pair will be
|
|
|
|
the "name" parameter. For example, if this backend is mounted at "aws",
|
2015-04-27 21:20:28 +00:00
|
|
|
then "aws/creds/deploy" would generate access keys for the "deploy" role.
|
2015-04-04 04:10:54 +00:00
|
|
|
|
|
|
|
The access keys will have a lease associated with them. The access keys
|
2015-04-11 03:35:14 +00:00
|
|
|
can be revoked by using the lease ID.
|
2015-04-04 04:10:54 +00:00
|
|
|
`
|