forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirewall_rule.go
265 lines (202 loc) · 5.86 KB
/
firewall_rule.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package govultr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
)
// FireWallRuleService is the interface to interact with the firewall rule endpoints on the Vultr API
// Link: https://www.vultr.com/api/#firewall
type FireWallRuleService interface {
Create(ctx context.Context, groupID, protocol, port, network, notes string) (*FirewallRule, error)
Delete(ctx context.Context, groupID, ruleID string) error
ListByIPType(ctx context.Context, groupID, ipType string) ([]FirewallRule, error)
List(ctx context.Context, groupID string) ([]FirewallRule, error)
}
// FireWallRuleServiceHandler handles interaction with the firewall rule methods for the Vultr API
type FireWallRuleServiceHandler struct {
client *Client
}
// FirewallRule represents a Vultr firewall rule
type FirewallRule struct {
RuleNumber int `json:"rulenumber"`
Action string `json:"action"`
Protocol string `json:"protocol"`
Port string `json:"port"`
Network *net.IPNet `json:"network"`
Notes string `json:"notes"`
}
// UnmarshalJSON implements a custom unmarshaler on FirewallRule
// This is done to help reduce data inconsistency with V1 of the Vultr API
// It also merges the subnet & subnet_mask into a single type of *net.IPNet
func (f *FirewallRule) UnmarshalJSON(data []byte) (err error) {
if f == nil {
*f = FirewallRule{}
}
// Pull out all of the data that was given to us and put it into a map
var fields map[string]interface{}
err = json.Unmarshal(data, &fields)
if err != nil {
return err
}
// Unmarshal RuleNumber
value := fmt.Sprintf("%v", fields["rulenumber"])
number, _ := strconv.Atoi(value)
f.RuleNumber = number
// Unmarshal all other strings
action := fmt.Sprintf("%v", fields["action"])
if action == "<nil>" {
action = ""
}
f.Action = action
protocol := fmt.Sprintf("%v", fields["protocol"])
if protocol == "<nil>" {
protocol = ""
}
f.Protocol = protocol
port := fmt.Sprintf("%v", fields["port"])
if port == "<nil>" {
port = ""
}
f.Port = port
notes := fmt.Sprintf("%v", fields["notes"])
if notes == "<nil>" {
notes = ""
}
f.Notes = notes
// Unmarshal subnet_size & subnet and convert to *net.IP
value = fmt.Sprintf("%v", fields["subnet_size"])
if len(value) == 0 || value == "<nil>" {
value = "0"
}
subnetSize, _ := strconv.Atoi(value)
subnet := fmt.Sprintf("%v", fields["subnet"])
if subnet == "<nil>" {
subnet = ""
}
if len(subnet) > 0 {
_, ipNet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", subnet, subnetSize))
if err != nil {
return errors.New("an issue has occurred while parsing subnet")
}
f.Network = ipNet
}
return
}
// Create will create a rule in a firewall group.
func (f *FireWallRuleServiceHandler) Create(ctx context.Context, groupID, protocol, port, cdirBlock, notes string) (*FirewallRule, error) {
uri := "/v1/firewall/rule_create"
ip, ipNet, err := net.ParseCIDR(cdirBlock)
if err != nil {
return nil, err
}
values := url.Values{
"FIREWALLGROUPID": {groupID},
"direction": {"in"},
"protocol": {protocol},
"subnet": {ip.String()},
}
// mask
mask, _ := ipNet.Mask.Size()
values.Add("subnet_size", strconv.Itoa(mask))
// ip Type
if ipNet.IP.To4() != nil {
values.Add("ip_type", "v4")
} else {
values.Add("ip_type", "v6")
}
// Optional params
if port != "" {
values.Add("port", port)
}
if notes != "" {
values.Add("notes", notes)
}
req, err := f.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return nil, err
}
firewallRule := new(FirewallRule)
err = f.client.DoWithContext(ctx, req, firewallRule)
if err != nil {
return nil, err
}
return firewallRule, nil
}
// Delete will delete a firewall rule on your Vultr account
func (f *FireWallRuleServiceHandler) Delete(ctx context.Context, groupID, ruleID string) error {
uri := "/v1/firewall/rule_delete"
values := url.Values{
"FIREWALLGROUPID": {groupID},
"rulenumber": {ruleID},
}
req, err := f.client.NewRequest(ctx, http.MethodPost, uri, values)
if err != nil {
return err
}
err = f.client.DoWithContext(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// List will list the current firewall rules in a firewall group.
// ipType values that can be passed in are "v4", "v6"
func (f *FireWallRuleServiceHandler) ListByIPType(ctx context.Context, groupID, ipType string) ([]FirewallRule, error) {
uri := "/v1/firewall/rule_list"
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("FIREWALLGROUPID", groupID)
q.Add("direction", "in")
q.Add("ip_type", ipType)
req.URL.RawQuery = q.Encode()
var firewallRuleMap map[string]FirewallRule
err = f.client.DoWithContext(ctx, req, &firewallRuleMap)
if err != nil {
return nil, err
}
var firewallRule []FirewallRule
for _, f := range firewallRuleMap {
firewallRule = append(firewallRule, f)
}
return firewallRule, nil
}
// List will return both ipv4 an ipv6 firewall rules that are defined within a firewall group
func (f *FireWallRuleServiceHandler) List(ctx context.Context, groupID string) ([]FirewallRule, error) {
uri := "/v1/firewall/rule_list"
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("FIREWALLGROUPID", groupID)
q.Add("direction", "in")
q.Add("ip_type", "v4")
req.URL.RawQuery = q.Encode()
var firewallRuleMap map[string]FirewallRule
// V4 call
err = f.client.DoWithContext(ctx, req, &firewallRuleMap)
if err != nil {
return nil, err
}
// V6 call
q.Del("ip_type")
q.Add("ip_type", "v6")
req.URL.RawQuery = q.Encode()
err = f.client.DoWithContext(ctx, req, &firewallRuleMap)
if err != nil {
return nil, err
}
var firewallRule []FirewallRule
for _, f := range firewallRuleMap {
firewallRule = append(firewallRule, f)
}
return firewallRule, nil
}