forked from Juniper/contrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_rule_type.go
221 lines (187 loc) · 5.77 KB
/
policy_rule_type.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package models
import (
"fmt"
"reflect"
"strconv"
"github.com/pkg/errors"
"github.com/Juniper/asf/pkg/errutil"
"github.com/Juniper/asf/pkg/format"
)
const (
// SRCToDSTDirection is the standard direction in PolicyRuleType.
SRCToDSTDirection = ">"
// TODO: Generate these from the enum in the schema.
// IPv6Ethertype is the Ethertype for IPv6.
IPv6Ethertype = "IPv6"
// IPv4Ethertype is the Ethertype for IPv4.
IPv4Ethertype = "IPv4"
// AnyProtocol matches any protocol in PolicyRuleType.
AnyProtocol = "any"
// ICMPProtocol matches ICMP protocol in PolicyRuleType.
ICMPProtocol = "icmp"
// TCPProtocol matches TCP protocol in PolicyRuleType.
TCPProtocol = "tcp"
// UDPProtocol matches UDP protocol in PolicyRuleType.
UDPProtocol = "udp"
// ICMP6Protocol matches ICMPv6 protocol in PolicyRuleType.
ICMP6Protocol = "icmp6"
)
// EqualRule checks if rule contains same data as other rule.
func (m PolicyRuleType) EqualRule(other PolicyRuleType) bool {
m.RuleUUID = ""
m.LastModified = ""
m.Created = ""
other.RuleUUID = ""
other.LastModified = ""
other.Created = ""
return reflect.DeepEqual(m, other)
}
var avaiableProtocols = []string{AnyProtocol, ICMPProtocol, TCPProtocol, UDPProtocol, ICMP6Protocol}
var isAvailableProtocol = format.BoolMap(avaiableProtocols)
// ValidateProtocol checks if protocol is valid rule protocol.
func (m *PolicyRuleType) ValidateProtocol() error {
proto := m.GetProtocol()
if isAvailableProtocol[proto] {
return nil
}
number, err := strconv.Atoi(proto)
if err != nil {
return errutil.ErrorBadRequestf("Rule with invalid protocol: %v.", proto)
}
if number < 0 || number > 255 {
return errutil.ErrorBadRequestf("Rule with invalid protocol: %v.", number)
}
return nil
}
// PolicyAddressPair is a single combination of source and destination specifications from a PolicyRuleType.
type PolicyAddressPair struct {
PolicyRule *PolicyRuleType
SourceAddress, DestinationAddress *AddressType
SourcePort, DestinationPort *PortType
}
// IsIngress checks whether the pair is ingress (remote to local addresses) or egress (local to remote addresses).
func (pair *PolicyAddressPair) IsIngress() (bool, error) {
switch {
case pair.DestinationAddress.IsSecurityGroupLocal():
return true, nil
case pair.SourceAddress.IsSecurityGroupLocal():
return false, nil
default:
return false, neitherAddressIsLocal{
sourceAddress: pair.SourceAddress,
destinationAddress: pair.DestinationAddress,
}
}
}
type neitherAddressIsLocal struct {
sourceAddress, destinationAddress *AddressType
}
func (err neitherAddressIsLocal) Error() string {
return fmt.Sprintf("neither source nor destination address is local. Source address: %v. Destination address: %v",
err.sourceAddress, err.destinationAddress)
}
func (m *PolicyRuleType) allAddressCombinations() (pairs []PolicyAddressPair) {
for _, sourceAddress := range m.SRCAddresses {
for _, sourcePort := range m.SRCPorts {
for _, destinationAddress := range m.DSTAddresses {
for _, destinationPort := range m.DSTPorts {
pairs = append(pairs, PolicyAddressPair{
PolicyRule: m,
SourceAddress: sourceAddress,
SourcePort: sourcePort,
DestinationAddress: destinationAddress,
DestinationPort: destinationPort,
})
}
}
}
}
return pairs
}
var ipV6ProtocolStringToNumber = map[string]string{
ICMPProtocol: "58",
ICMP6Protocol: "58",
TCPProtocol: "6",
UDPProtocol: "17",
}
var ipV4ProtocolStringToNumber = map[string]string{
ICMPProtocol: "1",
ICMP6Protocol: "58",
TCPProtocol: "6",
UDPProtocol: "17",
}
// ValidateSubnetsWithEthertype validates if every subnet
// within source and destination addresses matches rule ethertype.
func (m *PolicyRuleType) ValidateSubnetsWithEthertype() error {
if m.GetEthertype() != "" {
for _, addr := range m.GetSRCAddresses() {
if err := addr.Subnet.ValidateWithEthertype(m.GetEthertype()); err != nil {
return err
}
}
for _, addr := range m.GetDSTAddresses() {
if err := addr.Subnet.ValidateWithEthertype(m.GetEthertype()); err != nil {
return err
}
}
}
return nil
}
// HasSecurityGroup returns true if any of addresses points at Security Group.
func (m *PolicyRuleType) HasSecurityGroup() bool {
for _, addr := range m.GetSRCAddresses() {
if addr.GetSecurityGroup() != "" {
return true
}
}
for _, addr := range m.GetDSTAddresses() {
if addr.GetSecurityGroup() != "" {
return true
}
}
return false
}
// IsAnySecurityGroupAddrLocal returns true if at least one of addresses contains
// 'local' Security Group.
func (m *PolicyRuleType) IsAnySecurityGroupAddrLocal() bool {
for _, addr := range m.GetSRCAddresses() {
if addr.IsSecurityGroupLocal() {
return true
}
}
for _, addr := range m.GetDSTAddresses() {
if addr.IsSecurityGroupLocal() {
return true
}
}
return false
}
// ACLProtocol returns the protocol in a format suitable for an AclRuleType.
func (m *PolicyRuleType) ACLProtocol() (string, error) {
protocol := m.GetProtocol()
ethertype := m.GetEthertype()
if protocol == "" || protocol == AnyProtocol || isNumeric(protocol) {
return protocol, nil
}
protocol, err := numericProtocolForEthertype(protocol, ethertype)
if err != nil {
return "", errors.Wrap(err, "failed to convert protocol for an ACL")
}
return protocol, nil
}
func isNumeric(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
func numericProtocolForEthertype(protocol, ethertype string) (numericProtocol string, err error) {
var ok bool
if ethertype == IPv6Ethertype {
numericProtocol, ok = ipV6ProtocolStringToNumber[protocol]
} else {
numericProtocol, ok = ipV4ProtocolStringToNumber[protocol]
}
if !ok {
return "", errors.Errorf("unknown protocol %q for ethertype %q", protocol, ethertype)
}
return numericProtocol, nil
}