-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
57 lines (47 loc) · 1.46 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
package main
import (
"encoding/json"
"fmt"
"log"
mapset "github.com/deckarep/golang-set/v2"
)
// Settings defines the settings of the policy
type Settings struct {
ValidUsers mapset.Set[string] `json:"validUsers"`
ValidActions mapset.Set[string] `json:"validActions"`
ValidResources mapset.Set[string] `json:"validResources"`
}
func validateSettings(input []byte) []byte {
var response SettingsValidationResponse
settings := &Settings{
// this is required to make the unmarshal work
ValidUsers: mapset.NewSet[string](),
ValidActions: mapset.NewSet[string](),
ValidResources: mapset.NewSet[string](),
}
if err := json.Unmarshal(input, &settings); err != nil {
response = RejectSettings(Message(fmt.Sprintf("cannot unmarshal settings: %v", err)))
} else {
response = validateCliSettings(settings)
}
responseBytes, err := json.Marshal(&response)
if err != nil {
log.Fatalf("cannot marshal validation response: %v", err)
}
return responseBytes
}
func validateCliSettings(settings *Settings) SettingsValidationResponse {
if settings.ValidUsers.Cardinality() == 0 {
return RejectSettings(Message(
"At least one valid user must be specified"))
}
if settings.ValidActions.Cardinality() == 0 {
return RejectSettings(Message(
"At least one valid action must be specified"))
}
if settings.ValidResources.Cardinality() == 0 {
return RejectSettings(Message(
"At least one valid resource must be specified"))
}
return AcceptSettings()
}