vitess.io/vitess@v0.16.2/go/vt/vtadmin/http/debug/env.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package debug 18 19 import ( 20 "fmt" 21 "net/http" 22 "os" 23 "sort" 24 "strings" 25 26 "vitess.io/vitess/go/flagutil" 27 vtadmindebug "vitess.io/vitess/go/vt/vtadmin/debug" 28 ) 29 30 var ( 31 // SanitizeEnv is the set of environment variables to sanitize their values 32 // in the Env http handler. 33 SanitizeEnv flagutil.StringSetFlag 34 // OmitEnv is the set of environment variables to omit entirely in the Env 35 // http handler. 36 OmitEnv flagutil.StringSetFlag 37 ) 38 39 // Env responds with a plaintext listing of key=value pairs of the environment 40 // variables, sorted by key name. 41 // 42 // If a variable appears in OmitEnv, it is excluded entirely. If a variable 43 // appears in SanitizeEnv, its value is replaced with a sanitized string, 44 // including if there was no value set in the environment. 45 func Env(w http.ResponseWriter, r *http.Request) { 46 vars := readEnv() 47 48 msg := &strings.Builder{} 49 for i, kv := range vars { 50 msg.WriteString(fmt.Sprintf("%s=%s", kv[0], kv[1])) 51 if i < len(vars)-1 { 52 msg.WriteByte('\n') 53 } 54 } 55 56 w.Write([]byte(msg.String())) 57 } 58 59 func readEnv() [][2]string { 60 env := os.Environ() 61 vars := make([][2]string, 0, len(env)) 62 63 var key, value string 64 for _, ev := range env { 65 parts := strings.SplitN(ev, "=", 2) 66 switch len(parts) { 67 case 0: 68 key = ev 69 case 1: 70 key = parts[0] 71 default: 72 key = parts[0] 73 value = parts[1] 74 } 75 76 if key == "" { 77 continue 78 } 79 80 if OmitEnv.ToSet().Has(key) { 81 continue 82 } 83 84 if SanitizeEnv.ToSet().Has(key) { 85 value = vtadmindebug.SanitizeString(value) 86 } 87 88 vars = append(vars, [2]string{ 89 key, 90 value, 91 }) 92 } 93 94 // Sort by env var name, ascending. 95 sort.SliceStable(vars, func(i, j int) bool { 96 left, right := vars[i], vars[j] 97 return left[0] < right[0] 98 }) 99 100 return vars 101 }