-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8268 from tinyspeck/am_vtadmin_debug
[vtadmin] Add debug endpoints
- Loading branch information
Showing
5 changed files
with
205 additions
and
1 deletion.
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
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,80 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flagutil | ||
|
||
import ( | ||
"flag" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
var ( | ||
_ flag.Value = (*StringSetFlag)(nil) | ||
_ pflag.Value = (*StringSetFlag)(nil) | ||
) | ||
|
||
// StringSetFlag can be used to collect multiple instances of a flag into a set | ||
// of values. | ||
// | ||
// For example, defining the following: | ||
// | ||
// var x flagutil.StringSetFlag | ||
// flag.Var(&x, "foo", "") | ||
// | ||
// And then specifying "-foo x -foo y -foo x", will result in a set of {x, y}. | ||
// | ||
// In addition to implemnting the standard flag.Value interface, it also | ||
// provides an implementation of pflag.Value, so it is usable in libraries like | ||
// cobra. | ||
type StringSetFlag struct { | ||
set sets.String | ||
} | ||
|
||
// ToSet returns the underlying string set, or an empty set if the underlying | ||
// set is nil. | ||
func (set *StringSetFlag) ToSet() sets.String { | ||
if set.set == nil { | ||
set.set = sets.NewString() | ||
} | ||
|
||
return set.set | ||
} | ||
|
||
// Set is part of the pflag.Value and flag.Value interfaces. | ||
func (set *StringSetFlag) Set(s string) error { | ||
if set.set == nil { | ||
set.set = sets.NewString(s) | ||
return nil | ||
} | ||
|
||
set.set.Insert(s) | ||
return nil | ||
} | ||
|
||
// String is part of the pflag.Value and flag.Value interfaces. | ||
func (set *StringSetFlag) String() string { | ||
if set.set == nil { | ||
return "" | ||
} | ||
|
||
return strings.Join(set.set.List(), ", ") | ||
} | ||
|
||
// Type is part of the pflag.Value interface. | ||
func (set *StringSetFlag) Type() string { return "StringSetFlag" } |
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
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
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,102 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package debug | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"vitess.io/vitess/go/flagutil" | ||
) | ||
|
||
var ( | ||
// SanitizeEnv is the set of environment variables to sanitize their values | ||
// in the Env http handler. | ||
SanitizeEnv flagutil.StringSetFlag | ||
// OmitEnv is the set of environment variables to omit entirely in the Env | ||
// http handler. | ||
OmitEnv flagutil.StringSetFlag | ||
) | ||
|
||
const sanitized = "********" | ||
|
||
// Env responds with a plaintext listing of key=value pairs of the environment | ||
// variables, sorted by key name. | ||
// | ||
// If a variable appears in OmitEnv, it is excluded entirely. If a variable | ||
// appears in SanitizeEnv, its value is replaced with a sanitized string, | ||
// including if there was no value set in the environment. | ||
func Env(w http.ResponseWriter, r *http.Request) { | ||
vars := readEnv() | ||
|
||
msg := &strings.Builder{} | ||
for i, kv := range vars { | ||
msg.WriteString(fmt.Sprintf("%s=%s", kv[0], kv[1])) | ||
if i < len(vars)-1 { | ||
msg.WriteByte('\n') | ||
} | ||
} | ||
|
||
w.Write([]byte(msg.String())) | ||
} | ||
|
||
func readEnv() [][2]string { | ||
env := os.Environ() | ||
vars := make([][2]string, 0, len(env)) | ||
|
||
var key, value string | ||
for _, ev := range env { | ||
parts := strings.SplitN(ev, "=", 2) | ||
switch len(parts) { | ||
case 0: | ||
key = ev | ||
case 1: | ||
key = parts[0] | ||
default: | ||
key = parts[0] | ||
value = parts[1] | ||
} | ||
|
||
if key == "" { | ||
continue | ||
} | ||
|
||
if OmitEnv.ToSet().Has(key) { | ||
continue | ||
} | ||
|
||
if SanitizeEnv.ToSet().Has(key) { | ||
value = sanitized | ||
} | ||
|
||
vars = append(vars, [2]string{ | ||
key, | ||
value, | ||
}) | ||
} | ||
|
||
// Sort by env var name, ascending. | ||
sort.SliceStable(vars, func(i, j int) bool { | ||
left, right := vars[i], vars[j] | ||
return left[0] < right[0] | ||
}) | ||
|
||
return vars | ||
} |