Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-pack/filebeat/input/http_endpoint: Add validation for empty URL config #36816

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ is collected by it.
- Ensure winlog input retains metric collection when handling recoverable errors. {issue}36479[36479] {pull}36483[36483]
- Revert error introduced in {pull}35734[35734] when symlinks can't be resolved in filestream. {pull}36557[36557]
- Fix ignoring external input configuration in `take_over: true` mode {issue}36378[36378] {pull}36395[36395]
- Add validation to http_endpoint config for empty URL {pull}36816[36816] {issue}36772[36772]

*Heartbeat*

Expand Down
4 changes: 4 additions & 0 deletions x-pack/filebeat/input/http_endpoint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (c *config) Validate() error {
return errors.New("crc.provider is required when crc.secret is defined")
}

if c.URL == "" {
bhapas marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("webhook path URL can not be empty")
}

return nil
}

Expand Down
60 changes: 60 additions & 0 deletions x-pack/filebeat/input/http_endpoint/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 http_endpoint

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_validateConfig(t *testing.T) {
testCases := []struct {
name string // Sub-test name.
config config // Load config parameters.
wantError error // Expected error
}{
{
name: "invalid URL",
config: config{
URL: "",
ResponseBody: `{"message": "success"}`,
Method: http.MethodPost,
},
wantError: fmt.Errorf("webhook path URL can not be empty"),
},
{
name: "invalid method",
config: config{
URL: "/",
ResponseBody: `{"message": "success"}`,
Method: "random",
},
wantError: fmt.Errorf("method must be POST, PUT or PATCH: random"),
},
{
name: "invalid URL",
config: config{
URL: "/",
ResponseBody: "",
Method: http.MethodPost,
},
wantError: fmt.Errorf("response_body must be valid JSON"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.config.URL = ""
bhapas marked this conversation as resolved.
Show resolved Hide resolved
// Execute config validation
err := tc.config.Validate()

// Validate responses
assert.Equal(t, tc.wantError, err)
})
}
}