Skip to content

Commit 5179d41

Browse files
committed
Error handling
1 parent 93ae4d4 commit 5179d41

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

internal/pkg/policy/parsed_policy.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ func NewParsedPolicy(ctx context.Context, bulker bulk.Bulk, p model.Policy) (*Pa
7777
return nil, err
7878
}
7979
for name, policyOutput := range p.Data.Outputs {
80-
ks := secret.ProcessOutputSecret(policyOutput, secretValues)
80+
ks, err := secret.ProcessOutputSecret(policyOutput, secretValues)
81+
if err != nil {
82+
return nil, fmt.Errorf("failed to replace secrets in output section of policy '%s': %w", name, err)
83+
}
8184
for _, key := range ks {
8285
secretKeys = append(secretKeys, "outputs."+name+"."+key)
8386
}
@@ -92,7 +95,10 @@ func NewParsedPolicy(ctx context.Context, bulker bulk.Bulk, p model.Policy) (*Pa
9295
// Replace secrets in 'agent.download' section of policy
9396
if agentDownload, exists := p.Data.Agent["download"]; exists {
9497
if section, ok := agentDownload.(map[string]interface{}); ok {
95-
agentDownloadSecretKeys := secret.ProcessMapSecrets(section, secretValues)
98+
agentDownloadSecretKeys, err := secret.ProcessMapSecrets(section, secretValues)
99+
if err != nil {
100+
return nil, fmt.Errorf("failed to replace secrets in agent.download section of policy: %w", err)
101+
}
96102
for _, key := range agentDownloadSecretKeys {
97103
secretKeys = append(secretKeys, "agent.download."+key)
98104
}
@@ -101,7 +107,10 @@ func NewParsedPolicy(ctx context.Context, bulker bulk.Bulk, p model.Policy) (*Pa
101107
}
102108

103109
// Replace secrets in `fleet` section of policy
104-
fleetSecretKeys := secret.ProcessMapSecrets(p.Data.Fleet, secretValues)
110+
fleetSecretKeys, err := secret.ProcessMapSecrets(p.Data.Fleet, secretValues)
111+
if err != nil {
112+
return nil, fmt.Errorf("failed to replace secrets in fleet section of policy: %w", err)
113+
}
105114
for _, key := range fleetSecretKeys {
106115
secretKeys = append(secretKeys, "fleet."+key)
107116
}

internal/pkg/secret/secret.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package secret
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
1011
"regexp"
1112
"strconv"
1213
"strings"
@@ -323,17 +324,20 @@ func setSecretPath(section smap.Map, secretValue string, secretPaths []string) {
323324
}
324325

325326
// Read secret from output and mutate output with secret value
326-
func ProcessOutputSecret(output smap.Map, secretValues map[string]string) []string {
327+
func ProcessOutputSecret(output smap.Map, secretValues map[string]string) ([]string, error) {
327328

328329
// Unfortunately, there are two ways (formats) of specifying secret references in
329330
// policies: inline and path (see https://github.com/elastic/fleet-server/pull/5852).
330331
// So we try replacing secret references in both formats.
331332

332-
keys := processMapWithInlineSecrets(output, secretValues)
333+
keys, err := processMapWithInlineSecrets(output, secretValues)
334+
if err != nil {
335+
return nil, fmt.Errorf("failed processing output secret with inline secrets: %w", err)
336+
}
333337
k := processMapWithPathSecrets(output, secretValues)
334338

335339
keys = append(keys, k...)
336-
return keys
340+
return keys, nil
337341
}
338342

339343
// processMapWithPathSecrets reads secrets from the output and mutates the output with the secret values using
@@ -370,26 +374,31 @@ func processMapWithPathSecrets(m smap.Map, secretValues map[string]string) []str
370374
return keys
371375
}
372376

373-
func processMapWithInlineSecrets(m smap.Map, secretValues map[string]string) []string {
377+
func processMapWithInlineSecrets(m smap.Map, secretValues map[string]string) ([]string, error) {
374378
replacedM, keys := replaceInlineSecretRefsInMap(m, secretValues)
375379
for _, key := range keys {
376380
rm := smap.Map(replacedM)
377-
m.Set(key, rm.Get(key))
381+
if err := m.Set(key, rm.Get(key)); err != nil {
382+
return nil, fmt.Errorf("failed processing map with inline secrets: failed to set secret value for key %s: %w", key, err)
383+
}
378384
}
379-
return keys
385+
return keys, nil
380386
}
381387

382388
// ProcessMapSecrets reads and replaces secrets in the agent.download section of the policy
383-
func ProcessMapSecrets(m smap.Map, secretValues map[string]string) []string {
389+
func ProcessMapSecrets(m smap.Map, secretValues map[string]string) ([]string, error) {
384390
// Unfortunately, there are two ways (formats) of specifying secret references in
385391
// policies: inline and path (see https://github.com/elastic/fleet-server/pull/5852).
386392
// So we try replacing secret references in both formats.
387393

388-
keys := processMapWithInlineSecrets(m, secretValues)
394+
keys, err := processMapWithInlineSecrets(m, secretValues)
395+
if err != nil {
396+
return nil, fmt.Errorf("failed processing map secrets with inline secrets: %w", err)
397+
}
389398
k := processMapWithPathSecrets(m, secretValues)
390399

391400
keys = append(keys, k...)
392-
return keys
401+
return keys, nil
393402
}
394403

395404
// replaceStringRef replaces values matching a secret ref regex, e.g. $co.elastic.secret{<secret ref>} -> <secret value>

internal/pkg/secret/secret_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestProcessOutputSecret(t *testing.T) {
309309
"sslother": "sslother_value",
310310
"sslkey": "sslkey_value",
311311
}
312-
keys := ProcessOutputSecret(output, secretValues)
312+
keys, err := ProcessOutputSecret(output, secretValues)
313313
assert.NoError(t, err)
314314

315315
assert.Equal(t, expectOutput, output)

0 commit comments

Comments
 (0)