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

Specify pulumi access token per command run #263

Merged
merged 3 commits into from
Mar 14, 2024
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 @@
[#264](https://github.com/pulumi/esc/pull/264)

### Bug Fixes

- Specify pulumi access token per command run.
[#263](https://github.com/pulumi/esc/pull/263)
35 changes: 29 additions & 6 deletions cmd/esc/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ func newLoginCmd(esc *escCommand) *cobra.Command {
if err != nil {
return fmt.Errorf("could not determine current cloud: %w", err)
}
if account == nil {
backendURL = "https://api.pulumi.com"
} else {
backendURL = account.BackendURL
}

backendURL = esc.workspace.GetCurrentCloudURL(account)
shared = isShared
}

Expand Down Expand Up @@ -157,7 +154,33 @@ func (esc *escCommand) getCachedClient(ctx context.Context) error {
return fmt.Errorf("could not determine current cloud: %w", err)
}
if account == nil {
return fmt.Errorf("no credentials. Please run `%v login` to log in.", esc.command)
backendURL := esc.workspace.GetCurrentCloudURL(nil)

nAccount, err := esc.login.Login(
ctx,
backendURL,
false,
"esc",
"Pulumi ESC environments",
nil,
false,
display.Options{Color: esc.colors},
)
Comment on lines +157 to +168
Copy link
Member

Choose a reason for hiding this comment

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

I think the only remaining question I have is what the experience is like if no access token is specified via PULUMI_ACCESS_TOKEN? If we fall through to the err != nil case below I think that we'll be regressing the UX, as the user will see could not determine current cloud instead of no credentials. Please run esc login to log in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is the same experience you have with the pulumi cli where it asks you to authenticate

image image

Copy link
Member

Choose a reason for hiding this comment

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

excellent; thanks!

Copy link
Member

Choose a reason for hiding this comment

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

what about in non-interactive scenarios?

Copy link
Member

Choose a reason for hiding this comment

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

(try running with the output redirected, for example)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the output
image


if err != nil {
return fmt.Errorf("could not determine current cloud: %w", err)
}

defaultOrg, err := esc.workspace.GetBackendConfigDefaultOrg(backendURL, nAccount.Username)
if err != nil {
return fmt.Errorf("could not determine default org: %w", err)
}

account = &workspace.Account{
Account: *nAccount,
BackendURL: backendURL,
DefaultOrg: defaultOrg,
}
}

if err := esc.checkBackendURL(account.BackendURL); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions cmd/esc/cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"testing"

"github.com/pulumi/esc/cmd/esc/cli/workspace"
"github.com/pulumi/pulumi/pkg/v3/backend/httpstate"
pulumi_workspace "github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/stretchr/testify/assert"
)

func TestNoCreds(t *testing.T) {
fs := testFS{}
esc := &escCommand{workspace: workspace.New(fs, &testPulumiWorkspace{})}
esc := &escCommand{
workspace: workspace.New(fs, &testPulumiWorkspace{}),
login: httpstate.NewLoginManager(),
}
err := esc.getCachedClient(context.Background())
assert.ErrorContains(t, err, "no credentials")

esc.command = "pulumi"
err = esc.getCachedClient(context.Background())
assert.ErrorContains(t, err, "pulumi login")
assert.ErrorContains(t, err, "could not determine current cloud")
}

func TestInvalidSelfHostedBackend(t *testing.T) {
Expand Down
17 changes: 17 additions & 0 deletions cmd/esc/cli/workspace/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import (
"errors"
"fmt"
"io/fs"
"os"
"path"

"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)

// PulumiBackendURLEnvVar is an environment variable which can be used to set the backend that will be
// used instead of the currently logged in backend or the current projects backend.
const PulumiBackendURLEnvVar = "PULUMI_BACKEND_URL"

// Account holds details about a Pulumi account.
type Account struct {
workspace.Account
Expand Down Expand Up @@ -62,6 +67,18 @@ func (w *Workspace) GetAccount(backendURL string) (*Account, error) {
}, nil
}

func (w *Workspace) GetCurrentCloudURL(account *Account) string {
if account != nil {
return account.BackendURL
}

if backend := os.Getenv(PulumiBackendURLEnvVar); backend != "" {
return backend
}

return "https://api.pulumi.com"
}

// GetCurrentAccount returns information about the currently logged-in account.
func (w *Workspace) GetCurrentAccount(shared bool) (*Account, bool, error) {
// Read esc account.
Expand Down
Loading