goldwarden-vaultwarden-bitw.../cli/cmd/ssh.go

164 lines
3.7 KiB
Go
Raw Normal View History

2023-07-17 03:23:26 +02:00
package cmd
import (
2024-06-02 23:18:37 +02:00
"bytes"
2023-07-17 03:23:26 +02:00
"fmt"
"os"
2023-07-17 03:23:26 +02:00
"github.com/atotto/clipboard"
2024-05-04 01:06:24 +02:00
"github.com/quexten/goldwarden/cli/ipc/messages"
2023-07-17 03:23:26 +02:00
"github.com/spf13/cobra"
)
var sshCmd = &cobra.Command{
Use: "ssh",
Short: "Commands for managing SSH keys",
Long: `Commands for managing SSH keys.`,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
2023-07-17 03:23:26 +02:00
},
}
2024-05-27 06:55:08 +02:00
// sshAddCmd represents the ssh add command
2023-07-17 03:23:26 +02:00
var sshAddCmd = &cobra.Command{
Use: "add",
2024-05-27 06:55:08 +02:00
Short: "Creates a new SSH key and adds it to the SSH Agent.",
Long: `Creates a new SSH key and adds it to the SSH Agent.
The key is stored as a secure note. Consult the documentation for more information.`,
2023-07-17 03:23:26 +02:00
Run: func(cmd *cobra.Command, args []string) {
2024-03-15 16:22:45 +01:00
err := loginIfRequired()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2023-08-21 18:37:34 +02:00
2023-07-17 03:23:26 +02:00
name, _ := cmd.Flags().GetString("name")
2023-07-18 22:07:40 +02:00
copyToClipboard, _ := cmd.Flags().GetBool("clipboard")
2023-07-17 03:23:26 +02:00
2023-09-20 03:05:44 +02:00
result, err := commandClient.SendToAgent(messages.CreateSSHKeyRequest{
2023-07-17 03:23:26 +02:00
Name: name,
})
if err != nil {
2023-09-11 22:45:01 +02:00
handleSendToAgentError(err)
2023-07-17 03:23:26 +02:00
return
}
switch result.(type) {
2023-09-20 03:05:44 +02:00
case messages.CreateSSHKeyResponse:
response := result.(messages.CreateSSHKeyResponse)
2023-07-17 03:23:26 +02:00
fmt.Println(response.Digest)
2023-07-18 22:07:40 +02:00
if copyToClipboard {
2024-03-15 16:22:45 +01:00
err := clipboard.WriteAll(string(response.Digest))
if err != nil {
panic(err)
}
2023-07-18 22:07:40 +02:00
}
return
2023-09-20 03:05:44 +02:00
case messages.ActionResponse:
fmt.Println("Error: " + result.(messages.ActionResponse).Message)
2023-07-17 03:23:26 +02:00
return
}
},
}
var listSSHCmd = &cobra.Command{
Use: "list",
Short: "Lists all SSH keys in your vault",
Long: `Lists all SSH keys in your vault.`,
Run: func(cmd *cobra.Command, args []string) {
2024-03-15 16:22:45 +01:00
err := loginIfRequired()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2023-08-21 18:37:34 +02:00
2023-09-20 03:05:44 +02:00
result, err := commandClient.SendToAgent(messages.GetSSHKeysRequest{})
2023-07-17 03:23:26 +02:00
if err != nil {
2023-09-11 22:45:01 +02:00
handleSendToAgentError(err)
2023-07-17 03:23:26 +02:00
return
}
switch result.(type) {
2023-09-20 03:05:44 +02:00
case messages.GetSSHKeysResponse:
response := result.(messages.GetSSHKeysResponse)
2023-07-17 03:23:26 +02:00
for _, key := range response.Keys {
fmt.Println(key)
}
return
2023-09-20 03:05:44 +02:00
case messages.ActionResponse:
fmt.Println("Error: " + result.(messages.ActionResponse).Message)
2023-07-17 03:23:26 +02:00
return
}
},
}
2024-06-02 23:18:37 +02:00
var importSSHCmd = &cobra.Command{
Use: "import",
Short: "Imports an SSH key into your vault",
Long: `Imports an SSH key into your vault.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("Error: No filename for SSH key specified")
return
}
2024-06-02 23:18:37 +02:00
filename := args[0]
fmt.Println("Importing SSH key from " + filename)
name, _ := cmd.Flags().GetString("name")
if name == "" {
name = "Imported SSH Key"
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Println("Error: File does not exist")
return
}
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error: " + err.Error())
return
}
buf := new(bytes.Buffer)
buf.ReadFrom(file)
key := buf.String()
result, err := commandClient.SendToAgent(messages.ImportSSHKeyRequest{
Key: key,
Name: name,
})
if err != nil {
handleSendToAgentError(err)
return
}
switch result.(type) {
case messages.ImportSSHKeyResponse:
response := result.(messages.ImportSSHKeyResponse)
if response.Success {
fmt.Println("Success")
} else {
fmt.Println("Error: " + response.ErrorMsg)
}
return
case messages.ActionResponse:
fmt.Println("Error: " + result.(messages.ActionResponse).Message)
return
}
},
}
2023-07-17 03:23:26 +02:00
func init() {
rootCmd.AddCommand(sshCmd)
sshCmd.AddCommand(sshAddCmd)
sshAddCmd.PersistentFlags().String("name", "", "")
_ = sshAddCmd.MarkFlagRequired("name")
2023-07-18 22:07:40 +02:00
sshAddCmd.PersistentFlags().Bool("clipboard", false, "Copy the public key to the clipboard")
2023-07-17 03:23:26 +02:00
sshCmd.AddCommand(listSSHCmd)
2024-06-02 23:18:37 +02:00
importSSHCmd.PersistentFlags().String("name", "", "")
sshCmd.AddCommand(importSSHCmd)
2023-07-17 03:23:26 +02:00
}