Skip to content

Commit

Permalink
Store the hostname in the secret store
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpogran committed Jun 7, 2024
1 parent ad26c94 commit 273a5ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/api/terraformCloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export function earlySetupForHostname(hostname: string) {
earlyApiClient.use(pluginLogger());
}

export function apiSetup() {
export function apiSetup(hostname: string) {
TerraformCloudHost = hostname;
TerraformCloudAPIUrl = `https://${TerraformCloudHost}/api/v2`;
TerraformCloudWebUrl = `https://${TerraformCloudHost}/app`;
// ApiClient setup
apiClient = new Zodios(TerraformCloudAPIUrl, [
...accountEndpoints,
Expand Down
14 changes: 10 additions & 4 deletions src/providers/tfc/authenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class TerraformCloudSession implements vscode.AuthenticationSession {
* @param accessToken The personal access token to use for authentication
* @param account The user account for the specified token
*/
constructor(public readonly accessToken: string, public account: vscode.AuthenticationSessionAccountInformation) {}
constructor(
public readonly accessToken: string,
public readonly hostName: string,
public account: vscode.AuthenticationSessionAccountInformation,
) {}
}

interface TerraformCloudToken {
Expand Down Expand Up @@ -69,13 +73,13 @@ class TerraformCloudSessionHandler {
},
});

const session = new TerraformCloudSession(token, {
const session = new TerraformCloudSession(token, hostname, {
label: user.data.attributes.username,
id: user.data.id,
});

await this.secretStorage.store(this.sessionKey, JSON.stringify(session));
apiSetup();
apiSetup(session.hostName);
return session;
} catch (error) {
if (error instanceof ZodiosError) {
Expand Down Expand Up @@ -111,7 +115,7 @@ export class TerraformCloudAuthenticationProvider implements vscode.Authenticati
private sessionHandler: TerraformCloudSessionHandler;
// this property is used to determine if the session has been changed in another window of VS Code
// it's a promise, so we can set in the constructor where we can't execute async code
private sessionPromise: Promise<vscode.AuthenticationSession | undefined>;
private sessionPromise: Promise<TerraformCloudSession | undefined>;
private disposable: vscode.Disposable | undefined;

private _onDidChangeSessions =
Expand Down Expand Up @@ -160,6 +164,8 @@ export class TerraformCloudAuthenticationProvider implements vscode.Authenticati
try {
const session = await this.sessionPromise;
if (session) {
earlySetupForHostname(session.hostName);
apiSetup(session.hostName);
this.logger.info('Successfully fetched HCP Terraform session');
await vscode.commands.executeCommand('setContext', 'terraform.cloud.signed-in', true);
return [session];
Expand Down

0 comments on commit 273a5ab

Please sign in to comment.