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

querier: Add validation for querier address flags #4897

Merged
merged 4 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#4856](https://github.com/thanos-io/thanos/pull/4856) Mixin: Add Query Frontend Grafana dashboard.
- [#4874](https://github.com/thanos-io/thanos/pull/4874) Query: Add `--endpoint-strict` flag to statically configure Thanos API server endpoints. It is similar to `--store-strict` but supports passing any Thanos gRPC APIs: StoreAPI, MetadataAPI, RulesAPI, TargetsAPI and ExemplarsAPI.
- [#4868](https://github.com/thanos-io/thanos/pull/4868) Rule: Support ruleGroup limit introduced by Prometheus v2.31.0.
- [#4897](https://github.com/thanos-io/thanos/pull/4897) Querier: Add validation for querier address flags.

### Fixed

Expand Down
49 changes: 31 additions & 18 deletions cmd/thanos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,24 @@ func registerQuery(app *extkingpin.App) {
}
}

if dup := firstDuplicate(*stores); dup != "" {
return errors.Errorf("Address %s is duplicated for --store flag.", dup)
if err := validateAddrs(*endpoints); err != nil {
return errors.Wrap(err, "validating --endpoint flags")
}

if dup := firstDuplicate(*ruleEndpoints); dup != "" {
return errors.Errorf("Address %s is duplicated for --rule flag.", dup)
if err := validateAddrs(*stores); err != nil {
return errors.Wrap(err, "validating --store flags")
}

if dup := firstDuplicate(*metadataEndpoints); dup != "" {
return errors.Errorf("Address %s is duplicated for --metadata flag.", dup)
if err := validateAddrs(*ruleEndpoints); err != nil {
return errors.Wrap(err, "validating --rule flags")
}

if dup := firstDuplicate(*exemplarEndpoints); dup != "" {
return errors.Errorf("Address %s is duplicated for --exemplar flag.", dup)
if err := validateAddrs(*metadataEndpoints); err != nil {
return errors.Wrap(err, "validating --metadata flags")
}

if err := validateAddrs(*exemplarEndpoints); err != nil {
return errors.Wrap(err, "validating --exemplar flags")
}

httpLogOpts, err := logging.ParseHTTPOptions(*reqLogDecision, reqLogConfig)
Expand All @@ -217,8 +221,8 @@ func registerQuery(app *extkingpin.App) {
return errors.Wrap(err, "error while parsing config for request logging")
}

if dup := firstDuplicate(*targetEndpoints); dup != "" {
return errors.Errorf("Address %s is duplicated for --target flag.", dup)
if err := validateAddrs(*targetEndpoints); err != nil {
return errors.Wrap(err, "validating --target flags")
}

var fileSD *file.Discovery
Expand Down Expand Up @@ -733,20 +737,29 @@ func removeDuplicateEndpointSpecs(logger log.Logger, duplicatedStores prometheus
return deduplicated
}

// firstDuplicate returns the first duplicate string in the given string slice
// or empty string if none was found.
func firstDuplicate(ss []string) string {
// validateAddrs checks an address slice for duplicates and empty or invalid elements.
func validateAddrs(addrs []string) error {
set := map[string]struct{}{}

for _, s := range ss {
if _, ok := set[s]; ok {
return s
for _, addr := range addrs {
if addr == "" {
return errors.New("Address is empty.")
}

qtypeAndName := strings.SplitN(addr, "+", 2)
hostAndPort := strings.SplitN(addr, ":", 2)
if len(qtypeAndName) != 2 && len(hostAndPort) != 2 {
return errors.Errorf("Address %s is not of <host>:<port> format or a valid DNS query.", addr)
}

set[s] = struct{}{}
Copy link
Member

@GiedriusS GiedriusS Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also call https://pkg.go.dev/net#ParseIP here to check hostAndPort[0]? scratch that, it is probably tricky to check whether we were given a hostname or if we were given an IP address

if _, ok := set[addr]; ok {
return errors.Errorf("Address %s is duplicated.", addr)
}

set[addr] = struct{}{}
}

return ""
return nil
}

// engineFactory creates from 1 to 3 promql.Engines depending on
Expand Down
1 change: 0 additions & 1 deletion test/e2e/info_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func TestInfo(t *testing.T) {
sidecar1.InternalEndpoint("grpc"),
sidecar2.InternalEndpoint("grpc"),
sidecar3.InternalEndpoint("grpc"),
sidecar3.InternalEndpoint("grpc"),
store.InternalEndpoint("grpc"),
).
Build()
Expand Down