Windows Username/Password Injection into CloudFormation

0

A customer built Active Directory on the AWS and wants to use CloudFormation template to spin up Windows machines. When launching an EC2 machine, it should run a PowerShell script to join to the AD Domain. But the script should know the username and password of the AD admin account which has the 'AD Join' permission.

Now this customer needs to inject the AD admin's username and password into the User Data section of CloudFormation template. What options are available? What are the pros and cons?

  • Getting username/password as CloudFormation Input Parameter?
  • Getting username/password from a securely managed S3 object?

Wonder how other people handle this usual scenario.

管理員
已提問 9 年前檢視次數 620 次
2 個答案
0
已接受的答案

Both are good options. S3 has been widely adopted for passing long credentials (e.g. keys) to CloudFormation stacks, however there is another option now; KMS. Benefits of this approach include an audit trail of decryptions, automated rotation of password encryption and not having to use an entire S3 bucket.

The following is an example of how KMS encrypted content can be referenced from a Mapping and an IAM role used by the instance in question to decrypt the key:

"Mappings": {
       "KMSSecrets": {
            "GithubSSHKey": {
                "CMKRegion": "us-east-1",
                "CipherTextBlob": "CiAKP2NjuykOmiYWxqmifMaTak0q.........",
                "IAMRoleName": "tmaddox-githubconsumer"
            }
      }
},
"Resources":
         "IAMInstanceProfileWWW": {
            "Properties": {
                "Path": "/",
                "Roles": [{"Fn::FindInMap": [ "KMSSecrets", "GithubSSHKey", "IAMRoleName"]}]
            },
            "Type": "AWS::IAM::InstanceProfile"
        },
        "WWWAdminServer": {
            "Metadata": {
                "AWS::CloudFormation::Init": {"config": {"packages": {"yum": {
                    "git": []
                }}}}
            },
            "Properties": {
                "IamInstanceProfile": {"Ref": "IAMInstanceProfileWWW"},
                ...
                "UserData": {"Fn::Base64": {"Fn::Join": [ "", [
                        "#!/bin/bash\n",
                        "yum update -y\n",
                        "echo -e \"\n### Running cfn-init\"\n",
                        "/opt/aws/bin/cfn-init",
                        "    -s ",
                        {"Ref": "AWS::StackName"},
                        "    -r WWWAdminServer ",
                        "    --region ",
                        {"Ref": "AWS::Region"},
                        "\n",
                        "",
                        "echo -e \"\n### Saving Github's SSH Host Key\"\n",
                        "GITHUB_HOST_KEY='github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=='\n",
                        "if [ ! -f /root/.ssh/known_hosts ] || grep -v $GITHUB_HOST_KEY /root/.ssh/known_hosts; then\n",
                        "  echo $GITHUB_HOST_KEY >> /root/.ssh/known_hosts\n",
                        "fi\n",
                        "",
                        "echo -e \"\n### Fetching code\"\n",
                        "echo '", {"Fn::FindInMap": [ "KMSSecrets", "GithubSSHKey", "CipherTextBlob" ]}, "'",
                        "    | base64 -d > /root/.ssh/github.pem.encrypted\n",
                        "aws kms decrypt",
                        "    --output text",
                        "    --query Plaintext",
                        "    --region ", {"Fn::FindInMap": ["KMSSecrets", "GithubSSHKey", "CMKRegion" ]},
                        "    --ciphertext-blob fileb:///root/.ssh/github.pem.encrypted",
                        "    | base64 -d > /root/.ssh/git.pem\n",
                        "",
                        "rm -f /root/.ssh/github.pem.encrypted\n",
                        "chmod 600 /root/.ssh/git.pem\n",
                        "rm -rf /var/www/html\n",
                        "eval `ssh-agent`\n",
                        "ssh-add /root/.ssh/git.pem\n",
                        "git clone https://github.com/magento/magento2.git /var/www/html\n",
                        "rm -f /root/.ssh/git.pem\n",
                        ""
                    ]
                ]}}
            },
            "Type": "AWS::EC2::Instance"
        },
AWS
已回答 9 年前
profile picture
專家
已審閱 1 年前
0

I would store the Ad Credentials in a AWS Secret and the use powershell in user data to retrieve the secret value while joining the domain

https://aws.amazon.com/de/secrets-manager/

https://docs.aws.amazon.com/powershell/latest/reference/items/Get-SECSecretValue.html

profile picture
David
已回答 10 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南