mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			136 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			136 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package aws
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 
 | |
| 	"github.com/hashicorp/aws-sdk-go/aws"
 | |
| 	"github.com/hashicorp/aws-sdk-go/gen/iam"
 | |
| 	"github.com/hashicorp/vault/logical"
 | |
| 	"github.com/hashicorp/vault/logical/framework"
 | |
| 	"github.com/mitchellh/mapstructure"
 | |
| )
 | |
| 
 | |
| func pathUser(b *backend) *framework.Path {
 | |
| 	return &framework.Path{
 | |
| 		Pattern: `(?P<name>\w+)`,
 | |
| 		Fields: map[string]*framework.FieldSchema{
 | |
| 			"name": &framework.FieldSchema{
 | |
| 				Type:        framework.TypeString,
 | |
| 				Description: "Name of the policy",
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		Callbacks: map[logical.Operation]framework.OperationFunc{
 | |
| 			logical.ReadOperation: b.pathUserRead,
 | |
| 		},
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (b *backend) pathUserRead(
 | |
| 	req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
 | |
| 	policyName := d.Get("name").(string)
 | |
| 
 | |
| 	// Read the policy
 | |
| 	policy, err := req.Storage.Get("policy/" + policyName)
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("error retrieving policy: %s", err)
 | |
| 	}
 | |
| 	if policy == nil {
 | |
| 		return logical.ErrorResponse(fmt.Sprintf(
 | |
| 			"Policy '%s' not found", policyName)), nil
 | |
| 	}
 | |
| 
 | |
| 	// Use the helper to create the secret
 | |
| 	return b.secretAccessKeysCreate(
 | |
| 		req.Storage, policyName, string(policy.Value))
 | |
| }
 | |
| 
 | |
| func pathUserRollback(req *logical.Request, _kind string, data interface{}) error {
 | |
| 	var entry walUser
 | |
| 	if err := mapstructure.Decode(data, &entry); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	username := entry.UserName
 | |
| 
 | |
| 	// Get the client
 | |
| 	client, err := clientIAM(req.Storage)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	// Get information about this user
 | |
| 	groupsResp, err := client.ListGroupsForUser(&iam.ListGroupsForUserRequest{
 | |
| 		UserName: aws.String(username),
 | |
| 		MaxItems: aws.Integer(1000),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	groups := groupsResp.Groups
 | |
| 
 | |
| 	policiesResp, err := client.ListUserPolicies(&iam.ListUserPoliciesRequest{
 | |
| 		UserName: aws.String(username),
 | |
| 		MaxItems: aws.Integer(1000),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	policies := policiesResp.PolicyNames
 | |
| 
 | |
| 	keysResp, err := client.ListAccessKeys(&iam.ListAccessKeysRequest{
 | |
| 		UserName: aws.String(username),
 | |
| 		MaxItems: aws.Integer(1000),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	keys := keysResp.AccessKeyMetadata
 | |
| 
 | |
| 	// Revoke all keys
 | |
| 	for _, k := range keys {
 | |
| 		err = client.DeleteAccessKey(&iam.DeleteAccessKeyRequest{
 | |
| 			AccessKeyID: k.AccessKeyID,
 | |
| 			UserName:    aws.String(username),
 | |
| 		})
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Delete any policies
 | |
| 	for _, p := range policies {
 | |
| 		err = client.DeleteUserPolicy(&iam.DeleteUserPolicyRequest{
 | |
| 			UserName:   aws.String(username),
 | |
| 			PolicyName: aws.String(p),
 | |
| 		})
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Remove the user from all their groups
 | |
| 	for _, g := range groups {
 | |
| 		err = client.RemoveUserFromGroup(&iam.RemoveUserFromGroupRequest{
 | |
| 			GroupName: g.GroupName,
 | |
| 			UserName:  aws.String(username),
 | |
| 		})
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Delete the user
 | |
| 	err = client.DeleteUser(&iam.DeleteUserRequest{
 | |
| 		UserName: aws.String(username),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| type walUser struct {
 | |
| 	UserName string
 | |
| }
 | 
