64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
//go:build windows
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSecretSettingsAreProtectedAtRest(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
mgr, err := NewManager(ManagerOptions{DataRoot: dataRoot, HostVersion: "dev"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer mgr.Close()
|
|
|
|
const pluginID = "test.secret.settings"
|
|
if err := mgr.Registry().Register(PluginDef{
|
|
Manifest: Manifest{
|
|
ID: pluginID,
|
|
Version: "1.0.0",
|
|
Name: LocalizedText{EnUS: "Secret settings test"},
|
|
Settings: &SettingsSchema{Properties: map[string]SettingProperty{
|
|
"password": {Type: "string", Secret: true},
|
|
}},
|
|
},
|
|
Source: SourceBuiltin,
|
|
Desired: true,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mgr.Registry().Reconcile(context.Background())
|
|
|
|
const vmKey = "builtin.driver.reims-macos:fixture"
|
|
const password = "windows-dpapi-fixture"
|
|
if err := mgr.SetSettings(pluginID, vmKey, map[string]any{"password": password}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
masked := mgr.GetSettings(pluginID, vmKey, true)["password"]
|
|
if masked != "••••••••" {
|
|
t.Fatalf("masked password = %v", masked)
|
|
}
|
|
stored := mgr.GetSettings(pluginID, vmKey, false)["password"].(string)
|
|
if strings.Contains(stored, password) || !strings.HasPrefix(stored, protectedSecretPrefix) {
|
|
t.Fatalf("stored secret was not encrypted: %q", stored)
|
|
}
|
|
plain, err := mgr.SecretSetting(pluginID, vmKey, "password")
|
|
if err != nil || plain != password {
|
|
t.Fatalf("SecretSetting = %q, %v", plain, err)
|
|
}
|
|
stateBytes, err := os.ReadFile(filepath.Join(NewPluginsLayout(dataRoot).Root, StateFileName))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(stateBytes), password) {
|
|
t.Fatal("settings state contains plaintext secret")
|
|
}
|
|
}
|