Skip to content

Commit 1d63dde

Browse files
authored
Fixes http.Verifier so it uses its http client (#4270)
also add test with proxy for the Verifier.
1 parent 82efe13 commit 1d63dde

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Fixes an issue where the Elastic Agent did not utilize the download settings when downloading the artifact signature file.
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; a word indicating the component this changeset affects.
22+
component:
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
#pr: https://github.com/owner/repo/1234
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/4237

internal/pkg/agent/application/upgrade/artifact/download/http/verifier.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ func (v *Verifier) getPublicAsc(sourceURI string) ([]byte, error) {
171171
return nil, errors.New(err, "failed create request for loading public key", errors.TypeNetwork, errors.M(errors.MetaKeyURI, sourceURI))
172172
}
173173

174-
// TODO: receive a http.Client
175-
resp, err := http.DefaultClient.Do(req)
174+
resp, err := v.client.Do(req)
176175
if err != nil {
177176
return nil, errors.New(err, "failed loading public key", errors.TypeNetwork, errors.M(errors.MetaKeyURI, sourceURI))
178177
}

internal/pkg/agent/application/upgrade/artifact/download/http/verifier_test.go

+53-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"fmt"
1010
"math/rand"
1111
"net/http"
12+
"net/http/httptest"
13+
"net/url"
1214
"os"
1315
"testing"
1416
"time"
@@ -19,42 +21,76 @@ import (
1921
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
2022
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
2123
"github.com/elastic/elastic-agent/pkg/core/logger"
24+
"github.com/elastic/elastic-agent/testing/proxytest"
2225
)
2326

2427
func TestVerify(t *testing.T) {
2528
targetDir := t.TempDir()
2629

2730
log, _ := logger.New("", false)
2831
timeout := 30 * time.Second
29-
testCases := getRandomTestCases()
32+
testCases := getRandomTestCases()[0:1]
3033
server, pub := getElasticCoServer(t)
31-
elasticClient := getElasticCoClient(server)
32-
// artifact/download/http.Verifier uses http.DefaultClient, thus we need to
33-
// change it.
34-
http.DefaultClient = &elasticClient
3534

3635
config := &artifact.Config{
37-
SourceURI: source,
36+
SourceURI: server.URL + "/downloads",
3837
TargetDirectory: targetDir,
3938
HTTPTransportSettings: httpcommon.HTTPTransportSettings{
4039
Timeout: timeout,
4140
},
4241
}
4342

44-
for _, testCase := range testCases {
45-
testName := fmt.Sprintf("%s-binary-%s", testCase.system, testCase.arch)
43+
t.Run("without proxy", func(t *testing.T) {
44+
runTests(t, testCases, config, log, pub)
45+
})
46+
47+
t.Run("with proxy", func(t *testing.T) {
48+
brokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49+
w.WriteHeader(http.StatusTeapot)
50+
t.Log("[brokenServer] wrong server, is the proxy working?")
51+
_, _ = w.Write([]byte(`wrong server, is the proxy working?`))
52+
}))
53+
serverURL, err := url.Parse(server.URL)
54+
require.NoError(t, err, "could not parse server URL \"%s\"",
55+
server.URL)
56+
57+
proxy := proxytest.New(t,
58+
proxytest.WithRewriteFn(func(u *url.URL) {
59+
u.Host = serverURL.Host
60+
}),
61+
proxytest.WithRequestLog("proxy", func(_ string, _ ...any) {}))
62+
63+
proxyURL, err := url.Parse(proxy.LocalhostURL)
64+
require.NoError(t, err, "could not parse server URL \"%s\"",
65+
server.URL)
66+
67+
config := *config
68+
config.SourceURI = brokenServer.URL + "/downloads"
69+
config.Proxy = httpcommon.HTTPClientProxySettings{
70+
URL: (*httpcommon.ProxyURI)(proxyURL),
71+
}
72+
73+
runTests(t, testCases, &config, log, pub)
74+
})
75+
}
76+
77+
func runTests(t *testing.T, testCases []testCase, config *artifact.Config, log *logger.Logger, pub []byte) {
78+
for _, tc := range testCases {
79+
testName := fmt.Sprintf("%s-binary-%s", tc.system, tc.arch)
4680
t.Run(testName, func(t *testing.T) {
47-
config.OperatingSystem = testCase.system
48-
config.Architecture = testCase.arch
81+
config.OperatingSystem = tc.system
82+
config.Architecture = tc.arch
4983

50-
upgradeDetails := details.NewDetails("8.12.0", details.StateRequested, "")
51-
testClient := NewDownloaderWithClient(log, config, elasticClient, upgradeDetails)
52-
artifact, err := testClient.Download(context.Background(), beatSpec, version)
53-
if err != nil {
54-
t.Fatal(err)
55-
}
84+
upgradeDetails := details.NewDetails(
85+
"8.12.0", details.StateRequested, "")
86+
downloader, err := NewDownloader(log, config, upgradeDetails)
87+
require.NoError(t, err, "could not create new downloader")
88+
89+
pkgPath, err := downloader.Download(context.Background(), beatSpec, version)
90+
require.NoErrorf(t, err, "failed downloading %s v%s",
91+
beatSpec.Artifact, version)
5692

57-
_, err = os.Stat(artifact)
93+
_, err = os.Stat(pkgPath)
5894
if err != nil {
5995
t.Fatal(err)
6096
}

0 commit comments

Comments
 (0)