Skip to content

Commit f9039fa

Browse files
authored
fix(http-hooks): resolve profile env in url and body (#544)
1 parent f93ce20 commit f9039fa

3 files changed

Lines changed: 60 additions & 23 deletions

File tree

monitor/hook/sender.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/creativeprojects/clog"
2121
"github.com/creativeprojects/resticprofile/config"
2222
"github.com/creativeprojects/resticprofile/constants"
23+
"github.com/creativeprojects/resticprofile/util"
2324
"github.com/creativeprojects/resticprofile/util/templates"
2425
)
2526

@@ -71,12 +72,17 @@ func NewSender(certificates []string, userAgent string, timeout time.Duration, d
7172
}
7273
}
7374

74-
func (s *Sender) Send(cfg config.SendMonitoringSection, ctx Context) error {
75+
func (s *Sender) Send(cfg config.SendMonitoringSection, ctx Context, env *util.Environment) error {
7576
if cfg.URL.Value() == "" {
7677
return errors.New("URL field is empty")
7778
}
78-
url := resolveURL(cfg.URL.Value(), ctx)
79-
publicUrl := resolveURL(cfg.URL.String(), ctx)
79+
80+
if env == nil {
81+
env = util.NewDefaultEnvironment(os.Environ()...)
82+
}
83+
84+
url := resolveURL(cfg.URL.Value(), ctx, env)
85+
publicUrl := resolveURL(cfg.URL.String(), ctx, env)
8086
method := cfg.Method
8187
if method == "" {
8288
method = http.MethodGet
@@ -94,7 +100,7 @@ func (s *Sender) Send(cfg config.SendMonitoringSection, ctx Context) error {
94100
bodyReader = bytes.NewBufferString(body)
95101
}
96102
if cfg.Body != "" {
97-
body = resolveBody(cfg.Body, ctx)
103+
body = resolveBody(cfg.Body, ctx, env)
98104
bodyReader = bytes.NewBufferString(body)
99105
}
100106

@@ -203,7 +209,7 @@ func getRootCAs(certificates []string) *x509.CertPool {
203209
return caCertPool
204210
}
205211

206-
func resolveBody(body string, ctx Context) string {
212+
func resolveBody(body string, ctx Context, env *util.Environment) string {
207213
return os.Expand(body, func(s string) string {
208214
switch s {
209215
case constants.EnvProfileName:
@@ -228,12 +234,12 @@ func resolveBody(body string, ctx Context) string {
228234
return "$" // allow to escape "$" as "$$"
229235

230236
default:
231-
return os.Getenv(s)
237+
return env.Get(s)
232238
}
233239
})
234240
}
235241

236-
func resolveURL(url string, ctx Context) string {
242+
func resolveURL(url string, ctx Context, env *util.Environment) string {
237243
return os.Expand(url, func(s string) string {
238244
switch s {
239245
case constants.EnvProfileName:
@@ -258,7 +264,7 @@ func resolveURL(url string, ctx Context) string {
258264
return "$" // allow to escape "$" as "$$"
259265

260266
default:
261-
return os.Getenv(s)
267+
return env.Get(s)
262268
}
263269
})
264270
}

monitor/hook/sender_test.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616

1717
"github.com/creativeprojects/clog"
1818
"github.com/creativeprojects/resticprofile/config"
19+
"github.com/creativeprojects/resticprofile/constants"
20+
"github.com/creativeprojects/resticprofile/util"
1921
"github.com/stretchr/testify/assert"
2022
"github.com/stretchr/testify/require"
21-
"github.com/creativeprojects/resticprofile/constants"
2223
)
2324

2425
func TestSend(t *testing.T) {
@@ -95,7 +96,7 @@ func TestSend(t *testing.T) {
9596
}
9697

9798
sender := NewSender(nil, "resticprofile_test", 10*time.Second, false)
98-
err := sender.Send(testCase.cfg, ctx)
99+
err := sender.Send(testCase.cfg, ctx, nil)
99100
assert.NoError(t, err)
100101

101102
assert.Equal(t, testCase.calls, calls)
@@ -114,7 +115,7 @@ func TestDryRun(t *testing.T) {
114115
sender := NewSender(nil, "", time.Second, true)
115116
err := sender.Send(config.SendMonitoringSection{
116117
URL: config.NewConfidentialValue(server.URL),
117-
}, Context{})
118+
}, Context{}, nil)
118119
assert.NoError(t, err)
119120

120121
assert.Equal(t, uint32(0), atomic.LoadUint32(&calls))
@@ -133,7 +134,7 @@ func TestSenderTimeout(t *testing.T) {
133134
sender := NewSender(nil, "resticprofile_test", 300*time.Millisecond, false)
134135
err := sender.Send(config.SendMonitoringSection{
135136
URL: config.NewConfidentialValue(server.URL),
136-
}, Context{})
137+
}, Context{}, nil)
137138
assert.Error(t, err)
138139

139140
assert.Equal(t, uint32(1), atomic.LoadUint32(&startedCalls))
@@ -151,15 +152,15 @@ func TestInsecureRequests(t *testing.T) {
151152
// 1: request will fail TLS
152153
err := sender.Send(config.SendMonitoringSection{
153154
URL: config.NewConfidentialValue(server.URL),
154-
}, Context{})
155+
}, Context{}, nil)
155156
assert.Error(t, err)
156157
assert.Equal(t, 0, calls)
157158

158159
// 2: request allowing bad certificate
159160
err = sender.Send(config.SendMonitoringSection{
160161
URL: config.NewConfidentialValue(server.URL),
161162
SkipTLS: true,
162-
}, Context{})
163+
}, Context{}, nil)
163164
assert.NoError(t, err)
164165
assert.Equal(t, 1, calls)
165166
}
@@ -179,7 +180,7 @@ func TestRequestWithCA(t *testing.T) {
179180
// 1: request will fail TLS
180181
err := sender.Send(config.SendMonitoringSection{
181182
URL: config.NewConfidentialValue(server.URL),
182-
}, Context{})
183+
}, Context{}, nil)
183184
assert.Error(t, err)
184185
assert.Equal(t, 0, calls)
185186

@@ -196,7 +197,7 @@ func TestRequestWithCA(t *testing.T) {
196197
sender = NewSender([]string{filename}, "resticprofile_test", 300*time.Millisecond, false)
197198
err = sender.Send(config.SendMonitoringSection{
198199
URL: config.NewConfidentialValue(server.URL),
199-
}, Context{})
200+
}, Context{}, nil)
200201
assert.NoError(t, err)
201202
assert.Equal(t, 1, calls)
202203
}
@@ -210,7 +211,7 @@ func TestFailedRequest(t *testing.T) {
210211
sender := NewSender(nil, "resticprofile_test", 300*time.Millisecond, false)
211212
err := sender.Send(config.SendMonitoringSection{
212213
URL: config.NewConfidentialValue(server.URL),
213-
}, Context{})
214+
}, Context{}, nil)
214215
assert.Error(t, err)
215216
}
216217

@@ -230,7 +231,7 @@ func TestUserAgent(t *testing.T) {
230231
Headers: []config.SendMonitoringHeader{
231232
{Name: agentHeader, Value: config.NewConfidentialValue(testAgent)},
232233
},
233-
}, Context{})
234+
}, Context{}, nil)
234235
assert.NoError(t, err)
235236
assert.Equal(t, 1, calls)
236237
}
@@ -269,7 +270,7 @@ func TestConfidentialURL(t *testing.T) {
269270
config.ProcessConfidentialValues(profile)
270271

271272
sender := NewSender(nil, "", 300*time.Millisecond, false)
272-
err := sender.Send(profile.Backup.SendBefore[0], Context{})
273+
err := sender.Send(profile.Backup.SendBefore[0], Context{}, nil)
273274
require.NoError(t, err)
274275
assert.Equal(t, 1, calls)
275276
}
@@ -320,7 +321,7 @@ func TestURLEncoding(t *testing.T) {
320321
sender := NewSender(nil, "", 300*time.Millisecond, false)
321322
err := sender.Send(config.SendMonitoringSection{
322323
URL: config.NewConfidentialValue(serverURL),
323-
}, ctx)
324+
}, ctx, nil)
324325
assert.NoError(t, err)
325326
assert.Equal(t, 1, calls)
326327
}
@@ -359,7 +360,7 @@ func TestConfidentialHeader(t *testing.T) {
359360
config.ProcessConfidentialValues(profile)
360361

361362
sender := NewSender(nil, "", 300*time.Millisecond, false)
362-
err := sender.Send(profile.Backup.SendBefore[0], Context{})
363+
err := sender.Send(profile.Backup.SendBefore[0], Context{}, nil)
363364
require.NoError(t, err)
364365
assert.Equal(t, 1, calls)
365366
}
@@ -393,7 +394,7 @@ func TestParseTemplate(t *testing.T) {
393394
URL: config.NewConfidentialValue(server.URL),
394395
Method: http.MethodPost,
395396
BodyTemplate: filename,
396-
}, ctx)
397+
}, ctx, nil)
397398
assert.NoError(t, err)
398399
}
399400

@@ -418,3 +419,33 @@ func TestResponseSanitizer(t *testing.T) {
418419
assert.Equal(t, test[1], responseContentSanitizer.ReplaceAllString(test[0], " "), "test #%d", i)
419420
}
420421
}
422+
423+
func TestCustomEnv(t *testing.T) {
424+
calls := 0
425+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
426+
buffer := bytes.Buffer{}
427+
_, err := buffer.ReadFrom(r.Body)
428+
assert.NoError(t, err)
429+
r.Body.Close()
430+
431+
assert.Equal(t, "some test value\n", buffer.String())
432+
433+
calls++
434+
}))
435+
defer server.Close()
436+
437+
t.Setenv("TEST_MONITOR_URL", "should never be read")
438+
t.Setenv("SOME_OS_ENV", "should never be read")
439+
440+
env := util.NewDefaultEnvironment()
441+
env.Put("TEST_MONITOR_URL", server.URL)
442+
env.Put("TEST_BODY_VALUE", "some test value")
443+
444+
sender := NewSender(nil, "", 300*time.Millisecond, false)
445+
err := sender.Send(config.SendMonitoringSection{
446+
URL: config.NewConfidentialValue("$TEST_MONITOR_URL"),
447+
Body: "$TEST_BODY_VALUE\n$SOME_OS_ENV",
448+
}, Context{}, env)
449+
assert.NoError(t, err)
450+
assert.Equal(t, 1, calls)
451+
}

wrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ func (r *resticWrapper) sendMonitoring(sections []config.SendMonitoringSection,
699699
for i, section := range sections {
700700
clog.Debugf("starting %q from %s %d/%d", sendType, command, i+1, len(sections))
701701
term.FlushAllOutput()
702-
err := r.sender.Send(section, r.getContextWithError(err))
702+
err := r.sender.Send(section, r.getContextWithError(err), r.profile.GetEnvironment(true))
703703
if err != nil {
704704
clog.Warningf("%q returned an error: %s", sendType, err.Error())
705705
}

0 commit comments

Comments
 (0)