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

63 lines
2.1 KiB
Go
Raw Normal View History

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-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)
_, err = messages.IPCMessageFromPayload(messages.ActionResponse{
2023-07-17 03:23:26 +02:00
Success: true,
})
if err != nil {
panic(err)
}
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
}
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
}