Skip to content

Instantly share code, notes, and snippets.

@tristanmorgan
Last active March 15, 2024 05:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanmorgan/ac541f5689272a34e6135977c2bdf8ad to your computer and use it in GitHub Desktop.
Save tristanmorgan/ac541f5689272a34e6135977c2bdf8ad to your computer and use it in GitHub Desktop.
Terraform Cloud Token Helper (save into macOS keychain)
disable_checkpoint = true
credentials_helper helper {
args = []
}
# save this in home.
#!/bin/sh
set -e
case $1 in
store)
security add-generic-password -U -a "TF-$USER" -c "htfm" -C "htfm" -D "Hashicorp Terraform" -s "$2" -w "$(jq -r .token)"
;;
get)
/bin/echo -n "{\"token\": \"$(security find-generic-password -g -a "TF-$USER" -s "$2" -w)\"}"
;;
forget)
security delete-generic-password -a "TF-$USER" -s "$2"
;;
*)
echo "Usage: $0 get|forget|store"
;;
esac
# save this into your ~/.terraform.d/plugins/ folder.
@tristanmorgan
Copy link
Author

Terraform Enterprise Creds Helper is a post about how this came to be.

@tristanmorgan
Copy link
Author

Just found a Golang implementation terraform-credentials-env that loads your token from environment variables.

@bendrucker
Copy link

Hi, you might be interested in https://github.com/bendrucker/terraform-credentials-keychain, which implements this helper in Go. Thanks for posting the gist, wasn't aware of this functionality until I stumbled onto it/your Medium post.

@tristanmorgan
Copy link
Author

Thanks @bendrucker, and if hashicorp/terraform-provider-tfe#146 /111 gets fixed we will have more places to use helpers.

@tristanmorgan
Copy link
Author

@tristanmorgan
Copy link
Author

tristanmorgan commented Jun 9, 2020

and the API changed.... terraform-credentials-credstore --host=credstore.example.com store app.terraform.io for example.

@bendrucker
Copy link

The API is unchanged. Optional flags can be included before get/store/forget.

@tristanmorgan
Copy link
Author

updated gist to use JQ to extract the token during the store action.
Read: I hadn't tested terraform login with it before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment