goldwarden-vaultwarden-bitw.../agent/actions/config.go

196 lines
7.0 KiB
Go
Raw Normal View History

2023-07-17 03:23:26 +02:00
package actions
import (
"encoding/json"
"io"
"net/http"
2023-07-17 03:23:26 +02:00
"github.com/quexten/goldwarden/agent/config"
"github.com/quexten/goldwarden/agent/sockets"
"github.com/quexten/goldwarden/agent/vault"
2023-09-20 03:05:44 +02:00
"github.com/quexten/goldwarden/ipc/messages"
2023-07-17 03:23:26 +02:00
)
2023-09-20 03:05:44 +02:00
func handleSetApiURL(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
apiURL := messages.ParsePayload(request).(messages.SetApiURLRequest).Value
2023-07-17 03:23:26 +02:00
cfg.ConfigFile.ApiUrl = apiURL
err = cfg.WriteConfig()
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
2023-07-17 03:23:26 +02:00
Success: false,
Message: err.Error(),
})
}
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
2023-07-17 03:23:26 +02:00
Success: true,
})
}
2023-09-20 03:05:44 +02:00
func handleSetIdentity(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
identity := messages.ParsePayload(request).(messages.SetIdentityURLRequest).Value
2023-07-17 03:23:26 +02:00
cfg.ConfigFile.IdentityUrl = identity
err = cfg.WriteConfig()
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
2023-07-17 03:23:26 +02:00
Success: false,
Message: err.Error(),
})
}
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
2023-07-17 03:23:26 +02:00
Success: true,
})
}
2023-09-20 03:05:44 +02:00
func handleSetNotifications(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
notifications := messages.ParsePayload(request).(messages.SetNotificationsURLRequest).Value
cfg.ConfigFile.NotificationsUrl = notifications
err = cfg.WriteConfig()
if err != nil {
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
2023-09-20 03:05:44 +02:00
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: true,
})
}
func handleSetVaultURL(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
vaultURL := messages.ParsePayload(request).(messages.SetVaultURLRequest).Value
cfg.ConfigFile.VaultUrl = vaultURL
err = cfg.WriteConfig()
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: true,
})
}
type ConfigResponse struct {
Version string `json:"version"`
GitHash string `json:"gitHash"`
Environment struct {
Vault string `json:"vault"`
Api string `json:"api"`
Identity string `json:"identity"`
Notifications string `json:"notifications"`
}
}
func handleSetURLsAutomatically(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
autoconfigBaseURL := messages.ParsePayload(request).(messages.SetURLsAutomaticallyRequest).Value
// make http request
resp, err := http.Get(autoconfigBaseURL + "/api/config")
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
// parse response
var configResponse ConfigResponse
body, err := io.ReadAll(resp.Body)
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
err = json.Unmarshal(body, &configResponse)
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
cfg.ConfigFile.ApiUrl = configResponse.Environment.Api
cfg.ConfigFile.IdentityUrl = configResponse.Environment.Identity
cfg.ConfigFile.NotificationsUrl = configResponse.Environment.Notifications
cfg.ConfigFile.VaultUrl = configResponse.Environment.Vault
err = cfg.WriteConfig()
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: true,
})
}
func handleGetConfigEnvironment(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
return messages.IPCMessageFromPayload(messages.GetConfigEnvironmentResponse{
ApiURL: cfg.ConfigFile.ApiUrl,
IdentityURL: cfg.ConfigFile.IdentityUrl,
NotificationsURL: cfg.ConfigFile.NotificationsUrl,
VaultURL: cfg.ConfigFile.VaultUrl,
})
}
2024-01-04 21:53:38 +01:00
func handleSetClientID(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
clientID := messages.ParsePayload(request).(messages.SetClientIDRequest).Value
cfg.SetClientID(clientID)
err = cfg.WriteConfig()
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: true,
})
}
func handleSetClientSecret(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
clientSecret := messages.ParsePayload(request).(messages.SetClientSecretRequest).Value
cfg.SetClientSecret(clientSecret)
err = cfg.WriteConfig()
if err != nil {
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: false,
Message: err.Error(),
})
}
return messages.IPCMessageFromPayload(messages.ActionResponse{
Success: true,
})
}
2023-12-30 18:53:01 +01:00
func handleGetRuntimeConfig(request messages.IPCMessage, cfg *config.Config, vault *vault.Vault, ctx *sockets.CallingContext) (response messages.IPCMessage, err error) {
return messages.IPCMessageFromPayload(messages.GetRuntimeConfigResponse{
UseMemguard: cfg.ConfigFile.RuntimeConfig.UseMemguard,
SSHAgentSocketPath: cfg.ConfigFile.RuntimeConfig.SSHAgentSocketPath,
GoldwardenSocketPath: cfg.ConfigFile.RuntimeConfig.GoldwardenSocketPath,
})
}
2023-07-17 03:23:26 +02:00
func init() {
2023-09-20 03:05:44 +02:00
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetIdentityURLRequest{}), handleSetIdentity)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetApiURLRequest{}), handleSetApiURL)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetNotificationsURLRequest{}), handleSetNotifications)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetVaultURLRequest{}), handleSetVaultURL)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetURLsAutomaticallyRequest{}), handleSetURLsAutomatically)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.GetConfigEnvironmentRequest{}), handleGetConfigEnvironment)
2023-12-30 18:53:01 +01:00
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.GetRuntimeConfigRequest{}), handleGetRuntimeConfig)
2024-01-04 21:53:38 +01:00
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetClientIDRequest{}), handleSetClientID)
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.SetClientSecretRequest{}), handleSetClientSecret)
2023-07-17 03:23:26 +02:00
}