grafana/pkg/services/secrets/manager/helpers.go
Marcus Efraimsson 6768c6c059
Chore: Remove public vars in setting package (#81018)
Removes the public variable setting.SecretKey plus some other ones. 
Introduces some new functions for creating setting.Cfg.
2024-01-23 12:36:22 +01:00

58 lines
1.7 KiB
Go

package manager
import (
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"
"github.com/grafana/grafana/pkg/infra/usagestats"
encryptionprovider "github.com/grafana/grafana/pkg/services/encryption/provider"
encryptionservice "github.com/grafana/grafana/pkg/services/encryption/service"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/kmsproviders/osskmsproviders"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/setting"
)
func SetupTestService(tb testing.TB, store secrets.Store) *SecretsService {
return setupTestService(tb, store, featuremgmt.WithFeatures())
}
func SetupDisabledTestService(tb testing.TB, store secrets.Store) *SecretsService {
return setupTestService(tb, store, featuremgmt.WithFeatures(featuremgmt.FlagDisableEnvelopeEncryption))
}
func setupTestService(tb testing.TB, store secrets.Store, features featuremgmt.FeatureToggles) *SecretsService {
tb.Helper()
defaultKey := "SdlklWklckeLS"
raw, err := ini.Load([]byte(`
[security]
secret_key = ` + defaultKey + `
[security.encryption]
data_keys_cache_ttl = 5m
data_keys_cache_cleanup_interval = 1ns`))
require.NoError(tb, err)
cfg := &setting.Cfg{Raw: raw}
encProvider := encryptionprovider.Provider{}
usageStats := &usagestats.UsageStatsMock{}
encryption, err := encryptionservice.ProvideEncryptionService(encProvider, usageStats, cfg)
require.NoError(tb, err)
secretsService, err := ProvideSecretsService(
store,
osskmsproviders.ProvideService(encryption, cfg, features),
encryption,
cfg,
features,
&usagestats.UsageStatsMock{T: tb},
)
require.NoError(tb, err)
return secretsService
}