GODT-2251: Store gluon cache in user cache rather than user data

This commit is contained in:
James Houlahan
2023-01-16 16:27:41 +01:00
parent fdbc380421
commit 03c8c323bc
10 changed files with 53 additions and 31 deletions

View File

@ -89,12 +89,12 @@ func newVault(locations *locations.Locations) (*vault.Vault, bool, bool, error)
vaultKey = key vaultKey = key
} }
gluonDir, err := locations.ProvideGluonPath() gluonCacheDir, err := locations.ProvideGluonCachePath()
if err != nil { if err != nil {
return nil, false, false, fmt.Errorf("could not provide gluon path: %w", err) return nil, false, false, fmt.Errorf("could not provide gluon path: %w", err)
} }
vault, corrupt, err := vault.New(vaultDir, gluonDir, vaultKey) vault, corrupt, err := vault.New(vaultDir, gluonCacheDir, vaultKey)
if err != nil { if err != nil {
return nil, false, false, fmt.Errorf("could not create vault: %w", err) return nil, false, false, fmt.Errorf("could not create vault: %w", err)
} }

View File

@ -216,19 +216,19 @@ func newBridge(
return nil, fmt.Errorf("failed to load TLS config: %w", err) return nil, fmt.Errorf("failed to load TLS config: %w", err)
} }
gluonDir, err := getGluonDir(vault) gluonCacheDir, err := getGluonDir(vault)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get Gluon directory: %w", err) return nil, fmt.Errorf("failed to get Gluon directory: %w", err)
} }
gluonDBDir, err := locator.ProvideGluonPath() gluonConfigDir, err := locator.ProvideGluonConfigPath()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get Gluon Database directory: %w", err) return nil, fmt.Errorf("failed to get Gluon Database directory: %w", err)
} }
imapServer, err := newIMAPServer( imapServer, err := newIMAPServer(
gluonDir, gluonCacheDir,
gluonDBDir, gluonConfigDir,
curVersion, curVersion,
tlsConfig, tlsConfig,
reporter, reporter,

View File

@ -490,7 +490,7 @@ func TestBridge_InitGluonDirectory(t *testing.T) {
_, err = os.ReadDir(bridge.ApplyGluonCachePathSuffix(b.GetGluonCacheDir())) _, err = os.ReadDir(bridge.ApplyGluonCachePathSuffix(b.GetGluonCacheDir()))
require.False(t, os.IsNotExist(err)) require.False(t, os.IsNotExist(err))
_, err = os.ReadDir(bridge.ApplyGluonDBPathSuffix(configDir)) _, err = os.ReadDir(bridge.ApplyGluonConfigPathSuffix(configDir))
require.False(t, os.IsNotExist(err)) require.False(t, os.IsNotExist(err))
}) })
}) })
@ -533,7 +533,7 @@ func TestBridge_ChangeCacheDirectory(t *testing.T) {
_, err = os.ReadDir(bridge.ApplyGluonCachePathSuffix(currentCacheDir)) _, err = os.ReadDir(bridge.ApplyGluonCachePathSuffix(currentCacheDir))
require.True(t, os.IsNotExist(err)) require.True(t, os.IsNotExist(err))
// Database should not have changed. // Database should not have changed.
_, err = os.ReadDir(bridge.ApplyGluonDBPathSuffix(configDir)) _, err = os.ReadDir(bridge.ApplyGluonConfigPathSuffix(configDir))
require.False(t, os.IsNotExist(err)) require.False(t, os.IsNotExist(err))
// New path should have Gluon sub-folder. // New path should have Gluon sub-folder.

View File

@ -225,13 +225,13 @@ func ApplyGluonCachePathSuffix(basePath string) string {
return filepath.Join(basePath, "backend", "store") return filepath.Join(basePath, "backend", "store")
} }
func ApplyGluonDBPathSuffix(basePath string) string { func ApplyGluonConfigPathSuffix(basePath string) string {
return filepath.Join(basePath, "backend", "db") return filepath.Join(basePath, "backend", "db")
} }
// nolint:funlen // nolint:funlen
func newIMAPServer( func newIMAPServer(
gluonCacheDir, gluonDBDir string, gluonCacheDir, gluonConfigDir string,
version *semver.Version, version *semver.Version,
tlsConfig *tls.Config, tlsConfig *tls.Config,
reporter reporter.Reporter, reporter reporter.Reporter,
@ -240,11 +240,11 @@ func newIMAPServer(
tasks *async.Group, tasks *async.Group,
) (*gluon.Server, error) { ) (*gluon.Server, error) {
gluonCacheDir = ApplyGluonCachePathSuffix(gluonCacheDir) gluonCacheDir = ApplyGluonCachePathSuffix(gluonCacheDir)
gluonDBDir = ApplyGluonDBPathSuffix(gluonDBDir) gluonConfigDir = ApplyGluonConfigPathSuffix(gluonConfigDir)
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"gluonStore": gluonCacheDir, "gluonStore": gluonCacheDir,
"gluonDB": gluonDBDir, "gluonDB": gluonConfigDir,
"version": version, "version": version,
"logClient": logClient, "logClient": logClient,
"logServer": logServer, "logServer": logServer,
@ -276,7 +276,7 @@ func newIMAPServer(
imapServer, err := gluon.New( imapServer, err := gluon.New(
gluon.WithTLS(tlsConfig), gluon.WithTLS(tlsConfig),
gluon.WithDataDir(gluonCacheDir), gluon.WithDataDir(gluonCacheDir),
gluon.WithDatabaseDir(gluonDBDir), gluon.WithDatabaseDir(gluonConfigDir),
gluon.WithStoreBuilder(new(storeBuilder)), gluon.WithStoreBuilder(new(storeBuilder)),
gluon.WithLogger(imapClientLog, imapServerLog), gluon.WithLogger(imapClientLog, imapServerLog),
getGluonVersionInfo(version), getGluonVersionInfo(version),

View File

@ -120,7 +120,7 @@ func (bridge *Bridge) GetGluonCacheDir() string {
} }
func (bridge *Bridge) GetGluonConfigDir() (string, error) { func (bridge *Bridge) GetGluonConfigDir() (string, error) {
return bridge.locator.ProvideGluonPath() return bridge.locator.ProvideGluonConfigPath()
} }
func (bridge *Bridge) SetGluonDir(ctx context.Context, newGluonDir string) error { func (bridge *Bridge) SetGluonDir(ctx context.Context, newGluonDir string) error {
@ -353,10 +353,10 @@ func (bridge *Bridge) FactoryReset(ctx context.Context) {
}, bridge.usersLock) }, bridge.usersLock)
// Wipe the vault. // Wipe the vault.
gluonDir, err := bridge.locator.ProvideGluonPath() gluonCacheDir, err := bridge.locator.ProvideGluonCachePath()
if err != nil { if err != nil {
logrus.WithError(err).Error("Failed to provide gluon dir") logrus.WithError(err).Error("Failed to provide gluon dir")
} else if err := bridge.vault.Reset(gluonDir); err != nil { } else if err := bridge.vault.Reset(gluonCacheDir); err != nil {
logrus.WithError(err).Error("Failed to reset vault") logrus.WithError(err).Error("Failed to reset vault")
} }

View File

@ -26,7 +26,8 @@ import (
type Locator interface { type Locator interface {
ProvideSettingsPath() (string, error) ProvideSettingsPath() (string, error)
ProvideLogsPath() (string, error) ProvideLogsPath() (string, error)
ProvideGluonPath() (string, error) ProvideGluonCachePath() (string, error)
ProvideGluonConfigPath() (string, error)
GetLicenseFilePath() string GetLicenseFilePath() string
GetDependencyLicensesLink() string GetDependencyLicensesLink() string
Clear() error Clear() error

View File

@ -138,14 +138,24 @@ func (l *Locations) ProvideSettingsPath() (string, error) {
return l.getSettingsPath(), nil return l.getSettingsPath(), nil
} }
// ProvideGluonPath returns a location for gluon data. // ProvideGluonConfigPath returns a location for gluon data.
// It creates it if it doesn't already exist. // It creates it if it doesn't already exist.
func (l *Locations) ProvideGluonPath() (string, error) { func (l *Locations) ProvideGluonConfigPath() (string, error) {
if err := os.MkdirAll(l.getGluonPath(), 0o700); err != nil { if err := os.MkdirAll(l.getGluonConfigPath(), 0o700); err != nil {
return "", err return "", err
} }
return l.getGluonPath(), nil return l.getGluonConfigPath(), nil
}
// ProvideGluonCachePath returns a location for gluon message cache data (does not need to be persistent).
// It creates it if it doesn't already exist.
func (l *Locations) ProvideGluonCachePath() (string, error) {
if err := os.MkdirAll(l.getGluonCachePath(), 0o700); err != nil {
return "", err
}
return l.getGluonCachePath(), nil
} }
// ProvideLogsPath returns a location for user logs (e.g. ~/.local/share/<company>/<app>/logs). // ProvideLogsPath returns a location for user logs (e.g. ~/.local/share/<company>/<app>/logs).
@ -178,7 +188,11 @@ func (l *Locations) ProvideUpdatesPath() (string, error) {
return l.getUpdatesPath(), nil return l.getUpdatesPath(), nil
} }
func (l *Locations) getGluonPath() string { func (l *Locations) getGluonCachePath() string {
return filepath.Join(l.userCache, "gluon")
}
func (l *Locations) getGluonConfigPath() string {
return filepath.Join(l.userData, "gluon") return filepath.Join(l.userData, "gluon")
} }
@ -231,7 +245,7 @@ func (l *Locations) Clean() error {
l.GetGuiLockFile(), l.GetGuiLockFile(),
l.getLogsPath(), l.getLogsPath(),
l.getUpdatesPath(), l.getUpdatesPath(),
l.getGluonPath(), l.getGluonConfigPath(),
).Do() ).Do()
} }

View File

@ -47,7 +47,7 @@ type Vault struct {
} }
// New constructs a new encrypted data vault at the given filepath using the given encryption key. // New constructs a new encrypted data vault at the given filepath using the given encryption key.
func New(vaultDir, gluonDir string, key []byte) (*Vault, bool, error) { func New(vaultDir, gluonCacheDir string, key []byte) (*Vault, bool, error) {
if err := os.MkdirAll(vaultDir, 0o700); err != nil { if err := os.MkdirAll(vaultDir, 0o700); err != nil {
return nil, false, err return nil, false, err
} }
@ -64,7 +64,7 @@ func New(vaultDir, gluonDir string, key []byte) (*Vault, bool, error) {
return nil, false, err return nil, false, err
} }
vault, corrupt, err := newVault(filepath.Join(vaultDir, "vault.enc"), gluonDir, gcm) vault, corrupt, err := newVault(filepath.Join(vaultDir, "vault.enc"), gluonCacheDir, gcm)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }

View File

@ -88,12 +88,19 @@ func (s *scenario) theUserChangesTheGluonPath() error {
} }
func (s *scenario) theUserDeletesTheGluonFiles() error { func (s *scenario) theUserDeletesTheGluonFiles() error {
path, err := s.t.locator.ProvideGluonPath() if path, err := s.t.locator.ProvideGluonCachePath(); err != nil {
if err != nil { return fmt.Errorf("failed to get gluon cache path: %w", err)
return err } else if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove gluon cache path: %w", err)
} }
return os.RemoveAll(path) if path, err := s.t.locator.ProvideGluonConfigPath(); err != nil {
return fmt.Errorf("failed to get gluon config path: %w", err)
} else if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove gluon config path: %w", err)
}
return nil
} }
func (s *scenario) theUserHasDisabledAutomaticUpdates() error { func (s *scenario) theUserHasDisabledAutomaticUpdates() error {

View File

@ -100,13 +100,13 @@ func (t *testCtx) initBridge() (<-chan events.Event, error) {
} }
// Get the default gluon path. // Get the default gluon path.
gluonDir, err := t.locator.ProvideGluonPath() gluonCacheDir, err := t.locator.ProvideGluonCachePath()
if err != nil { if err != nil {
return nil, fmt.Errorf("could not get gluon dir: %w", err) return nil, fmt.Errorf("could not get gluon dir: %w", err)
} }
// Create the vault. // Create the vault.
vault, corrupt, err := vault.New(vaultDir, gluonDir, t.storeKey) vault, corrupt, err := vault.New(vaultDir, gluonCacheDir, t.storeKey)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not create vault: %w", err) return nil, fmt.Errorf("could not create vault: %w", err)
} else if corrupt { } else if corrupt {