Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Claudio Beatrice <[email protected]>
  • Loading branch information
Al-Pragliola and omissis committed Sep 1, 2023
1 parent 98a9103 commit d5e2855
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 1 addition & 7 deletions configs/helm-values/kube-apiserver-proxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ app:
configFile:
middlewares:
bodyFilter:
enabled: true
config:
- methods: ["PATCH", "POST"]
paths:
- path: "/api/v1/namespaces/*/pods/*"
type: "glob"
filter: "{\"metadata\":{\"labels\":{\"app\": \"*\"}}}"
enabled: false

clusterRole:
rules:
Expand Down
13 changes: 6 additions & 7 deletions deployments/helm/kube-apiserver-proxy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ app:
middlewares:
bodyFilter:
enabled: false
config:
- methods: ["PATCH", "POST"]
paths:
- path: "/api/v1/namespaces/*/pods/*"
type: "glob" # optional
filter: "{\"metadata\":{\"labels\":{\"app\": \"*\"}}}"

# config:
# - methods: ["PATCH"]
# paths:
# - path: "/api/v1/namespaces/*/pods/*"
# type: "glob" # optional
# filter: "{\"metadata\":{\"labels\":{\"example\": \"*\"}}}"
queries:
- name: listPodsinNamespace
method: GET
Expand Down
22 changes: 16 additions & 6 deletions pkg/http/middleware/bodyfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"golang.org/x/exp/slices"

"github.com/omissis/kube-apiserver-proxy/pkg/config"
http2 "github.com/omissis/kube-apiserver-proxy/pkg/http"
kaspHttp "github.com/omissis/kube-apiserver-proxy/pkg/http"
)

var ErrDuringBodyFilter = fmt.Errorf("error during body filter")
Expand All @@ -32,7 +32,9 @@ func BodyFilter(next http.Handler, conf []config.BodyFilterConfig) http2.Middlew
}

if r.Body == nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
slog.Warning("empty request body")

http.Error(w, "Empty request body", http.StatusBadRequest)

return
}
Expand All @@ -43,26 +45,34 @@ func BodyFilter(next http.Handler, conf []config.BodyFilterConfig) http2.Middlew
filterTarget := map[string]any{}

if err := bodyDecoder.Decode(&filteredBody); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
slog.Error("cannot decode body", "error", err, "body", r.Body)

http.Error(w, "Decoding Error", http.StatusBadRequest)

return
}

if err := json.Unmarshal([]byte(c.Filter), &filterTarget); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
slog.Error("cannot unmarshal filter config", "error", err, "filterConfig", c.Filter)

http.Error(w, "Unmarshalling Error", http.StatusInternalServerError)

return
}

if err := FillTargetFromBody(filteredBody, filterTarget); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
slog.Error("cannot create filtered body", "error", err, "filteredBody", filteredBody, "filterTarget", filterTarget)

http.Error(w, "Mapping Error", http.StatusInternalServerError)

return
}

bodyFromTarget, err := json.Marshal(filterTarget)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
slog.Error("cannot marshal filtered body", "error", err, "filterTarget", filterTarget)

http.Error(w, "Marshalling Error", http.StatusInternalServerError)

return
}
Expand Down

0 comments on commit d5e2855

Please sign in to comment.