-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (68 loc) · 1.92 KB
/
main.go
File metadata and controls
83 lines (68 loc) · 1.92 KB
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
package main
import (
"fmt"
"regexp"
"strings"
"github.com/jncmaguire/openapi-testing/internal/istio"
"github.com/jncmaguire/openapi-testing/internal/openapi"
)
func main() {
requirements, err := openapi.GetSecurityRequirements("./resources/openapi.json") // render braces in path?
fmt.Println(requirements)
if err != nil {
panic(err)
}
rules, err := istio.GetAuthorizationRules(`./resources/authorizationpolicy.yaml`)
if err != nil {
panic(err)
}
fmt.Println(rules)
ruleHasRequirement := make([]bool, len(rules))
requirementHasRule := make([]bool, len(requirements))
for i := range requirements {
for j := range rules {
if ruleRequirementMatch(rules[j], requirements[i]) {
ruleHasRequirement[j] = true
requirementHasRule[i] = true
break
}
}
if !requirementHasRule[i] {
fmt.Println("missing policy for OpenAPI requirement", requirements[i].Path)
}
}
for i := range rules {
if !ruleHasRequirement[i] {
fmt.Println("missing permission for policy", rules[i])
}
}
}
func ruleRequirementMatch(rule istio.Rule, requirement openapi.SecurityRequirement) bool {
// first, match the To
for _, to := range rule.To {
// check that the method matches
if !to.MethodAllowed(requirement.Method) {
return false
}
// check that it matches at least 1 path
for path, allowed := range to.Paths {
// replace * with .*
if !regexp.MustCompile(strings.Replace(path, `*`, `.*`, -1)).MatchString(requirement.Path) {
continue
}
if !allowed { // check that it doesn't match a not Path
return false
}
}
}
// next, match the When
foundScopes := make(map[string]struct{})
for _, when := range rule.When {
for _, req := range requirement.Requirements[`petstore_auth`] { // hardcoding for simplicity
if when.Values[req] { // add to found list if found
foundScopes[req] = struct{}{}
}
}
}
return len(foundScopes) == len(requirement.Requirements[`petstore_auth`])
}