From fe9001d2660783fc81b7c92b422365463a7c1364 Mon Sep 17 00:00:00 2001 From: sawnjordan Date: Tue, 1 Jul 2025 17:07:29 +0545 Subject: [PATCH 1/2] feat: on sso setup, removed extra prompt and setup via start url and region --- internal/sso/session.go | 9 +++------ internal/sso/session_test.go | 35 ++++------------------------------- internal/sso/sso.go | 28 ++++++++-------------------- 3 files changed, 15 insertions(+), 57 deletions(-) diff --git a/internal/sso/session.go b/internal/sso/session.go index 0be21fd..4c472b8 100644 --- a/internal/sso/session.go +++ b/internal/sso/session.go @@ -61,15 +61,12 @@ func (c *RealSSOClient) loadOrCreateSession() (string, *models.SSOSession, error return "", nil, fmt.Errorf("failed to prompt for SSO start URL: %w", err) } - region, err := c.Prompter.PromptForRegion("ap-south-1") + region, err := c.Prompter.PromptForRegion("us-east-1") if err != nil { return "", nil, fmt.Errorf("failed to prompt for SSO region: %w", err) } - scopes, err := c.Prompter.PromptWithDefault("SSO registration scopes (comma separated)", "sso:account:access") - if err != nil { - return "", nil, fmt.Errorf("failed to prompt for SSO scopes: %w", err) - } + scopes := "sso:account:access" ssoSession = &models.SSOSession{ Name: name, @@ -235,7 +232,7 @@ func (c *RealSSOClient) runSSOLogin(sessionName string) error { fmt.Println("\nInitiating AWS SSO login... (this may open a browser window)") - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() if err := c.Executor.RunInteractiveCommand(ctx, "aws", "sso", "login", "--sso-session", sessionName); err != nil { diff --git a/internal/sso/session_test.go b/internal/sso/session_test.go index 60ed80f..95cbc7c 100644 --- a/internal/sso/session_test.go +++ b/internal/sso/session_test.go @@ -43,8 +43,7 @@ func TestLoadOrCreateSession(t *testing.T) { mockPrompts: []mockPrompt{ {"PromptWithDefault", "SSO session name", "default-sso", "test-session", nil}, {"PromptRequired", "SSO start URL (e.g., https://my-sso-portal.awsapps.com/start)", "", "https://test.awsapps.com/start", nil}, - {"PromptForRegion", "SSO region (Default: ap-south-1):", "ap-south-1", "us-west-2", nil}, - {"PromptWithDefault", "SSO registration scopes (comma separated)", "sso:account:access", "sso:account:access", nil}, + {"PromptForRegion", "us-east-1", "us-east-1", "us-west-2", nil}, }, wantSession: &models.SSOSession{ Name: "test-session", @@ -54,30 +53,7 @@ func TestLoadOrCreateSession(t *testing.T) { }, wantConfigPath: "", }, - { - name: "Use existing single session - exact name match", - initialConfig: &models.Config{ - SSOSessions: []models.SSOSession{ - { - Name: "existing-session", - StartURL: "https://existing.awsapps.com/start", - Region: "us-east-1", - }, - }, - }, - mockPrompts: []mockPrompt{ - {"PromptWithDefault", "SSO session name", "default-sso", "existing-session", nil}, - {"PromptRequired", "SSO start URL (e.g., https://my-sso-portal.awsapps.com/start)", "", "https://existing.awsapps.com/start", nil}, - {"PromptForRegion", "SSO region (Default: ap-south-1):", "ap-south-1", "us-east-1", nil}, - {"PromptWithDefault", "SSO registration scopes (comma separated)", "sso:account:access", "sso:account:access", nil}, - }, - wantSession: &models.SSOSession{ - Name: "existing-session", - StartURL: "https://existing.awsapps.com/start", - Region: "us-east-1", - Scopes: "sso:account:access", - }, - }, + { name: "Region prompt error", initialConfig: &models.Config{ @@ -86,7 +62,7 @@ func TestLoadOrCreateSession(t *testing.T) { mockPrompts: []mockPrompt{ {"PromptWithDefault", "SSO session name", "default-sso", "test-session", nil}, {"PromptRequired", "SSO start URL (e.g., https://my-sso-portal.awsapps.com/start)", "", "https://test.awsapps.com/start", nil}, - {"PromptForRegion", "SSO region (Default: ap-south-1):", "ap-south-1", "", errors.New("invalid region")}, + {"PromptForRegion", "us-east-1", "us-east-1", "", errors.New("invalid region")}, }, wantErr: true, errContains: "failed to prompt for SSO region", @@ -100,6 +76,7 @@ func TestLoadOrCreateSession(t *testing.T) { mockPrompter := mock_sso.NewMockPrompter(ctrl) + // Set up expected mock calls for _, mp := range tt.mockPrompts { switch mp.method { case "PromptWithDefault": @@ -114,10 +91,6 @@ func TestLoadOrCreateSession(t *testing.T) { mockPrompter.EXPECT(). PromptForRegion(mp.defaultValue). Return(mp.response, mp.err) - case "SelectFromList": - mockPrompter.EXPECT(). - SelectFromList(mp.label, gomock.Any()). - Return(mp.response, mp.err) } } diff --git a/internal/sso/sso.go b/internal/sso/sso.go index 8deb051..2809a66 100644 --- a/internal/sso/sso.go +++ b/internal/sso/sso.go @@ -45,42 +45,30 @@ func (c *RealSSOClient) SetupSSO() error { return fmt.Errorf("failed to select role: %w", err) } - profileName, region, err := c.promptProfileDetails(ssoSession.Region) - if err != nil { - if errors.Is(err, promptUtils.ErrInterrupted) { - return nil - } - return fmt.Errorf("failed to prompt profile details: %w", err) - } + profileName := ssoSession.Name + "-profile" - if err := c.configureAWSProfile(profileName, ssoSession.Name, ssoSession.Region, ssoSession.StartURL, accountID, role, region); err != nil { + if err := c.configureAWSProfile(profileName, ssoSession.Name, ssoSession.Region, ssoSession.StartURL, accountID, role, ssoSession.Region); err != nil { return fmt.Errorf("failed to configure AWS profile: %w", err) } defaultConfigured := profileName == "default" + if !defaultConfigured { - setDefault, err := c.Prompter.PromptYesNo("Set this as the default profile? [Y/n]", true) - if err != nil { - if errors.Is(err, promptUtils.ErrInterrupted) { - return nil - } - return fmt.Errorf("failed to prompt for default profile: %w", err) - } - if setDefault { - if err := c.configureAWSProfile("default", ssoSession.Name, ssoSession.Region, ssoSession.StartURL, accountID, role, region); err != nil { - return fmt.Errorf("failed to configure AWS default profile: %w", err) - } - defaultConfigured = true + if err := c.configureAWSProfile("default", ssoSession.Name, ssoSession.Region, ssoSession.StartURL, accountID, role, ssoSession.Region); err != nil { + return fmt.Errorf("failed to configure AWS default profile: %w", err) } + defaultConfigured = true } printSummary(profileName, ssoSession.Name, ssoSession.StartURL, ssoSession.Region, accountID, role, "", "", "") fmt.Printf("\nSuccessfully configured AWS profile '%s'!\n", profileName) + if defaultConfigured { fmt.Println("You can now use AWS CLI commands without specifying --profile") } else { fmt.Printf("You can now use this profile with AWS CLI commands using: --profile %s\n", profileName) } + return nil } From 002e96688a0bc6f5e936310a51f9cf6c6b876192 Mon Sep 17 00:00:00 2001 From: sawnjordan Date: Tue, 1 Jul 2025 17:31:28 +0545 Subject: [PATCH 2/2] fix: increased the sso setup time limit from 30 sec to 10 min --- internal/common/services.go | 2 +- internal/sso/client.go | 2 +- utils/common/common.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/common/services.go b/internal/common/services.go index 6a01fb5..d2fb5b4 100644 --- a/internal/common/services.go +++ b/internal/common/services.go @@ -221,7 +221,7 @@ func (s *Services) IsAWSConfigured() bool { return false } - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() creds, err := s.Provider.AwsConfig.Credentials.Retrieve(ctx) diff --git a/internal/sso/client.go b/internal/sso/client.go index 112b0ba..a4fa885 100644 --- a/internal/sso/client.go +++ b/internal/sso/client.go @@ -177,7 +177,7 @@ func (c *RealSSOClient) SSOLogin(awsProfile string, refresh, noBrowser bool) err } args = append(args, "--profile", awsProfile) - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() err := c.Executor.RunInteractiveCommand(ctx, "aws", args...) diff --git a/utils/common/common.go b/utils/common/common.go index 1e44f7e..e56234f 100644 --- a/utils/common/common.go +++ b/utils/common/common.go @@ -71,7 +71,7 @@ func terminateProcess(pid int) error { } } - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() for { select {