This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_block_api_calls.go
184 lines (150 loc) · 6.11 KB
/
create_block_api_calls.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
package masters
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-11-01/network"
"github.com/Azure/go-autorest/autorest/to"
"github.com/giantswarm/microerror"
"github.com/giantswarm/azure-operator/service/controller/internal/state"
"github.com/giantswarm/azure-operator/service/controller/key"
)
const (
temporarySecurityRuleName = "temporaryFlatcarMigration"
)
type namedRule struct {
name string
rule *network.SecurityRule
}
func (r *Resource) blockAPICallsTransition(ctx context.Context, obj interface{}, currentState state.State) (state.State, error) {
cr, err := key.ToCustomResource(obj)
if err != nil {
return "", microerror.Mask(err)
}
rules := getMasterRules()
masterFound, err := r.ensureSecurityRules(ctx, key.ResourceGroupName(cr), key.MasterSecurityGroupName(cr), rules)
if err != nil {
return "", microerror.Mask(err)
}
if !masterFound {
r.logger.LogCtx(ctx, "level", "debug", "message", "Security rules not in place yet")
return currentState, nil
}
r.logger.LogCtx(ctx, "level", "debug", "message", "Security rules found")
return DeploymentUninitialized, nil
}
func (r *Resource) ensureSecurityRules(ctx context.Context, resourceGroup string, securityGroupName string, rules []namedRule) (bool, error) {
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("Looking for existence of %d security rules in security group %s", len(rules), securityGroupName))
found := true
for _, rule := range rules {
ruleName := rule.name
exists, err := r.securityRuleExists(ctx, resourceGroup, securityGroupName, ruleName)
if err != nil {
return false, microerror.Mask(err)
}
if !exists {
found = false
// Create security rule
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("Creating security rule %s", *rule.rule.Description))
err = r.createSecurityRule(ctx, resourceGroup, securityGroupName, ruleName, *rule.rule)
if err != nil {
return false, microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("Created security rule %s", *rule.rule.Description))
}
}
// The 'found' bool is true if all rules are in place, false otherwise.
return found, nil
}
func (r *Resource) unblockAPICallsTransition(ctx context.Context, obj interface{}, currentState state.State) (state.State, error) {
cr, err := key.ToCustomResource(obj)
if err != nil {
return currentState, microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("deleting security rule %s from security group %s", temporarySecurityRuleName, key.WorkerSecurityGroupName(cr)))
rules := getMasterRules()
for _, namedRule := range rules {
// Delete security rule for masters.
err = r.deleteSecurityRule(ctx, key.ResourceGroupName(cr), key.MasterSecurityGroupName(cr), namedRule.name)
if IsNotFound(err) {
// Rule not exists, ok to continue.
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("security rule %s from security group %s was not found", namedRule.name, key.MasterSecurityGroupName(cr)))
} else if err != nil {
// In case of error just retry.
return currentState, microerror.Mask(err)
}
}
r.logger.LogCtx(ctx, "level", "debug", "message", "deleted temporary security rules")
return RestartKubeletOnWorkers, nil
}
func (r *Resource) createSecurityRule(ctx context.Context, resourceGroup string, securityGroupname string, securityRuleName string, securityRule network.SecurityRule) error {
c, err := r.getSecurityRulesClient(ctx)
if err != nil {
return microerror.Mask(err)
}
_, err = c.CreateOrUpdate(context.Background(), resourceGroup, securityGroupname, securityRuleName, securityRule)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *Resource) securityRuleExists(ctx context.Context, resourceGroup string, securityGroupname string, securityRuleName string) (bool, error) {
c, err := r.getSecurityRulesClient(ctx)
if err != nil {
return false, microerror.Mask(err)
}
_, err = c.Get(context.Background(), resourceGroup, securityGroupname, securityRuleName)
if IsNotFound(err) {
return false, nil
} else if err != nil {
return false, microerror.Mask(err)
}
return true, nil
}
func (r *Resource) deleteSecurityRule(ctx context.Context, resourceGroup string, securityGroupname string, securityRuleName string) error {
c, err := r.getSecurityRulesClient(ctx)
if err != nil {
return microerror.Mask(err)
}
_, err = c.Delete(context.Background(), resourceGroup, securityGroupname, securityRuleName)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func getMasterRules() []namedRule {
mastersRules := []namedRule{
{
name: fmt.Sprintf("%s-outbound", temporarySecurityRuleName),
rule: &network.SecurityRule{
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessDeny,
Description: to.StringPtr("Temporarily block internet access to masters during flatcar migration"),
DestinationPortRange: to.StringPtr("*"),
DestinationAddressPrefix: to.StringPtr("*"),
Direction: network.SecurityRuleDirectionOutbound,
Protocol: network.SecurityRuleProtocolAsterisk,
Priority: to.Int32Ptr(3000),
SourceAddressPrefix: to.StringPtr("*"),
SourcePortRange: to.StringPtr("*"),
},
},
},
{
name: fmt.Sprintf("%s-inbound", temporarySecurityRuleName),
rule: &network.SecurityRule{
SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{
Access: network.SecurityRuleAccessDeny,
Description: to.StringPtr("Temporarily block incoming traffic to masters' 443 port during flatcar migration"),
DestinationPortRange: to.StringPtr("443"),
DestinationAddressPrefix: to.StringPtr("*"),
Direction: network.SecurityRuleDirectionInbound,
Protocol: network.SecurityRuleProtocolAsterisk,
Priority: to.Int32Ptr(3001),
SourceAddressPrefix: to.StringPtr("*"),
SourcePortRange: to.StringPtr("*"),
},
},
},
}
return mastersRules
}