forked from tkmgo/haproxyconfigparser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathacl.go
83 lines (73 loc) · 1.78 KB
/
acl.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
package haproxyconfigparser
import (
"fmt"
"strings"
)
func CreateUseBackendClauses(judge string, source []string) (*UseBackendClauses, error) {
// https://www.haproxy.com/doc/aloha/7.0/haproxy/conditions.html
dest := &UseBackendClauses{}
if judge == "if" {
dest.ReverseJudge = false
} else if judge == "unless" {
dest.ReverseJudge = true
} else {
return dest, fmt.Errorf("expected if|unless, but '%s'", judge)
}
// TODO suport [!], such as !if_xxx
buf := make([]string, 0)
for _, n := range source {
if n == "or" || n == "OR" || n == "||" {
if len(buf) > 0 {
dest.Any = append(dest.Any, buf)
}
} else if n == "and" || n == "AND" {
continue
} else {
buf = append(buf, n)
}
}
if len(buf) > 0 {
dest.Any = append(dest.Any, buf)
}
return dest, nil
}
func backendReferenceByAcl(frontend Frontend, backends []Backend) error {
for _, ub := range frontend.UseBackends {
b, err := findBackendByName(ub.Name, backends)
if err != nil {
return err
}
ub.Backend = b
for _, a := range ub.Condition.Any {
// TODO support or/and conditions
for _, s := range a {
//TODO Handle ! correctly instead of ignoring it
if strings.HasPrefix(s, "!") {
s = strings.TrimLeft(s, "!")
}
acl, err := findAclByName(s, &frontend)
if err != nil {
return err
}
ub.Acls = append(ub.Acls, acl)
}
}
}
return nil
}
func findAclByName(name string, frontend *Frontend) (*Acl, error) {
for _, acl := range frontend.Acls {
if acl.Name == name {
return acl, nil
}
}
return &Acl{}, fmt.Errorf("ACL '%s' not found", name)
}
func findBackendByName(name string, backends []Backend) (*Backend, error) {
for _, b := range backends {
if b.Name == name {
return &b, nil
}
}
return nil, fmt.Errorf("Backend '%s' not found", name)
}