From 298076a9b3c0fb703b9e0f5c37d661fdb759dfa5 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Thu, 16 Jan 2025 13:23:39 +0900 Subject: [PATCH] feat: add internal grpc connection Signed-off-by: Youngjin Jo --- cmd/other/setting.go | 16 +++++++-- pkg/transport/service.go | 77 +++++++++++++++++++++++++++++----------- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/cmd/other/setting.go b/cmd/other/setting.go index b537174..dd6e374 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -167,6 +167,7 @@ var settingInitProxyCmd = &cobra.Command{ endpointStr := args[0] appFlag, _ := cmd.Flags().GetBool("app") userFlag, _ := cmd.Flags().GetBool("user") + internalFlag, _ := cmd.Flags().GetBool("internal") if !appFlag && !userFlag { pterm.Error.Println("You must specify either --app or --user flag.") @@ -174,6 +175,19 @@ var settingInitProxyCmd = &cobra.Command{ return } + // Internal flag can only be used with --app flag + if internalFlag && userFlag { + pterm.DefaultBox.WithTitle("Internal Flag Not Allowed"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4). + WithBoxStyle(pterm.NewStyle(pterm.FgRed)). + Println("The --internal flag can only be used with the --app flag.\n" + + "Example usage:\n" + + " $ cfctl setting init proxy --app --internal") + return + } + // Get environment name from user input result, err := pterm.DefaultInteractiveTextInput. WithDefaultText("default"). @@ -254,8 +268,6 @@ var settingInitProxyCmd = &cobra.Command{ } } - internalFlag, _ := cmd.Flags().GetBool("internal") - // Update configuration updateSetting(envName, endpointStr, envSuffix, internalFlag) }, diff --git a/pkg/transport/service.go b/pkg/transport/service.go index 02b58b0..057c3a8 100644 --- a/pkg/transport/service.go +++ b/pkg/transport/service.go @@ -674,33 +674,68 @@ func fetchJSONResponse(config *Config, serviceName string, verb string, resource if err != nil { if strings.Contains(err.Error(), "ERROR_AUTHENTICATE_FAILURE") || strings.Contains(err.Error(), "Token is invalid or expired") { - // Create a styled error message box - headerBox := pterm.DefaultBox.WithTitle("Authentication Error"). - WithTitleTopCenter(). - WithRightPadding(4). - WithLeftPadding(4). - WithBoxStyle(pterm.NewStyle(pterm.FgLightRed)) - errorExplain := "Your authentication token has expired or is invalid.\n" + - "Please login again to refresh your credentials." + // Check if current environment is app type + if strings.HasSuffix(config.Environment, "-app") { + headerBox := pterm.DefaultBox.WithTitle("App Token Required"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4). + WithBoxStyle(pterm.NewStyle(pterm.FgLightRed)) - headerBox.Println(errorExplain) - fmt.Println() + appTokenExplain := "Please create a Domain Admin App in SpaceONE Console.\n" + + "This requires Domain Admin privilege.\n\n" + + "Or Please create a Workspace App in SpaceONE Console.\n" + + "This requires Workspace Owner privilege." - steps := []string{ - "1. Run 'cfctl login'", - "2. Enter your credentials when prompted", - "3. Try your command again", - } + headerBox.Println(appTokenExplain) + fmt.Println() - instructionBox := pterm.DefaultBox.WithTitle("Required Steps"). - WithTitleTopCenter(). - WithRightPadding(4). - WithLeftPadding(4) + steps := []string{ + "1. Go to SpaceONE Console", + "2. Navigate to either 'Admin > App Page' or specific 'Workspace > App page'", + "3. Click 'Create' to create your App", + "4. Copy the generated App Token", + fmt.Sprintf("5. Update token in your config file:\n Path: ~/.cfctl/setting.yaml\n Environment: %s", config.Environment), + } - instructionBox.Println(strings.Join(steps, "\n\n")) + instructionBox := pterm.DefaultBox.WithTitle("Required Steps"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4) - return nil, fmt.Errorf("authentication required") + instructionBox.Println(strings.Join(steps, "\n\n")) + + return nil, fmt.Errorf("app token required") + } else { + // Original user authentication error message + headerBox := pterm.DefaultBox.WithTitle("Authentication Error"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4). + WithBoxStyle(pterm.NewStyle(pterm.FgLightRed)) + + errorExplain := "Your authentication token has expired or is invalid.\n" + + "Please login again to refresh your credentials." + + headerBox.Println(errorExplain) + fmt.Println() + + steps := []string{ + "1. Run 'cfctl login'", + "2. Enter your credentials when prompted", + "3. Try your command again", + } + + instructionBox := pterm.DefaultBox.WithTitle("Required Steps"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4) + + instructionBox.Println(strings.Join(steps, "\n\n")) + + return nil, fmt.Errorf("authentication required") + } } return nil, fmt.Errorf("failed to invoke method %s: %v", fullMethod, err) }