/
access_log_subscription_manager.go
230 lines (209 loc) · 7.67 KB
/
access_log_subscription_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
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
package lattice
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/vpclattice"
an_aws "github.com/aws/aws-application-networking-k8s/pkg/aws"
"github.com/aws/aws-application-networking-k8s/pkg/aws/services"
"github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
)
//go:generate mockgen -destination access_log_subscription_manager_mock.go -package lattice github.com/aws/aws-application-networking-k8s/pkg/deploy/lattice AccessLogSubscriptionManager
type AccessLogSubscriptionManager interface {
Create(ctx context.Context, accessLogSubscription *lattice.AccessLogSubscription) (*lattice.AccessLogSubscriptionStatus, error)
Update(ctx context.Context, accessLogSubscription *lattice.AccessLogSubscription) (*lattice.AccessLogSubscriptionStatus, error)
Delete(ctx context.Context, accessLogSubscriptionArn string) error
}
type defaultAccessLogSubscriptionManager struct {
log gwlog.Logger
cloud an_aws.Cloud
}
func NewAccessLogSubscriptionManager(
log gwlog.Logger,
cloud an_aws.Cloud,
) *defaultAccessLogSubscriptionManager {
return &defaultAccessLogSubscriptionManager{
log: log,
cloud: cloud,
}
}
func (m *defaultAccessLogSubscriptionManager) Create(
ctx context.Context,
accessLogSubscription *lattice.AccessLogSubscription,
) (*lattice.AccessLogSubscriptionStatus, error) {
vpcLatticeSess := m.cloud.Lattice()
sourceArn, err := m.getSourceArn(ctx, accessLogSubscription.Spec.SourceType, accessLogSubscription.Spec.SourceName)
if err != nil {
return nil, err
}
tags := m.cloud.DefaultTagsMergedWith(services.Tags{
lattice.AccessLogPolicyTagKey: aws.String(accessLogSubscription.Spec.ALPNamespacedName.String()),
})
createALSInput := &vpclattice.CreateAccessLogSubscriptionInput{
ResourceIdentifier: sourceArn,
DestinationArn: &accessLogSubscription.Spec.DestinationArn,
Tags: tags,
}
createALSOutput, err := vpcLatticeSess.CreateAccessLogSubscriptionWithContext(ctx, createALSInput)
if err == nil {
return &lattice.AccessLogSubscriptionStatus{
Arn: *createALSOutput.Arn,
}, nil
}
switch e := err.(type) {
case *vpclattice.AccessDeniedException:
return nil, services.NewInvalidError(e.Message())
case *vpclattice.ResourceNotFoundException:
if *e.ResourceType == "SERVICE_NETWORK" || *e.ResourceType == "SERVICE" {
return nil, services.NewNotFoundError(string(accessLogSubscription.Spec.SourceType), accessLogSubscription.Spec.SourceName)
}
return nil, services.NewInvalidError(e.Message())
case *vpclattice.ConflictException:
/*
* Conflict may arise if we retry creation due to a failure elsewhere in the controller,
* so we check if the conflicting ALS was created for the same ALP via its tags.
* If it is the same ALP, return success. Else, return ConflictError.
*/
listALSInput := &vpclattice.ListAccessLogSubscriptionsInput{
ResourceIdentifier: sourceArn,
}
listALSOutput, err := vpcLatticeSess.ListAccessLogSubscriptionsWithContext(ctx, listALSInput)
if err != nil {
return nil, err
}
for _, als := range listALSOutput.Items {
if *als.DestinationArn == accessLogSubscription.Spec.DestinationArn {
listTagsInput := &vpclattice.ListTagsForResourceInput{
ResourceArn: als.Arn,
}
listTagsOutput, err := vpcLatticeSess.ListTagsForResourceWithContext(ctx, listTagsInput)
if err != nil {
return nil, err
}
value, exists := listTagsOutput.Tags[lattice.AccessLogPolicyTagKey]
if exists && *value == accessLogSubscription.Spec.ALPNamespacedName.String() {
return &lattice.AccessLogSubscriptionStatus{
Arn: *als.Arn,
}, nil
}
}
}
return nil, services.NewConflictError(
string(accessLogSubscription.Spec.SourceType),
accessLogSubscription.Spec.SourceName,
e.Message(),
)
default:
return nil, err
}
}
func (m *defaultAccessLogSubscriptionManager) Update(
ctx context.Context,
accessLogSubscription *lattice.AccessLogSubscription,
) (*lattice.AccessLogSubscriptionStatus, error) {
vpcLatticeSess := m.cloud.Lattice()
// If the source is modified, we need to replace the ALS
getALSInput := &vpclattice.GetAccessLogSubscriptionInput{
AccessLogSubscriptionIdentifier: aws.String(accessLogSubscription.Status.Arn),
}
getALSOutput, err := vpcLatticeSess.GetAccessLogSubscriptionWithContext(ctx, getALSInput)
if err != nil {
switch e := err.(type) {
case *vpclattice.AccessDeniedException:
return nil, services.NewInvalidError(e.Message())
case *vpclattice.ResourceNotFoundException:
return m.Create(ctx, accessLogSubscription)
default:
return nil, err
}
}
sourceArn, err := m.getSourceArn(ctx, accessLogSubscription.Spec.SourceType, accessLogSubscription.Spec.SourceName)
if err != nil {
return nil, err
}
if *getALSOutput.ResourceArn != *sourceArn {
return m.replaceAccessLogSubscription(ctx, accessLogSubscription)
}
// Source is not modified, try to update destinationArn in the existing ALS
updateALSInput := &vpclattice.UpdateAccessLogSubscriptionInput{
AccessLogSubscriptionIdentifier: aws.String(accessLogSubscription.Status.Arn),
DestinationArn: aws.String(accessLogSubscription.Spec.DestinationArn),
}
updateALSOutput, err := vpcLatticeSess.UpdateAccessLogSubscriptionWithContext(ctx, updateALSInput)
if err == nil {
return &lattice.AccessLogSubscriptionStatus{
Arn: *updateALSOutput.Arn,
}, nil
}
switch e := err.(type) {
case *vpclattice.AccessDeniedException:
return nil, services.NewInvalidError(e.Message())
case *vpclattice.ResourceNotFoundException:
if *e.ResourceType == "SERVICE_NETWORK" || *e.ResourceType == "SERVICE" {
return nil, services.NewNotFoundError(string(accessLogSubscription.Spec.SourceType), accessLogSubscription.Spec.SourceName)
}
return m.Create(ctx, accessLogSubscription)
case *vpclattice.ConflictException:
/*
* A conflict can happen when the destination type of the new ALS is different from the original.
* To gracefully handle this, we create a new ALS with the new destination, then delete the old one.
*/
return m.replaceAccessLogSubscription(ctx, accessLogSubscription)
default:
return nil, err
}
}
func (m *defaultAccessLogSubscriptionManager) Delete(
ctx context.Context,
accessLogSubscriptionArn string,
) error {
vpcLatticeSess := m.cloud.Lattice()
deleteALSInput := &vpclattice.DeleteAccessLogSubscriptionInput{
AccessLogSubscriptionIdentifier: aws.String(accessLogSubscriptionArn),
}
_, err := vpcLatticeSess.DeleteAccessLogSubscriptionWithContext(ctx, deleteALSInput)
if err != nil {
if _, ok := err.(*vpclattice.ResourceNotFoundException); !ok {
return err
}
}
return nil
}
func (m *defaultAccessLogSubscriptionManager) getSourceArn(
ctx context.Context,
sourceType lattice.SourceType,
sourceName string,
) (*string, error) {
vpcLatticeSess := m.cloud.Lattice()
switch sourceType {
case lattice.ServiceNetworkSourceType:
serviceNetwork, err := vpcLatticeSess.FindServiceNetwork(ctx, sourceName)
if err != nil {
return nil, err
}
return serviceNetwork.SvcNetwork.Arn, nil
case lattice.ServiceSourceType:
service, err := vpcLatticeSess.FindService(ctx, sourceName)
if err != nil {
return nil, err
}
return service.Arn, nil
default:
return nil, fmt.Errorf("unsupported source type: %s", sourceType)
}
}
func (m *defaultAccessLogSubscriptionManager) replaceAccessLogSubscription(
ctx context.Context,
accessLogSubscription *lattice.AccessLogSubscription,
) (*lattice.AccessLogSubscriptionStatus, error) {
newAlsStatus, err := m.Create(ctx, accessLogSubscription)
if err != nil {
return nil, err
}
err = m.Delete(ctx, accessLogSubscription.Status.Arn)
if err != nil {
return nil, err
}
return newAlsStatus, nil
}