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

Prompt for custom tfcloud host #1678

Merged
merged 8 commits into from
Jun 26, 2024

Conversation

Convez
Copy link
Contributor

@Convez Convez commented Jan 25, 2024

This is a simplified version of PR #1673 , as requested
This closes #1671 and closes #1505
Allow TFC host selection during login. Hosts already in TFC credentials file are displayed. Options for insertion of new hosts added:
image
image

N.B. Logging to a new hostname does not update the TFC credentials file (Maybe to be implemented in the future?)

@Convez Convez requested a review from a team as a code owner January 25, 2024 08:57
@Convez Convez changed the title Prompt for custom tfcloud host [Simplified PR] - Prompt for custom tfcloud host Jan 25, 2024
@Convez Convez changed the title [Simplified PR] - Prompt for custom tfcloud host Prompt for custom tfcloud host Feb 5, 2024
Copy link
Contributor

@jpogran jpogran left a comment

Choose a reason for hiding this comment

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

Hi @Convez, thanks for the updated PR. I appreciate your patience with the delays in reviewing your original PR and this one. I'm grateful for your continued contributions. The delay is not a reflection of the importance of your contributions, it's a result of new priorities and the need to manage shifting timelines.

For now, with your permission, I can do a couple of things to get some forward movement here. I would like to extract the bug fix for the Terraform credential path on Windows myself into a separate PR and attribute you for it. Then I'll rebase this PR so there is not so many merge commits in it and get it in a ready to review state. My team will be discussing TFE support in our quarterly planning in 2 weeks, after that I will let you know the results of that meeting and how that applies to this PR.

I again thank you for your patience.

@jpogran
Copy link
Contributor

jpogran commented Apr 15, 2024

Hey @Convez, I've extracted and merged #1735, which contains your bug fix for the credentials file.

With your permission I can rebase this PR against main and keep i up to date.

@willjprice
Copy link

Bump.

@Convez
Copy link
Contributor Author

Convez commented May 30, 2024

Hey @Convez, I've extracted and merged #1735, which contains your bug fix for the credentials file.

With your permission I can rebase this PR against main and keep i up to date.

Yes, of course. Thank you very much. Sorry for the (very) late reply

@jpogran jpogran force-pushed the feat/custom-tfcloud-host-simple branch from 9d8a9fb to 51dd4a7 Compare May 30, 2024 19:11
@jpogran
Copy link
Contributor

jpogran commented Jun 7, 2024

Hey @Convez, thanks for your patience. There are/have been several things in the air that either needed to land or be started before I could come back to this. The good news is I am picking this back up now.

I've rebased and made a small commit updating some HCP Terraform renaming, which is minor. Then I have tested it against a test TFE instance that has over 10,000 workspaces, and the good news is it works which was a concern of ours since we didn't load test this extension with TFE workloads in mind. The bad news is I've hit two bugs, but only one is related to this PR.

The one related to this PR is it not remembering the hostname after closing out VS Code. This seems to hit mostly with the temp profile, but did happen with my personal install. I think I found the resolution by storing the hostname along with the token in the secret store. This way it is present when the extension starts again. I still have to test it with expired tokens to make sure all the auth related scenarios still work, but I think that resolves it.

The bug not related to your PR is refreshing the workspace view after loading a significant number of workspaces seems to fail with a duplicate key. I'll resolve that separately.

The next steps is to finish out the testing auth scenarios, then we can go through the review process and get this merged.

@Convez
Copy link
Contributor Author

Convez commented Jun 9, 2024

Hello @jpogran ,

I see, please let me know if there is anything I can do to help from my side 😄

@jpogran jpogran force-pushed the feat/custom-tfcloud-host-simple branch 2 times, most recently from 9abab90 to 0137d57 Compare June 18, 2024 18:27
Copy link
Member

@ansgarm ansgarm left a comment

Choose a reason for hiding this comment

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

Hey 👋

I just test drove this PR a little and noticed a small inconsistency (which seems to have existed before) when opting to use an existing token from the Terraform CLI.

Steps to reproduce:

  1. Have an existing ~/.terraform.d/credentials.tfrc.json file with e.g. credentials for app.terraform.io (doesn't really matter)
  2. Login to HCP Terraform
  3. Select "New Instance"
  4. Put in some endpoint for which we don't have credentials in the TF CLI file
  5. Select "Stored user token"
  6. There's no error message but we also are not logged in
    (this also happens for the "Default HCP Terraform Instance" login option if there are no credentials for app.terraform.io in the credentials.tfrc.json file)

A similar thing I noticed (that may be related) is that when selecting "Existing user token – Enter a token manually" and then entering an empty string the same behavior seems to happen (i.e. nothing happens, we are not logged in and no message is displayed). However, in this case I could see entering an empty string as a way of aborting (But the input also states "Press Esc to cancel" so there's that 😄)

Other than that, I could successfully log into a different instance 🎉 (I used it to log into the HCP Terraform staging instance for testing as I don't have a TFE instance ready).

Also: Do we need to add some note somewhere that it's only possible to log into one endpoint at a time? When I logged into the staging instance, another VS Code window of mine popped up from the background and told me that I was logged out of HCP Terraform and prompted me to login again 😅 Is this something we need to handle better? Is it already known, @jpogran?

Copy link
Member

@dbanck dbanck left a comment

Choose a reason for hiding this comment

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

With an existing session/login present, I'm getting this error on this branch
CleanShot 2024-06-20 at 11 18 03@2x

Steps to reproduce

  1. checkout main
  2. start the extension
  3. login with your HCP Terraform credentials
  4. close the extension
  5. switch to this branch
  6. start the extension again

We might need something like a default fallback to HCP Terraform?

@jpogran jpogran force-pushed the feat/custom-tfcloud-host-simple branch from 4a7a40c to 501ceaa Compare June 21, 2024 15:08
@jpogran
Copy link
Contributor

jpogran commented Jun 21, 2024

I just test drove this PR a little and noticed a small inconsistency (which seems to have existed before) when opting to use an existing token from the Terraform CLI.

This was a failure of the token check inside createSession and the session handler. The session handler was supposed to throw an InvalidToken exception that is caught and re-prompts the user. In practice we threw an Error before getting to the session handler, which exited the method without an error (because createSession requires you to return a rejected promise and not throw).

This should be fixed now.

A similar thing I noticed (that may be related) is that when selecting "Existing user token – Enter a token manually" and then entering an empty string the same behavior seems to happen (i.e. nothing happens, we are not logged in and no message is displayed). However, in this case I could see entering an empty string as a way of aborting (But the input also states "Press Esc to cancel" so there's that 😄)

This should be fixed by the above fix, which handles this similar scenario where there isn't a token returned.

Also: Do we need to add some note somewhere that it's only possible to log into one endpoint at a time? When I logged into the staging instance, another VS Code window of mine popped up from the background and told me that I was logged out of HCP Terraform and prompted me to login again 😅 Is this something we need to handle better? Is it already known, @jpogran?

Previously we only supported one endpoint, so there was only one login/token allowed and we would trigger a session refresh in all windows if one window changed. Now that we are accepting custom endpoints, it is a possibility but would require more work than this PR intends to cover. I can ticket this request

@jpogran
Copy link
Contributor

jpogran commented Jun 21, 2024

With an existing session/login present, I'm getting this error

We might need something like a default fallback to HCP Terraform?

This is because on load, we only checked if there was a persisted session and not if it had all of the properties we expected. This is fixed by checking to see if the stored session has a hostname, and if not adding the default HCP Terraform hostname then calling the session handler store method. This is a safe assumption because we know all prior sessions were only for HCP Terraform and we can update the stored session accordingly.

@jpogran jpogran requested a review from dbanck June 21, 2024 15:49
Copy link
Member

@ansgarm ansgarm left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@dbanck dbanck left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the hostname issue. It now works fine when starting from an existing login (main).

I added a suggestion to reduce duplication in client creation, but feel free to ignore it.

Copy link
Member

@dbanck dbanck left a comment

Choose a reason for hiding this comment

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

I just noticed that I have to select approve because I previously requested changes. Oops

Convez and others added 7 commits June 26, 2024 09:20
This commit renames all instances of "Terraform Cloud" to "HCP Terraform" in the authentication provider in accordance with the branding changes.
This commit stores the hostname in the secret store when the user logs in. This ensures that the hostname is available when the user logs in again.

This is necessary because the hostname is used to construct the API URL and the Web URL for a given TFE or HCP Terraform instance.
This changes the order of hostname options in the quick pick by moving the new hostname option to the bottom of the list and presenting existing host names first. This makes it easier for users to select an existing hostname if they have one, as that is most likely the more common workflow.
This change adds the ability to identify TFE instances by making a call to the `/ping` endpoint. This will allow the user to see the name of the instance they are connecting to in the quickpick menu.

There is still some hardcoding of 'HCP Terraform' in the code that needs to be updated to be more generic, but that can be handled in a separate set of work.
This handles sessions created before this version without a hostname to connect to. This will set the hostname to the default Terraform Cloud API URL if it is undefined.
Instead of rebuilding the entire apiClient, we use the `pluginBaseURL` plugin to override the previous `baseURL`. This allows us to keep the endpoint setup in one place.
@jpogran jpogran force-pushed the feat/custom-tfcloud-host-simple branch from f2bf20c to f1f80d1 Compare June 26, 2024 13:25
@jpogran jpogran merged commit 5435632 into hashicorp:main Jun 26, 2024
12 checks passed
@jpogran
Copy link
Contributor

jpogran commented Jun 26, 2024

@Convez this is now merged to main, and will go out with the next release. Thank you for your patience, we really appreciate the contribution!

Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow terraform cloud instances other than app.terraform.io Support Terraform Enterprise in the TFC view
5 participants