From bbee90a291f150edbb0e05cf3c93fc6620871fb4 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 1 Oct 2024 04:33:37 +0200 Subject: [PATCH 1/4] refactor: remove unused replace directives (#5590) goja is not used in elastic agent see go mod graph | grep groja --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 4ab8d20563d..65afb808bd0 100644 --- a/go.mod +++ b/go.mod @@ -453,8 +453,6 @@ require ( replace ( github.com/Shopify/sarama => github.com/elastic/sarama v1.19.1-0.20220310193331-ebc2b0d8eef3 - github.com/dop251/goja => github.com/andrewkroh/goja v0.0.0-20190128172624-dd2ac4456e20 - github.com/dop251/goja_nodejs => github.com/dop251/goja_nodejs v0.0.0-20171011081505-adff31b136e6 // openshift removed all tags from their repo, use the pseudoversion from the release-3.9 branch HEAD // See https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/12d41f40b0d408b0167633d8095160d3343d46ac/go.mod#L38 github.com/openshift/api v3.9.0+incompatible => github.com/openshift/api v0.0.0-20180801171038-322a19404e37 From d0c7beab00a3a863154b01953993ba5f41ffaad9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 03:01:46 +0000 Subject: [PATCH 2/4] build(deps): bump github.com/elastic/go-sysinfo from 1.14.1 to 1.14.2 (#5603) * build(deps): bump github.com/elastic/go-sysinfo from 1.14.1 to 1.14.2 Bumps [github.com/elastic/go-sysinfo](https://github.com/elastic/go-sysinfo) from 1.14.1 to 1.14.2. - [Release notes](https://github.com/elastic/go-sysinfo/releases) - [Commits](https://github.com/elastic/go-sysinfo/compare/v1.14.1...v1.14.2) --- updated-dependencies: - dependency-name: github.com/elastic/go-sysinfo dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] * Update NOTICE.txt --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] --- NOTICE.txt | 8 ++++---- go.mod | 4 ++-- go.sum | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/NOTICE.txt b/NOTICE.txt index f2a510280ab..876c417755d 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -2320,11 +2320,11 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-licenser@v0. -------------------------------------------------------------------------------- Dependency : github.com/elastic/go-sysinfo -Version: v1.14.1 +Version: v1.14.2 Licence type (autodetected): Apache-2.0 -------------------------------------------------------------------------------- -Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.14.1/LICENSE.txt: +Contents of probable licence file $GOMODCACHE/github.com/elastic/go-sysinfo@v1.14.2/LICENSE.txt: Apache License @@ -15131,11 +15131,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- Dependency : golang.org/x/sys -Version: v0.24.0 +Version: v0.25.0 Licence type (autodetected): BSD-3-Clause -------------------------------------------------------------------------------- -Contents of probable licence file $GOMODCACHE/golang.org/x/sys@v0.24.0/LICENSE: +Contents of probable licence file $GOMODCACHE/golang.org/x/sys@v0.25.0/LICENSE: Copyright 2009 The Go Authors. diff --git a/go.mod b/go.mod index 65afb808bd0..b683324f1b9 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/elastic/elastic-transport-go/v8 v8.6.0 github.com/elastic/go-elasticsearch/v8 v8.15.0 github.com/elastic/go-licenser v0.4.2 - github.com/elastic/go-sysinfo v1.14.1 + github.com/elastic/go-sysinfo v1.14.2 github.com/elastic/go-ucfg v0.8.8 github.com/elastic/mock-es v0.0.0-20240712014503-e5b47ece0015 github.com/elastic/opentelemetry-collector-components/processor/elasticinframetricsprocessor v0.11.0 @@ -64,7 +64,7 @@ require ( golang.org/x/crypto v0.26.0 golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 golang.org/x/sync v0.8.0 - golang.org/x/sys v0.24.0 + golang.org/x/sys v0.25.0 golang.org/x/term v0.23.0 golang.org/x/text v0.17.0 golang.org/x/time v0.5.0 diff --git a/go.sum b/go.sum index 6e0cf045b9e..d2943b24072 100644 --- a/go.sum +++ b/go.sum @@ -259,8 +259,8 @@ github.com/elastic/go-licenser v0.4.2 h1:bPbGm8bUd8rxzSswFOqvQh1dAkKGkgAmrPxbUi+ github.com/elastic/go-licenser v0.4.2/go.mod h1:W8eH6FaZDR8fQGm+7FnVa7MxI1b/6dAqxz+zPB8nm5c= github.com/elastic/go-structform v0.0.12 h1:HXpzlAKyej8T7LobqKDThUw7BMhwV6Db24VwxNtgxCs= github.com/elastic/go-structform v0.0.12/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4= -github.com/elastic/go-sysinfo v1.14.1 h1:BpY/Utfz75oKSpsQnbAJmmlnT3gBV9WFsopBEYgjhZY= -github.com/elastic/go-sysinfo v1.14.1/go.mod h1:FKUXnZWhnYI0ueO7jhsGV3uQJ5hiz8OqM5b3oGyaRr8= +github.com/elastic/go-sysinfo v1.14.2 h1:DeIy+pVfdRsd08Nx2Xjh+dUS+jrEEI7LGc29U/BKVWo= +github.com/elastic/go-sysinfo v1.14.2/go.mod h1:jPSuTgXG+dhhh0GKIyI2Cso+w5lPJ5PvVqKlL8LV/Hk= github.com/elastic/go-ucfg v0.8.8 h1:54KIF/2zFKfl0MzsSOCGOsZ3O2bnjFQJ0nDJcLhviyk= github.com/elastic/go-ucfg v0.8.8/go.mod h1:4E8mPOLSUV9hQ7sgLEJ4bvt0KhMuDJa8joDT2QGAEKA= github.com/elastic/go-windows v1.0.2 h1:yoLLsAsV5cfg9FLhZ9EXZ2n2sQFKeDYrHenkcivY4vI= @@ -1536,8 +1536,8 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= From 218c984c7cf2612270baa170c82a2671f9fd31e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 1 Oct 2024 10:43:33 +0200 Subject: [PATCH 3/4] Allow setting ES api key via env variable in container mode (#5536) Also remove defaults for username and password set this way. --- _meta/config/elastic-agent.docker.yml.tmpl | 5 +-- ...6144746-container-default-credentials.yaml | 32 +++++++++++++++++++ .../1726145045-container-api-key.yaml | 32 +++++++++++++++++++ elastic-agent.docker.yml | 5 +-- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 changelog/fragments/1726144746-container-default-credentials.yaml create mode 100644 changelog/fragments/1726145045-container-api-key.yaml diff --git a/_meta/config/elastic-agent.docker.yml.tmpl b/_meta/config/elastic-agent.docker.yml.tmpl index 10f9c4147c8..d0c421c75b7 100644 --- a/_meta/config/elastic-agent.docker.yml.tmpl +++ b/_meta/config/elastic-agent.docker.yml.tmpl @@ -5,8 +5,9 @@ outputs: default: type: elasticsearch hosts: '${ELASTICSEARCH_HOSTS:http://elasticsearch:9200}' - username: '${ELASTICSEARCH_USERNAME:elastic}' - password: '${ELASTICSEARCH_PASSWORD:changeme}' + username: '${ELASTICSEARCH_USERNAME:}' + password: '${ELASTICSEARCH_PASSWORD:}' + api_key: '${ELASTICSEARCH_API_KEY:}' preset: balanced inputs: diff --git a/changelog/fragments/1726144746-container-default-credentials.yaml b/changelog/fragments/1726144746-container-default-credentials.yaml new file mode 100644 index 00000000000..560262122cf --- /dev/null +++ b/changelog/fragments/1726144746-container-default-credentials.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: breaking-change + +# Change summary; a 80ish characters long description of the change. +summary: Remove default credentials when running Elastic Agent in container mode + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +description: ELASTICSEARCH_USERNAME and ELASTICSEARCH_PASSWORD now need to be explicitly set when running the agent in container mode + +# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. +component: elastic-agent + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +#pr: https://github.com/owner/repo/1234 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +#issue: https://github.com/owner/repo/1234 diff --git a/changelog/fragments/1726145045-container-api-key.yaml b/changelog/fragments/1726145045-container-api-key.yaml new file mode 100644 index 00000000000..72cf4b5fc1c --- /dev/null +++ b/changelog/fragments/1726145045-container-api-key.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: enhancement + +# Change summary; a 80ish characters long description of the change. +summary: Support ELASTICSEARCH_API_KEY environment variable when running in container mode + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +#description: + +# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. +component: elastic-agent + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +#pr: https://github.com/owner/repo/1234 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +#issue: https://github.com/owner/repo/1234 diff --git a/elastic-agent.docker.yml b/elastic-agent.docker.yml index a0a6d5da8f6..d9089358600 100644 --- a/elastic-agent.docker.yml +++ b/elastic-agent.docker.yml @@ -5,8 +5,9 @@ outputs: default: type: elasticsearch hosts: '${ELASTICSEARCH_HOSTS:http://elasticsearch:9200}' - username: '${ELASTICSEARCH_USERNAME:elastic}' - password: '${ELASTICSEARCH_PASSWORD:changeme}' + username: '${ELASTICSEARCH_USERNAME:}' + password: '${ELASTICSEARCH_PASSWORD:}' + api_key: '${ELASTICSEARCH_API_KEY:}' preset: balanced inputs: From a9d54fb0c48005bf6055c6af7e855e3575fd8abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 1 Oct 2024 11:04:00 +0200 Subject: [PATCH 4/4] Fix linter errors (#5442) * Remove unused symbols * Fix bodycloser linter errors * Fix errcheck linter errors * Fix staticcheck linter errors * Use default configuration for nakedret linter * Fix various linter errors --- .golangci.yml | 4 --- dev-tools/cmd/buildfleetcfg/buildfleetcfg.go | 13 ++++++-- dev-tools/licenses/license_generate.go | 12 ++++++-- dev-tools/mage/check.go | 4 +-- dev-tools/mage/gotool/go.go | 4 --- dev-tools/mage/kubernetes/kubectl.go | 20 ------------- dev-tools/mage/target/common/package.go | 6 +++- .../monitoring/processes_cloud_test.go | 2 ++ .../artifact/download/headers_rtt_test.go | 2 +- internal/pkg/agent/errors/error_test.go | 12 +++++--- .../pkg/fleetapi/uploader/uploader_test.go | 12 ++++++++ pkg/component/fake/component/comp/dialer.go | 26 ---------------- .../fake/component/comp/dialer_windows.go | 26 ---------------- pkg/component/runtime/service_command.go | 30 +++++++++---------- pkg/control/v1/client/dial.go | 2 +- pkg/control/v1/client/dial_windows.go | 2 +- pkg/control/v2/client/dial.go | 2 +- pkg/control/v2/client/dial_windows.go | 2 +- testing/upgradetest/upgrader.go | 2 +- 19 files changed, 68 insertions(+), 115 deletions(-) delete mode 100644 pkg/component/fake/component/comp/dialer.go delete mode 100644 pkg/component/fake/component/comp/dialer_windows.go diff --git a/.golangci.yml b/.golangci.yml index 399b5f7e496..4c0c3bf0aff 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -124,10 +124,6 @@ linters-settings: # Select the Go version to target. The default is '1.13'. go: "1.22.7" - nakedret: - # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 - max-func-lines: 0 - nolintlint: # Enable to ensure that nolint directives are all used. Default is true. allow-unused: false diff --git a/dev-tools/cmd/buildfleetcfg/buildfleetcfg.go b/dev-tools/cmd/buildfleetcfg/buildfleetcfg.go index 9c4e294807d..612142382b1 100644 --- a/dev-tools/cmd/buildfleetcfg/buildfleetcfg.go +++ b/dev-tools/cmd/buildfleetcfg/buildfleetcfg.go @@ -83,7 +83,11 @@ func main() { return } - os.WriteFile(output, data, 0640) + err = os.WriteFile(output, data, 0640) + if err != nil { + fmt.Fprintf(os.Stderr, "Error while writing the file, err: %+v\n", err) + } + return } @@ -94,11 +98,11 @@ func gen(path string, l string) ([]byte, error) { } if len(files) > 1 { - return nil, fmt.Errorf("Can only embed a single configuration file") + return nil, fmt.Errorf("can only embed a single configuration file") } var buf bytes.Buffer - tmpl.Execute(&buf, struct { + err = tmpl.Execute(&buf, struct { Pack string Files []string License string @@ -107,6 +111,9 @@ func gen(path string, l string) ([]byte, error) { Files: files, License: l, }) + if err != nil { + return nil, err + } return format.Source(buf.Bytes()) } diff --git a/dev-tools/licenses/license_generate.go b/dev-tools/licenses/license_generate.go index 6a6a32c9177..5f063e51614 100644 --- a/dev-tools/licenses/license_generate.go +++ b/dev-tools/licenses/license_generate.go @@ -58,9 +58,12 @@ func main() { Headers["Elasticv2"] = string(content) var buf bytes.Buffer - Template.Execute(&buf, data{ + err = Template.Execute(&buf, data{ Licenses: Headers, }) + if err != nil { + panic(err) + } bs, err := format.Source(buf.Bytes()) if err != nil { @@ -68,8 +71,11 @@ func main() { } if output == "-" { - os.Stdout.Write(bs) + _, err = os.Stdout.Write(bs) } else { - os.WriteFile(output, bs, 0640) + err = os.WriteFile(output, bs, 0640) + } + if err != nil { + panic(err) } } diff --git a/dev-tools/mage/check.go b/dev-tools/mage/check.go index 3c95fe7cbb5..db93278ddc5 100644 --- a/dev-tools/mage/check.go +++ b/dev-tools/mage/check.go @@ -24,7 +24,7 @@ import ( // It checks the file permissions of python test cases and YAML files. // It checks .go source files using 'go vet'. func Check() error { - fmt.Println(">> check: Checking source code for common problems") //nolint:forbidigo // it's ok to use fmt.println in mage + fmt.Println(">> check: Checking source code for common problems") mg.Deps(GoVet, CheckYAMLNotExecutable, devtools.CheckNoChanges) @@ -69,7 +69,7 @@ func GoVet() error { // CheckLicenseHeaders checks license headers in .go files. func CheckLicenseHeaders() error { - fmt.Println(">> fmt - go-licenser: Checking for missing headers") //nolint:forbidigo // it's ok to use fmt.println in mage + fmt.Println(">> fmt - go-licenser: Checking for missing headers") mg.Deps(InstallGoLicenser) licenser := gotool.Licenser diff --git a/dev-tools/mage/gotool/go.go b/dev-tools/mage/gotool/go.go index 095226976f9..1f9448414a6 100644 --- a/dev-tools/mage/gotool/go.go +++ b/dev-tools/mage/gotool/go.go @@ -210,10 +210,6 @@ func runVGo(cmd string, args *Args) error { }, cmd, args) } -func runGo(cmd string, args *Args) error { - return execGoWith(sh.RunWith, cmd, args) -} - func execGoWith( fn func(map[string]string, string, ...string) error, cmd string, args *Args, diff --git a/dev-tools/mage/kubernetes/kubectl.go b/dev-tools/mage/kubernetes/kubectl.go index 84cc4371efa..b96c81ea165 100644 --- a/dev-tools/mage/kubernetes/kubectl.go +++ b/dev-tools/mage/kubernetes/kubectl.go @@ -96,23 +96,3 @@ func kubectlIn(env map[string]string, stdout, stderr io.Writer, input string, ar return c.Run() } - -func kubectlStart(env map[string]string, stdout, stderr io.Writer, args ...string) (*exec.Cmd, error) { - c := exec.Command("kubectl", args...) - c.Env = os.Environ() - for k, v := range env { - c.Env = append(c.Env, k+"="+v) - } - c.Stdout = stdout - c.Stderr = stderr - c.Stdin = nil - - if mg.Verbose() { - fmt.Println("exec:", "kubectl", strings.Join(args, " ")) - } - - if err := c.Start(); err != nil { - return nil, err - } - return c, nil -} diff --git a/dev-tools/mage/target/common/package.go b/dev-tools/mage/target/common/package.go index ca245e79635..c2237a6de0c 100644 --- a/dev-tools/mage/target/common/package.go +++ b/dev-tools/mage/target/common/package.go @@ -48,7 +48,11 @@ func PackageSystemTests() error { parent := filepath.Dir(targetFile) if !fileExists(parent) { fmt.Printf(">> creating parent dir: %s", parent) - os.Mkdir(parent, 0750) + err = os.Mkdir(parent, 0750) + if err != nil { + fmt.Printf(">> %s", err) + return err + } } err = devtools.Tar(systemTestsDir, targetFile) diff --git a/internal/pkg/agent/application/monitoring/processes_cloud_test.go b/internal/pkg/agent/application/monitoring/processes_cloud_test.go index 7f83c755ba1..f4d6bc904e3 100644 --- a/internal/pkg/agent/application/monitoring/processes_cloud_test.go +++ b/internal/pkg/agent/application/monitoring/processes_cloud_test.go @@ -90,6 +90,7 @@ func TestExpectedCloudProcessID(t *testing.T) { } for _, tc := range testcases { + tc := tc // make a copy to avoid implicit memory aliasing t.Run(tc.name, func(t *testing.T) { assert.Equal(t, tc.id, expectedCloudProcessID(&tc.component)) }) @@ -142,6 +143,7 @@ func TestMatchesCloudProcessID(t *testing.T) { } for _, tc := range testcases { + tc := tc // make a copy to avoid implicit memory aliasing t.Run(tc.name, func(t *testing.T) { assert.Equal(t, tc.matches, matchesCloudProcessID(&tc.component, tc.processID)) }) diff --git a/internal/pkg/agent/application/upgrade/artifact/download/headers_rtt_test.go b/internal/pkg/agent/application/upgrade/artifact/download/headers_rtt_test.go index 440b727fe35..faf80276eef 100644 --- a/internal/pkg/agent/application/upgrade/artifact/download/headers_rtt_test.go +++ b/internal/pkg/agent/application/upgrade/artifact/download/headers_rtt_test.go @@ -29,7 +29,7 @@ func TestAddingHeaders(t *testing.T) { rtt := WithHeaders(c.Transport, Headers) c.Transport = rtt - resp, err := c.Get(server.URL) + resp, err := c.Get(server.URL) //nolint:noctx // this is fine in tests require.NoError(t, err) b, err := io.ReadAll(resp.Body) defer resp.Body.Close() diff --git a/internal/pkg/agent/errors/error_test.go b/internal/pkg/agent/errors/error_test.go index d45695f268a..e85e197ff8c 100644 --- a/internal/pkg/agent/errors/error_test.go +++ b/internal/pkg/agent/errors/error_test.go @@ -64,7 +64,8 @@ func TestErrorsWrap(t *testing.T) { ew := fmt.Errorf("wrapper: %w", ce) outer := New(ew) - outerCustom, ok := outer.(Error) + var outerCustom Error + ok := errors.As(outer, &outerCustom) if !ok { t.Error("expected Error") return @@ -109,7 +110,8 @@ func TestErrors(t *testing.T) { for _, tc := range cases { actualErr := New(tc.args...) - agentErr, ok := actualErr.(Error) + var agentErr Error + ok := errors.As(actualErr, &agentErr) if !ok { t.Errorf("[%s] expected Error", tc.id) continue @@ -151,7 +153,8 @@ func TestMetaFold(t *testing.T) { err3 := New("level3", err2, M("key1", "level3"), M("key3", "level3")) err4 := New("level4", err3) - resultingErr, ok := err4.(Error) + var resultingErr Error + ok := errors.As(err4, &resultingErr) if !ok { t.Fatal("error is not Error") } @@ -186,7 +189,8 @@ func TestMetaCallDoesNotModifyCollection(t *testing.T) { err3 := New("level3", err2, M("key1", "level3"), M("key3", "level3")) err4 := New("level4", err3) - resultingErr, ok := err4.(agentError) + var resultingErr agentError + ok := errors.As(err4, &resultingErr) if !ok { t.Fatal("error is not Error") } diff --git a/internal/pkg/fleetapi/uploader/uploader_test.go b/internal/pkg/fleetapi/uploader/uploader_test.go index 97dd2d6bfc9..11b4974da8e 100644 --- a/internal/pkg/fleetapi/uploader/uploader_test.go +++ b/internal/pkg/fleetapi/uploader/uploader_test.go @@ -135,6 +135,12 @@ func Test_retrySender_Send(t *testing.T) { assert.Equal(t, tc.err, err) assert.Equal(t, tc.status, resp.StatusCode) } + defer func() { + if resp != nil && resp.Body != nil { + err := resp.Body.Close() + assert.NoError(t, err) + } + }() sender.AssertExpectations(t) }) } @@ -166,6 +172,12 @@ func Test_retrySender_bodyValidation(t *testing.T) { wait: backoff, } resp, err := c.Send(context.Background(), "POST", "/", nil, nil, bytes.NewReader([]byte("abcd"))) + defer func() { + if resp != nil && resp.Body != nil { + err := resp.Body.Close() + assert.NoError(t, err) + } + }() require.NoError(t, err) assert.Equal(t, resp.StatusCode, 200) assert.Equal(t, []byte("abcd"), body1) diff --git a/pkg/component/fake/component/comp/dialer.go b/pkg/component/fake/component/comp/dialer.go deleted file mode 100644 index 9ea9b8e76c6..00000000000 --- a/pkg/component/fake/component/comp/dialer.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License 2.0; -// you may not use this file except in compliance with the Elastic License 2.0. - -//go:build !windows - -package comp - -import ( - "context" - "crypto/x509" - "net" - "strings" - - "google.golang.org/grpc" - "google.golang.org/grpc/credentials" -) - -func dialContext(ctx context.Context, addr string, cp *x509.CertPool, serverName string) (*grpc.ClientConn, error) { - return grpc.DialContext(ctx, strings.TrimPrefix(addr, "unix://"), grpc.WithContextDialer(dialer), grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(cp, serverName))) -} - -func dialer(ctx context.Context, addr string) (net.Conn, error) { - var d net.Dialer - return d.DialContext(ctx, "unix", addr) -} diff --git a/pkg/component/fake/component/comp/dialer_windows.go b/pkg/component/fake/component/comp/dialer_windows.go deleted file mode 100644 index 56a2e269378..00000000000 --- a/pkg/component/fake/component/comp/dialer_windows.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License 2.0; -// you may not use this file except in compliance with the Elastic License 2.0. - -//go:build windows - -package comp - -import ( - "context" - "crypto/x509" - "net" - - "google.golang.org/grpc" - "google.golang.org/grpc/credentials" - - "github.com/elastic/elastic-agent-libs/api/npipe" -) - -func dialContext(ctx context.Context, addr string, cp *x509.CertPool, serverName string) (*grpc.ClientConn, error) { - return grpc.DialContext(ctx, npipe.TransformString(addr), grpc.WithContextDialer(dialer), grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(cp, serverName))) -} - -func dialer(ctx context.Context, addr string) (net.Conn, error) { - return npipe.DialContext(npipe.TransformString(addr))(ctx, "", "") -} diff --git a/pkg/component/runtime/service_command.go b/pkg/component/runtime/service_command.go index 5009c755e53..702be287855 100644 --- a/pkg/component/runtime/service_command.go +++ b/pkg/component/runtime/service_command.go @@ -55,24 +55,22 @@ func executeCommand(ctx context.Context, log *logger.Logger, binaryPath string, // channel for the last error message from the stderr output errch := make(chan string, 1) ctxStderr := contextio.NewReader(ctx, proc.Stderr) - if ctxStderr != nil { - go func() { - var errText string - scanner := bufio.NewScanner(ctxStderr) - for scanner.Scan() { - line := scanner.Text() - if len(line) > 0 { - txt := strings.TrimSpace(line) - if len(txt) > 0 { - errText = txt - // Log error output line - log.Error(errText) - } + go func() { + var errText string + scanner := bufio.NewScanner(ctxStderr) + for scanner.Scan() { + line := scanner.Text() + if len(line) > 0 { + txt := strings.TrimSpace(line) + if len(txt) > 0 { + errText = txt + // Log error output line + log.Error(errText) } } - errch <- errText - }() - } + } + errch <- errText + }() procState := <-proc.Wait() if errors.Is(ctx.Err(), context.DeadlineExceeded) { diff --git a/pkg/control/v1/client/dial.go b/pkg/control/v1/client/dial.go index 92c871559a8..31db92bd713 100644 --- a/pkg/control/v1/client/dial.go +++ b/pkg/control/v1/client/dial.go @@ -18,7 +18,7 @@ import ( ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return grpc.DialContext( + return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking ctx, strings.TrimPrefix(control.Address(), "unix://"), grpc.WithTransportCredentials(insecure.NewCredentials()), diff --git a/pkg/control/v1/client/dial_windows.go b/pkg/control/v1/client/dial_windows.go index 824578bed70..6cb76ac8637 100644 --- a/pkg/control/v1/client/dial_windows.go +++ b/pkg/control/v1/client/dial_windows.go @@ -19,7 +19,7 @@ import ( ) func dialContext(ctx context.Context) (*grpc.ClientConn, error) { - return grpc.DialContext( + return grpc.DialContext( //nolint:staticcheck // Only the deprecated version allows this call to be blocking ctx, control.Address(), grpc.WithTransportCredentials(insecure.NewCredentials()), diff --git a/pkg/control/v2/client/dial.go b/pkg/control/v2/client/dial.go index 49c022a4ce8..7647a4c9cee 100644 --- a/pkg/control/v2/client/dial.go +++ b/pkg/control/v2/client/dial.go @@ -21,7 +21,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return grpc.DialContext(ctx, address, opts...) + return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking } func dialer(ctx context.Context, addr string) (net.Conn, error) { diff --git a/pkg/control/v2/client/dial_windows.go b/pkg/control/v2/client/dial_windows.go index 79d58af4faf..0ab339e1fdc 100644 --- a/pkg/control/v2/client/dial_windows.go +++ b/pkg/control/v2/client/dial_windows.go @@ -23,7 +23,7 @@ func dialContext(ctx context.Context, address string, maxMsgSize int, opts ...gr grpc.WithContextDialer(dialer), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)), ) - return grpc.DialContext(ctx, address, opts...) + return grpc.DialContext(ctx, address, opts...) //nolint:staticcheck // Only the deprecated version allows this call to be blocking } func dialer(ctx context.Context, addr string) (net.Conn, error) { diff --git a/testing/upgradetest/upgrader.go b/testing/upgradetest/upgrader.go index 07ed91856a1..7094e49fda5 100644 --- a/testing/upgradetest/upgrader.go +++ b/testing/upgradetest/upgrader.go @@ -516,7 +516,7 @@ func WaitHealthyAndVersion(ctx context.Context, f *atesting.Fixture, versionInfo // If we're in an upgrade process, the versions might not match // so we wait to see if we get to a stable version if errors.Is(err, ErrVerMismatch) { - logger.Logf("version mismatch, ignoring it, time until timeout: %s", deadline.Sub(time.Now())) + logger.Logf("version mismatch, ignoring it, time until timeout: %s", time.Until(deadline)) continue } if err == nil {