/
firewall.go
225 lines (195 loc) · 6.58 KB
/
firewall.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
222
223
224
225
package goaviatrix
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"golang.org/x/net/context"
log "github.com/sirupsen/logrus"
)
type Policy struct {
SrcIP string `form:"s_ip,omitempty" json:"s_ip,omitempty"`
DstIP string `form:"d_ip,omitempty" json:"d_ip,omitempty"`
Protocol string `form:"protocol,omitempty" json:"protocol,omitempty"`
Port string `form:"port,omitempty" json:"port,omitempty"`
Action string `form:"deny_allow,omitempty" json:"deny_allow,omitempty"`
LogEnabled string `form:"log_enable,omitempty" json:"log_enable,omitempty"`
Description string `form:"description,omitempty" json:"description,omitempty"`
Position int `form:"position,omitempty" json:"position,omitempty"`
}
// Gateway simple struct to hold firewall details
type Firewall struct {
CID string `form:"CID,omitempty"`
Action string `form:"action,omitempty"`
GwName string `form:"vpc_name,omitempty" json:"vpc_name,omitempty"`
BasePolicy string `form:"base_policy,omitempty" json:"base_policy,omitempty"`
BaseLogEnabled string `form:"base_policy_log_enable,omitempty" json:"base_policy_log_enable,omitempty"`
PolicyList []*Policy `json:"security_rules,omitempty"`
NewPolicy string `form:"new_policy,omitempty"`
}
type FirewallResp struct {
Return bool `json:"return"`
Results Firewall `json:"results"`
Reason string `json:"reason"`
}
func (c *Client) SetBasePolicy(firewall *Firewall) error {
form := map[string]string{
"CID": c.CID,
"action": "set_vpc_base_policy",
"vpc_name": firewall.GwName,
"base_policy": firewall.BasePolicy,
"base_policy_log_enable": firewall.BaseLogEnabled,
}
return c.PostAPI(form["action"], form, BasicCheck)
}
func (c *Client) UpdatePolicy(firewall *Firewall) error {
firewall.CID = c.CID
firewall.Action = "update_access_policy"
// If the PolicyList is nil it will be encoded as 'null'.
// Instead, we want to set PolicyList to an empty slice so that it is encoded as '[]'.
if firewall.PolicyList == nil {
firewall.PolicyList = []*Policy{}
}
args, err := json.Marshal(firewall.PolicyList)
if err != nil {
return err
}
firewall.NewPolicy = string(args)
return c.PostAPI(firewall.Action, firewall, BasicCheck)
}
func (c *Client) GetPolicy(firewall *Firewall) (*Firewall, error) {
form := map[string]string{
"CID": c.CID,
"action": "vpc_access_policy",
"vpc_name": firewall.GwName,
}
checkFunc := func(act, method, reason string, ret bool) error {
if !ret {
if strings.Contains(reason, "does not exist") {
log.Errorf("Couldn't find Aviatrix Firewall policies for gateway %s: %s", firewall.GwName, reason)
return ErrNotFound
}
return fmt.Errorf("rest API %s %s failed: %s", act, method, reason)
}
return nil
}
var data FirewallResp
err := c.GetAPI(&data, form["action"], form, checkFunc)
if err != nil {
return nil, err
}
return &data.Results, nil
}
func (c *Client) ValidatePolicy(policy *Policy) error {
if policy.Action != "allow" && policy.Action != "deny" && policy.Action != "force-drop" {
return fmt.Errorf("valid 'action' is only 'allow', 'deny' or 'force-drop'")
}
protocolDefaultValues := []string{"all", "tcp", "udp", "icmp", "sctp", "rdp", "dccp"}
protocolVal := []string{policy.Protocol}
if policy.Protocol == "" || len(Difference(protocolVal, protocolDefaultValues)) != 0 {
return fmt.Errorf("protocol can only be one of {'all', 'tcp', 'udp', 'icmp', 'sctp', 'rdp', 'dccp'}")
}
if policy.Protocol == "all" && policy.Port != "0:65535" {
return fmt.Errorf("port should be '0:65535' for protocol 'all'")
}
if policy.Protocol == "icmp" && (policy.Port != "") {
return fmt.Errorf("port should be empty for protocol 'icmp'")
}
return nil
}
func PolicyToMap(p *Policy) map[string]interface{} {
port := p.Port
if p.Protocol == "all" && p.Port == "" {
port = "0:65535"
}
logEnabled := false
if p.LogEnabled == "on" {
logEnabled = true
}
return map[string]interface{}{
"src_ip": p.SrcIP,
"dst_ip": p.DstIP,
"protocol": p.Protocol,
"port": port,
"action": p.Action,
"log_enabled": logEnabled,
"description": p.Description,
}
}
func (c *Client) AddFirewallPolicy(fw *Firewall) error {
rules, err := json.Marshal(fw.PolicyList)
if err != nil {
return fmt.Errorf("could not marshal firewall policies: %v", err)
}
form := map[string]interface{}{
"CID": c.CID,
"action": "append_stateful_firewall_rules",
"gateway_name": fw.GwName,
"rules": string(rules),
}
return c.PostAPIContext2(context.Background(), nil, form["action"].(string), form, BasicCheck)
}
func (c *Client) DeleteFirewallPolicy(fw *Firewall) error {
rules, err := json.Marshal(fw.PolicyList)
if err != nil {
return fmt.Errorf("could not marshal firewall policies: %v", err)
}
form := map[string]interface{}{
"CID": c.CID,
"action": "delete_stateful_firewall_rules",
"gateway_name": fw.GwName,
"rules": string(rules),
}
checkFunc := func(act, method, reason string, ret bool) error {
if !ret {
// Tried to delete a rule that did not exist, we don't need to fail the apply.
if strings.Contains(reason, "Empty rules in Stateful Firewall to delete") {
return nil
}
return fmt.Errorf("rest API %s %s failed: %s", act, method, reason)
}
return nil
}
return c.PostAPIContext2(context.Background(), nil, form["action"].(string), form, checkFunc)
}
func (c *Client) GetFirewallPolicy(fw *Firewall) (*Firewall, error) {
foundFirewall, err := c.GetPolicy(fw)
if err != nil {
if err == ErrNotFound {
return nil, err
}
return nil, fmt.Errorf("could not list firewall rules: %v", err)
}
rule := fw.PolicyList[0]
found := false
for i, p := range foundFirewall.PolicyList {
if p.SrcIP == rule.SrcIP &&
p.DstIP == rule.DstIP &&
p.Protocol == rule.Protocol &&
p.Port == rule.Port &&
p.Action == rule.Action {
found = true
fw.PolicyList = []*Policy{p}
fw.PolicyList[0].Position = i + 1
break
}
}
if !found {
return nil, ErrNotFound
}
return fw, nil
}
func (c *Client) InsertFirewallPolicy(fw *Firewall) error {
rules, err := json.Marshal(fw.PolicyList)
if err != nil {
return fmt.Errorf("could not marshal firewall policies: %v", err)
}
form := map[string]interface{}{
"CID": c.CID,
"action": "insert_stateful_firewall_rules",
"gateway_name": fw.GwName,
"rules": string(rules),
"position": strconv.Itoa(fw.PolicyList[0].Position),
}
return c.PostAPIContext2(context.Background(), nil, form["action"].(string), form, BasicCheck)
}