@@ -15,6 +15,7 @@ import (
1515 "github.com/stretchr/testify/require"
1616
1717 "github.com/elastic/fleet-server/v7/internal/pkg/model"
18+ ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
1819)
1920
2021//go:embed testdata/test_policy.json
@@ -29,6 +30,9 @@ var logstashOutputPolicy string
2930//go:embed testdata/remote_es_policy.json
3031var testPolicyRemoteES string
3132
33+ //go:embed testdata/policy_with_secrets_mixed.json
34+ var policyWithSecretsMixed string
35+
3236func TestNewParsedPolicy (t * testing.T ) {
3337 // Run two formatting of the same payload to validate that the sha2 remains the same
3438 testcases := []struct {
@@ -102,3 +106,36 @@ func TestNewParsedPolicyRemoteES(t *testing.T) {
102106 // Validate that default was found
103107 require .Equal (t , "remote" , pp .Default .Name )
104108}
109+
110+ // TestParsedPolicyMixedSecretsReplacement tests that secrets specified in a policy
111+ // using either the `secrets.<path-to-key>.<key>.id:<secret ref>` format or the
112+ // `<path>: $co.elastic.secret{<secret ref>}` format are both replaced correctly.
113+ func TestParsedPolicyMixedSecretsReplacement (t * testing.T ) {
114+ // Load the model into the policy object
115+ var m model.Policy
116+ var d model.PolicyData
117+ err := json .Unmarshal ([]byte (policyWithSecretsMixed ), & d )
118+ require .NoError (t , err )
119+
120+ m .Data = & d
121+
122+ bulker := ftesting .NewMockBulk ()
123+ pp , err := NewParsedPolicy (context .TODO (), bulker , m )
124+ require .NoError (t , err )
125+
126+ // Validate that secrets were identified
127+ require .Len (t , pp .SecretKeys , 4 )
128+ require .Contains (t , pp .SecretKeys , "outputs.fs-output.type" )
129+ require .Contains (t , pp .SecretKeys , "outputs.fs-output.ssl.key" )
130+ require .Contains (t , pp .SecretKeys , "inputs.0.streams.0.auth.basic.password" )
131+ require .Contains (t , pp .SecretKeys , "inputs.0.streams.1.auth.basic.password" )
132+
133+ // Validate that secret references were replaced
134+ firstInputStreams := pp .Inputs [0 ]["streams" ].([]any )
135+ firstInputFirstStream := firstInputStreams [0 ].(map [string ]any )
136+ firstInputSecondStream := firstInputStreams [1 ].(map [string ]any )
137+ require .Equal (t , "0Mx2UZoBTAyw4gQKSaao_value" , firstInputFirstStream ["auth.basic.password" ])
138+ require .Equal (t , "0Mx2UZoBTAyw4gQKSaao_value" , firstInputSecondStream ["auth.basic.password" ])
139+ require .Equal (t , "abcdef123_value" , pp .Policy .Data .Outputs ["fs-output" ]["type" ])
140+ require .Equal (t , "w8yELZoBTAyw4gQK9KZ7_value" , pp .Policy .Data .Outputs ["fs-output" ]["ssl" ].(map [string ]interface {})["key" ])
141+ }
0 commit comments