/
securitygrouptasks.go
executable file
·329 lines (288 loc) · 8.52 KB
/
securitygrouptasks.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright 2018-2023, CS Systemes d'Information, http://csgroup.eu
*
* 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 operations
import (
"context"
"reflect"
"github.com/CS-SI/SafeScale/v22/lib/backend/resources/enums/hostproperty"
propertiesv1 "github.com/CS-SI/SafeScale/v22/lib/backend/resources/properties/v1"
"github.com/CS-SI/SafeScale/v22/lib/utils/data"
"github.com/CS-SI/SafeScale/v22/lib/utils/data/serialize"
"github.com/CS-SI/SafeScale/v22/lib/utils/debug"
"github.com/CS-SI/SafeScale/v22/lib/utils/fail"
"github.com/CS-SI/SafeScale/v22/lib/utils/valid"
"golang.org/x/sync/errgroup"
)
// taskUnbindFromHost unbinds a Host from the security group
// params is intended to receive a '*host'
func (instance *SecurityGroup) taskUnbindFromHost(
inctx context.Context, params interface{},
) (_ interface{}, ferr fail.Error) {
defer fail.OnPanic(&ferr)
if valid.IsNil(instance) {
return nil, fail.InvalidInstanceError()
}
ctx, cancel := context.WithCancel(inctx)
defer cancel()
type result struct {
rTr interface{}
rErr fail.Error
}
chRes := make(chan result)
go func() {
defer close(chRes)
hostInstance, ok := params.(*Host)
if !ok {
chRes <- result{nil, fail.InvalidParameterError("params", "must be a '*Host'")}
return
}
if hostInstance == nil {
chRes <- result{nil, fail.InvalidParameterCannotBeNilError("params")}
return
}
sgID, err := instance.GetID()
if err != nil {
chRes <- result{nil, fail.ConvertError(err)}
return
}
hid, err := hostInstance.GetID()
if err != nil {
chRes <- result{nil, fail.ConvertError(err)}
return
}
// Unbind Security Group from Host on provider side
xerr := instance.Service().UnbindSecurityGroupFromHost(ctx, sgID, hid)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
switch xerr.(type) {
case *fail.ErrNotFound:
// if the security group is not bound to the host, considered as a success and continue
debug.IgnoreError2(ctx, xerr)
default:
chRes <- result{nil, xerr}
return
}
}
// Updates host metadata regarding Security Groups
xerr = hostInstance.Alter(ctx, func(_ data.Clonable, props *serialize.JSONProperties) fail.Error {
return props.Alter(hostproperty.SecurityGroupsV1, func(clonable data.Clonable) fail.Error {
hsgV1, ok := clonable.(*propertiesv1.HostSecurityGroups)
if !ok {
return fail.InconsistentError("'*propertiesv1.HostSecurityGroups' expected, '%s' provided", reflect.TypeOf(clonable).String())
}
delete(hsgV1.ByID, sgID)
delete(hsgV1.ByName, instance.GetName())
return nil
})
})
chRes <- result{nil, xerr}
}()
select {
case res := <-chRes:
return res.rTr, res.rErr
case <-ctx.Done():
return nil, fail.ConvertError(ctx.Err())
case <-inctx.Done():
return nil, fail.ConvertError(inctx.Err())
}
}
type taskUnbindFromHostsAttachedToSubnetParams struct {
subnetID, subnetName string
subnetHosts *propertiesv1.SubnetHosts
onRemoval bool
}
// taskUnbindFromHostsAttachedToSubnet unbinds security group from hosts attached to a Subnet
// 'params' expects to be a *propertiesv1.SubnetHosts
func (instance *SecurityGroup) taskUnbindFromHostsAttachedToSubnet(
inctx context.Context, params interface{},
) (_ interface{}, ferr fail.Error) {
defer fail.OnPanic(&ferr)
if valid.IsNil(instance) {
return nil, fail.InvalidInstanceError()
}
p, ok := params.(taskUnbindFromHostsAttachedToSubnetParams)
if !ok {
return nil, fail.InvalidParameterError("params", "must be a 'taskUnbindFromHostsAttachedToSubnetParams'")
}
ctx, cancel := context.WithCancel(inctx)
defer cancel()
type result struct {
rTr interface{}
rErr fail.Error
}
chRes := make(chan result)
go func() {
defer close(chRes)
if len(p.subnetHosts.ByID) > 0 {
tg := new(errgroup.Group)
svc := instance.Service()
for k, v := range p.subnetHosts.ByID {
k, _ := k, v
tg.Go(func() error {
hostInstance, xerr := LoadHost(ctx, svc, k)
if xerr != nil {
switch xerr.(type) {
case *fail.ErrNotFound:
// if Host is not found, consider operation as a success and continue
debug.IgnoreError2(ctx, xerr)
return nil
default:
return xerr
}
}
_, err := instance.taskUnbindFromHost(ctx, hostInstance)
return err
})
}
werr := fail.ConvertError(tg.Wait())
werr = debug.InjectPlannedFail(werr)
if werr != nil {
chRes <- result{nil, werr}
return
}
}
chRes <- result{nil, nil}
}()
select {
case res := <-chRes:
return res.rTr, res.rErr
case <-ctx.Done():
return nil, fail.ConvertError(ctx.Err())
case <-inctx.Done():
return nil, fail.ConvertError(inctx.Err())
}
}
// taskBindEnabledOnHost binds the SG if needed (applying rules) and enables it on Host
// params is intended to receive a non-empty string corresponding to host ID
// returns:
// - nil, nil: everything is ok
// - nil, *fail.ErrInvalidParameter: some parameters are invalid
// - nil, *fail.ErrAborted: received abortion signal
// - nil, *fail.ErrNotFound: Host identified by params not found
func (instance *SecurityGroup) taskBindEnabledOnHost(
inctx context.Context, params interface{},
) (_ interface{}, ferr fail.Error) {
defer fail.OnPanic(&ferr)
if valid.IsNil(instance) {
return nil, fail.InvalidInstanceError()
}
ctx, cancel := context.WithCancel(inctx)
defer cancel()
type result struct {
rTr interface{}
rErr fail.Error
}
chRes := make(chan result)
go func() {
defer close(chRes)
hostID, ok := params.(string)
if !ok || hostID == "" {
chRes <- result{nil, fail.InvalidParameterError("params", "must be a non-empty string")}
return
}
hostInstance, innerXErr := LoadHost(ctx, instance.Service(), hostID)
if innerXErr != nil {
switch innerXErr.(type) {
case *fail.ErrNotFound:
// host vanished, considered as a success
debug.IgnoreError2(ctx, innerXErr)
default:
chRes <- result{nil, innerXErr}
return
}
} else {
// Before enabling SG on Host, make sure the SG is bound to Host
xerr := hostInstance.BindSecurityGroup(ctx, instance, true)
if xerr != nil {
switch xerr.(type) {
case *fail.ErrDuplicate:
chRes <- result{nil, hostInstance.EnableSecurityGroup(ctx, instance)}
return
default:
chRes <- result{nil, xerr}
return
}
}
}
chRes <- result{nil, nil}
}()
select {
case res := <-chRes:
return res.rTr, res.rErr
case <-ctx.Done():
return nil, fail.ConvertError(ctx.Err())
case <-inctx.Done():
return nil, fail.ConvertError(inctx.Err())
}
}
// taskBindDisabledOnHost removes rules of security group from host
// params is intended to receive a non-empty string corresponding to host ID
func (instance *SecurityGroup) taskBindDisabledOnHost(
inctx context.Context, params interface{},
) (_ interface{}, ferr fail.Error) {
defer fail.OnPanic(&ferr)
if valid.IsNil(instance) {
return nil, fail.InvalidInstanceError()
}
ctx, cancel := context.WithCancel(inctx)
defer cancel()
type result struct {
rTr interface{}
rErr fail.Error
}
chRes := make(chan result)
go func() {
defer close(chRes)
hostID, ok := params.(string)
if !ok || hostID == "" {
chRes <- result{nil, fail.InvalidParameterError("params", "must be a non-empty string")}
return
}
svc := instance.Service()
hostInstance, innerXErr := LoadHost(ctx, svc, hostID)
if innerXErr != nil {
switch innerXErr.(type) {
case *fail.ErrNotFound:
// host vanished, considered as a success
debug.IgnoreError2(ctx, innerXErr)
default:
chRes <- result{nil, innerXErr}
return
}
} else {
xerr := hostInstance.BindSecurityGroup(ctx, instance, false)
if xerr != nil {
switch xerr.(type) {
case *fail.ErrDuplicate:
chRes <- result{nil, hostInstance.DisableSecurityGroup(ctx, instance)}
return
default:
chRes <- result{nil, xerr}
return
}
}
}
chRes <- result{nil, nil}
}()
select {
case res := <-chRes:
return res.rTr, res.rErr
case <-ctx.Done():
return nil, fail.ConvertError(ctx.Err())
case <-inctx.Done():
return nil, fail.ConvertError(inctx.Err())
}
}