Skip to content

Commit

Permalink
Fix erroneously created http url directory path for monitoring (#1811)
Browse files Browse the repository at this point in the history
* Fix erroneously created http url directory path for monitoring

* Add changelog
  • Loading branch information
aleksmaus authored Nov 29, 2022
1 parent c7790ee commit 4ca690a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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: bug-fix

# Change summary; a 80ish characters long description of the change.
summary: fix erroneous http url directory created for monitoring

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
#description:

# Affected component; a word indicating the component this changeset affects.
component:

# PR number; 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: 1811

# Issue number; 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: 1234
8 changes: 7 additions & 1 deletion internal/pkg/agent/application/monitoring/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package monitoring

import (
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -75,7 +76,7 @@ func exposeMetricsEndpoint(
}

func createAgentMonitoringDrop(drop string) error {
if drop == "" || runtime.GOOS == "windows" {
if drop == "" || runtime.GOOS == "windows" || isHttpUrl(drop) {
return nil
}

Expand All @@ -98,3 +99,8 @@ func createAgentMonitoringDrop(drop string) error {

return os.Chown(path, os.Geteuid(), os.Getegid())
}

func isHttpUrl(s string) bool {
u, err := url.Parse(strings.TrimSpace(s))
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}
65 changes: 65 additions & 0 deletions internal/pkg/agent/application/monitoring/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package monitoring

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestIsHTTPUrl(t *testing.T) {

tests := []struct {
name string
s string
res bool
}{
{
name: "empty",
},
{
name: "/",
s: "/",
},
{
name: "relative",
s: "foo/bar",
},
{
name: "absolute",
s: "/foo/bar",
},
{
name: "file",
s: "file://foo/bar",
},
{
name: "http",
s: "http://localhost:5691",
res: true,
},
{
name: "https",
s: "https://localhost:5691",
res: true,
},
{
name: "http space prefix",
s: " http://localhost:5691",
res: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
res := isHttpUrl(tc.s)
diff := cmp.Diff(tc.res, res)
if diff != "" {
t.Error(diff)
}
})
}
}

0 comments on commit 4ca690a

Please sign in to comment.