-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathauth_login.go
More file actions
308 lines (260 loc) · 10 KB
/
auth_login.go
File metadata and controls
308 lines (260 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package cmd
import (
"context"
"errors"
"fmt"
"os"
"sort"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/spf13/cobra"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/internal/tui/templates/term"
"github.com/cloudposse/atmos/pkg/auth"
"github.com/cloudposse/atmos/pkg/auth/credentials"
authTypes "github.com/cloudposse/atmos/pkg/auth/types"
"github.com/cloudposse/atmos/pkg/auth/validation"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/telemetry"
"github.com/cloudposse/atmos/pkg/ui/theme"
u "github.com/cloudposse/atmos/pkg/utils"
)
// authLoginCmd logs in using a configured identity.
var authLoginCmd = &cobra.Command{
Use: "login",
Short: "Authenticate using a configured identity",
Long: "Authenticate to cloud providers using an identity defined in `atmos.yaml`.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
ValidArgsFunction: ComponentsArgCompletion,
RunE: executeAuthLoginCommand,
}
func executeAuthLoginCommand(cmd *cobra.Command, args []string) error {
handleHelpRequest(cmd, args)
// Load atmos config.
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
if err != nil {
return fmt.Errorf(errUtils.ErrWrapFormat, errUtils.ErrFailedToInitConfig, err)
}
defer perf.Track(&atmosConfig, "cmd.executeAuthLoginCommand")()
// Create auth manager.
authManager, err := createAuthManager(&atmosConfig.Auth)
if err != nil {
return fmt.Errorf(errUtils.ErrWrapFormat, errUtils.ErrFailedToInitializeAuthManager, err)
}
// Check if --provider flag was provided.
providerName, _ := cmd.Flags().GetString("provider")
// Perform authentication based on whether provider or identity was specified.
ctx := context.Background()
var whoami *authTypes.WhoamiInfo
if providerName != "" {
// Provider-level authentication (e.g., for SSO auto-provisioning).
whoami, err = authManager.AuthenticateProvider(ctx, providerName)
if err != nil {
// Return error directly - it already has ErrorBuilder context with hints.
return err
}
} else {
// Try identity-level authentication first.
var needsProviderFallback bool
whoami, needsProviderFallback, err = authenticateIdentity(ctx, cmd, authManager)
if needsProviderFallback {
// No identities available - fall back to provider authentication.
// This enables seamless first-login with auto_provision_identities.
providerName, err = getProviderForFallback(authManager)
if err != nil {
return err
}
whoami, err = authManager.AuthenticateProvider(ctx, providerName)
if err != nil {
// Return error directly - it already has ErrorBuilder context with hints.
return err
}
} else if err != nil {
return err
}
}
// Display success message using Atmos theme.
displayAuthSuccess(whoami)
return nil
}
// authenticateIdentity handles identity-level authentication with default and interactive selection.
// Returns (WhoamiInfo, needsProviderFallback, error) where needsProviderFallback indicates whether
// to fall back to provider-level authentication (when no identities are available).
func authenticateIdentity(ctx context.Context, cmd *cobra.Command, authManager auth.AuthManager) (*authTypes.WhoamiInfo, bool, error) {
// Get identity from flag or use default.
// Use centralized function that handles Cobra's NoOptDefVal quirk correctly.
identityName := GetIdentityFromFlags(cmd, os.Args)
// Check if user wants to interactively select identity.
forceSelect := identityName == IdentityFlagSelectValue
// If no identity specified, get the default identity (which prompts if needed).
// If --identity flag was used without value, forceSelect will be true.
if identityName == "" || forceSelect {
var err error
identityName, err = authManager.GetDefaultIdentity(forceSelect)
if err != nil {
// Check if we should fall back to provider-based auth.
// This happens when no identities are available (e.g., first login with auto_provision_identities).
if errors.Is(err, errUtils.ErrNoIdentitiesAvailable) ||
errors.Is(err, errUtils.ErrNoDefaultIdentity) {
return nil, true, nil
}
// Return error directly - it already has ErrorBuilder context with hints.
return nil, false, err
}
}
// Perform identity authentication.
whoami, err := authManager.Authenticate(ctx, identityName)
if err != nil {
// Return error directly - it already has ErrorBuilder context with hints.
return nil, false, err
}
return whoami, false, nil
}
// CreateAuthManager creates a new auth manager with all required dependencies.
// Exported for use by command packages (e.g., terraform package).
func CreateAuthManager(authConfig *schema.AuthConfig) (auth.AuthManager, error) {
return createAuthManager(authConfig)
}
// createAuthManager creates a new auth manager with all required dependencies.
func createAuthManager(authConfig *schema.AuthConfig) (auth.AuthManager, error) {
credStore := credentials.NewCredentialStore()
validator := validation.NewValidator()
return auth.NewAuthManager(authConfig, credStore, validator, nil)
}
const (
secondsPerMinute = 60
minutesPerHour = 60
)
// formatDuration formats a duration into a human-readable string.
func formatDuration(d time.Duration) string {
if d < 0 {
return "expired"
}
hours := int(d.Hours())
minutes := int(d.Minutes()) % minutesPerHour
seconds := int(d.Seconds()) % secondsPerMinute
if hours > 0 {
return fmt.Sprintf("%dh %dm", hours, minutes)
}
if minutes > 0 {
return fmt.Sprintf("%dm %ds", minutes, seconds)
}
return fmt.Sprintf("%ds", seconds)
}
// displayAuthSuccess displays a styled success message with authentication details.
func displayAuthSuccess(whoami *authTypes.WhoamiInfo) {
// Display checkmark with success message.
u.PrintfMessageToTUI("\n%s Authentication successful!\n\n", theme.Styles.Checkmark)
// Build table rows.
var rows [][]string
rows = append(rows, []string{"Provider", whoami.Provider})
rows = append(rows, []string{"Identity", whoami.Identity})
if whoami.Account != "" {
rows = append(rows, []string{"Account", whoami.Account})
}
if whoami.Region != "" {
rows = append(rows, []string{"Region", whoami.Region})
}
if whoami.Expiration != nil {
expiresStr := whoami.Expiration.Format("2006-01-02 15:04:05 MST")
duration := formatDuration(time.Until(*whoami.Expiration))
// Style duration with darker gray.
durationStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#808080"))
expiresStr = fmt.Sprintf("%s %s", expiresStr, durationStyle.Render(fmt.Sprintf("(%s)", duration)))
rows = append(rows, []string{"Expires", expiresStr})
}
// Create minimal charmbracelet table.
// Note: Padding variation across platforms was causing snapshot test failures.
// The table auto-sizes columns but the final width varied (Linux: 40 chars, macOS: 45 chars).
// Removed `.Width()` constraint as it was causing word-wrapping issues.
t := table.New().
Rows(rows...).
BorderTop(false).
BorderBottom(false).
BorderLeft(false).
BorderRight(false).
BorderRow(false).
BorderColumn(false).
StyleFunc(func(row, col int) lipgloss.Style {
if col == 0 {
// Key column - use cyan color.
return lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.ColorCyan)).
Padding(0, 1, 0, 2)
}
// Value column - default color with padding.
return lipgloss.NewStyle().Padding(0, 1)
})
fmt.Fprintf(os.Stderr, "%s\n\n", t)
}
func init() {
authLoginCmd.Flags().StringP("provider", "p", "", "Provider name to authenticate with (for SSO auto-provisioning)")
authCmd.AddCommand(authLoginCmd)
}
// providerLister is an interface for listing providers (subset of auth.AuthManager).
type providerLister interface {
ListProviders() []string
}
// isInteractive checks if we're running in an interactive terminal.
// Interactive mode requires stdin to be a TTY (for user input) and must not be in CI.
func isInteractive() bool {
return term.IsTTYSupportForStdin() && !telemetry.IsCI()
}
// getProviderForFallback determines which provider to use when no identities are configured.
// If only one provider exists, it is auto-selected.
// If multiple providers exist and interactive, prompts user.
// If multiple providers exist and non-interactive, returns error with helpful message.
func getProviderForFallback(authManager providerLister) (string, error) {
providers := authManager.ListProviders()
if len(providers) == 0 {
return "", errUtils.ErrNoProvidersAvailable
}
// Auto-select if only one provider.
if len(providers) == 1 {
return providers[0], nil
}
// Multiple providers - need interactive selection or error.
if !isInteractive() {
return "", fmt.Errorf("%w: use --provider flag to specify which provider", errUtils.ErrNoDefaultProvider)
}
return promptForProvider("No identities configured. Select a provider:", providers)
}
// promptForProvider prompts the user to select a provider from the given list.
func promptForProvider(message string, providers []string) (string, error) {
if len(providers) == 0 {
return "", errUtils.ErrNoProvidersAvailable
}
// Sort providers alphabetically for consistent ordering.
sortedProviders := make([]string, len(providers))
copy(sortedProviders, providers)
sort.Strings(sortedProviders)
var selectedProvider string
// Create custom keymap that adds ESC to quit keys.
keyMap := huh.NewDefaultKeyMap()
keyMap.Quit = key.NewBinding(
key.WithKeys("ctrl+c", "esc"),
key.WithHelp("ctrl+c/esc", "quit"),
)
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title(message).
Description("Press ctrl+c or esc to exit").
Options(huh.NewOptions(sortedProviders...)...).
Value(&selectedProvider),
),
).WithKeyMap(keyMap)
if err := form.Run(); err != nil {
// Check if user aborted (Ctrl+C, ESC, etc.).
if errors.Is(err, huh.ErrUserAborted) {
return "", errUtils.ErrUserAborted
}
return "", fmt.Errorf("%w: %w", errUtils.ErrUnsupportedInputType, err)
}
return selectedProvider, nil
}