Skip to content

Commit 7287ece

Browse files
committed
Implement replacing secrets in agent.download section
1 parent fb4d6e0 commit 7287ece

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

internal/pkg/model/schema.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/policy/parsed_policy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ func NewParsedPolicy(ctx context.Context, bulker bulk.Bulk, p model.Policy) (*Pa
8787
policyInputs, keys := secret.ProcessInputsSecrets(p.Data, secretValues)
8888
secretKeys = append(secretKeys, keys...)
8989

90+
// FIXME: Replace secrets in 'agent.download' section of policy
91+
if agentDownload, exists := p.Data.Agent["download"]; exists {
92+
if section, ok := agentDownload.(map[string]interface{}); ok {
93+
agentDownloadSecretKeys, err := secret.ProcessAgentDownloadSecrets(ctx, section, bulker)
94+
if err != nil {
95+
return nil, fmt.Errorf("error processing agent secrets: %w", err)
96+
}
97+
for _, key := range agentDownloadSecretKeys {
98+
secretKeys = append(secretKeys, "agent.download."+key)
99+
}
100+
}
101+
}
102+
90103
// Done replacing secrets.
91104
p.Data.SecretReferences = nil
92105

internal/pkg/policy/parsed_policy_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ func TestParsedPolicyMixedSecretsReplacement(t *testing.T) {
125125
require.NoError(t, err)
126126

127127
// Validate that secrets were identified
128-
require.Len(t, pp.SecretKeys, 4)
128+
require.Len(t, pp.SecretKeys, 5)
129129
require.Contains(t, pp.SecretKeys, "outputs.fs-output.type")
130130
require.Contains(t, pp.SecretKeys, "outputs.fs-output.ssl.key")
131131
require.Contains(t, pp.SecretKeys, "inputs.0.streams.0.auth.basic.password")
132132
require.Contains(t, pp.SecretKeys, "inputs.0.streams.1.auth.basic.password")
133+
require.Contains(t, pp.SecretKeys, "agent.download.ssl.key")
133134

134135
// Validate that secret references were replaced
135136
firstInputStreams := pp.Inputs[0]["streams"].([]any)

internal/pkg/secret/secret.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,52 +274,52 @@ func replaceInlineSecretRefsInSlice(arr []any, secrets map[string]string) ([]any
274274
return result, keys
275275
}
276276

277-
type OutputSecret struct {
277+
type Secret struct {
278278
Path []string
279279
ID string
280280
}
281281

282-
func getSecretIDAndPath(secret smap.Map) []OutputSecret {
283-
outputSecrets := make([]OutputSecret, 0)
282+
func getSecretIDAndPath(secret smap.Map) []Secret {
283+
secrets := make([]Secret, 0)
284284

285285
secretID := secret.GetString("id")
286286
if secretID != "" {
287-
outputSecrets = append(outputSecrets, OutputSecret{
287+
secrets = append(secrets, Secret{
288288
Path: make([]string, 0),
289289
ID: secretID,
290290
})
291291

292-
return outputSecrets
292+
return secrets
293293
}
294294

295295
for secretKey := range secret {
296-
newOutputSecrets := getSecretIDAndPath(secret.GetMap(secretKey))
296+
newSecrets := getSecretIDAndPath(secret.GetMap(secretKey))
297297

298-
for _, secret := range newOutputSecrets {
299-
path := append([]string{secretKey}, secret.Path...)
300-
outputSecrets = append(outputSecrets, OutputSecret{
298+
for _, newSecret := range newSecrets {
299+
path := append([]string{secretKey}, newSecret.Path...)
300+
secrets = append(secrets, Secret{
301301
Path: path,
302-
ID: secret.ID,
302+
ID: newSecret.ID,
303303
})
304304
}
305305
}
306306

307-
return outputSecrets
307+
return secrets
308308
}
309309

310-
func setSecretPath(output smap.Map, secretValue string, secretPaths []string) {
310+
func setSecretPath(section smap.Map, secretValue string, secretPaths []string) {
311311
// Break the recursion
312312
if len(secretPaths) == 1 {
313-
output[secretPaths[0]] = secretValue
313+
section[secretPaths[0]] = secretValue
314314
return
315315
}
316316
path, secretPaths := secretPaths[0], secretPaths[1:]
317317

318-
if output.GetMap(path) == nil {
319-
output[path] = make(map[string]interface{})
318+
if section.GetMap(path) == nil {
319+
section[path] = make(map[string]interface{})
320320
}
321321

322-
setSecretPath(output.GetMap(path), secretValue, secretPaths)
322+
setSecretPath(section.GetMap(path), secretValue, secretPaths)
323323
}
324324

325325
// Read secret from output and mutate output with secret value
@@ -378,6 +378,42 @@ func processOutputWithInlineSecrets(output smap.Map, secretValues map[string]str
378378
return keys
379379
}
380380

381+
// ProcessAgentDownloadSecrets reads and replaces secrets in the agent.download section of the policy
382+
func ProcessAgentDownloadSecrets(ctx context.Context, agentDownload smap.Map, bulker bulk.Bulk) ([]string, error) {
383+
secrets := agentDownload.GetMap(FieldSecrets)
384+
delete(agentDownload, FieldSecrets)
385+
386+
secretReferences := make([]model.SecretReferencesItems, 0)
387+
agentDownloadSecrets := getSecretIDAndPath(secrets)
388+
keys := make([]string, 0, len(agentDownloadSecrets))
389+
390+
for _, secret := range agentDownloadSecrets {
391+
secretReferences = append(secretReferences, model.SecretReferencesItems{
392+
ID: secret.ID,
393+
})
394+
}
395+
if len(secretReferences) == 0 {
396+
return nil, nil
397+
}
398+
secretValues, err := GetSecretValues(ctx, secretReferences, bulker)
399+
if err != nil {
400+
return nil, err
401+
}
402+
for _, secret := range agentDownloadSecrets {
403+
var key string
404+
for _, p := range secret.Path {
405+
if key == "" {
406+
key = p
407+
continue
408+
}
409+
key = key + "." + p
410+
}
411+
keys = append(keys, key)
412+
setSecretPath(agentDownload, secretValues[secret.ID], secret.Path)
413+
}
414+
return keys, nil
415+
}
416+
381417
// replaceStringRef replaces values matching a secret ref regex, e.g. $co.elastic.secret{<secret ref>} -> <secret value>
382418
// and does this for multiple matches
383419
// returns the resulting string value, and if any replacements were made

internal/pkg/server/namespaces_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func Test_Agent_Namespace_test1(t *testing.T) {
171171
},
172172
OutputPermissions: json.RawMessage(`{"default": {} }`),
173173
Inputs: []map[string]interface{}{},
174-
Agent: json.RawMessage(`{"monitoring": {"use_output":"default"}}`),
174+
Agent: map[string]interface{}{"monitoring": {"use_output": "default"}},
175175
}
176176

177177
_, err = dl.CreatePolicy(ctx, srv.bulker, model.Policy{

internal/pkg/server/remote_es_output_integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Test_Agent_Remote_ES_Output(t *testing.T) {
165165
},
166166
OutputPermissions: json.RawMessage(`{"default": {}, "remoteES": {}}`),
167167
Inputs: []map[string]interface{}{},
168-
Agent: json.RawMessage(`{"monitoring": {"use_output":"remoteES"}}`),
168+
Agent: map[string]interface{}{"monitoring": {"use_output": "remoteES"}},
169169
}
170170

171171
_, err = dl.CreatePolicy(ctx, srv.bulker, model.Policy{
@@ -319,7 +319,7 @@ func Test_Agent_Remote_ES_Output_ForceUnenroll(t *testing.T) {
319319
},
320320
OutputPermissions: json.RawMessage(`{"default": {}, "remoteES": {}}`),
321321
Inputs: []map[string]interface{}{},
322-
Agent: json.RawMessage(`{"monitoring": {"use_output":"remoteES"}}`),
322+
Agent: map[string]interface{}{"monitoring": {"use_output": "remoteES"}},
323323
}
324324

325325
_, err = dl.CreatePolicy(ctx, srv.bulker, model.Policy{
@@ -440,7 +440,7 @@ func Test_Agent_Remote_ES_Output_Unenroll(t *testing.T) {
440440
},
441441
OutputPermissions: json.RawMessage(`{"default": {}, "remoteES": {}}`),
442442
Inputs: []map[string]interface{}{},
443-
Agent: json.RawMessage(`{"monitoring": {"use_output":"remoteES"}}`),
443+
Agent: map[string]interface{}{"monitoring": {"use_output": "remoteES"}},
444444
}
445445

446446
_, err = dl.CreatePolicy(ctx, srv.bulker, model.Policy{

0 commit comments

Comments
 (0)