Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the current user in a cloudformation template?

I want to create a KMS key using CloudFormation. I want to be able to provide the user executing the cloudformation YAML file (I'll call them "cloudformation-runner"), administrative access to the key they create.

I can setup the IAM policy to provide that user ("cloudformation-runner") access to the KMS Administrative APIs. However, for the user to be able to update/delete the key that was just created, I also need to specify a KeyPolicy that lets them do it. To do this, how can I get the current username ("cloudformation-runner") within the CloudFormation script?

Here is how my template for the KMS key looks, how do I get the current user as the principal?

    MyKey:
          Type: AWS::KMS::Key
          Properties:
            Description: "..."
            KeyPolicy:
              Version: "2012-10-17"
              Id: "MyId"
              Statement:
                -
                  Sid: "Allow administration of the key"
                  Effect: "Allow"
                  Principal:
                    AWS:
                      - # TODO: Get Current User
                  Action:
                    - "kms:Create*"
                    - "kms:Describe*"
                    - "kms:Enable*"
                    - "kms:List*"
                    - "kms:Put*"
                    - "kms:Update*"
                    - "kms:Revoke*"
                    - "kms:Disable*"
                    - "kms:Get*"
                    - "kms:Delete*"
                    - "kms:ScheduleKeyDeletion"
                    - "kms:CancelKeyDeletion"
                  Resource: "*"

I can manually hardcode the ARN for the IAM user. However, that makes the template less portable - as people need to manually update the username within this file.

like image 434
Aishwar Avatar asked Oct 20 '18 15:10

Aishwar


People also ask

How do you reference existing resources in CloudFormation?

To import existing resources into a CloudFormation stack, you need to provide: A template that describes the entire stack, including both the resources to import and (for existing stacks) the resources that are already part of the stack. Each resource to import must have a DeletionPolicy attribute in the template.

How do I find my stack name in CloudFormation?

After selecting a stack template, specify the stack name and values for the parameters that were defined in the template. With parameters, you can customize your stack at creation time. Your parameter values can be used in the stack template to modify how resources are configured.


1 Answers

You can't access the current user once it can be an IAM Role running the CloudFormation template instead of an IAM User. But you can pass the username as a parameter.

I would like to give you an example that I think works well for your context:

  1. The "cloudformation-runner" can be a Role instead of a user. This Role can have a policy giving privileges to create KMS keys.
  2. The CloudFormation template can receive the IAM username and key name as parameters. From an IAM username, you can create the user ARN using CF functions.
  3. Besides creating the KMS keys, the parameters can be used to create the IAM policy and attach to the user giving read/write privileges to the newly created key.

That way your key creation process can create the key and give user privileges at the same time.

like image 152
Anderson Marques Avatar answered Sep 29 '22 21:09

Anderson Marques