I am attempting to use a Bitbucket Pipeline Runner executing as an AWS ECS Task hosted within ECS-EC2 and bound to an IAM Role for the purpose of eliminating the storage of AWS credentials of any kind within Bitbucket. I feel like I am very close to a solution but hung up on seemingly not being able to pass a run-time variable from the outer Pipeline Runner container to the internal container executing a "Step".
The fundamental problem is that the "inner" container executing a Step is not running under the auspices of the IAM role like the outer container orchestrating the Pipeline is. The basis of a solution, however, could take shape in having the preamble of a Script of a Step retrieve such credentials by curl'ing a standard endpoint with the "task credential ID" which is exposed as an environment variable within the outer ECS Task.
I have POC'd this by doing three things...
Firstly, by making the Entrypoint and Command of the ECS Task be respectively ["bash", "-c"] and ["echo $AWS_CONTAINER_CREDENTIALS_RELATIVE_URI && /opt/atlassian/pipelines/runner/entrypoint.sh"]
Secondly, by capturing the echo'd value of AWS_CONTAINER_CREDENTIALS_RELATIVE_URI from CloudWatch Logs and plugging it into my Bitbucket Pipeline as an environment variable...
And lastly by retrieving short-lived credentials for the bound IAM Role in the following way inside a Step's Script...
- CREDS=`curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`
- export AWS_ACCESS_KEY_ID=`echo $CREDS | jq -r .AccessKeyId`
- export AWS_SECRET_ACCESS_KEY=`echo $CREDS | jq -r .SecretAccessKey`
- export AWS_SESSION_TOKEN=`echo $CREDS | jq -r .Token`
... after which an invocation of "aws s3 ls" validates the successful retrieval of credentials.
So I have demonstrated that it is possible for the inner container to inherit the credentialing of the IAM Role bound to the outer container if only I can somehow inject the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable present in the outer container into the inner container.
Sadly the entrypoint.sh included in the docker-public.packages.atlassian.com/sox/atlassian/bitbucket-pipelines-runner:1 container image does not appear to have any invocation option that facilitates such pass-through. Furthermore, the way the pipeline runner configures the docker daemon's Access Authorization Plugin appears to preclude such solutions as either running a web server (netcat) within the outer container that the inner container can query or having the outer process inject a config/secret/whatever into the docker daemon that the inner container can then read.
Is there _any_ way that I can contrive to pass just a single string value from the outer container to the inner container? That is all I need to accomplish my goal -- having pipeline steps run with IAM Role credentials instead of needing the static credentials of an IAM User.
I note that others are generally eager for an environment variable pass-through feature...
https://community.atlassian.com/t5/Bitbucket-questions/Access-private-runner-host-environment-variables/qaq-p/1859496?tempId=eyJvaWRjX2NvbnNlbnRfbGFuZ3VhZ2VfdmVyc2lvbiI6IjIuMCIsIm9pZGNfY29uc2VudF9ncmFudGVkX2F0IjoxNjQzNjUyODE2NjAyfQ%3D%3D
... and that a ticket has been opened for this feature request...
https://jira.atlassian.com/browse/BCLOUD-21523
I furthermore note that others are specifically interested in solving for my use case of leveraging IAM Role derived credentials and seemingly suffering the lack of a solution...
https://community.atlassian.com/t5/Bitbucket-questions/Bitbucket-runner-does-not-work-with-AWS-EC2-Instance-Iam-Role/qaq-p/1822489
Can anyone offer advice/updates on whether either the environment variable pass-through faculty may soon be implemented, or if there is any way at all to inject a single string value into the inner Step container I could use for the credential retrieving I demo'd above, or if there is some other way altogether to crack the nut of getting the inner container access to IAM Role credentialing?
I have to imagine that solving this problem would be extremely valuable for a variety of Atlassian's customers.
For reference: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html