2023-07-17 03:23:26 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/quexten/goldwarden/agent/bitwarden"
|
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
|
|
|
"github.com/quexten/goldwarden/agent/sockets"
|
|
|
|
"github.com/quexten/goldwarden/agent/ssh"
|
2023-09-19 21:49:56 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/vault"
|
2023-09-20 03:05:44 +02:00
|
|
|
"github.com/quexten/goldwarden/ipc/messages"
|
2023-08-21 18:37:34 +02:00
|
|
|
"github.com/quexten/goldwarden/logging"
|
2023-07-17 03:23:26 +02:00
|
|
|
)
|
|
|
|
|
2023-08-21 18:37:34 +02:00
|
|
|
var actionsLog = logging.GetLogger("Goldwarden", "Actions")
|
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
|
|
|
|
|
|
|
cipher, publicKey := ssh.NewSSHKeyCipher(req.Name, vault.Keyring)
|
2023-09-20 03:05:44 +02:00
|
|
|
response, err = messages.IPCMessageFromPayload(messages.ActionResponse{
|
2023-07-17 03:23:26 +02:00
|
|
|
Success: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := cfg.GetToken()
|
|
|
|
ctx := context.WithValue(context.TODO(), bitwarden.AuthToken{}, token.AccessToken)
|
|
|
|
ciph, err := bitwarden.PostCipher(ctx, cipher, cfg)
|
|
|
|
if err == nil {
|
|
|
|
vault.AddOrUpdateSecureNote(ciph)
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
2023-07-17 03:23:26 +02:00
|
|
|
}
|