Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypt only once and hold calendar auth data in memory #91

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions internal/auth/yaml_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ import (
"io/fs"
"os"

"github.com/charmbracelet/log"
"github.com/inovex/CalendarSync/internal/config"
"gopkg.in/yaml.v3"
)

type YamlStorage struct {
StoragePath string
StorageEncryptionKey string
// Holds the decrypted CalendarAuth Config in memory, so the file does not have to be read multiple times
DecryptedAuth []CalendarAuth
alxndr13 marked this conversation as resolved.
Show resolved Hide resolved
}

func (y *YamlStorage) Setup(config config.AuthStorage, encryptionPassphrase string) error {
y.StorageEncryptionKey = encryptionPassphrase
y.StoragePath = config.Config["path"].(string)

log.Debug("Loading saved auth data into memory")
stor, err := y.readAndParseFile()
alxndr13 marked this conversation as resolved.
Show resolved Hide resolved
if errors.Is(err, os.ErrNotExist) {
log.Debug("No storage file found, skipping loading from memory")
} else {
// Put the data into DecryptedAuth
y.DecryptedAuth = stor.Calendars
}
return nil
}

Expand Down Expand Up @@ -47,10 +59,25 @@ func (y *YamlStorage) WriteCalendarAuth(newCal CalendarAuth) (bool, error) {
return false, err
}

// Adding freshly written CalendarAuth to memory
// Probably unneeded, the next time this data will be retrieved is on the next calendarsync run
log.Debugf("Adding calendar auth for cal %s to memory", newCal.CalendarID)
y.DecryptedAuth = append(y.DecryptedAuth, newCal)
alxndr13 marked this conversation as resolved.
Show resolved Hide resolved

return true, nil
}

func (y *YamlStorage) ReadCalendarAuth(calendarID string) (*CalendarAuth, error) {
// if we already decrypted the file, read from memory
if len(y.DecryptedAuth) > 0 {
alxndr13 marked this conversation as resolved.
Show resolved Hide resolved
for _, cal := range y.DecryptedAuth {
if cal.CalendarID == calendarID {
log.Debug("loaded auth data from memory", "calendarID", cal.CalendarID)
return &cal, nil
}
}
}

file, err := y.readAndParseFile()
if err != nil {
return nil, ignoreNoFile(err)
Expand Down
Loading