Skip to content
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

allow TF_TOKEN_... variables to specify host-specific credentials #477

Merged
merged 2 commits into from
Apr 21, 2022

Conversation

brandonc
Copy link
Collaborator

@brandonc brandonc commented Apr 15, 2022

Description

This is a companion PR to a change to terraform that adds host specific env svc creds. It should be possible for tfe to use the same method when specifying credentials for a particular tfc host.

Testing plan

  1. Build this code go build -o terraform-provider-tfe and update your CLI config to override it (see below)
  2. Don't specify a token in your tfe provider configuration block
  3. Remove or modify your CLI configuration credentials for the test host
  4. Run terraform using an env var with the correct token value (see below)
  5. This should work on Terraform 1.1 with local state. It would also work in latest Terraform built from source

I used this config:

provider "tfe" {
  hostname = "YOURHOST.com"
}

resource "tfe_organization" "test-organization" {
  name  = "my-org-name"
  email = "bcroft@hashicorp.com"
}

resource "tfe_workspace" "test" {
  name         = "my-workspace-name"
  organization = tfe_organization.test-organization.name
  tag_names    = ["test", "app"]
}

with this command:

TF_CLI_CONFIG_FILE=.terraformrc TF_TOKEN_YOURHOST_COM="MY_REAL_TOKEN" terraform apply

with this CLI config:

provider_installation {
  # Use /home/developer/tmp/terraform-null as an overridden package directory
  # for the hashicorp/null provider. This disables the version and checksum
  # verifications for this provider and forces Terraform to look for the
  # null provider plugin in the given directory.
  dev_overrides {
    "hashicorp/tfe" = "/Users/brandonc/hashicorp/terraform-provider-tfe"
  }

  # For all other providers, install them directly from their origin provider
  # registries as normal. If you omit this, Terraform will _only_ use
  # the dev_overrides block, and so no other providers will be available.
  direct {}
}

External links

Copy link
Contributor

@annawinkler annawinkler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌟 Tested and verified locally and it works!

Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fuego 🔥 👍 -- Last thing would be to update the Authentication section in this file: https://github.com/hashicorp/terraform-provider-tfe/blob/main/website/docs/index.html.markdown


ret := make(map[svchost.Hostname]string)
for _, ev := range os.Environ() {
eqIdx := strings.Index(ev, "=")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious here on readability: Would it be better to strings.Split() on = and compare each index rather than using ranges ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split might be slightly more readable, but this version doesn't do any array allocation and is shared with terraform already. I think I'd like to keep it.

@brandonc
Copy link
Collaborator Author

@sebasslash I added the docs. Good call

Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@brandonc brandonc merged commit 0afafc3 into main Apr 21, 2022
@brandonc brandonc deleted the brandonc/env_creds branch April 21, 2022 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants