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

cli: fix more login bugs #111

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

- Do not panic when `env set` is passed an empty value.
[#110](https://github.com/pulumi/esc/pull/110)

- Fix behavior for `esc login` when no existing credentials are present
[#111](https://github.com/pulumi/esc/pull/111)
10 changes: 9 additions & 1 deletion cmd/esc/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,15 @@ func (lm *testLoginManager) Login(
) (*workspace.Account, error) {
acct, ok := lm.creds.Accounts[cloudURL]
if !ok {
return nil, errors.New("unauthorized")
if cloudURL != "https://api.pulumi.com" {
return nil, errors.New("unauthorized")
}
acct := workspace.Account{
Username: "test-user",
AccessToken: "access-token",
}
lm.creds.Accounts[cloudURL] = acct
return &acct, nil
}
return &acct, nil
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/esc/cli/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func newLogoutCmd(esc *escCommand) *cobra.Command {
if err != nil {
return fmt.Errorf("getting current account: %w", err)
}
if account == nil {
fmt.Fprintf(esc.stdout, "Already logged out.\n")
return nil
}
backendURL = account.BackendURL
}
if err := esc.workspace.DeleteAccount(backendURL); err != nil {
Expand Down
24 changes: 24 additions & 0 deletions cmd/esc/cli/testdata/env-login-gh100.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This is a regression test for #100 that aims to capture issues logging in when no existing Pulumi
# credentials exist.
run: |
esc logout --all
esc login
esc env ls
esc logout
esc logout
stdout: |
> esc logout --all
Logged out of all clouds
> esc login
Logged in to pulumi.com as test-user (https://app.pulumi.com/test-user)
> esc env ls
> esc logout
Logged out of https://api.pulumi.com
> esc logout
Already logged out.
stderr: |
> esc logout --all
> esc login
> esc env ls
> esc logout
> esc logout
12 changes: 11 additions & 1 deletion cmd/esc/cli/workspace/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,18 @@ func (w *Workspace) DeleteAccount(backendURL string) error {

// SetCurrentAccount sets the currently logged-in account.
func (w *Workspace) SetCurrentAccount(account Account, shared bool) error {
// Read Pulumi creds.
pulumiCreds, err := w.pulumi.GetStoredCredentials()
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("reading Pulumi credentials: %w", err)
}

// If there is no current Pulumi account and we want to share the current Pulumi account, then we need to set the
// current Pulumi account.
setCurrent := shared && pulumiCreds.Current == ""

// Store the account in Pulumi creds.
if err := w.pulumi.StoreAccount(account.BackendURL, account.Account, false); err != nil {
if err := w.pulumi.StoreAccount(account.BackendURL, account.Account, setCurrent); err != nil {
return fmt.Errorf("writing Pulumi credentials: %w", err)
}

Expand Down