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-12 01:22:48 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/systemauth/biometrics"
|
2023-07-17 03:23:26 +02:00
|
|
|
"github.com/quexten/goldwarden/agent/vault"
|
|
|
|
"github.com/quexten/goldwarden/ipc"
|
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-12 18:56:35 +02:00
|
|
|
func handleAddSSH(msg ipc.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response ipc.IPCMessage, err error) {
|
2023-07-17 03:23:26 +02:00
|
|
|
req := msg.ParsedPayload().(ipc.CreateSSHKeyRequest)
|
|
|
|
|
|
|
|
cipher, publicKey := ssh.NewSSHKeyCipher(req.Name, vault.Keyring)
|
|
|
|
response, err = ipc.IPCMessageFromPayload(ipc.ActionResponse{
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err = ipc.IPCMessageFromPayload(ipc.CreateSSHKeyResponse{
|
|
|
|
Digest: strings.ReplaceAll(publicKey, "\n", "") + " " + req.Name,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:56:35 +02:00
|
|
|
func handleListSSH(msg ipc.IPCMessage, cfg *config.Config, vault *vault.Vault, callingContext *sockets.CallingContext) (response ipc.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", ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err = ipc.IPCMessageFromPayload(ipc.GetSSHKeysResponse{
|
|
|
|
Keys: keyStrings,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-12 01:22:48 +02:00
|
|
|
AgentActionsRegistry.Register(ipc.IPCMessageTypeCreateSSHKeyRequest, ensureEverything(biometrics.SSHKey, handleAddSSH))
|
2023-07-17 03:23:26 +02:00
|
|
|
AgentActionsRegistry.Register(ipc.IPCMessageTypeGetSSHKeysRequest, ensureIsNotLocked(ensureIsLoggedIn(handleListSSH)))
|
|
|
|
}
|