-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate.go
117 lines (95 loc) · 2.91 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"fmt"
mapset "github.com/deckarep/golang-set/v2"
"github.com/kubewarden/gjson"
kubewarden "github.com/kubewarden/policy-sdk-go"
kubewarden_protocol "github.com/kubewarden/policy-sdk-go/protocol"
)
func validate(payload []byte) ([]byte, error) {
validationRequest := kubewarden_protocol.ValidationRequest{}
err := json.Unmarshal(payload, &validationRequest)
if err != nil {
return kubewarden.RejectRequest(
kubewarden.Message(err.Error()),
kubewarden.Code(400))
}
// Create a Settings instance from the ValidationRequest object
settings, err := NewSettingsFromValidationReq(&validationRequest)
if err != nil {
return kubewarden.RejectRequest(
kubewarden.Message(err.Error()),
kubewarden.Code(400))
}
if !checkTlsSettings(payload, &settings) {
return kubewarden.RejectRequest(
kubewarden.Message("Not all hosts have TLS enabled"),
kubewarden.NoCode)
}
ports := parsePorts(payload)
if err := checkAllowedPorts(ports, &settings); err != nil {
return kubewarden.RejectRequest(
kubewarden.Message(err.Error()),
kubewarden.NoCode)
}
if err := checkDeniedPorts(ports, &settings); err != nil {
return kubewarden.RejectRequest(
kubewarden.Message(err.Error()),
kubewarden.NoCode)
}
return kubewarden.AcceptRequest()
}
func checkTlsSettings(payload []byte, settings *Settings) bool {
if !settings.RequireTls {
return true
}
tlsHost := mapset.NewThreadUnsafeSet[string]()
rulesHosts := mapset.NewThreadUnsafeSet[string]()
// still using gjson because it makes the code more compact
data := gjson.GetManyBytes(
payload,
"request.object.spec.tls.#.hosts|@flatten",
"request.object.spec.rules.#.host|@flatten")
data[0].ForEach(func(_, entry gjson.Result) bool {
tlsHost.Add(entry.String())
return true
})
data[1].ForEach(func(_, entry gjson.Result) bool {
rulesHosts.Add(entry.String())
return true
})
return tlsHost.Equal(rulesHosts)
}
func parsePorts(payload []byte) mapset.Set[uint64] {
ports := mapset.NewThreadUnsafeSet[uint64]()
// still using gjson because it makes the code more compact
data := gjson.GetBytes(
payload,
"request.object.spec.rules.#.http.paths.#.backend.service.port.number|@flatten")
data.ForEach(func(_, entry gjson.Result) bool {
ports.Add(entry.Uint())
return true
})
return ports
}
func checkAllowedPorts(ports mapset.Set[uint64], settings *Settings) error {
if settings.AllowPorts.Cardinality() == 0 {
return nil
}
notAllowed := ports.Difference(settings.AllowPorts)
if notAllowed.Cardinality() == 0 {
return nil
}
return fmt.Errorf("These ports are not on the allowed list: %v", notAllowed)
}
func checkDeniedPorts(ports mapset.Set[uint64], settings *Settings) error {
if settings.DenyPorts.Cardinality() == 0 {
return nil
}
denied := ports.Intersect(settings.DenyPorts)
if denied.Cardinality() == 0 {
return nil
}
return fmt.Errorf("These ports are explicitly denied: %v", denied)
}