Skip to content

Commit a622f54

Browse files
author
leonsteinhaeuser
committed
fix(api): add validation for passed URL params
1 parent 2143e11 commit a622f54

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
})

internal/api/restart.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
4475
func Restart(client *kubernetes.Clientset) func(w http.ResponseWriter, r *http.Request) {
4576
return func(w http.ResponseWriter, r *http.Request) {
4677
kindNamespaceName := getKindNamespaceNameFromRequest(r)

0 commit comments

Comments
 (0)