44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package companion
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
type configPortFixture struct {
|
|
fields map[string]string
|
|
patches []ConfigFieldPatch
|
|
}
|
|
|
|
func (fixture *configPortFixture) ReadConfig(context.Context) (map[string]string, error) {
|
|
return fixture.fields, nil
|
|
}
|
|
func (fixture *configPortFixture) ApplyConfigPatch(_ context.Context, _ string, fields []ConfigFieldPatch) (map[string]string, error) {
|
|
fixture.patches = fields
|
|
return map[string]string{"ServerName": "Moon", "hostPath": "C:/secret"}, nil
|
|
}
|
|
|
|
func TestVersionedAdapterUsesOnlyLogicalConfigValuesAndRedactsDiagnostics(t *testing.T) {
|
|
port := &configPortFixture{fields: map[string]string{"ServerName": "Moon", "hostPath": "C:/secret", "Password": "nope"}}
|
|
adapter := VersionedAdapter{ServerVersion: "0.9.700.90357", Config: port, DiagnosticsState: map[string]string{"status": "healthy", "hostPath": "C:/secret"}}
|
|
read, err := adapter.ReadConfiguration(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("read config: %v", err)
|
|
}
|
|
fields := read["fields"].(map[string]string)
|
|
if fields["ServerName"] != "Moon" || len(fields) != 1 {
|
|
t.Fatalf("unsafe config was exposed: %+v", fields)
|
|
}
|
|
patched, err := adapter.PatchConfiguration(context.Background(), map[string]any{"revision": "r1", "fields": []any{map[string]any{"key": "ServerName", "value": "Moon"}}})
|
|
if err != nil {
|
|
t.Fatalf("patch config: %v", err)
|
|
}
|
|
if len(port.patches) != 1 || patched["appliedFields"].(map[string]string)["hostPath"] != "" {
|
|
t.Fatalf("patch leaked unsafe details: %+v", patched)
|
|
}
|
|
diagnostics, _ := adapter.Diagnostics(context.Background())
|
|
if diagnostics["hostPath"] != nil || diagnostics["status"] != "healthy" {
|
|
t.Fatalf("diagnostics leaked unsafe details: %+v", diagnostics)
|
|
}
|
|
}
|