goldwarden-vaultwarden-bitw.../cmd/config.go

324 lines
7.5 KiB
Go
Raw Permalink Normal View History

2023-07-17 03:23:26 +02:00
package cmd
import (
"encoding/json"
2023-12-30 18:53:01 +01:00
"fmt"
2024-01-04 23:40:07 +01:00
"strings"
2023-12-30 18:53:01 +01:00
2023-09-20 03:05:44 +02:00
"github.com/quexten/goldwarden/ipc/messages"
2023-07-17 03:23:26 +02:00
"github.com/spf13/cobra"
)
var setApiUrlCmd = &cobra.Command{
Use: "set-api-url",
Short: "Set the api url",
Long: `Set the api url.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
url := args[0]
2023-09-20 03:05:44 +02:00
request := messages.SetApiURLRequest{}
2023-07-17 03:23:26 +02:00
request.Value = url
2023-08-21 13:52:06 +02:00
result, err := commandClient.SendToAgent(request)
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.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
2023-07-17 03:23:26 +02:00
} else {
fmt.Println("Setting api url failed: " + result.(messages.ActionResponse).Message)
2023-07-17 03:23:26 +02:00
}
default:
fmt.Println("Wrong IPC response type")
2023-07-17 03:23:26 +02:00
}
},
}
var setIdentityURLCmd = &cobra.Command{
Use: "set-identity-url",
Short: "Set the identity url",
Long: `Set the identity url.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
url := args[0]
2023-09-20 03:05:44 +02:00
request := messages.SetIdentityURLRequest{}
2023-07-17 03:23:26 +02:00
request.Value = url
2023-08-21 13:52:06 +02:00
result, err := commandClient.SendToAgent(request)
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.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
2023-07-17 03:23:26 +02:00
} else {
fmt.Println("Setting identity url failed: " + result.(messages.ActionResponse).Message)
2023-07-17 03:23:26 +02:00
}
default:
fmt.Println("Wrong IPC response type")
2023-07-17 03:23:26 +02:00
}
},
}
var setNotificationsURLCmd = &cobra.Command{
Use: "set-notifications-url",
Short: "Set the notifications url",
Long: `Set the notifications url.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
url := args[0]
2023-09-20 03:05:44 +02:00
request := messages.SetNotificationsURLRequest{}
request.Value = url
result, err := commandClient.SendToAgent(request)
if err != nil {
2023-09-11 22:45:01 +02:00
handleSendToAgentError(err)
return
}
switch result.(type) {
2023-09-20 03:05:44 +02:00
case messages.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
} else {
fmt.Println("Setting notifications url failed: " + result.(messages.ActionResponse).Message)
}
default:
fmt.Println("Wrong IPC response type")
}
},
}
var setVaultURLCmd = &cobra.Command{
Use: "set-vault-url",
Short: "Set the vault url",
Long: `Set the vault url.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
url := args[0]
request := messages.SetVaultURLRequest{}
request.Value = url
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result.(type) {
case messages.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
} else {
fmt.Println("Setting vault url failed: " + result.(messages.ActionResponse).Message)
}
default:
fmt.Println("Wrong IPC response type")
}
},
}
var setURLsAutomaticallyCmd = &cobra.Command{
Use: "set-server",
Short: "Set the urls automatically",
Long: `Set the api/identity/vault/notification urls automaticall from a base url.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
value := args[0]
request := messages.SetURLsAutomaticallyRequest{}
request.Value = value
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result.(type) {
case messages.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
} else {
fmt.Println("Setting urls automatically failed: " + result.(messages.ActionResponse).Message)
}
default:
fmt.Println("Wrong IPC response type")
}
},
}
var getEnvironmentCmd = &cobra.Command{
Use: "get-environment",
Short: "Get the environment",
Long: `Get the environment.`,
Run: func(cmd *cobra.Command, args []string) {
request := messages.GetConfigEnvironmentRequest{}
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result := result.(type) {
case messages.GetConfigEnvironmentResponse:
response := map[string]string{}
response["api"] = result.ApiURL
response["identity"] = result.IdentityURL
response["notifications"] = result.NotificationsURL
response["vault"] = result.VaultURL
responseJSON, _ := json.Marshal(response)
fmt.Println(string(responseJSON))
default:
fmt.Println("Wrong IPC response type")
}
},
}
2024-01-04 21:53:38 +01:00
var setApiClientIDCmd = &cobra.Command{
2024-01-04 22:41:33 +01:00
Use: "set-client-id",
Short: "Set the client id",
Long: `Set the client id.`,
2024-01-04 21:53:38 +01:00
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
id := args[0]
2024-01-04 23:40:07 +01:00
if len(id) >= 2 && strings.HasPrefix(id, "\"") && strings.HasSuffix(id, "\"") {
id = id[1 : len(id)-1]
}
id = strings.TrimSpace(id)
2024-01-04 21:53:38 +01:00
request := messages.SetClientIDRequest{}
request.Value = id
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result.(type) {
case messages.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
2024-01-04 21:53:38 +01:00
} else {
fmt.Println("Setting api client id failed: " + result.(messages.ActionResponse).Message)
2024-01-04 21:53:38 +01:00
}
default:
fmt.Println("Wrong IPC response type")
2024-01-04 21:53:38 +01:00
}
},
}
var setApiSecretCmd = &cobra.Command{
2024-01-04 22:41:33 +01:00
Use: "set-client-secret",
2024-01-04 21:53:38 +01:00
Short: "Set the api secret",
Long: `Set the api secret.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
secret := args[0]
2024-01-04 23:40:07 +01:00
if len(secret) >= 2 && strings.HasPrefix(secret, "\"") && strings.HasSuffix(secret, "\"") {
secret = secret[1 : len(secret)-1]
}
secret = strings.TrimSpace(secret)
2024-01-04 21:53:38 +01:00
request := messages.SetClientSecretRequest{}
request.Value = secret
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result.(type) {
case messages.ActionResponse:
if result.(messages.ActionResponse).Success {
fmt.Println("Done")
2024-01-04 21:53:38 +01:00
} else {
fmt.Println("Setting api secret failed: " + result.(messages.ActionResponse).Message)
2024-01-04 21:53:38 +01:00
}
default:
fmt.Println("Wrong IPC response type")
2024-01-04 21:53:38 +01:00
}
},
}
2023-12-30 18:53:01 +01:00
var getRuntimeConfigCmd = &cobra.Command{
Use: "get-runtime-config",
Short: "Get the runtime config",
Long: `Get the runtime config.`,
Run: func(cmd *cobra.Command, args []string) {
request := messages.GetRuntimeConfigRequest{}
result, err := commandClient.SendToAgent(request)
if err != nil {
handleSendToAgentError(err)
return
}
switch result := result.(type) {
case messages.GetRuntimeConfigResponse:
response := map[string]interface{}{}
response["useMemguard"] = result.UseMemguard
response["SSHAgentSocketPath"] = result.SSHAgentSocketPath
response["goldwardenSocketPath"] = result.GoldwardenSocketPath
responseJSON, _ := json.Marshal(response)
fmt.Println(string(responseJSON))
2023-12-30 18:53:01 +01:00
default:
fmt.Println("Wrong IPC response type")
2023-12-30 18:53:01 +01:00
}
},
}
2023-07-17 03:23:26 +02:00
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the configuration",
Long: `Manage the configuration.`,
}
func init() {
rootCmd.AddCommand(configCmd)
configCmd.AddCommand(setApiUrlCmd)
configCmd.AddCommand(setIdentityURLCmd)
configCmd.AddCommand(setNotificationsURLCmd)
configCmd.AddCommand(setVaultURLCmd)
configCmd.AddCommand(setURLsAutomaticallyCmd)
configCmd.AddCommand(getEnvironmentCmd)
2023-12-30 18:53:01 +01:00
configCmd.AddCommand(getRuntimeConfigCmd)
2024-01-04 21:53:38 +01:00
configCmd.AddCommand(setApiClientIDCmd)
configCmd.AddCommand(setApiSecretCmd)
2023-07-17 03:23:26 +02:00
}