diff --git a/config/http_config.go b/config/http_config.go index 689ea627..88457375 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -325,7 +325,7 @@ func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) { if err != nil { return nil, nil, err } - cfg.SetDirectory(filepath.Dir(filepath.Dir(filename))) + cfg.SetDirectory(filepath.Dir(filename)) return cfg, content, nil } diff --git a/config/http_config_test.go b/config/http_config_test.go index 708e993b..4d61ae12 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -2720,3 +2720,32 @@ func TestMultipleHeaders(t *testing.T) { _, err = client.Get(ts.URL) require.NoErrorf(t, err, "can't fetch URL: %v", err) } + +// TestLoadHTTPConfigFileResolvesPathsRelativeToConfigFile ensures that relative +// file paths inside a config loaded with LoadHTTPConfigFile are resolved +// relative to the directory containing the config file, as documented by the +// SetDirectory contract. +func TestLoadHTTPConfigFileResolvesPathsRelativeToConfigFile(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, "header-value", r.Header.Get("X-Test")) + w.WriteHeader(http.StatusNoContent) + })) + t.Cleanup(ts.Close) + + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "header-value"), []byte("header-value\n"), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "http.yml"), []byte(`http_headers: + X-Test: + files: [header-value] +`), 0o644)) + + cfg, _, err := LoadHTTPConfigFile(filepath.Join(dir, "http.yml")) + require.NoErrorf(t, err, "Error loading HTTP client config: %v", err) + require.Equal(t, filepath.Join(dir, "header-value"), cfg.HTTPHeaders.Headers["X-Test"].Files[0]) + + client, err := NewClientFromConfig(*cfg, "test") + require.NoErrorf(t, err, "Error creating HTTP Client: %v", err) + + _, err = client.Get(ts.URL) + require.NoErrorf(t, err, "can't fetch URL: %v", err) +} diff --git a/config/testdata/http.conf.basic-auth.bad-username.yaml b/config/testdata/http.conf.basic-auth.bad-username.yaml index bfb911b8..7b2f74ea 100644 --- a/config/testdata/http.conf.basic-auth.bad-username.yaml +++ b/config/testdata/http.conf.basic-auth.bad-username.yaml @@ -1,4 +1,4 @@ basic_auth: username: user - username_file: testdata/basic-auth-username + username_file: basic-auth-username password: foo \ No newline at end of file diff --git a/config/testdata/http.conf.basic-auth.good.yaml b/config/testdata/http.conf.basic-auth.good.yaml index 5e7aa41f..8b96a413 100644 --- a/config/testdata/http.conf.basic-auth.good.yaml +++ b/config/testdata/http.conf.basic-auth.good.yaml @@ -1,3 +1,3 @@ basic_auth: username: user - password_file: testdata/basic-auth-password + password_file: basic-auth-password diff --git a/config/testdata/http.conf.basic-auth.too-much.bad.yaml b/config/testdata/http.conf.basic-auth.too-much.bad.yaml index 7cfb9220..f282bf7c 100644 --- a/config/testdata/http.conf.basic-auth.too-much.bad.yaml +++ b/config/testdata/http.conf.basic-auth.too-much.bad.yaml @@ -1,4 +1,4 @@ basic_auth: username: user password: foo - password_file: testdata/basic-auth-password + password_file: basic-auth-password diff --git a/config/testdata/http.conf.basic-auth.username-file.good.yaml b/config/testdata/http.conf.basic-auth.username-file.good.yaml index 24c3c3cb..102dbe77 100644 --- a/config/testdata/http.conf.basic-auth.username-file.good.yaml +++ b/config/testdata/http.conf.basic-auth.username-file.good.yaml @@ -1,3 +1,3 @@ basic_auth: - username_file: testdata/basic-auth-username - password_file: testdata/basic-auth-password \ No newline at end of file + username_file: basic-auth-username + password_file: basic-auth-password \ No newline at end of file diff --git a/config/testdata/http.conf.headers-multiple.good.yaml b/config/testdata/http.conf.headers-multiple.good.yaml index f9dcdc05..b8e6d7e0 100644 --- a/config/testdata/http.conf.headers-multiple.good.yaml +++ b/config/testdata/http.conf.headers-multiple.good.yaml @@ -5,4 +5,4 @@ http_headers: values: [value2a] secrets: [value2b, value2c] three: - files: [testdata/headers-file-a, testdata/headers-file-b, testdata/headers-file-c] + files: [headers-file-a, headers-file-b, headers-file-c] diff --git a/config/testdata/http.conf.headers.good.yaml b/config/testdata/http.conf.headers.good.yaml index f8a02364..5b46cf7f 100644 --- a/config/testdata/http.conf.headers.good.yaml +++ b/config/testdata/http.conf.headers.good.yaml @@ -4,4 +4,4 @@ http_headers: two: secrets: [value2] three: - files: [testdata/headers-file] + files: [headers-file]