Skip to content

Commit

Permalink
Implement force option to protect overwriting file (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof authored Apr 13, 2022
1 parent be3dfb8 commit f6cc001
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ cluster: karbon_cluster_name
user: admin
insecure: true
verbose: false
force: false
kubie: false
#ssh-agent: false
#ssh-file: false
Expand All @@ -104,6 +105,7 @@ you can also use the following environement variable
`KARBON_USER`
`KARBON_INSECURE`
`KARBON_VERBOSE`
`KARBON_FORCE`
`KARBON_PASSWORD`
`KARBON_KUBIE`
`KARBON_KUBIE_PATH`
Expand All @@ -115,6 +117,10 @@ precedence is

`FLAGS` => `ENV` => `CONFIG FILE` => `DEFAULT`

## File overwrite

You can use the `--force` option to overwrite any existing file(s) like kubeconfig or ssh key/cert.

## Password

This tools never stored the password. You can use the `KARBON_PASSWORD` env variable otherwise it should be provided in an interactive way.
Expand Down
14 changes: 13 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (nutanix *nutanixCluster) listKarbonClusters() ([]map[string]interface{}, e
return clusters, nil
}

func saveKeyFile(cluster string, sshResponseJSON map[string]interface{}) error {
func saveKeyFile(cluster string, sshResponseJSON map[string]interface{}, force bool) error {

privateKey := []byte(sshResponseJSON["private_key"].(string))
certificate := []byte(sshResponseJSON["certificate"].(string))
Expand All @@ -111,11 +111,23 @@ func saveKeyFile(cluster string, sshResponseJSON map[string]interface{}) error {

// Write the private key
privateKeyFile := filepath.Join(sshDir, cluster)

_, err = os.Stat(privateKeyFile)
if err == nil && !force {
return fmt.Errorf("file %s already exist, use force option to overwrite it", privateKeyFile)
}

err = ioutil.WriteFile(privateKeyFile, privateKey, 0600)
cobra.CheckErr(err)

// Write the certificate
certificateFile := filepath.Join(sshDir, fmt.Sprintf("%s-cert.pub", cluster))

_, err = os.Stat(certificateFile)
if err == nil && !force {
return fmt.Errorf("file %s already exist, use force option to overwrite it", certificateFile)
}

err = ioutil.WriteFile(certificateFile, certificate, 0600)
cobra.CheckErr(err)

Expand Down
10 changes: 9 additions & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in
viper.BindPFlag("kubie-path", cmd.Flags().Lookup("kubie-path"))
viper.BindPFlag("ssh-agent", cmd.Flags().Lookup("ssh-agent"))
viper.BindPFlag("ssh-file", cmd.Flags().Lookup("ssh-file"))
viper.BindPFlag("force", cmd.Flags().Lookup("force"))
},
Run: func(cmd *cobra.Command, args []string) {

Expand Down Expand Up @@ -107,6 +108,11 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in
cobra.CheckErr(err)
}

_, err = os.Stat(kubeconfig)
if err == nil && !viper.GetBool("force") {
cobra.CheckErr(fmt.Errorf("file %s already exist, use force option to overwrite it", kubeconfig))
}

err = ioutil.WriteFile(kubeconfig, data, 0600)
cobra.CheckErr(err)

Expand All @@ -132,7 +138,7 @@ If option enabled retrieve SSH key/cert and add them to ssh-agent or in file in
cobra.CheckErr(err)

if viper.GetBool("ssh-file") {
err = saveKeyFile(karbonCluster, karbonSSH)
err = saveKeyFile(karbonCluster, karbonSSH, viper.GetBool("force"))
cobra.CheckErr(err)
}

Expand Down Expand Up @@ -164,6 +170,8 @@ func init() {

loginCmd.Flags().BoolP("insecure", "k", false, "Skip certificate verification (this is insecure)")

loginCmd.Flags().Bool("force", false, "Overwrite file(s) if already exist")

loginCmd.Flags().Bool("kubie", false, "Store kubeconfig in independent file in kubie-path directory")

userHomeDir, err := os.UserHomeDir()
Expand Down

0 comments on commit f6cc001

Please sign in to comment.