Skip to content

Fix environment variable parsing, alias Terraform provider OAuth environment variables, and fix typo#49

Open
SVHawk13 wants to merge 3 commits intocontabo:mainfrom
SVHawk13:auth_env_vars
Open

Fix environment variable parsing, alias Terraform provider OAuth environment variables, and fix typo#49
SVHawk13 wants to merge 3 commits intocontabo:mainfrom
SVHawk13:auth_env_vars

Conversation

@SVHawk13
Copy link

Description

This PR improves environment variable parsing and consistency. It additionally fixes a minor typo:

Fix OAuth environment variable parsing on Linux

Issue: The CLI was using viper.AutomaticEnv() without a key replacer. This caused configuration keys with dashes to be converted to environment variable names that are not compatible with Linux shells such as Bash (for example, oauth2-clientid becomes CNTB_OAUTH2-CLIENTID).

Changes:

  • Set the viper env key replacer to an instance of strings.NewReplacer("-", "_") to translate dashes in configuration keys to underscores when viper.AutomaticEnv maps them to environment variable names.

Improve consistency with Terraform provider

Issue: The CLI uses configuration keys that map to environment variable names that differ from the environment variable names used by the Contabo Terraform provider. For example, the Terraform provider uses CNTB_OAUTH2_PASS while cntb uses CNTB_OAUTH2_PASSWORD. This makes configuration inconsistent between Terraform workflows and CLI workflows.

Changes:

  • Added fmtEnvKeys helper function to format environment variable names consistently for use with viper.BindEnv.
  • Added environment variable aliases for OAuth 2 environment variables using viper.BindEnv for consistency with the Terraform provider. The CLI now accepts both the environment variable names generated by viper.AutomaticEnv and the environment variable names used by the Terraform provider. The environment variable names from viper.AutomaticEnv take precedence, followed by the aliased names in definition order. For example, CNTB_OAUTH2_PASSWORD and CNTB_OAUTH2_PASS are now both valid environment variables. If CNTB_OAUTH2_PASSWORD and CNTB_OAUTH2_PASS are both set, the value of CNTB_OAUTH2_PASSWORD is used.

Fix a minor typo

Corrected "oauth2" misspelled as "oauht2".

Testing

The follow have been manually tested locally on Fedora 43:

  • Verified the configuration keys oauth2-clientid, oauth2-client-secret, oauth2-tokenurl, oauth2-user, and oauth2-password are set to the value of the environment variable that corresponds to the transformed configuration key name, such as the configuration key oauth2-clientid mapping to the environment variable name CNTB_OAUTH2_CLIENTID.
  • Verified the aliased environment variable names set the value of their corresponding configuration keys, and that the values are the same as when using the non-aliased names from viper.AutomaticEnv.
  • Verified the environment variables from viper.AutomaticEnv take precedence over the aliased environment variables names.
  • Verified the values from the OAuth2 environment variables were able to be used to authenticate successfully with the Contabo API.

Adds support for CNTB_OAUTH2_CLIENT_ID, CNTB_OAUTH2_PASS, and
CNTB_OAUTH2_TOKEN_URL environment variables to match the docs for the
Terraform provider while maintaining backward compatibility with the
existing environment variable names.
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.

1 participant