2021-10-12 09:08:07 -05:00
|
|
|
package manager
|
2021-10-01 07:39:57 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-01-30 02:21:27 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
|
2021-11-29 08:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/usagestats"
|
2022-08-02 08:08:09 -05:00
|
|
|
encryptionprovider "github.com/grafana/grafana/pkg/services/encryption/provider"
|
|
|
|
encryptionservice "github.com/grafana/grafana/pkg/services/encryption/service"
|
2022-01-26 11:44:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2021-11-17 13:43:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/kmsproviders/osskmsproviders"
|
2021-10-12 09:08:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
2021-10-01 07:39:57 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
func SetupTestService(tb testing.TB, store secrets.Store) *SecretsService {
|
2022-07-15 11:33:34 -05:00
|
|
|
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.FeatureManager) *SecretsService {
|
2021-10-12 09:08:07 -05:00
|
|
|
tb.Helper()
|
2021-10-01 07:39:57 -05:00
|
|
|
defaultKey := "SdlklWklckeLS"
|
|
|
|
if len(setting.SecretKey) > 0 {
|
|
|
|
defaultKey = setting.SecretKey
|
|
|
|
}
|
|
|
|
raw, err := ini.Load([]byte(`
|
|
|
|
[security]
|
2022-03-16 14:05:13 -05:00
|
|
|
secret_key = ` + defaultKey + `
|
|
|
|
|
|
|
|
[security.encryption]
|
|
|
|
data_keys_cache_ttl = 5m
|
|
|
|
data_keys_cache_cleanup_interval = 1ns`))
|
2021-10-12 09:08:07 -05:00
|
|
|
require.NoError(tb, err)
|
2022-01-26 11:44:20 -06:00
|
|
|
|
2021-11-04 11:47:21 -05:00
|
|
|
cfg := &setting.Cfg{Raw: raw}
|
|
|
|
settings := &setting.OSSImpl{Cfg: cfg}
|
2021-10-01 07:39:57 -05:00
|
|
|
|
2022-08-02 08:08:09 -05:00
|
|
|
encProvider := encryptionprovider.Provider{}
|
|
|
|
usageStats := &usagestats.UsageStatsMock{}
|
|
|
|
|
|
|
|
encryption, err := encryptionservice.ProvideEncryptionService(encProvider, usageStats, settings)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
2021-11-17 03:52:45 -06:00
|
|
|
secretsService, err := ProvideSecretsService(
|
2021-10-12 09:08:07 -05:00
|
|
|
store,
|
2022-02-01 12:24:59 -06:00
|
|
|
osskmsproviders.ProvideService(encryption, settings, features),
|
2021-11-17 03:52:45 -06:00
|
|
|
encryption,
|
2021-10-01 07:39:57 -05:00
|
|
|
settings,
|
2022-02-01 12:24:59 -06:00
|
|
|
features,
|
2021-11-29 08:35:15 -06:00
|
|
|
&usagestats.UsageStatsMock{T: tb},
|
2021-10-01 07:39:57 -05:00
|
|
|
)
|
2021-11-17 03:52:45 -06:00
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
return secretsService
|
2021-10-01 07:39:57 -05:00
|
|
|
}
|