Add lastsynced and websocket status to vault status

This commit is contained in:
Bernd Schoolmann 2023-12-26 21:52:49 +01:00
parent c862a1a5f8
commit e687ceb3fd
No known key found for this signature in database
5 changed files with 58 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/quexten/goldwarden/agent/bitwarden/crypto"
"github.com/quexten/goldwarden/agent/bitwarden/models"
@ -43,6 +44,7 @@ func DoFullSync(ctx context.Context, vault *vault.Vault, config *config.Config,
return err
}
} else {
vault.SetLastSynced(time.Now().Unix())
log.Info("Sync successful, initializing keyring and vault...")
}

View File

@ -88,6 +88,7 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
defer c.Close()
websocketLog.Info("Connected to websocket server...")
vault.SetWebsocketConnected(true)
done := make(chan struct{})
//handshake required for official bitwarden implementation
@ -101,7 +102,7 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
websocketLog.Error("Error reading websocket message %s", err)
return
}
if len(message) < 3 {
if len(message) < 5 {
//ignore empty messages
continue
}
@ -146,6 +147,8 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
} else {
vault.AddOrUpdateLogin(cipher)
}
vault.SetLastSynced(time.Now().Unix())
break
case SyncCipherCreate:
websocketLog.Warn("Create requested for cipher " + cipherid)
@ -166,6 +169,7 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
} else {
vault.AddOrUpdateLogin(cipher)
}
vault.SetLastSynced(time.Now().Unix())
break
case SyncSendCreate, SyncSendUpdate, SyncSendDelete:
@ -219,6 +223,7 @@ func connectToWebsocket(ctx context.Context, vault *vault.Vault, cfg *config.Con
}()
<-done
vault.SetWebsocketConnected(false)
return nil
}

View File

@ -14,21 +14,25 @@ import (
var vaultLog = logging.GetLogger("Goldwarden", "Vault")
type Vault struct {
Keyring *crypto.Keyring
logins map[string]models.Cipher
secureNotes map[string]models.Cipher
sshKeyNoteIDs []string
envCredentials map[string]string
mu sync.Mutex
Keyring *crypto.Keyring
logins map[string]models.Cipher
secureNotes map[string]models.Cipher
sshKeyNoteIDs []string
envCredentials map[string]string
lastSynced int64
websockedConnected bool
mu sync.Mutex
}
func NewVault(keyring *crypto.Keyring) *Vault {
return &Vault{
Keyring: keyring,
logins: make(map[string]models.Cipher),
secureNotes: make(map[string]models.Cipher),
sshKeyNoteIDs: make([]string, 0),
envCredentials: make(map[string]string),
Keyring: keyring,
logins: make(map[string]models.Cipher),
secureNotes: make(map[string]models.Cipher),
sshKeyNoteIDs: make([]string, 0),
envCredentials: make(map[string]string),
lastSynced: 0,
websockedConnected: false,
}
}
@ -403,3 +407,29 @@ func (vault *Vault) GetSecureNote(uuid string) (models.Cipher, error) {
return models.Cipher{}, errors.New("cipher not found")
}
func (vault *Vault) SetLastSynced(lastSynced int64) {
vault.lockMutex()
vault.lastSynced = lastSynced
vault.unlockMutex()
}
func (vault *Vault) GetLastSynced() int64 {
vault.lockMutex()
defer vault.unlockMutex()
return vault.lastSynced
}
func (vault *Vault) SetWebsocketConnected(connected bool) {
vault.lockMutex()
vault.websockedConnected = connected
vault.unlockMutex()
}
func (vault *Vault) IsWebsocketConnected() bool {
vault.lockMutex()
defer vault.unlockMutex()
return vault.websockedConnected
}

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"time"
"github.com/quexten/goldwarden/ipc/messages"
"github.com/spf13/cobra"
@ -110,7 +111,9 @@ var statusCmd = &cobra.Command{
fmt.Println("{")
fmt.Println(" \"locked\":", status.Locked, ",")
fmt.Println(" \"loginEntries\":", status.NumberOfLogins, ",")
fmt.Println(" \"noteEntries\":", status.NumberOfNotes)
fmt.Println(" \"noteEntries\":", status.NumberOfNotes, ",")
fmt.Println(" \"lastSynced\": \"" + time.Unix(status.LastSynced, 0).String() + "\",")
fmt.Println(" \"websocketConnected\":", status.WebsockedConnected)
fmt.Println("}")
default:
println("Wrong response type")

View File

@ -21,11 +21,11 @@ type VaultStatusRequest struct {
}
type VaultStatusResponse struct {
Locked bool
NumberOfLogins int
NumberOfNotes int
// todo websocket status
// todo last synced
Locked bool
NumberOfLogins int
NumberOfNotes int
LastSynced int64
WebsockedConnected bool
}
func init() {