-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass credentials to local-exec OR extract credentials via properties #8242
Comments
I took a crack at implementing option 2. See #8517 I've also attached some linux64 binaries for those who might want to help test it out: https://github.com/twang817/terraform-provider-aws/releases/tag/v2.8.0-8517 |
Oh, I do like it! |
Note, that the PR does result in the access/secret keys being stored in the state file. The docs seem to make warnings that state data may be sensitive: https://www.terraform.io/docs/state/sensitive-data.html I do not believe it is necessary to take any more measures to protect secrets from the state file -- as state files are already deemed potentially sensitive and that there already exists a mechanism to protect the state file via backend encryption (at least, when remote state is being used). It may be worth it, however, to move this into a separate data source. This allows users to continue using CallerIdentity without worrying about sensitive data in the state file. Furthermore, suddenly introducing this change into CallerIdentity may not be a pleasant surprise for all the existing plans that currently use CallerIdentity and have no need for credentials. Users that want access to the credentials must decide for themselves whether the risks of keeping keys in the state file are acceptable. Thoughts? |
Related to sensitive data stored on state files, if one use assumed roles to perform these tasks within the provider, the credentials stored will be temporary ones. (key, secret and token) |
I am also in an environment where credentials are temporary and the security risk for a stored credential exists only for a few hours up until the credential expires. Nonetheless, there are many people who use Terraform with permanent credentials (and perhaps even local state files) and the storage of credentials in state files may be of concern. This should ultimately be up to every engineer/organization to weigh the pros and cons and decide for themselves. It is probably worthwhile, however, to be very explicit about this risk in the docs. As far as using |
Until we don't have a option on aws-cli to assume role or a way to inject the AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY on environment on local-exec I wrote the following code. I hope it helps someone.
|
For those of you using aws-vault for your IAM user and then using @bertonha's local-exec script: Be sure to unset |
I use In the meantime, the following workaround that constructs an assumable
|
What about a different option? If there was a generic resource that just passed the parameters to the API/CLI using the credentials in the provider, it could provide the ability to do pretty much anything not currently implemented as its own dedicated resource. Here's what i'm working with at the moment as an example:
It could just look something like this:
|
I have a working solution, would like to hear if anyone has any further recommendations or simple a 👍 I went for something similar to the second case suggested by @deitch (providing a way to extract the credentials from a given provider) however i done so using a new data source d/aws_credentials, leaving resource "null_resource" "this" {
provisioner "local-exec" {
command = "some-aws-command"
environment {
AWS_SESSION_TOKEN = data.aws_credentials.default.token
AWS_SECRET_ACCESS_KEY = data.aws_credentials.default.secret_key
AWS_ACCESS_KEY_ID = data.aws_credentials.default.access_key
AWS_SECURITY_TOKEN = data.aws_credentials.default.token
}
}
} small print: exposes credentials into the state so i wouldn't suggest using alongside long life credentials nor an insecure backend. In light of that it may not ever be able to be merged but if the cons don't scare you compile it locally for your use case. |
It would be great to have a solution that doesn't store the credentials in the state. Also, regarding @Omarimcblack's PR, since the PR thread is closed:
I think you would have to replace the entire AWS provider, because a separate provider wouldn't be able to access the credentials of the hashicorp aws provider. |
For anyone looking for a "terraformy" way of getting the role arn, 'aws_session_context' data source will convert session arn into a role arn and save wrestling with regular expressions and string formatting. |
Should be |
We appreciate the interest, continued discussion, and suggested workarounds here. After extensive discussion, the Terraform AWS Provider maintainers have opted to close this request due to its potential security implications, which could inadvertently affect all users of the provider. As it stands, the local-exec provisioner operates after Terraform has provisioned resources, thereby lacking access to the AWS Provider's session credentials for those resources. The proposed solution would entail adding these credentials to the state, thereby exposing them to any entity with access to that file. Several workarounds within this issue have shown success in utilizing credentials with local-exec. While these approaches may not be ideal, the maintainers believe they are safer alternatives compared to potentially exposing plaintext credentials to malicious use. Thank you for your understanding. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Community Note
Description
If you need to execute a local command, via local-exec provisioner - whether part of a standard aws resource or via
null_resource
- the AWS credentials are not passed to that command. Almost all implementations of AWS CLI/SDK accept the usage ofAWS_PROFILE
,AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
, so any could consume it.Reasoning: I might have something I want to execute outside of terraform due to unsupported resources (e.g. directory upload #3020 ) or to keep some things out of terraform state, or to go through compliance.
In theory, those commands should have access to the AWS vars with which I launched terraform, but that usually isn't good enough for several reasons:
provider "aws"
clauseprovider "aws"
clauseprovider "aws"
clauses, each with a different alias, and would want to run one command with one instance, another command with the otherFurther,
provider "aws"
already has all of the support for fixed credentials, profiles, and even assuming roles to get temporary credentials viasts
. A local command should, in principle, run just like any other resource.I see two ways of doing this:
local-exec
provisioners, pass the relevant credentials -AWS_SECRET_ACCESS_KEY
/AWS_ACCESS_KEY_ID
- into the environment. I am not sure this can be done directly. If it can, I believe it only would work if thelocal-exec
provisioner is attached to anaws_*
resource, and not anull_resource
.aws_caller_identity
, although there might be a better way.The first case would be straightforward:
The second case is just explicit:
For this to work, we would need to ensure that
access_key
andsecret_key
are the actual ones used, e.g. temporary credentials via assume role or similar, or ones retrieved via using a profile, and not ones passed in via theprovider "aws"
config, unless those are the ones actually being used.New or Affected Resource(s)
aws_caller_identity
Potential Terraform Configuration
See the examples above.
Thanks!
The text was updated successfully, but these errors were encountered: