hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.12k stars 9.47k forks source link

`terraform login` with cli config set can not read config file #35440

Open MichalChotvac opened 1 month ago

MichalChotvac commented 1 month ago

Terraform Version

v1.9.1

Terraform Configuration Files

credentials "app.terraform.io" {
  token = "xxxxxx.atlasv1.zzzzzzzzzzzzz"
}

Debug Output

There are some problems with the CLI configuration:

Error: Error reading C:\DIR_NAME\terraform: read C:\DIR_NAME\terraform: Incorrect function.

As a result of the above problems, Terraform may not behave as intended.

Expected Behavior

Login to Terraform HCL work without any issues

Actual Behavior

There is an error message saying there are problems with CLI configuration while running any terraform command. Error message is displayed even if the TF_CLI_CONFIG_FILE is not set and default APPDATA path is used. Full error is in Debug Ouput.

Steps to Reproduce

  1. create an API token to access the HCP Terraform API
  2. set TF_CLI_CONFIG_FILE to different path than %APPDATA%
  3. put the token to a file terraform.tfrc (also tried terraform.rc) in credentials block
  4. run any terraform command

Additional Context

No response

References

No response

apparentlymart commented 1 month ago

Hi @MichalChotvac! Sorry for this strange behavior and thanks for reporting it.

I think I recognize "Incorrect function" as the English localization of the windows error code that means that Terraform attempted to perform a filesystem operation that doesn't make sense for the file in question.

Given that, I think an important question is what is actually at the file path indicated in the error message on your system. Is that path referring to a directory, or to a file? Is there anything already in that directory/file?

Note that TF_CLI_CONFIG_FILE is supposed to refer to a single file rather than to a directory of files. If you set it to refer to a directory then Terraform will probably try to "open" that directory as if it were a file, which could potentially cause this error. If that turns out to be the cause here then I would suggest that we fix it by explicitly checking whether the given path points to a file and return a more actionable error message if not, rather than just returning directly the error message returned by the operating system.

MichalChotvac commented 1 month ago

Hi @apparentlymart ,thank you for your response and sorry for being late in mine. You were right, I have set the variable to the directory. But meantime I have updated terraform to v1.9.2 and specified a file (*.rc) in TF_CLI_CONFIG_FILE variable, but now I am seeing other error:

PS C:\xx\xx> terraform login
╷
│ Error: Credentials for app.terraform.io are manually configured
│
│ The "terraform login" command cannot log in because credentials for this host are already configured in a CLI
│ configuration file.
│
│ To log in, first revoke the existing credentials and remove that block from the CLI configuration.
╵
apparentlymart commented 1 month ago

Hi @MichalChotvac,

The terraform login command works by adding a new file to the default directory where Terraform searches for CLI configuration files, and so it isn't really compatible with the situation where you have overridden Terraform to use only one specific CLI configuration file.

If you want to use terraform login then you must not set TF_CLI_CONFIG_FILE. Or conversely, if you want to use TF_CLI_CONFIG_FILE then you will need to manually add the credentials "app.terraform.io" block to your overriding CLI configuration file, instead of using terraform login. (Or you can use one of the other supported methods for providing credentials for a service hostname, such as an environment variable.)