github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_configure_credentials.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cli
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/tiflow/pkg/cmd/factory"
    20  	"github.com/pingcap/tiflow/pkg/cmd/util"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  type configureCredentialsOptions struct{}
    25  
    26  func (o *configureCredentialsOptions) run(cmd *cobra.Command) error {
    27  	cmd.Println("1) TLS Client Certificate")
    28  	cmd.Println("2) TiDB User Credentials")
    29  	cmd.Print("Select Credential Type [default 1]:")
    30  
    31  	option, err := readInput()
    32  	if err != nil {
    33  		cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    34  		return fmt.Errorf("invalid input")
    35  	}
    36  	if option == "" {
    37  		option = "1"
    38  	}
    39  
    40  	res, err := factory.ReadFromDefaultPath()
    41  	if err != nil {
    42  		mag := "the default config file contains invalid data"
    43  		return fmt.Errorf("%s: %w", mag, err)
    44  	}
    45  
    46  	switch option {
    47  	case "1":
    48  		cmd.Printf("CA Certificate path [%s]:", res.CaPath)
    49  		caPath, err := readInput()
    50  		if err != nil {
    51  			cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    52  			return fmt.Errorf("invalid input")
    53  		}
    54  		if caPath != "" {
    55  			res.CaPath = caPath
    56  		}
    57  
    58  		cmd.Printf("Client Certificate path [%s]:", res.CertPath)
    59  		certPath, err := readInput()
    60  		if err != nil {
    61  			cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    62  			return fmt.Errorf("invalid input")
    63  		}
    64  		if certPath != "" {
    65  			res.CertPath = certPath
    66  		}
    67  
    68  		cmd.Printf("Client Private Key path [%s]:", res.KeyPath)
    69  		keyPath, err := readInput()
    70  		if err != nil {
    71  			cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    72  			return fmt.Errorf("invalid input")
    73  		}
    74  		if keyPath != "" {
    75  			res.KeyPath = keyPath
    76  		}
    77  	case "2":
    78  		cmd.Printf("TiCDC User name [%s]:", res.User)
    79  		user, err := readInput()
    80  		if err != nil {
    81  			cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    82  			return fmt.Errorf("invalid input")
    83  		}
    84  		if user != "" {
    85  			res.User = user
    86  		}
    87  
    88  		cmd.Printf("TiCDC Password  [%s]:", res.Password)
    89  		password, err := readInput()
    90  		if err != nil {
    91  			cmd.Printf("Received invalid input: %s, abort the command.\n", err.Error())
    92  			return fmt.Errorf("invalid input")
    93  		}
    94  		if password != "" {
    95  			res.Password = password
    96  		}
    97  	default:
    98  		cmd.Printf("Received invalid input: %s, abort the command.\n", option)
    99  		return fmt.Errorf("invalid input")
   100  	}
   101  
   102  	return res.StoreToDefaultPath()
   103  }
   104  
   105  // newConfigureCredentials creates the `cli configure-credentials` command
   106  func newConfigureCredentials() *cobra.Command {
   107  	o := &configureCredentialsOptions{}
   108  
   109  	command := &cobra.Command{
   110  		Use:   "configure-credentials",
   111  		Short: "Configure tls or authentication credentials",
   112  		Args:  cobra.NoArgs,
   113  		Run: func(cmd *cobra.Command, args []string) {
   114  			util.CheckErr(o.run(cmd))
   115  		},
   116  	}
   117  
   118  	return command
   119  }