generated from kubewarden/go-policy-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.go
170 lines (145 loc) · 4.42 KB
/
settings.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
mapset "github.com/deckarep/golang-set/v2"
"github.com/kubewarden/gjson"
kubewarden "github.com/kubewarden/policy-sdk-go"
)
// A wrapper around the standard regexp.Regexp struct
// that implements marshalling and unmarshalling
type RegularExpression struct {
*regexp.Regexp
}
// Convenience method to build a regular expression
func CompileRegularExpression(expr string) (*RegularExpression, error) {
nativeRegExp, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return &RegularExpression{nativeRegExp}, nil
}
// UnmarshalText satisfies the encoding.TextMarshaler interface,
// also used by json.Unmarshal.
func (r *RegularExpression) UnmarshalText(text []byte) error {
nativeRegExp, err := regexp.Compile(string(text))
if err != nil {
return err
}
r.Regexp = nativeRegExp
return nil
}
// MarshalText satisfies the encoding.TextMarshaler interface,
// also used by json.Marshal.
func (r *RegularExpression) MarshalText() ([]byte, error) {
if r.Regexp != nil {
return []byte(r.Regexp.String()), nil
}
return nil, nil
}
type Settings struct {
DeniedLabels mapset.Set[string] `json:"denied_labels"`
MandatoryLabels mapset.Set[string] `json:"mandatory_labels"`
ConstrainedLabels map[string]*RegularExpression `json:"constrained_labels"`
}
// Builds a new Settings instance starting from a validation
// request payload:
//
// {
// "request": ...,
// "settings": {
// "denied_labels": [...],
// "mandatory_labels": [...],
// "constrained_labels": { ... }
// }
// }
func NewSettingsFromValidationReq(payload []byte) (Settings, error) {
settingsJson := gjson.GetBytes(payload, "settings")
settings := Settings{}
err := json.Unmarshal([]byte(settingsJson.Raw), &settings)
if err != nil {
return Settings{}, err
}
return settings, nil
}
// Builds a new Settings instance starting from a Settings
// payload:
//
// {
// "denied_names": [ ... ],
// "constrained_labels": { ... }
// }
func NewSettingsFromValidateSettingsPayload(payload []byte) (Settings, error) {
settings := Settings{}
err := json.Unmarshal(payload, &settings)
if err != nil {
return Settings{}, err
}
return settings, nil
}
func (s *Settings) Valid() (bool, error) {
constrainedLabels := mapset.NewThreadUnsafeSet[string]()
for label := range s.ConstrainedLabels {
constrainedLabels.Add(label)
}
errors := []string{}
constrainedAndDenied := constrainedLabels.Intersect(s.DeniedLabels)
if constrainedAndDenied.Cardinality() != 0 {
violations := constrainedAndDenied.ToSlice()
errors = append(
errors,
fmt.Sprintf(
"These labels cannot be constrained and denied at the same time: %s",
strings.Join(violations, ","),
),
)
}
mandatoryAndDenied := s.MandatoryLabels.Intersect(s.DeniedLabels)
if mandatoryAndDenied.Cardinality() != 0 {
violations := mandatoryAndDenied.ToSlice()
errors = append(
errors,
fmt.Sprintf(
"These labels cannot be mandatory and denied at the same time: %s",
strings.Join(violations, ","),
),
)
}
if len(errors) > 0 {
return false, fmt.Errorf("%s", strings.Join(errors, "; "))
}
return true, nil
}
func (s *Settings) UnmarshalJSON(data []byte) error {
// This is needed becaus golang-set v2.3.0 has a bug that prevents
// the correct unmarshalling of ThreadUnsafeSet types.
rawSettings := struct {
DeniedLabels []string `json:"denied_labels"`
MandatoryLabels []string `json:"mandatory_labels"`
ConstrainedLabels map[string]*RegularExpression `json:"constrained_labels"`
}{}
err := json.Unmarshal(data, &rawSettings)
if err != nil {
return err
}
s.DeniedLabels = mapset.NewThreadUnsafeSet[string](rawSettings.DeniedLabels...)
s.MandatoryLabels = mapset.NewThreadUnsafeSet[string](rawSettings.MandatoryLabels...)
s.ConstrainedLabels = rawSettings.ConstrainedLabels
return nil
}
func validateSettings(payload []byte) ([]byte, error) {
settings, err := NewSettingsFromValidateSettingsPayload(payload)
if err != nil {
// this happens when one of the user-defined regular expressions are invalid
return kubewarden.RejectSettings(
kubewarden.Message(fmt.Sprintf("Provided settings are not valid: %v", err)))
}
valid, err := settings.Valid()
if valid {
return kubewarden.AcceptSettings()
}
return kubewarden.RejectSettings(
kubewarden.Message(fmt.Sprintf("Provided settings are not valid: %v", err)))
}