-
Notifications
You must be signed in to change notification settings - Fork 2
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
Porting account functionality for TS SDK #86
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2025, Pulumi Corporation. All rights reserved. | ||
|
||
import path from "path"; | ||
import fs from "fs"; | ||
import { Account, Credentials, EscCredentials } from "workspace_models"; | ||
|
||
/* | ||
Pulumi workspace and account logic for python SDK. | ||
This is a partial port of ESC and Pulumi CLI code found in | ||
https://github.com/pulumi/esc/tree/main/cmd/esc/cli/workspace | ||
*/ | ||
|
||
// Returns the path of the ".pulumi" folder where Pulumi puts its artifacts. | ||
export function getPulumiHomeDir(): string { | ||
// Allow the folder we use to be overridden by an environment variable | ||
const dirEnv = process.env.PULUMI_HOME; | ||
if (dirEnv) { | ||
return dirEnv; | ||
} | ||
|
||
// Otherwise, use the current user's home dir + .pulumi | ||
const homeDir = process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH; | ||
if (!homeDir) { | ||
throw new Error('Unable to determine home directory.'); | ||
} | ||
|
||
return path.join(homeDir, '.pulumi'); | ||
} | ||
|
||
// Returns the path of the ".esc" folder inside Pulumi home dir. | ||
export function getEscBookkeepingDir(): string { | ||
const homeDir = getPulumiHomeDir(); | ||
return path.join(homeDir, '.esc'); | ||
} | ||
|
||
// Returns the path to the esc credentials file on disk. | ||
export function getPathToCredsFile(dir: string): string { | ||
return path.join(dir, 'credentials.json'); | ||
} | ||
|
||
// Returns the current account name from the ESC credentials file. | ||
export function getEscCurrentAccountName(): string | null { | ||
try { | ||
const credsFile = getPathToCredsFile(getEscBookkeepingDir()); | ||
if (!fs.existsSync(credsFile)) { | ||
return null | ||
} | ||
const parsedData = JSON.parse(fs.readFileSync(credsFile, 'utf-8')); | ||
return (parsedData as EscCredentials)?.name; | ||
} catch (error) { | ||
console.error(`An unexpected error occurred: ${error}`); | ||
return null; | ||
} | ||
} | ||
|
||
// Reads and parses credentials from the Pulumi credentials file. | ||
export function getStoredCredentials(): Credentials | null { | ||
try { | ||
const credsFile = getPathToCredsFile(getPulumiHomeDir()); | ||
if (!fs.existsSync(credsFile)) { | ||
return null | ||
} | ||
const parsedData = JSON.parse(fs.readFileSync(credsFile, 'utf8')); | ||
return parsedData as Credentials | ||
} catch (error) { | ||
console.error(`An unexpected error occurred: ${error}`); | ||
return null; | ||
} | ||
} | ||
|
||
// Gets current account values from credentials file. | ||
export function getCurrentAccount(): {account?: Account, backendUrl?: string } { | ||
let backendUrl = getEscCurrentAccountName(); | ||
const pulumiCredentials = getStoredCredentials(); | ||
if (!pulumiCredentials) { | ||
return {}; | ||
} | ||
if (!backendUrl) { | ||
backendUrl = pulumiCredentials.current; | ||
} | ||
if (!backendUrl || !(backendUrl in pulumiCredentials.accounts)) { | ||
return {}; | ||
} | ||
return { | ||
account: pulumiCredentials.accounts[backendUrl], | ||
backendUrl, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2025, Pulumi Corporation. All rights reserved. | ||
|
||
/* | ||
Models for Pulumi workspace and account logic for python SDK. | ||
This is a partial port of ESC and Pulumi CLI code found in | ||
https://github.com/pulumi/esc/tree/main/cmd/esc/cli/workspace | ||
*/ | ||
|
||
export interface Account { | ||
accessToken: string; | ||
username: string; | ||
organizations: string; | ||
} | ||
|
||
export interface Credentials { | ||
current: string; | ||
accessTokens: { [key: string]: string }; | ||
accounts: { [key: string]: Account }; | ||
} | ||
|
||
export interface EscCredentials { | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2025, Pulumi Corporation. | ||
|
||
import assert from "assert"; | ||
import { DefaultClient } from "index"; | ||
import { after, before, describe, it } from "node:test"; | ||
import path from "path"; | ||
|
||
describe("ESC", async () => { | ||
let tokenBefore: string | undefined | ||
let backendBefore: string | undefined | ||
let homeBefore: string | undefined | ||
|
||
before(async () => { | ||
tokenBefore = process.env.PULUMI_ACCESS_TOKEN; | ||
backendBefore = process.env.PULUMI_BACKEND_URL; | ||
homeBefore = process.env.PULUMI_HOME; | ||
delete process.env.PULUMI_ACCESS_TOKEN; | ||
delete process.env.PULUMI_BACKEND_URL; | ||
}); | ||
|
||
after(async () => { | ||
process.env.PULUMI_ACCESS_TOKEN = tokenBefore; | ||
process.env.PULUMI_BACKEND_URL = backendBefore; | ||
process.env.PULUMI_HOME = homeBefore; | ||
}); | ||
|
||
it("test no creds at all", async () => { | ||
process.env.PULUMI_HOME = "/not_real_dir"; | ||
let client = DefaultClient(); | ||
assert.equal(client.config.basePath, undefined); | ||
assert.equal(client.config.accessToken, undefined); | ||
}); | ||
|
||
it("test just pulumi creds", async () => { | ||
process.env.PULUMI_HOME = path.dirname(process.cwd()) + "/test/test_pulumi_home"; | ||
let client = DefaultClient(); | ||
assert.equal(client.config.basePath, "https://api.moolumi.com/api/esc"); | ||
assert.equal(client.config.accessToken, "pul-fake-token-moo"); | ||
}); | ||
|
||
it("test pulumi creds with esc", async () => { | ||
process.env.PULUMI_HOME = path.dirname(process.cwd()) + "/test/test_pulumi_home_esc"; | ||
let client = DefaultClient(); | ||
assert.equal(client.config.basePath, "https://api.boolumi.com/api/esc"); | ||
assert.equal(client.config.accessToken, "pul-fake-token-boo"); | ||
}); | ||
|
||
it("test pulumi creds bad format", async () => { | ||
process.env.PULUMI_HOME = path.dirname(process.cwd()) + "/test/test_pulumi_home_bad_format"; | ||
let client = DefaultClient(); | ||
assert.equal(client.config.basePath, undefined); | ||
assert.equal(client.config.accessToken, undefined); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line isn't necessary since everything is false by default. the only reason we specify the others above as false is because of the wildcard for
sdk/typescript