1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package routes
18
19import (
20	"fmt"
21	"html/template"
22	"io/ioutil"
23	"net/http"
24	"path"
25	"sync"
26
27	"k8s.io/klog/v2"
28
29	"k8s.io/apiserver/pkg/server/mux"
30)
31
32var (
33	lock            = &sync.RWMutex{}
34	registeredFlags = map[string]debugFlag{}
35)
36
37// DebugFlags adds handlers for flags under /debug/flags.
38type DebugFlags struct {
39}
40
41// Install registers the APIServer's flags handler.
42func (f DebugFlags) Install(c *mux.PathRecorderMux, flag string, handler func(http.ResponseWriter, *http.Request)) {
43	c.UnlistedHandle("/debug/flags", http.HandlerFunc(f.Index))
44	c.UnlistedHandlePrefix("/debug/flags/", http.HandlerFunc(f.Index))
45
46	url := path.Join("/debug/flags", flag)
47	c.UnlistedHandleFunc(url, handler)
48
49	f.addFlag(flag)
50}
51
52// Index responds with the `/debug/flags` request.
53// For example, "/debug/flags/v" serves the "--v" flag.
54// Index responds to a request for "/debug/flags/" with an HTML page
55// listing the available flags.
56func (f DebugFlags) Index(w http.ResponseWriter, r *http.Request) {
57	lock.RLock()
58	defer lock.RUnlock()
59	if err := indexTmpl.Execute(w, registeredFlags); err != nil {
60		klog.Error(err)
61	}
62}
63
64var indexTmpl = template.Must(template.New("index").Parse(`<html>
65<head>
66<title>/debug/flags/</title>
67</head>
68<body>
69/debug/flags/<br>
70<br>
71flags:<br>
72<table>
73{{range .}}
74<tr>{{.Flag}}<br>
75{{end}}
76</table>
77<br>
78full flags configurable<br>
79</body>
80</html>
81`))
82
83type debugFlag struct {
84	Flag string
85}
86
87func (f DebugFlags) addFlag(flag string) {
88	lock.Lock()
89	defer lock.Unlock()
90	registeredFlags[flag] = debugFlag{flag}
91}
92
93// StringFlagSetterFunc is a func used for setting string type flag.
94type StringFlagSetterFunc func(string) (string, error)
95
96// StringFlagPutHandler wraps an http Handler to set string type flag.
97func StringFlagPutHandler(setter StringFlagSetterFunc) http.HandlerFunc {
98	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
99		switch {
100		case req.Method == "PUT":
101			body, err := ioutil.ReadAll(req.Body)
102			if err != nil {
103				writePlainText(http.StatusBadRequest, "error reading request body: "+err.Error(), w)
104				return
105			}
106			defer req.Body.Close()
107			response, err := setter(string(body))
108			if err != nil {
109				writePlainText(http.StatusBadRequest, err.Error(), w)
110				return
111			}
112			writePlainText(http.StatusOK, response, w)
113			return
114		default:
115			writePlainText(http.StatusNotAcceptable, "unsupported http method", w)
116			return
117		}
118	})
119}
120
121// writePlainText renders a simple string response.
122func writePlainText(statusCode int, text string, w http.ResponseWriter) {
123	w.Header().Set("Content-Type", "text/plain")
124	w.Header().Set("X-Content-Type-Options", "nosniff")
125	w.WriteHeader(statusCode)
126	fmt.Fprintln(w, text)
127}
128