File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,7 @@ func main() {
7878 r .Route ("/service" , func (r chi.Router ) {
7979 r .Get ("/" , api .ListApplications (* appConfig ))
8080 r .Route ("/{kind}/{namespace}/{name}" , func (r chi.Router ) {
81+ r .Use (api .MiddlewareValidation (* appConfig ))
8182 r .Post ("/restart" , api .Restart (k8sClient ))
8283 r .Get ("/status" , api .Status (ldgr ))
8384 })
Original file line number Diff line number Diff line change @@ -41,6 +41,37 @@ func getKindNamespaceNameFromRequest(r *http.Request) k8s.KindNamespaceName {
4141 }
4242}
4343
44+ func MiddlewareValidation (config config.Config ) func (http.Handler ) http.Handler {
45+ return func (next http.Handler ) http.Handler {
46+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
47+ kindNamespaceName := getKindNamespaceNameFromRequest (r )
48+ isFound := false
49+
50+ if kindNamespaceName .Kind == "" || kindNamespaceName .Namespace == "" || kindNamespaceName .Name == "" {
51+ http .Error (w , "invalid request" , http .StatusBadRequest )
52+ return
53+ }
54+
55+ if kindNamespaceName .Kind != "Deployment" && kindNamespaceName .Kind != "StatefulSet" {
56+ http .Error (w , "invalid kind" , http .StatusBadRequest )
57+ return
58+ }
59+
60+ for _ , service := range config .Services {
61+ if service .Kind == kindNamespaceName .Kind && service .Namespace == kindNamespaceName .Namespace && service .Name == kindNamespaceName .Name {
62+ isFound = true
63+ break
64+ }
65+ }
66+ if ! isFound {
67+ http .Error (w , "service not found" , http .StatusNotFound )
68+ return
69+ }
70+ next .ServeHTTP (w , r )
71+ })
72+ }
73+ }
74+
4475func Restart (client * kubernetes.Clientset ) func (w http.ResponseWriter , r * http.Request ) {
4576 return func (w http.ResponseWriter , r * http.Request ) {
4677 kindNamespaceName := getKindNamespaceNameFromRequest (r )
You can’t perform that action at this time.
0 commit comments