-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
securitygrouprules.go
204 lines (180 loc) · 5.79 KB
/
securitygrouprules.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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mocknetworking
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"github.com/google/uuid"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
)
type ruleListResponse struct {
SecurityGroupRules []rules.SecGroupRule `json:"security_group_rules"`
}
type ruleGetResponse struct {
SecurityGroupRule rules.SecGroupRule `json:"security_group_rule"`
}
type ruleCreateRequest struct {
SecurityGroupRule rules.CreateOpts `json:"security_group_rule"`
}
func (m *MockClient) mockSecurityGroupRules() {
re := regexp.MustCompile(`/security-group-rules/?`)
handler := func(w http.ResponseWriter, r *http.Request) {
m.mutex.Lock()
defer m.mutex.Unlock()
w.Header().Add("Content-Type", "application/json")
sgrID := re.ReplaceAllString(r.URL.Path, "")
switch r.Method {
case http.MethodGet:
if sgrID == "" {
r.ParseForm()
m.listSecurityGroupRules(w, r.Form)
} else {
m.getSecurityGroupRule(w, sgrID)
}
case http.MethodPost:
m.createSecurityGroupRule(w, r)
case http.MethodDelete:
m.deleteSecurityGroupRule(w, sgrID)
default:
w.WriteHeader(http.StatusBadRequest)
}
}
m.Mux.HandleFunc("/security-group-rules/", handler)
m.Mux.HandleFunc("/security-group-rules", handler)
}
func (m *MockClient) listSecurityGroupRules(w http.ResponseWriter, vals url.Values) {
w.WriteHeader(http.StatusOK)
sgrs := filterRules(m.securityGroupRules, vals)
resp := ruleListResponse{
SecurityGroupRules: sgrs,
}
respB, err := json.Marshal(resp)
if err != nil {
panic(fmt.Sprintf("failed to marshal %+v", resp))
}
_, err = w.Write(respB)
if err != nil {
panic("failed to write body")
}
}
func (m *MockClient) getSecurityGroupRule(w http.ResponseWriter, ruleID string) {
if rule, ok := m.securityGroupRules[ruleID]; ok {
resp := ruleGetResponse{
SecurityGroupRule: rule,
}
respB, err := json.Marshal(resp)
if err != nil {
panic(fmt.Sprintf("failed to marshal %+v", resp))
}
_, err = w.Write(respB)
if err != nil {
panic("failed to write body")
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func (m *MockClient) deleteSecurityGroupRule(w http.ResponseWriter, ruleID string) {
if _, ok := m.securityGroupRules[ruleID]; ok {
delete(m.securityGroupRules, ruleID)
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func (m *MockClient) createSecurityGroupRule(w http.ResponseWriter, r *http.Request) {
var create ruleCreateRequest
err := json.NewDecoder(r.Body).Decode(&create)
if err != nil {
panic("error decoding create rule request")
}
w.WriteHeader(http.StatusAccepted)
rule := rules.SecGroupRule{
ID: uuid.New().String(),
PortRangeMax: create.SecurityGroupRule.PortRangeMax,
PortRangeMin: create.SecurityGroupRule.PortRangeMin,
Protocol: string(create.SecurityGroupRule.Protocol),
RemoteIPPrefix: create.SecurityGroupRule.RemoteIPPrefix,
EtherType: string(create.SecurityGroupRule.EtherType),
RemoteGroupID: create.SecurityGroupRule.RemoteGroupID,
Direction: string(create.SecurityGroupRule.Direction),
SecGroupID: create.SecurityGroupRule.SecGroupID,
}
m.securityGroupRules[rule.ID] = rule
resp := ruleGetResponse{
SecurityGroupRule: rule,
}
respB, err := json.Marshal(resp)
if err != nil {
panic(fmt.Sprintf("failed to marshal %+v", resp))
}
_, err = w.Write(respB)
if err != nil {
panic("failed to write body")
}
}
func filterRules(allRules map[string]rules.SecGroupRule, vals url.Values) []rules.SecGroupRule {
sgrs := make([]rules.SecGroupRule, 0)
securityGroupIDFilter := vals.Get("security_group_id")
directionFilter := vals.Get("direction")
ethertypeFilter := vals.Get("ethertype")
portRangeMaxFilter := vals.Get("port_range_max")
portRangeMinFilter := vals.Get("port_range_min")
protocolFilter := vals.Get("protocol")
remoteGroupIDFilter := vals.Get("remote_group_id")
// Example query string from cloudup
// ?direction=ingressðertype=IPv4&port_range_max=53&port_range_min=53&protocol=udp&remote_group_id=3b39402b-320c-4e18-a2a1-11b5577a850f&security_group_id=df829d89-637c-4aff-8a46-716977f73464
for _, s := range allRules {
if securityGroupIDFilter != "" && s.SecGroupID != securityGroupIDFilter {
continue
}
if directionFilter != "" && s.Direction != directionFilter {
continue
}
if ethertypeFilter != "" && s.EtherType != ethertypeFilter {
continue
}
if portRangeMaxFilter != "" {
portRangeMax, err := strconv.ParseInt(portRangeMaxFilter, 10, 64)
if err != nil {
panic(fmt.Sprintf("failed to parse port_range_max parameter %v", err))
}
if int64(s.PortRangeMax) != portRangeMax {
continue
}
}
if portRangeMinFilter != "" {
portRangeMin, err := strconv.ParseInt(portRangeMinFilter, 10, 64)
if err != nil {
panic(fmt.Sprintf("failed to parse port_range_max parameter %v", err))
}
if int64(s.PortRangeMin) != portRangeMin {
continue
}
}
if protocolFilter != "" && s.Protocol != protocolFilter {
continue
}
// If a query doesn't provide remote_group_id this indicates we want to filter for rules
// with an empty string value rather than not filter for remote_group_id
if s.RemoteGroupID != remoteGroupIDFilter {
continue
}
sgrs = append(sgrs, s)
}
return sgrs
}