From eaa0e364932807a2110f3963c1a12ae1ac717b26 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 11 Dec 2024 14:43:12 +0900 Subject: [PATCH 1/5] chore: remove error when change environment Signed-off-by: Youngjin Jo --- cmd/root.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6ac3f90..be74c50 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -94,11 +94,18 @@ func init() { pterm.DisableColor() } - // Skip configuration check for settings init commands - if len(os.Args) >= 3 && os.Args[1] == "settings" && os.Args[2] == "init" { - // Skip configuration check for initialization - } else { - // Try to add dynamic service commands + // Determine if the current command is 'setting environment -l' + skipDynamicCommands := false + if len(os.Args) >= 3 && os.Args[1] == "setting" && os.Args[2] == "environment" { + for _, arg := range os.Args[3:] { + if arg == "-l" || arg == "--list" { + skipDynamicCommands = true + break + } + } + } + + if !skipDynamicCommands { if err := addDynamicServiceCommands(); err != nil { showInitializationGuide(err) } From c0e0b0205e1989690842de6204d2d452e0ead90c Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 11 Dec 2024 14:46:07 +0900 Subject: [PATCH 2/5] chore: remove url Signed-off-by: Youngjin Jo --- cmd/other/setting.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/other/setting.go b/cmd/other/setting.go index c381dd7..d57342b 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -46,9 +46,9 @@ var settingInitURLCmd = &cobra.Command{ Short: "Initialize configuration with a URL", Long: `Specify a URL to initialize the environment configuration.`, Args: cobra.NoArgs, - Example: ` cfctl setting init url -u https://spaceone.spaceone.megazone.io --app + Example: ` cfctl setting init url -u https://example.com --app or - cfctl setting init url -u https://spaceone.spaceone.megazone.io --user`, + cfctl setting init url -u https://example.com --user`, Run: func(cmd *cobra.Command, args []string) { urlStr, _ := cmd.Flags().GetString("url") appFlag, _ := cmd.Flags().GetBool("app") From de67abee421eb19e9f0f47cb85e883b4cb5d6a3f Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 11 Dec 2024 15:15:28 +0900 Subject: [PATCH 3/5] chore: remove setting.toml of cache directory Signed-off-by: Youngjin Jo --- cmd/other/setting.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/other/setting.go b/cmd/other/setting.go index d57342b..d17aeee 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -182,7 +182,7 @@ var settingInitLocalCmd = &cobra.Command{ if appFlag { updateLocalSetting(envName, "app", mainSettingPath) } else { - updateLocalSetting(envName, "user", filepath.Join(settingDir, "cache", "setting.toml")) + updateLocalSetting(envName, "user", mainSettingPath) } // Update the current environment in the main setting @@ -226,7 +226,12 @@ func updateLocalSetting(envName, settingType, settingPath string) { // Set environment configuration v.Set(fmt.Sprintf("environments.%s.endpoint", envName), "grpc://localhost:50051") - v.Set(fmt.Sprintf("environments.%s.token", envName), "") + if settingType == "app" { + v.Set(fmt.Sprintf("environments.%s.token", envName), "") + v.Set(fmt.Sprintf("environments.%s.proxy", envName), true) + } else if settingType == "user" { + v.Set(fmt.Sprintf("environments.%s.proxy", envName), false) + } // Write configuration if err := v.WriteConfig(); err != nil { @@ -404,12 +409,6 @@ var showCmd = &cobra.Command{ return } - // Load user configuration - if err := loadSetting(userV, userSettingPath); err != nil { - pterm.Error.Println(err) - return - } - currentEnv := getCurrentEnvironment(appV) if currentEnv == "" { pterm.Sprintf("No environment set in %s\n", appSettingPath) From 7b54ad5ea726ea701acbaa0313afc62f1541dc52 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 11 Dec 2024 16:51:13 +0900 Subject: [PATCH 4/5] fix: do not issue token when refresh_token is valid Signed-off-by: Youngjin Jo --- cmd/other/login.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/cmd/other/login.go b/cmd/other/login.go index 2b8132d..970680b 100644 --- a/cmd/other/login.go +++ b/cmd/other/login.go @@ -1598,7 +1598,7 @@ func validateAndDecodeToken(token string) (map[string]interface{}, error) { } // Check required fields - requiredFields := []string{"exp", "rol", "did"} + requiredFields := []string{"exp", "did"} for _, field := range requiredFields { if _, ok := claims[field]; !ok { return nil, fmt.Errorf("invalid token format: missing required field '%s'", field) @@ -1668,34 +1668,18 @@ func getValidTokens(currentEnv string) (accessToken, refreshToken, newAccessToke envCacheDir := filepath.Join(homeDir, ".cfctl", "cache", currentEnv) - // Try to read and validate grant token first - if newAccessToken, err = readTokenFromFile(envCacheDir, "grant_token"); err == nil { - claims, err := validateAndDecodeToken(newAccessToken) + if refreshToken, err = readTokenFromFile(envCacheDir, "refresh_token"); err == nil { + claims, err := validateAndDecodeToken(refreshToken) if err == nil { - // Check if token has expired if exp, ok := claims["exp"].(float64); ok { if time.Now().Unix() < int64(exp) { accessToken, _ = readTokenFromFile(envCacheDir, "access_token") - refreshToken, _ = readTokenFromFile(envCacheDir, "refresh_token") + newAccessToken, _ = readTokenFromFile(envCacheDir, "grant_token") return accessToken, refreshToken, newAccessToken, nil } } } } - // If grant token is invalid or expired, check refresh token - if accessToken, err = readTokenFromFile(envCacheDir, "access_token"); err == nil { - if refreshToken, err = readTokenFromFile(envCacheDir, "refresh_token"); err == nil { - claims, err := validateAndDecodeToken(refreshToken) - if err == nil { - if exp, ok := claims["exp"].(float64); ok { - if time.Now().Unix() < int64(exp) { - return accessToken, refreshToken, "", nil - } - } - } - } - } - return "", "", "", fmt.Errorf("no valid tokens found") } From 655a80a410cd7b05c4dd9b790331cf03e8df627f Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 11 Dec 2024 17:00:12 +0900 Subject: [PATCH 5/5] refactor: do not overwrite issue_token and refresh_token when refresh_token is valid Signed-off-by: Youngjin Jo --- cmd/other/login.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cmd/other/login.go b/cmd/other/login.go index 970680b..0d9e2ac 100644 --- a/cmd/other/login.go +++ b/cmd/other/login.go @@ -451,10 +451,7 @@ func executeUserLogin(currentEnv string) { } accessToken, refreshToken, newAccessToken, err := getValidTokens(currentEnv) - if err == nil && refreshToken != "" && !isTokenExpired(refreshToken) { - // Use existing valid refresh token - accessToken = newAccessToken - } else { + if err != nil || refreshToken == "" || isTokenExpired(refreshToken) { // Get new tokens with password password := promptPassword() accessToken, refreshToken, err = issueToken(baseUrl, tempUserID, password, domainID)