2023-07-17 03:23:26 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
2024-05-04 01:06:24 +02:00
|
|
|
"github.com/quexten/goldwarden/cli/agent/bitwarden"
|
|
|
|
"github.com/quexten/goldwarden/cli/agent/config"
|
|
|
|
"github.com/quexten/goldwarden/cli/agent/sockets"
|
|
|
|
"github.com/quexten/goldwarden/cli/agent/ssh"
|
|
|
|
"github.com/quexten/goldwarden/cli/agent/systemauth"
|
|
|
|
"github.com/quexten/goldwarden/cli/agent/vault"
|
|
|
|
"github.com/quexten/goldwarden/cli/ipc/messages"
|
2023-07-17 03:23:26 +02:00
|
|
|
)
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleAddSSH(msg messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
|
|
|
req := messages.ParsePayload(msg).(messages.CreateSSHKeyRequest)
|
2023-07-17 03:23:26 +02:00
|
|
|
|
2024-06-02 23:18:37 +02:00
|
|
|
cipher, publicKey, err := ssh.NewSSHKeyCipher(req.Name, vault.Keyring)
|
|
|
|
if err != nil {
|
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:23:11 +01:00
|
|
|
_, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-06-02 23:18:37 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
token, err := cfg.GetToken()
|
2024-03-15 16:22:45 +01:00
|
|
|
if err != nil {
|
|
|
|
actionsLog.Warn(err.Error())
|
|
|
|
}
|
2023-07-17 03:23:26 +02:00
|
|
|
ctx := context.WithValue(context.TODO(), bitwarden.AuthToken{}, token.AccessToken)
|
2024-04-28 17:27:55 +02:00
|
|
|
postedCipher, err := bitwarden.PostCipher(ctx, cipher, cfg)
|
2023-07-17 03:23:26 +02:00
|
|
|
if err == nil {
|
2024-04-28 17:27:55 +02:00
|
|
|
vault.AddOrUpdateSecureNote(postedCipher)
|
2023-07-17 03:23:26 +02:00
|
|
|
} else {
|
|
|
|
actionsLog.Warn("Error posting ssh key cipher: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.CreateSSHKeyResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Digest: strings.ReplaceAll(publicKey, "\n", "") + " " + req.Name,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
func handleListSSH(msg messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
keys := vault.GetSSHKeys()
|
|
|
|
keyStrings := make([]string, 0)
|
|
|
|
for _, key := range keys {
|
|
|
|
keyStrings = append(keyStrings, strings.ReplaceAll(key.PublicKey+" "+key.Name, "\n", ""))
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.GetSSHKeysResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Keys: keyStrings,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:18:37 +02:00
|
|
|
func handleImportSSH(msg messages.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response messages.IPCMessage, err error) {
|
|
|
|
req := messages.ParsePayload(msg).(messages.ImportSSHKeyRequest)
|
|
|
|
|
|
|
|
cipher, _, err := ssh.SSHKeyCipherFromKey(req.Name, req.Key, vault.Keyring)
|
|
|
|
if err != nil {
|
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := cfg.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
actionsLog.Warn(err.Error())
|
|
|
|
}
|
|
|
|
ctx := context.WithValue(context.TODO(), bitwarden.AuthToken{}, token.AccessToken)
|
|
|
|
postedCipher, err := bitwarden.PostCipher(ctx, cipher, cfg)
|
|
|
|
if err == nil {
|
|
|
|
vault.AddOrUpdateSecureNote(postedCipher)
|
|
|
|
} else {
|
|
|
|
actionsLog.Warn("Error posting ssh key cipher: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ImportSSHKeyResponse{
|
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:23:26 +02:00
|
|
|
func init() {
|
2023-09-20 03:05:44 +02:00
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.CreateSSHKeyRequest{}), ensureEverything(systemauth.SSHKey, handleAddSSH))
|
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.GetSSHKeysRequest{}), ensureIsNotLocked(ensureIsLoggedIn(handleListSSH)))
|
2024-06-02 23:18:37 +02:00
|
|
|
AgentActionsRegistry.Register(messages.MessageTypeForEmptyPayload(messages.ImportSSHKeyRequest{}), ensureEverything(systemauth.SSHKey, handleImportSSH))
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|