mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2026-02-04 08:18:34 +00:00
feat(BRIDGE-356): Added retry logic for unavailable preferred keychain on Linux; Feature flag support before bridge initialization; Refactored some bits of the code;
This commit is contained in:
@ -38,14 +38,15 @@ var pollJitter = 2 * time.Minute //nolint:gochecknoglobals
|
||||
const filename = "unleash_flags"
|
||||
|
||||
const (
|
||||
EventLoopNotificationDisabled = "InboxBridgeEventLoopNotificationDisabled"
|
||||
IMAPAuthenticateCommandDisabled = "InboxBridgeImapAuthenticateCommandDisabled"
|
||||
UserRemovalGluonDataCleanupDisabled = "InboxBridgeUserRemovalGluonDataCleanupDisabled"
|
||||
UpdateUseNewVersionFileStructureDisabled = "InboxBridgeUpdateWithOsFilterDisabled"
|
||||
LabelConflictResolverDisabled = "InboxBridgeLabelConflictResolverDisabled"
|
||||
SMTPSubmissionRequestSentryReportDisabled = "InboxBridgeSmtpSubmissionRequestSentryReportDisabled"
|
||||
InternalLabelConflictResolverDisabled = "InboxBridgeUnexpectedFoldersLabelsStartupFixupDisabled"
|
||||
ItnternalLabelConflictNonEmptyMailboxDeletion = "InboxBridgeUnknownNonEmptyMailboxDeletion"
|
||||
EventLoopNotificationDisabled = "InboxBridgeEventLoopNotificationDisabled"
|
||||
IMAPAuthenticateCommandDisabled = "InboxBridgeImapAuthenticateCommandDisabled"
|
||||
UserRemovalGluonDataCleanupDisabled = "InboxBridgeUserRemovalGluonDataCleanupDisabled"
|
||||
UpdateUseNewVersionFileStructureDisabled = "InboxBridgeUpdateWithOsFilterDisabled"
|
||||
LabelConflictResolverDisabled = "InboxBridgeLabelConflictResolverDisabled"
|
||||
SMTPSubmissionRequestSentryReportDisabled = "InboxBridgeSmtpSubmissionRequestSentryReportDisabled"
|
||||
InternalLabelConflictResolverDisabled = "InboxBridgeUnexpectedFoldersLabelsStartupFixupDisabled"
|
||||
InternalLabelConflictNonEmptyMailboxDeletion = "InboxBridgeUnknownNonEmptyMailboxDeletion"
|
||||
LinuxVaultPreferredKeychainNotAvailableRetryDisabled = "InboxBridgeLinuxVaultPreferredKeychainNotAvailableRetryDisabled"
|
||||
)
|
||||
|
||||
type FeatureFlagValueProvider interface {
|
||||
|
||||
132
internal/unleash/startup.go
Normal file
132
internal/unleash/startup.go
Normal file
@ -0,0 +1,132 @@
|
||||
// Copyright (c) 2025 Proton AG
|
||||
//
|
||||
// This file is part of Proton Mail Bridge.
|
||||
//
|
||||
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package unleash
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/go-proton-api"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const startupCacheFilename = "unleash_startup_flags.json"
|
||||
|
||||
var logger = logrus.WithField("pkg", "unleash-startup") //nolint:gochecknoglobals
|
||||
|
||||
type FeatureFlagStartupStore map[string]bool
|
||||
|
||||
func (f FeatureFlagStartupStore) GetFlagValue(key string) bool {
|
||||
val, ok := f[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
func newAPIOptions(
|
||||
apiURL string,
|
||||
version *semver.Version,
|
||||
) []proton.Option {
|
||||
return []proton.Option{
|
||||
proton.WithHostURL(apiURL),
|
||||
proton.WithAppVersion(constants.AppVersion(version.Original())),
|
||||
proton.WithLogger(logrus.WithField("pkg", "gpa/unleash-startup")),
|
||||
proton.WithRetryCount(0),
|
||||
}
|
||||
}
|
||||
|
||||
func readStartupCacheFile(filepath string) (map[string]bool, error) {
|
||||
ffStore := make(map[string]bool)
|
||||
if filepath == "" {
|
||||
return ffStore, nil
|
||||
}
|
||||
|
||||
file, err := os.Open(filepath) //nolint:gosec
|
||||
if err != nil {
|
||||
return ffStore, err
|
||||
}
|
||||
|
||||
defer func(file *os.File) {
|
||||
if err := file.Close(); err != nil {
|
||||
logger.WithError(err).Error("Unable to close cache file after read")
|
||||
}
|
||||
}(file)
|
||||
|
||||
if err := json.NewDecoder(file).Decode(&ffStore); err != nil {
|
||||
return ffStore, err
|
||||
}
|
||||
return ffStore, nil
|
||||
}
|
||||
|
||||
func saveStartupCacheFile(ffStore map[string]bool, filepath string) error {
|
||||
if filepath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Create(filepath) //nolint:gosec
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(file *os.File) {
|
||||
if err := file.Close(); err != nil {
|
||||
logger.WithError(err).Error("Unable to close cache file after write")
|
||||
}
|
||||
}(file)
|
||||
|
||||
if err := json.NewEncoder(file).Encode(ffStore); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetStartupFeatureFlagsAndStore(apiURL string, curVersion *semver.Version, unleashCachePathProvider func() (string, error)) map[string]bool {
|
||||
var cacheFilepath string
|
||||
cacheDir, err := unleashCachePathProvider()
|
||||
if err != nil {
|
||||
logger.WithError(err).Warn("Unable to obtain feature flag cache filepath")
|
||||
} else {
|
||||
cacheFilepath = filepath.Clean(filepath.Join(cacheDir, startupCacheFilename))
|
||||
}
|
||||
|
||||
ffStore, err := readStartupCacheFile(cacheFilepath)
|
||||
if err != nil {
|
||||
logger.WithError(err).Warn("An issue occurred when reading the cache file")
|
||||
}
|
||||
|
||||
manager := proton.New(newAPIOptions(apiURL, curVersion)...)
|
||||
featureFlagResult, err := manager.GetFeatures(context.Background(), uuid.New())
|
||||
if err == nil {
|
||||
ffStore = readResponseData(featureFlagResult)
|
||||
} else {
|
||||
logger.WithError(err).Warn("Failed to obtain feature flags from API")
|
||||
}
|
||||
|
||||
if err := saveStartupCacheFile(ffStore, cacheFilepath); err != nil {
|
||||
logger.WithError(err).Warn("An issue occurred when saving the cache file")
|
||||
}
|
||||
|
||||
return ffStore
|
||||
}
|
||||
156
internal/unleash/startup_test.go
Normal file
156
internal/unleash/startup_test.go
Normal file
@ -0,0 +1,156 @@
|
||||
// Copyright (c) 2025 Proton AG
|
||||
//
|
||||
// This file is part of Proton Mail Bridge.
|
||||
//
|
||||
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package unleash
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReadStartupCacheFile_Success(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "valid_cache")
|
||||
file, err := os.Create(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
testData := map[string]bool{
|
||||
"feature1": true,
|
||||
"feature2": false,
|
||||
}
|
||||
err = json.NewEncoder(file).Encode(testData)
|
||||
require.NoError(t, err)
|
||||
err = file.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
startupCache, err := readStartupCacheFile(filePath)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testData, startupCache)
|
||||
}
|
||||
|
||||
func TestReadStartupCacheFile_InvalidFilePath(t *testing.T) {
|
||||
filePath := "badFilepath/hello"
|
||||
startupCache, err := readStartupCacheFile(filePath)
|
||||
require.Error(t, err)
|
||||
require.Empty(t, startupCache)
|
||||
}
|
||||
|
||||
func TestSaveStartupCacheFile_Success(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "test_cache")
|
||||
|
||||
testData := map[string]bool{
|
||||
"feature1": true,
|
||||
"feature2": false,
|
||||
"feature3": true,
|
||||
}
|
||||
|
||||
err := saveStartupCacheFile(testData, filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
savedData, err := readStartupCacheFile(filePath)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testData, savedData)
|
||||
}
|
||||
|
||||
func TestSaveStartupCacheFile_InvalidFilePath(t *testing.T) {
|
||||
badFilePath := "/some_random_dir/hey/hello"
|
||||
|
||||
testData := map[string]bool{
|
||||
"feature1": true,
|
||||
"feature2": false,
|
||||
}
|
||||
|
||||
err := saveStartupCacheFile(testData, badFilePath)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetStartupFeatureFlagsAndStore_FakeAPIURL(t *testing.T) {
|
||||
apiURL := "https://example.com"
|
||||
cacheProvider := func() (string, error) {
|
||||
return t.TempDir(), nil
|
||||
}
|
||||
|
||||
version, err := semver.NewVersion("3.99.99+test")
|
||||
require.NoError(t, err)
|
||||
|
||||
featureFlags := GetStartupFeatureFlagsAndStore(apiURL, version, cacheProvider)
|
||||
require.Empty(t, featureFlags)
|
||||
}
|
||||
|
||||
func TestGetStartupFeatureFlagsAndStore_RealAPIURL(t *testing.T) {
|
||||
apiURL := "https://mail-api.proton.me"
|
||||
cacheProvider := func() (string, error) {
|
||||
return t.TempDir(), nil
|
||||
}
|
||||
|
||||
version, err := semver.NewVersion("3.99.99+test")
|
||||
require.NoError(t, err)
|
||||
|
||||
featureFlags := GetStartupFeatureFlagsAndStore(apiURL, version, cacheProvider)
|
||||
require.NotEmpty(t, featureFlags)
|
||||
}
|
||||
|
||||
func TestGetStartupFeatureFlagsAndStore_FeatureFlagCacheRetention(t *testing.T) {
|
||||
fakeAPIURL := "https://example.com"
|
||||
realAPIURL := "https://mail-api.proton.me"
|
||||
|
||||
cacheDir := t.TempDir()
|
||||
cacheProvider := func() (string, error) {
|
||||
return cacheDir, nil
|
||||
}
|
||||
|
||||
version, err := semver.NewVersion("3.99.99+test")
|
||||
require.NoError(t, err)
|
||||
|
||||
featureFlags := GetStartupFeatureFlagsAndStore(realAPIURL, version, cacheProvider)
|
||||
require.NotEmpty(t, featureFlags)
|
||||
|
||||
featureFlagsFromCache := GetStartupFeatureFlagsAndStore(fakeAPIURL, version, cacheProvider)
|
||||
require.NotEmpty(t, featureFlagsFromCache)
|
||||
require.Equal(t, featureFlags, featureFlagsFromCache)
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
fakeAPIURL := "https://example.com"
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
cacheProvider := func() (string, error) {
|
||||
return tmpDir, nil
|
||||
}
|
||||
filePath := filepath.Join(tmpDir, startupCacheFilename)
|
||||
|
||||
testData := map[string]bool{
|
||||
"feature1": true,
|
||||
"feature2": false,
|
||||
"feature3": true,
|
||||
}
|
||||
err := saveStartupCacheFile(testData, filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
version, err := semver.NewVersion("3.99.99+git")
|
||||
require.NoError(t, err)
|
||||
|
||||
featureFlagsFromCache := GetStartupFeatureFlagsAndStore(fakeAPIURL, version, cacheProvider)
|
||||
require.NotEmpty(t, featureFlagsFromCache)
|
||||
require.Equal(t, testData, featureFlagsFromCache)
|
||||
}
|
||||
Reference in New Issue
Block a user