-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elasticsearch): remove hard-coded port 9200, validate URL input (#…
…941) This PR enables a flexible ES endpoints by removing hard-coded ES port in the endpoints section. This PR also sanitize/validate whether user input a valid URL or not by utilizing `url.Parse`. I'm wondering whether we do need to have a test or not since `url.Parse` function already have their own [test](https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/url/url_test.go) Please advise if we need more test cases This PR will close #940 --------- Signed-off-by: clavinjune <24659468+clavinjune@users.noreply.github.com>
- Loading branch information
1 parent
ddd5d02
commit 3b72dd1
Showing
2 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
autoscaler/controllers/gateway/config/elasticsearch_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/keyval-dev/odigos/autoscaler/controllers/gateway/config" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestElasticsearch_SanitizeURL(t *testing.T) { | ||
tt := []struct { | ||
_ struct{} | ||
URL string | ||
ExpectedURL string | ||
ExpectedErr string | ||
}{ | ||
{ | ||
URL: "http://localhost:9200/", | ||
ExpectedURL: "http://localhost:9200/", | ||
ExpectedErr: "", | ||
}, | ||
{ | ||
URL: "http://localhost", | ||
ExpectedURL: "http://localhost:9200", | ||
ExpectedErr: "", | ||
}, | ||
{ | ||
URL: "http:///localhost", | ||
ExpectedURL: "", | ||
ExpectedErr: "invalid URL", | ||
}, | ||
{ | ||
URL: "localhost", | ||
ExpectedURL: "", | ||
ExpectedErr: "invalid URI for request", | ||
}, | ||
{ | ||
URL: "http://user:pass@localhost:9200", | ||
ExpectedURL: "http://user:pass@localhost:9200", | ||
ExpectedErr: "", | ||
}, | ||
{ | ||
URL: "http://user:pass@localhost:80", | ||
ExpectedURL: "http://user:pass@localhost:80", | ||
ExpectedErr: "", | ||
}, | ||
{ | ||
URL: "https://foobar.com:8443", | ||
ExpectedURL: "https://foobar.com:8443", | ||
ExpectedErr: "", | ||
}, | ||
// IPs | ||
{ | ||
URL: "127.0.0.1:8080", | ||
ExpectedURL: "", | ||
ExpectedErr: "invalid URI for request", | ||
}, | ||
{ | ||
URL: "http://127.0.0.1:8080", | ||
ExpectedURL: "http://127.0.0.1:8080", | ||
ExpectedErr: "", | ||
}, | ||
{ | ||
URL: "[::1]:8080", | ||
ExpectedURL: "", | ||
ExpectedErr: "invalid URI for request", | ||
}, | ||
{ | ||
URL: "http://[::1]:8080", | ||
ExpectedURL: "http://[::1]:8080", | ||
ExpectedErr: "", | ||
}, | ||
} | ||
|
||
var es config.Elasticsearch | ||
for i := range tt { | ||
tc := tt[i] | ||
t.Run(tc.URL, func(t *testing.T) { | ||
t.Parallel() | ||
r := require.New(t) | ||
|
||
actualURL, actualErr := es.SanitizeURL(tc.URL) | ||
if tc.ExpectedErr != "" { | ||
r.Error(actualErr) | ||
r.Contains(actualErr.Error(), tc.ExpectedErr) | ||
} else { | ||
r.NoError(actualErr) | ||
r.Equal(tc.ExpectedURL, actualURL) | ||
} | ||
|
||
}) | ||
} | ||
} |