Skip to content

Commit

Permalink
check if every value is non-empty
Browse files Browse the repository at this point in the history
Signed-off-by: Kirchen99 <latias_latios@126.com>
  • Loading branch information
Kirchen99 committed Jun 17, 2022
1 parent 6c5fa58 commit 5fbd088
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
17 changes: 15 additions & 2 deletions injectproxy/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,26 @@ func NewRoutes(upstream *url.URL, label string, opts ...Option) (*routes, error)

func (r *routes) enforceLabel(h http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
err := req.ParseForm()
if err != nil {
prometheusAPIError(w, fmt.Sprintf("Bad request. The query parameter can not be parsed. %s", err.Error()), http.StatusBadRequest)
return
}

lvalues, present := req.Form[r.label]
if !present || len(lvalues) == 0 || lvalues[0] == "" {
if !present || len(lvalues) == 0 {
prometheusAPIError(w, fmt.Sprintf("Bad request. The %q query parameter must be provided.", r.label), http.StatusBadRequest)
return
}

// Check if every value is non-empty
for _, lvalue := range lvalues {
if lvalue == "" {
prometheusAPIError(w, fmt.Sprintf("Bad request. The %q query parameter must be provided.", r.label), http.StatusBadRequest)
return
}
}

lvalue := strings.Join(lvalues, "|")
req = req.WithContext(withLabelValue(req.Context(), lvalue))

Expand Down
30 changes: 23 additions & 7 deletions injectproxy/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,13 @@ func TestSeriesWithPost(t *testing.T) {

func TestQuery(t *testing.T) {
for _, tc := range []struct {
name string
labelv string
secondLabelv string
promQuery string
promQueryBody string
method string
name string
labelv string
secondLabelv string
forceAddSecondLabelv bool
promQuery string
promQueryBody string
method string

expCode int
expPromQuery string
Expand All @@ -693,6 +694,21 @@ func TestQuery(t *testing.T) {
expCode: http.StatusBadRequest,
method: http.MethodPost,
},
{
labelv: "default",
secondLabelv: "",
forceAddSecondLabelv: true,
name: `One of the "namespace" parameters empty returns an error`,
expCode: http.StatusBadRequest,
},
{
labelv: "default",
secondLabelv: "",
forceAddSecondLabelv: true,
name: `One of the "namespace" parameters empty returns an error for POSTs`,
expCode: http.StatusBadRequest,
method: http.MethodPost,
},
{
name: `No "query" parameter returns 200 with empty body`,
labelv: "default",
Expand Down Expand Up @@ -940,7 +956,7 @@ func TestQuery(t *testing.T) {
q := u.Query()
q.Set(queryParam, tc.promQuery)
q.Set(proxyLabel, tc.labelv)
if tc.secondLabelv != "" {
if tc.secondLabelv != "" || tc.forceAddSecondLabelv {
q.Add(proxyLabel, tc.secondLabelv)
}
u.RawQuery = q.Encode()
Expand Down

0 comments on commit 5fbd088

Please sign in to comment.