-
Notifications
You must be signed in to change notification settings - Fork 4
/
security_group.go
262 lines (216 loc) · 8.67 KB
/
security_group.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
package outscale
import (
"errors"
"fmt"
"net/http"
"time"
retry "github.com/avast/retry-go"
"github.com/docker/machine/libmachine/log"
osc "github.com/outscale/osc-sdk-go/v2"
)
var (
etcdPort = []int32{2379, 2380}
kubeApiPort int32 = 6443
nginxIngressHttpPort int32 = 80
nginxIngressHttpsPort int32 = 443
nodePort = []int32{30000, 32767}
kubePort = []int32{10250, 10252} // kube-scheduler and kube-controller-manager
kubeProxyPort int32 = 10256
canal1 int32 = 8472 // Canal/Flannel VXLAN overlay networking
canal2 int32 = 4789 // Flannel VXLAN overlay networking
)
func addSecurityGroupRule(d *OscDriver, sgId string, request *osc.CreateSecurityGroupRuleRequest) error {
// Get the client
oscApi, err := d.getClient()
if err != nil {
return err
}
var httpRes *http.Response
var response osc.CreateSecurityGroupRuleResponse
err = retry.Do(
func() error {
var response_error error
response, httpRes, response_error = oscApi.client.SecurityGroupRuleApi.CreateSecurityGroupRule(oscApi.context).CreateSecurityGroupRuleRequest(*request).Execute()
return wrapError(response_error, httpRes)
},
defaultThrottlingRetryOption...,
)
if err != nil {
return fmt.Errorf("Error while submitting the Security Group Rule creation request: %s", getErrorInfo(err, httpRes))
}
if !response.HasSecurityGroup() {
return errors.New("Error while creating the SecurityGroupRule")
}
return nil
}
func buildSecurityGroupRule(ipProtocol string, flow string, securityGroupId string, fromPort int32, toPort int32, ipRange string) *osc.CreateSecurityGroupRuleRequest {
securityGroupRuleRequest := osc.CreateSecurityGroupRuleRequest{}
securityGroupRuleRequest.SetIpProtocol(ipProtocol)
securityGroupRuleRequest.SetFlow(flow)
securityGroupRuleRequest.SetSecurityGroupId(securityGroupId)
securityGroupRuleRequest.SetFromPortRange(fromPort)
securityGroupRuleRequest.SetToPortRange(toPort)
securityGroupRuleRequest.SetIpRange(ipRange)
return &securityGroupRuleRequest
}
func createDefaultSecurityGroup(d *OscDriver) error {
log.Debug("Creating the Security Group")
// Get the client
oscApi, err := d.getClient()
if err != nil {
return err
}
request := osc.CreateSecurityGroupRequest{
Description: fmt.Sprintf("Security Group for docker-machine %s", d.GetMachineName()),
SecurityGroupName: fmt.Sprintf("docker-machine-%s-%d", d.GetMachineName(), time.Now().Unix()),
}
if ! d.PublicCloud {
request.SetNetId(d.netId)
}
var httpRes *http.Response
var response osc.CreateSecurityGroupResponse
err = retry.Do(
func() error {
var response_error error
response, httpRes, response_error = oscApi.client.SecurityGroupApi.CreateSecurityGroup(oscApi.context).CreateSecurityGroupRequest(request).Execute()
return wrapError(response_error, httpRes)
},
defaultThrottlingRetryOption...,
)
if err != nil {
return fmt.Errorf("Error while submitting the Security Group creation request: %s", getErrorInfo(err, httpRes))
}
if !response.HasSecurityGroup() {
return errors.New("Error while creating the SecurityGroup")
}
d.SecurityGroupId = response.SecurityGroup.GetSecurityGroupId()
// Add SSH rule
sshRuleRequest := buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, 22, 22, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, sshRuleRequest); err != nil {
log.Error("Error while adding the ssh rule in the SecurityGroup")
return err
}
// Add TCP Docker rule
dockerPortRuleRequest := buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, defaultDockerPort, defaultDockerPort, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, dockerPortRuleRequest); err != nil {
log.Error("Error while adding the docker rule in the SecurityGroup")
return err
}
// Add ETCD Port
ruleRequest := buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, etcdPort[0], etcdPort[1], "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the etcd rule in the SecurityGroup")
return err
}
// Add Kube api Port
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, kubeApiPort, kubeApiPort, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the kubeApi rule in the SecurityGroup")
return err
}
// Add nginxIngress HTTP Port
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, nginxIngressHttpPort, nginxIngressHttpPort, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the nginx ingress HTTP rule in the SecurityGroup")
return err
}
// Add nginxIngress HTTPS Port
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, nginxIngressHttpsPort, nginxIngressHttpsPort, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the nginx ingress HTTPS rule in the SecurityGroup")
return err
}
// Add node Port
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, nodePort[0], nodePort[1], "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the node port rule in the SecurityGroup")
return err
}
ruleRequest = buildSecurityGroupRule("udp", "Inbound", d.SecurityGroupId, nodePort[0], nodePort[1], "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the node port rule in the SecurityGroup")
return err
}
// Kube Port
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, kubePort[0], kubePort[1], "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the kube port rule in the SecurityGroup")
return err
}
ruleRequest = buildSecurityGroupRule("tcp", "Inbound", d.SecurityGroupId, kubeProxyPort, kubeProxyPort, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the kube port rule in the SecurityGroup")
return err
}
//Canal
ruleRequest = buildSecurityGroupRule("udp", "Inbound", d.SecurityGroupId, canal1, canal1, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the kube port rule in the SecurityGroup")
return err
}
ruleRequest = buildSecurityGroupRule("udp", "Inbound", d.SecurityGroupId, canal2, canal2, "0.0.0.0/0")
if err := addSecurityGroupRule(d, d.SecurityGroupId, ruleRequest); err != nil {
log.Error("Error while adding the kube port rule in the SecurityGroup")
return err
}
// Add extra tags
if err := addExtraTags(d, d.SecurityGroupId, d.extraTagsAll); err != nil {
return err
}
return nil
}
func deleteSecurityGroup(d *OscDriver, resourceId string) error {
log.Debug("Deletion the Security Group")
// Get the client
oscApi, err := d.getClient()
if err != nil {
return err
}
if resourceId == "" {
log.Warn("Skipping deletion of the security group because none was stored.")
return nil
}
request := osc.DeleteSecurityGroupRequest{
SecurityGroupId: &resourceId,
}
var httpRes *http.Response
err = retry.Do(
func() error {
var response_error error
_, httpRes, response_error = oscApi.client.SecurityGroupApi.DeleteSecurityGroup(oscApi.context).DeleteSecurityGroupRequest(request).Execute()
return wrapError(response_error, httpRes)
},
defaultThrottlingRetryOption...,
)
if err != nil {
return fmt.Errorf("Error while submitting the Security Group deletion request: %s", getErrorInfo(err, httpRes))
}
return nil
}
func isSecurityGroupExist(d *OscDriver, sgId string) (bool, error) {
log.Debugf("Chcek that the Security Group '%v' exists", sgId)
// Get the client
oscApi, err := d.getClient()
if err != nil {
return false, err
}
request := osc.ReadSecurityGroupsRequest{
Filters: &osc.FiltersSecurityGroup{
SecurityGroupIds: &[]string{sgId},
},
}
var httpRes *http.Response
var response osc.ReadSecurityGroupsResponse
err = retry.Do(
func() error {
var response_error error
response, httpRes, response_error = oscApi.client.SecurityGroupApi.ReadSecurityGroups(oscApi.context).ReadSecurityGroupsRequest(request).Execute()
return wrapError(response_error, httpRes)
},
defaultThrottlingRetryOption...,
)
if err != nil {
return false, fmt.Errorf("Error while submitting the Security Group Rule read request: %s", getErrorInfo(err, httpRes))
}
return response.HasSecurityGroups() && (len(response.GetSecurityGroups()) == 1), nil
}