forked from kubernetes-sigs/aws-load-balancer-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protection_manager.go
152 lines (134 loc) · 5.26 KB
/
protection_manager.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
package shield
import (
"context"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
shieldsdk "github.com/aws/aws-sdk-go/service/shield"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/cache"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/aws/services"
"time"
)
const (
defaultProtectionInfoByResourceARNCacheTTL = 10 * time.Minute
// service subscription rarely changes, cache it with longer period.
defaultSubscriptionStateCacheTTL = 2 * time.Hour
subscriptionStateCacheKey = "subscriptionState"
)
type ProtectionManager interface {
// CreateProtection creates shield protection for resource. returns protectionID.
CreateProtection(ctx context.Context, resourceARN string, protectionName string) (string, error)
// DeleteProtection deletes shield protection for resource.
DeleteProtection(ctx context.Context, resourceARN string, protectionID string) error
// GetProtection returns shield protection information for resource.
// returns nil if no protection exists.
GetProtection(ctx context.Context, resourceARN string) (*ProtectionInfo, error)
// IsSubscribed checks whether subscribed to shield service.
IsSubscribed(ctx context.Context) (bool, error)
}
func NewDefaultProtectionManager(shieldClient services.Shield, logger logr.Logger) *defaultProtectionManager {
return &defaultProtectionManager{
shieldClient: shieldClient,
logger: logger,
protectionInfoByResourceARNCache: cache.NewExpiring(),
protectionInfoByResourceARNCacheTTL: defaultProtectionInfoByResourceARNCacheTTL,
subscriptionStateCache: cache.NewExpiring(),
subscriptionStateCacheTTL: defaultSubscriptionStateCacheTTL,
}
}
var _ ProtectionManager = &defaultProtectionManager{}
type defaultProtectionManager struct {
shieldClient services.Shield
logger logr.Logger
protectionInfoByResourceARNCache *cache.Expiring
protectionInfoByResourceARNCacheTTL time.Duration
subscriptionStateCache *cache.Expiring
subscriptionStateCacheTTL time.Duration
}
type ProtectionInfo struct {
Name string
ID string
}
func (m *defaultProtectionManager) CreateProtection(ctx context.Context, resourceARN string, protectionName string) (string, error) {
req := &shieldsdk.CreateProtectionInput{
ResourceArn: awssdk.String(resourceARN),
Name: awssdk.String(protectionName),
}
m.logger.Info("enabling shield protection",
"resourceARN", resourceARN,
"protectionName", protectionName)
resp, err := m.shieldClient.CreateProtectionWithContext(ctx, req)
if err != nil {
return "", err
}
protectionID := awssdk.StringValue(resp.ProtectionId)
m.logger.Info("enabled shield protection",
"resourceARN", resourceARN,
"protectionName", protectionName,
"protectionID", protectionID)
protectionInfo := &ProtectionInfo{
Name: protectionName,
ID: protectionID,
}
m.protectionInfoByResourceARNCache.Set(resourceARN, protectionInfo, m.protectionInfoByResourceARNCacheTTL)
return protectionID, nil
}
func (m *defaultProtectionManager) DeleteProtection(ctx context.Context, resourceARN string, protectionID string) error {
req := &shieldsdk.DeleteProtectionInput{
ProtectionId: awssdk.String(protectionID),
}
m.logger.Info("disabling shield protection",
"resourceARN", resourceARN,
"protectionID", protectionID)
_, err := m.shieldClient.DeleteProtectionWithContext(ctx, req)
if err != nil {
return err
}
m.logger.Info("disabled shield protection",
"resourceARN", resourceARN)
var protectionInfo *ProtectionInfo
m.protectionInfoByResourceARNCache.Set(resourceARN, protectionInfo, m.protectionInfoByResourceARNCacheTTL)
return nil
}
func (m *defaultProtectionManager) GetProtection(ctx context.Context, resourceARN string) (*ProtectionInfo, error) {
rawCacheItem, exists := m.protectionInfoByResourceARNCache.Get(resourceARN)
if exists {
return rawCacheItem.(*ProtectionInfo), nil
}
req := &shieldsdk.DescribeProtectionInput{
ResourceArn: awssdk.String(resourceARN),
}
resp, err := m.shieldClient.DescribeProtectionWithContext(ctx, req)
var protectionInfo *ProtectionInfo
if err != nil {
aerr, ok := err.(awserr.Error)
if ok && aerr.Code() == shieldsdk.ErrCodeResourceNotFoundException {
protectionInfo = nil
} else {
return nil, err
}
}
if resp.Protection != nil {
protectionInfo = &ProtectionInfo{
Name: awssdk.StringValue(resp.Protection.Name),
ID: awssdk.StringValue(resp.Protection.Id),
}
}
m.protectionInfoByResourceARNCache.Set(resourceARN, protectionInfo, m.protectionInfoByResourceARNCacheTTL)
return protectionInfo, nil
}
func (m *defaultProtectionManager) IsSubscribed(ctx context.Context) (bool, error) {
rawCacheItem, exists := m.subscriptionStateCache.Get(subscriptionStateCacheKey)
if exists {
subscriptionState := rawCacheItem.(string)
return subscriptionState == shieldsdk.SubscriptionStateActive, nil
}
req := &shieldsdk.GetSubscriptionStateInput{}
resp, err := m.shieldClient.GetSubscriptionStateWithContext(ctx, req)
if err != nil {
return false, err
}
subscriptionState := awssdk.StringValue(resp.SubscriptionState)
m.subscriptionStateCache.Set(subscriptionStateCacheKey, subscriptionState, m.subscriptionStateCacheTTL)
return subscriptionState == shieldsdk.SubscriptionStateActive, nil
}