63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package integration
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
biz "kra/internal/biz/integration"
|
|
"kra/internal/integration/runtimeconfig"
|
|
"testing"
|
|
)
|
|
|
|
func TestSMSSecretsSurviveMaskedSaveAndRuntimeReload(t *testing.T) {
|
|
db, err := openWithDriver("sqlite", "file:"+t.Name()+"?mode=memory&cache=shared")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.AutoMigrate(&ConfigPO{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := &integrationRuntimeTestProvider{Data: &Data{gormDB: newReloadableDB(db)}, store: runtimeconfig.NewStore()}
|
|
repo := NewIntegrationConfigRepo(p)
|
|
for _, provider := range []string{"aliyun", "tencent"} {
|
|
values := biz.DefaultIntegrationConfig("sms", provider)
|
|
for k, v := range values {
|
|
if v == "" {
|
|
values[k] = "test-" + k
|
|
}
|
|
}
|
|
raw, _ := json.Marshal(values)
|
|
if err = repo.SaveIntegrationConfig(context.Background(), &biz.IntegrationConfig{Kind: "sms", Provider: provider, Enabled: true, Values: raw}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
masked, err := repo.FindIntegrationConfig(context.Background(), "sms", provider)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var public map[string]string
|
|
_ = json.Unmarshal(masked.Values, &public)
|
|
for k := range values {
|
|
if biz.IsIntegrationSecretKey(k) && public[k] != "******" {
|
|
t.Fatalf("unmasked key %s", k)
|
|
}
|
|
}
|
|
if err = repo.SaveIntegrationConfig(context.Background(), masked); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rows, err := ReadRuntime(db)
|
|
if err != nil || len(rows) != 2 {
|
|
t.Fatalf("runtime rows=%d error=%v", len(rows), err)
|
|
}
|
|
for _, row := range rows {
|
|
var values map[string]string
|
|
_ = json.Unmarshal(row.Values, &values)
|
|
key := "access_key_secret"
|
|
if row.Provider == "tencent" {
|
|
key = "secret_key"
|
|
}
|
|
if values[key] != "test-"+key {
|
|
t.Fatal("saved secret did not survive runtime reload")
|
|
}
|
|
}
|
|
}
|