-
Notifications
You must be signed in to change notification settings - Fork 70
/
rules.go
163 lines (145 loc) · 3.88 KB
/
rules.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
package rules
import (
"fmt"
"strconv"
"code.cloudfoundry.org/lager"
)
//go:generate counterfeiter -o ../fakes/rule.go --fake-name Rule . Rule
type Rule interface {
Enforce(table, chain string, ipt IPTables, logger lager.Logger) error
}
type GenericRule struct {
Properties []string
}
func (r GenericRule) Enforce(table, chain string, iptables IPTables, logger lager.Logger) error {
err := iptables.AppendUnique(table, chain, r.Properties...)
if err != nil {
logger.Error("append-rule", err)
return fmt.Errorf("appending rule: %s", err)
}
logger.Debug("enforce-rule", lager.Data{
"table": table,
"chain": chain,
"properties": fmt.Sprintf("%s", r.Properties),
})
return nil
}
func NewMarkAllowRule(destinationIP, protocol string, port int, tag string, sourceAppGUID, destinationAppGUID string) GenericRule {
return GenericRule{
Properties: []string{
"-d", destinationIP,
"-p", protocol,
"--dport", strconv.Itoa(port),
"-m", "mark", "--mark", fmt.Sprintf("0x%s", tag),
"--jump", "ACCEPT",
"-m", "comment", "--comment", fmt.Sprintf("src:%s dst:%s", sourceAppGUID, destinationAppGUID),
},
}
}
func NewMarkSetRule(sourceIP, tag, appGUID string) GenericRule {
return GenericRule{
Properties: []string{
"--source", sourceIP,
"--jump", "MARK", "--set-xmark", fmt.Sprintf("0x%s", tag),
"-m", "comment", "--comment", fmt.Sprintf("src:%s", appGUID),
},
}
}
func NewDefaultEgressRule(localSubnet, overlayNetwork string) GenericRule {
return GenericRule{
Properties: []string{
"--source", localSubnet,
"!", "-d", overlayNetwork,
"--jump", "MASQUERADE",
},
}
}
func NewLogRule(guardConditions []string, name string) GenericRule {
properties := append(
guardConditions,
"-m", "limit", "--limit", "2/min",
"--jump", "LOG",
"--log-prefix", name,
)
return GenericRule{Properties: properties}
}
func NewAcceptExistingLocalRule() GenericRule {
return GenericRule{
Properties: []string{
"-i", "cni-flannel0",
"-m", "state", "--state", "ESTABLISHED,RELATED",
"--jump", "ACCEPT",
},
}
}
func NewDefaultDenyLocalRule(localSubnet string) GenericRule {
return GenericRule{
Properties: []string{
"-i", "cni-flannel0",
"--source", localSubnet,
"-d", localSubnet,
"--jump", "REJECT",
},
}
}
func NewAcceptExistingRemoteRule(vni int) GenericRule {
return GenericRule{
Properties: []string{
"-i", fmt.Sprintf("flannel.%d", vni),
"-m", "state", "--state", "ESTABLISHED,RELATED",
"--jump", "ACCEPT",
},
}
}
func NewDefaultDenyRemoteRule(vni int) GenericRule {
return GenericRule{
Properties: []string{
"-i", fmt.Sprintf("flannel.%d", vni),
"--jump", "REJECT",
},
}
}
func NewNetOutRule(containerIP string, startIP string, endIP string) GenericRule {
return GenericRule{
Properties: []string{
"--source", containerIP,
"-m", "iprange",
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"--jump", "RETURN",
// "-m", "comment", "--comment", fmt.Sprintf("dst:%s", groupID),
},
}
}
func NewNetOutWithPortsRule(containerIP string, startIP string, endIP string, startPort int, endPort int, protocol string) GenericRule {
return GenericRule{
Properties: []string{
"--source", containerIP,
"-m", "iprange",
"-p", protocol,
"--dst-range", fmt.Sprintf("%s-%s", startIP, endIP),
"-m", protocol,
"--destination-port", fmt.Sprintf("%d:%d", startPort, endPort),
"--jump", "RETURN",
},
}
}
func NewNetOutRelatedEstablishedRule(subnet, overlayNetwork string) GenericRule {
return GenericRule{
Properties: []string{
"-s", subnet,
"!", "-d", overlayNetwork,
"-m", "state", "--state", "RELATED,ESTABLISHED",
"--jump", "RETURN",
},
}
}
func NewNetOutDefaultRejectRule(subnet, overlayNetwork string) GenericRule {
return GenericRule{
Properties: []string{
"-s", subnet,
"!", "-d", overlayNetwork,
"--jump", "REJECT",
"--reject-with", "icmp-port-unreachable",
},
}
}