-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.go
42 lines (34 loc) · 1.06 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"encoding/json"
"strings"
mapset "github.com/deckarep/golang-set/v2"
onelog "github.com/francoispqt/onelog"
kubewarden "github.com/kubewarden/policy-sdk-go"
)
func validate(payload []byte) ([]byte, error) {
validationRequest := RawValidationRequest{}
validationRequest.Settings = Settings{
ForbiddenResources: mapset.NewSet[string](),
}
decoder := json.NewDecoder(strings.NewReader(string(payload)))
decoder.DisallowUnknownFields()
err := decoder.Decode(&validationRequest)
if err != nil {
return kubewarden.RejectRequest(
kubewarden.Message(err.Error()),
kubewarden.Code(400))
}
request := validationRequest.Request
settings := validationRequest.Settings
logger.DebugWithFields("validating request", func(e onelog.Entry) {
e.String("user", request.User)
e.String("action", request.Action)
e.String("resource", request.Resource)
})
if settings.ForbiddenResources.Contains(request.Resource) {
request.Resource = settings.DefaultResource
return kubewarden.MutateRequest(request)
}
return kubewarden.AcceptRequest()
}