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

Feat: fallback to CLI config for token #248

Merged
merged 6 commits into from
Aug 7, 2024
Merged

Conversation

Praveen005
Copy link
Contributor

This pull request introduces the feature requested in #246

It Implements a priority-based token retrieval:

  1. Environment variable (CIVO_TOKEN).
  2. Token provided via a Credential file.
  3. Lastly, fall back to CLI config file (~/.civo.json)

Tests' Screenshots:

I have introduced coloured debug messages(removed from the main code), which correctly prints the token it abstracts:

Screenshot 2024-07-08 213657

Below, I have set the token using Civo CLI command civo apikey save CIVO_TOKEN clitoken96 which saves it in ~/.civo.json.
You can see, it correctly prints a different token(last coloured debug message):

Screenshot 2024-07-08 214536

@uzaxirr
Copy link
Member

uzaxirr commented Jul 9, 2024

@Praveen005 is this ready for review?

@Praveen005
Copy link
Contributor Author

Yes @uzaxirr it is ready for review.

@uzaxirr
Copy link
Member

uzaxirr commented Jul 9, 2024

@Praveen005 can you also build the provider locally, test it and attach corresponding screenshots.
Refer to this for local setup.

Copy link
Member

@uzaxirr uzaxirr left a comment

Choose a reason for hiding this comment

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

I've tested your branch locally. It works when credential_file is specified or env vars are there but it fails when it has to fallback to CLI configs i.e when credential_file is not specified and env vars are also absent. can you please verify this.

civo/provider.go Outdated
Comment on lines 37 to 41
"token": {
"credential_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CIVO_TOKEN", ""),
Description: "This is the Civo API token. Alternatively, this can also be specified using `CIVO_TOKEN` environment variable.",
Copy link
Member

Choose a reason for hiding this comment

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

Instead of removing token, we can deprecate this field from the schema. This would be a better way.

			"token": {
				Type:        schema.TypeString,
				Optional:    true,
				DefaultFunc: schema.EnvDefaultFunc("CIVO_TOKEN", ""),
				Deprecated: "\"token\" attribute is deprecated. Moving forward, please use \"credential_file\" attribute.",
				Description: "This is the Civo API token. Alternatively, this can also be specified using `CIVO_TOKEN` environment variable.",
			},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @uzaxirr I zeroed in on the likely bottleneck,

The token variable in provider.tf in build.sh script was still there, I removed it.

Also updated the provider.tf in examples directory.

I also incorporated your suggestion of deprecating the token field. Thanks for the heads up.

Following are the screen grabs from local build:
Screenshot 2024-07-09 172827
Screenshot 2024-07-09 172959
Screenshot 2024-07-09 173037

The resource didn't get created, likely because I don't have an active Civo Account.
Can you now try again?

Regards.

Copy link
Member

Choose a reason for hiding this comment

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

While you performed the above operation was there any token or credential_file attribute present in your provider.tf civo config block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@uzaxirr No, it was not in provider.tf. I will share you the terraform debug log which shows token being read from ~/.civo.json. The code need some more changes and I am working on that.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, great work so far

 * Last commit was not reading from the token variable, so fixed that.
 * Added one test case to cater for above change.
 * Since, we still can read token from the token attribute, we need not remove civo_api_key variable from build.sh.
@Praveen005
Copy link
Contributor Author

Hi @uzaxirr Thank you for your patience.

Here are the details about the changes:

  1. Earlier commit was not reading token from the token attribute, so fixed it.
  2. Added one test case, to cater for this change.
  3. In last commit I removed, civo_api_key and token = "$civo_api_key" from the local build script local.sh but that's not needed, so added them back.
  4. The code now, passes the local build test, all the unit tests and also correctly reads the token from ~/.civo.json

Screenshots:

  1. provider.tf file on which I'm running the local build:
    You can see, I haven't provided it the token nor the credential_file path. I haven't set the environment variable either.
    Meaning it will have to fall back to reading from ~/.civo.json
Screenshot 2024-07-10 130720
  1. No environment variable set:
Screenshot 2024-07-10 131059
  1. Token in ~/.civo.json
Screenshot 2024-07-10 131319
  1. Added debug log(removed from main code) to print the token when it is read:
Screenshot 2024-07-10 123037
  1. Starting local build:
Screenshot 2024-07-10 123512
  1. Terraform debug log, shows CIVO_TOKEN being correctly read from CLI config.
Screenshot 2024-07-10 123606
  1. See deprecation warning being correctly displayed:
Screenshot 2024-07-10 123628
  1. Terraform apply:
Screenshot 2024-07-10 123719
  1. Token again being read again after terraform apply command:
Screenshot 2024-07-10 123740
  1. Attempts being made to create the resource, but fails because I don't have an active civo account
Screenshot 2024-07-10 123828
  1. Passing all the unit tests:
Screenshot 2024-07-10 124039
  1. Test coverage:
Screenshot 2024-07-10 124059

Can you please review these changes again?

Regards.

@Praveen005 Praveen005 requested a review from uzaxirr July 10, 2024 20:20
Copy link
Member

@uzaxirr uzaxirr left a comment

Choose a reason for hiding this comment

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

Hey @Praveen005
I've pulled up your latest changes locally, again the same scenario works fine when crediential_file or CIVO_TOKEN env var is specified. but fails to obtain the token from CLI when nothing is specified. Please refer to the below screen shot

Screenshot 2024-07-15 at 10 43 56 AM

Also why do i see the Argument is deprecated warning even though i haven't specified any token field in my schema.

I would suggest you to create a CIVO account, set up the CLI and then try spinning up resources via terraform with all the different scnerios.

@Praveen005
Copy link
Contributor Author

I will check it right away, @uzaxirr.

 Added a validation function in token field, which will ensure warning is shown only when user uses the token variable.
Removed hardcoded api key name.
Made minor changes to unit tests
@Praveen005
Copy link
Contributor Author

Praveen005 commented Jul 15, 2024

Hi @uzaxirr, sorry it took way more time than I anticipated.

I believe, it fixes all the above issues you pointed. I have tested it thoroughly and it works fine, can you kindly check once from your end?

  1. I added a validation function under the token attribute in provider schema. The warnings are now logged only when you use the token variable.
    If you see the token field, I removed the deprecated message, because the warning was shown twice in quick succession, once because I used Deprecated attribute, while other in validateTokenUsage function.

  2. Earlier I had hardcoded the API key name to CIVO_TOKEN, and this was causing the trouble while reading the token from cli config. on your machine. I corrected it.

  3. This change now will enable the user to use multiple API keys.

  4. The credential file now will look like:

{
				"apikeys": {
					"my_token1": "123456",
					"my_token2": "785645"
				},
				"meta": {
					"current_apikey": "my_token1"
				}
}

Screenshots:

provider.tf

Screenshot 2024-07-15 222200

Network resource successfully created:

Screenshot 2024-07-15 222449

@uzaxirr
Copy link
Member

uzaxirr commented Jul 16, 2024

@Praveen005 can you test the following locally and attach screenshots for them.

  1. with credential_file attribute
  2. with token attribute
  3. with just the CIVO_TOKEN env var, no credential file or token
  4. Without credential_file, token or the env var (i.e token should be picked up from the CLI config)
  5. without credential_file, token, env var or ~/.civo.json file present (i.e some error should occour)

@Praveen005
Copy link
Contributor Author

Thank you for reviewing it @uzaxirr.

Here are all the relevant screenshots:

  1. Token provided only via credential_file attribute.
Screenshot 2024-07-17 023709 Screenshot 2024-07-17 023821
  1. Token provided only via token attribute
Screenshot 2024-07-17 024222 Warning message being correctly displayed: Screenshot 2024-07-17 024312 Screenshot 2024-07-17 024408
  1. Token provided only via CIVO_TOKEN environment variable
Screenshot 2024-07-17 023227 Screenshot 2024-07-17 023252
  1. Token available only in ~/.civo.json
Screenshot 2024-07-17 005638 Screenshot 2024-07-17 021108
  1. No token Provider at all.
Screenshot 2024-07-17 022426

Resource creation failed:

Screenshot 2024-07-17 022554

@fernando-villalba
Copy link
Contributor

Looks great to me so far. As a suggested improvement, if no token is found at all. We could point the user to where to get one:

https://dashboard.civo.com/security

For example:

"No token configuration found in $CIVO_TOKEN or ~/.civo.json, please go to https://dashboard.civo.com/security to fetch one"

@Praveen005
Copy link
Contributor Author

Hi @fernando-villalba,

I’ve updated the error message to be more expressive as suggested. Here is the new version:

Screenshot 2024-07-17 142936

Can you kindly, check it now?

Thank you!

@fernando-villalba
Copy link
Contributor

LGTM!

@Praveen005
Copy link
Contributor Author

Hi @alessandroargentieri,

I hope you're doing well. When you have a moment, could you please review this PR?

Thank you!

@uzaxirr
Copy link
Member

uzaxirr commented Jul 18, 2024

hey @Praveen005 the PR looks fine, i'm in process of updating the corresponding docs for this before we merge off this one

@Praveen005
Copy link
Contributor Author

Thank you @uzaxirr. Let me know if there's anything I can assist with regarding the documentation.

Regards!

@fernando-villalba
Copy link
Contributor

One more thing, when we have the updated documents, I would also add a link to the documentation in the deprecation message. Thank you for your hard work and patience @Praveen005 !

@Praveen005
Copy link
Contributor Author

Praveen005 commented Jul 18, 2024

@fernando-villalba That would be better.

Just a caveat, the link to the documentation will have to be added in validateTokenUsage function.

Screenshot 2024-07-18 143757

Also, in the documentation, should we mention the format in which user should store their token in the credential file?
Like:

{
  "apikeys": {
    "demo_key": "2p35nGkS9NQncivoko5qqX3FtF643RUfqjniiH2"
  },
  "meta": {
    "current_apikey": "demo_key"
  }
}

This way user can store multiple API keys for multiple accounts (home, work etc.). Also it would be consistent with the way, Civo CLI stores token in ~/.civo.json

@fernando-villalba
Copy link
Contributor

By the way, @Praveen005 and @uzaxirr

Sorry to be a stickler for detail but change credential_file to credentials_file as originally proposed on the ticket.

I know that technically we are only using one credential, the one token, but in a security context is weird referring to it as singular.

Other than that, let's get this out on next release - doing documentation for it now!

@Praveen005
Copy link
Contributor Author

Hi @fernando-villalba,

That is a lapse on my part, and I’ll be more mindful in future. Thanks for the heads up. I'll update the code to use credentials_file as suggested.

Regards,

Praveen

@uzaxirr
Copy link
Member

uzaxirr commented Aug 7, 2024

@Praveen005 can you do the above change on priority so that i could merge this PR

@Praveen005
Copy link
Contributor Author

Hey @uzaxirr, I have tested it again and it works as intended.

with credentials_file attribute:

Screenshot 2024-08-07 194640

with credential_file attribute:

Screenshot 2024-08-07 194725

Copy link
Member

@uzaxirr uzaxirr left a comment

Choose a reason for hiding this comment

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

Thankss a ton for this PR

@uzaxirr uzaxirr merged commit cbcaa08 into civo:master Aug 7, 2024
1 check passed
@Praveen005 Praveen005 deleted the fix246 branch August 7, 2024 16:24
@Praveen005
Copy link
Contributor Author

Thank you @uzaxirr . It means a lot.

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