Skip to content

Commit

Permalink
vttablet: debug/env enforce POST
Browse files Browse the repository at this point in the history
Signed-off-by: Sugu Sougoumarane <[email protected]>
  • Loading branch information
sougou committed Dec 29, 2020
1 parent 1533644 commit a4b5e7e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
10 changes: 8 additions & 2 deletions go/vt/vttablet/endtoend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,16 @@ func changeVar(t *testing.T, name, value string) (revert func()) {
if !ok {
t.Fatalf("%s not found in: %v", name, vals)
}
vals = framework.FetchJSON(fmt.Sprintf("/debug/env?format=json&varname=%s&value=%s", name, value))
vals = framework.PostJSON("/debug/env?format=json", map[string]string{
"varname": name,
"value": value,
})
verifyMapValue(t, vals, name, value)
return func() {
vals := framework.FetchJSON(fmt.Sprintf("/debug/env?format=json&varname=%s&value=%s", name, initial))
vals = framework.PostJSON("/debug/env?format=json", map[string]string{
"varname": name,
"value": fmt.Sprintf("%v", initial),
})
verifyMapValue(t, vals, name, initial)
}
}
Expand Down
18 changes: 18 additions & 0 deletions go/vt/vttablet/endtoend/framework/debugvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

Expand All @@ -37,6 +38,23 @@ func FetchJSON(urlPath string) map[string]interface{} {
return out
}

// PostJSON performs a post and fetches JSON content from the specified URL path and returns it
// as a map. The function returns an empty map on error.
func PostJSON(urlPath string, values map[string]string) map[string]interface{} {
urlValues := url.Values{}
for k, v := range values {
urlValues.Add(k, v)
}
out := map[string]interface{}{}
response, err := http.PostForm(fmt.Sprintf("%s%s", ServerAddress, urlPath), urlValues)
if err != nil {
return out
}
defer response.Body.Close()
_ = json.NewDecoder(response.Body).Decode(&out)
return out
}

// DebugVars parses /debug/vars and returns a map. The function returns
// an empty map on error.
func DebugVars() map[string]interface{} {
Expand Down
54 changes: 28 additions & 26 deletions go/vt/vttablet/tabletserver/debugenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,36 @@ func debugEnvHandler(tsv *TabletServer, w http.ResponseWriter, r *http.Request)
return
}

varname := r.FormValue("varname")
value := r.FormValue("value")
var msg string
setIntVal := func(f func(int)) {
ival, err := strconv.Atoi(value)
if err != nil {
msg = fmt.Sprintf("Failed setting value for %v: %v", varname, err)
return
if r.Method == "POST" {
varname := r.FormValue("varname")
value := r.FormValue("value")
setIntVal := func(f func(int)) {
ival, err := strconv.Atoi(value)
if err != nil {
msg = fmt.Sprintf("Failed setting value for %v: %v", varname, err)
return
}
f(ival)
msg = fmt.Sprintf("Setting %v to: %v", varname, value)
}
switch varname {
case "PoolSize":
setIntVal(tsv.SetPoolSize)
case "StreamPoolSize":
setIntVal(tsv.SetStreamPoolSize)
case "TxPoolSize":
setIntVal(tsv.SetTxPoolSize)
case "QueryCacheCapacity":
setIntVal(tsv.SetQueryPlanCacheCap)
case "MaxResultSize":
setIntVal(tsv.SetMaxResultSize)
case "WarnResultSize":
setIntVal(tsv.SetWarnResultSize)
case "Consolidator":
tsv.SetConsolidatorMode(value)
msg = fmt.Sprintf("Setting %v to: %v", varname, value)
}
f(ival)
msg = fmt.Sprintf("Setting %v to: %v", varname, value)
}
switch varname {
case "PoolSize":
setIntVal(tsv.SetPoolSize)
case "StreamPoolSize":
setIntVal(tsv.SetStreamPoolSize)
case "TxPoolSize":
setIntVal(tsv.SetTxPoolSize)
case "QueryCacheCapacity":
setIntVal(tsv.SetQueryPlanCacheCap)
case "MaxResultSize":
setIntVal(tsv.SetMaxResultSize)
case "WarnResultSize":
setIntVal(tsv.SetWarnResultSize)
case "Consolidator":
tsv.SetConsolidatorMode(value)
msg = fmt.Sprintf("Setting %v to: %v", varname, value)
}

var vars []envValue
Expand Down

0 comments on commit a4b5e7e

Please sign in to comment.