/
pubsub.go
471 lines (394 loc) · 16.5 KB
/
pubsub.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Copyright 2022 TriggerMesh Inc.
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 googlecloudauditlogssource
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/controller"
"knative.dev/pkg/reconciler"
"cloud.google.com/go/pubsub"
commonv1alpha1 "github.com/triggermesh/triggermesh/pkg/apis/common/v1alpha1"
"github.com/triggermesh/triggermesh/pkg/apis/sources"
"github.com/triggermesh/triggermesh/pkg/apis/sources/v1alpha1"
"github.com/triggermesh/triggermesh/pkg/reconciler/event"
"github.com/triggermesh/triggermesh/pkg/reconciler/skip"
)
const (
pubsubCollectionTopics = "topics"
pubsubCollectionSubs = "subscriptions"
pubsubLabelOwnerResource = "io-triggermesh_owner-resource"
pubsubLabelOwnerNamespace = "io-triggermesh_owner-namespace"
pubsubLabelOwnerName = "io-triggermesh_owner-name"
)
// EnsurePubSub ensures the existence of a Pub/Sub topic and associated
// subscription for receiving change notifications from a Cloud Audit Logs resource.
func EnsurePubSub(ctx context.Context, cli *pubsub.Client) (*v1alpha1.GCloudResourceName /*topic*/, error) {
if skip.Skip(ctx) {
return nil, nil
}
topic, err := ensurePubSubTopic(ctx, cli)
if err != nil {
return nil, fmt.Errorf("failed to reconcile Pub/Sub topic: %w", err)
}
if err := ensurePubSubSubscription(ctx, cli, topic); err != nil {
return nil, fmt.Errorf("failed to reconcile Pub/Sub subscription: %w", err)
}
return topic, nil
}
// EnsureNoPubSub ensures the Pub/Sub topic and associated subscription used
// for receiving change notifications from a Cloud Audit Logs resource are deleted.
func EnsureNoPubSub(ctx context.Context, cli *pubsub.Client) error {
if skip.Skip(ctx) {
return nil
}
if err := ensureNoPubSubSubscription(ctx, cli); err != nil {
return fmt.Errorf("failed to clean up Pub/Sub subscription: %w", err)
}
if err := ensureNoPubSubTopic(ctx, cli); err != nil {
return fmt.Errorf("failed to clean up Pub/Sub topic: %w", err)
}
return nil
}
// ensurePubSubTopic ensures the existence of a Pub/Sub topic for receiving
// change notifications from the Cloud Audit Logs resource.
// Required permissions:
// - pubsub.topic.get
// - pubsub.topics.create
func ensurePubSubTopic(ctx context.Context, cli *pubsub.Client) (*v1alpha1.GCloudResourceName, error) {
src := commonv1alpha1.ReconcilableFromContext(ctx).(*v1alpha1.GoogleCloudAuditLogsSource)
status := &src.Status
if userProvided := src.Spec.PubSub.Topic; userProvided != nil {
topicResName := userProvided
// safeguard, in case the OpenAPI validation let a bad resource name through
if userProvided.Collection != pubsubCollectionTopics {
status.MarkNotSubscribed(v1alpha1.ReasonFailedSync, "Not a topic: "+topicResName.String())
return nil, controller.NewPermanentError(
reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"The provided resource name %q does not represent a Pub/Sub topic", topicResName),
)
}
topic := cli.Topic(userProvided.Resource)
exists, err := topic.Exists(ctx)
switch {
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Access denied to Pub/Sub API: "+toErrMsg(err))
return nil, controller.NewPermanentError(failGetTopicEvent(topicResName.String(), err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Cannot look up topic: "+toErrMsg(err))
// wrap any other error to fail the reconciliation
return nil, fmt.Errorf("%w", failGetTopicEvent(topicResName.String(), err))
}
if !exists {
status.MarkNotSubscribed(v1alpha1.ReasonFailedSync,
"Provided topic does not exist: "+topicResName.String())
return nil, controller.NewPermanentError(
reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"The provided topic %q does not exist", topicResName),
)
}
status.Topic = topicResName
return userProvided, nil
}
topicID := pubsubResourceID(src)
topic := cli.Topic(topicID)
exists, err := topic.Exists(ctx)
switch {
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Access denied to Pub/Sub API: "+toErrMsg(err))
return nil, controller.NewPermanentError(failGetTopicEvent(topic.String(), err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Cannot look up topic: "+toErrMsg(err))
// wrap any other error to fail the reconciliation
return nil, fmt.Errorf("%w", failGetTopicEvent(topic.String(), err))
}
if !exists {
cfg := &pubsub.TopicConfig{
Labels: pubsubResourceLabels(src),
}
_, err = cli.CreateTopicWithConfig(ctx, topicID, cfg)
switch {
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Access denied to Pub/Sub API: "+toErrMsg(err))
return nil, controller.NewPermanentError(failCreateTopicEvent(topicID, err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Cannot create topic: "+toErrMsg(err))
// wrap any other error to fail the reconciliation
return nil, fmt.Errorf("%w", failCreateTopicEvent(topicID, err))
}
event.Normal(ctx, ReasonSubscribed, "Created topic %q", topicID)
}
topicResName := &v1alpha1.GCloudResourceName{}
if err := json.Unmarshal([]byte(strconv.Quote(topic.String())), topicResName); err != nil {
return nil, fmt.Errorf("failed to deserialize topic name: %w", err)
}
status.Topic = topicResName
return topicResName, nil
}
// ensureNoPubSubTopic ensures that the Pub/Sub topic created for receiving
// messages from the Cloud Audit Logs resource is deleted.
// Required permissions:
// - pubsub.topics.get
// - pubsub.topics.delete
func ensureNoPubSubTopic(ctx context.Context, cli *pubsub.Client) error {
src := commonv1alpha1.ReconcilableFromContext(ctx).(*v1alpha1.GoogleCloudAuditLogsSource)
status := src.Status
if src.Spec.PubSub.Topic != nil {
// do not delete topics managed by the user
return nil
}
topicResName := status.Topic
if topicResName == nil {
// topic was possibly never created
return nil
}
topic := cli.Topic(topicResName.Resource)
cfg, err := topic.Config(ctx)
switch {
case isDenied(err):
// it is unlikely that we recover from auth errors in the
// finalizer, so we simply record a warning event and return
event.Warn(ctx, ReasonFailedUnsubscribe,
"Access denied to Pub/Sub API. Ignoring: %s", toErrMsg(err))
return nil
case isNotFound(err):
event.Warn(ctx, ReasonUnsubscribed,
fmt.Sprintf("Topic %q not found, skipping deletion", topic))
return nil
case err != nil:
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedUnsubscribe,
"Failed to get Pub/Sub topic %q: %s", topic, toErrMsg(err))
}
if !assertPubSubTopicOwnership(src, cfg) {
event.Warn(ctx, ReasonUnsubscribed, "Topic %q is not owned by this source instance. "+
"Skipping deletion", topic)
return nil
}
err = topic.Delete(ctx)
switch {
case isDenied(err):
// it is unlikely that we recover from auth errors in the
// finalizer, so we simply record a warning event and return
event.Warn(ctx, ReasonFailedUnsubscribe,
"Access denied to Pub/Sub API. Ignoring: %s", toErrMsg(err))
return nil
case err != nil:
return failDeleteTopicEvent(topic.String(), err)
}
// do not return a reconciler.Event here, even of type "Normal",
// otherwise the caller assumes an error occured
event.Normal(ctx, ReasonUnsubscribed, "Deleted topic %q", topic)
return nil
}
// ensurePubSubSubscription ensures the existence of a Pub/Sub subscription for
// receiving messages from the desired topic.
// Required permissions:
// - pubsub.subscriptions.get
// - pubsub.subscriptions.create
// - pubsub.topics.attachSubscription
func ensurePubSubSubscription(ctx context.Context, cli *pubsub.Client, topicResName *v1alpha1.GCloudResourceName) error {
src := commonv1alpha1.ReconcilableFromContext(ctx).(*v1alpha1.GoogleCloudAuditLogsSource)
status := &src.Status
// safeguard, in case the function is called with a bad resource name
if topicResName.Collection != pubsubCollectionTopics {
status.MarkNotSubscribed(v1alpha1.ReasonFailedSync, "Not a topic: "+topicResName.String())
return controller.NewPermanentError(reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"The provided resource name %q does not represent a Pub/Sub topic", topicResName))
}
subsID := pubsubResourceID(src)
subs := cli.Subscription(subsID)
exists, err := subs.Exists(ctx)
switch {
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Access denied to Pub/Sub API: "+toErrMsg(err))
return controller.NewPermanentError(failGetSubscriptionEvent(topicResName.String(), err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Cannot look up subscription: "+toErrMsg(err))
return fmt.Errorf("%w", failGetSubscriptionEvent(topicResName.String(), err))
}
if !exists {
cfg := pubsub.SubscriptionConfig{
Topic: cli.Topic(topicResName.Resource),
Labels: pubsubResourceLabels(src),
}
_, err := cli.CreateSubscription(ctx, subsID, cfg)
switch {
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Access denied to Pub/Sub API: "+toErrMsg(err))
return controller.NewPermanentError(failCreateSubscriptionEvent(topicResName.String(), err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.GCloudReasonAPIError,
"Cannot subscribe to topic: "+toErrMsg(err))
return fmt.Errorf("%w", failCreateSubscriptionEvent(topicResName.String(), err))
}
}
// it is essential that we propagate the subscription's name
// here, otherwise BuildAdapter() won't be able to configure
// the Pub/Sub adapter properly
status.Subscription = makeSubscriptionResourceName(topicResName.Project, subsID)
if !status.GetCondition(v1alpha1.GoogleCloudAuditLogsConditionSubscribed).IsTrue() {
event.Normal(ctx, ReasonSubscribed, "Subscribed to topic %q", topicResName)
}
return nil
}
// ensureNoPubSubSubscription ensures that the Pub/Sub subscription created for
// receiving messages from the desired topic is deleted.
// Required permissions:
// - pubsub.subscriptions.get
// - pubsub.subscriptions.delete
func ensureNoPubSubSubscription(ctx context.Context, cli *pubsub.Client) error {
src := commonv1alpha1.ReconcilableFromContext(ctx).(*v1alpha1.GoogleCloudAuditLogsSource)
status := src.Status
subsResName := status.Topic
if subsResName == nil {
// subscription was possibly never created
return nil
}
subs := cli.Subscription(subsResName.Resource)
cfg, err := subs.Config(ctx)
switch {
case isDenied(err):
// it is unlikely that we recover from auth errors in the
// finalizer, so we simply record a warning event and return
event.Warn(ctx, ReasonFailedUnsubscribe,
"Access denied to Pub/Sub API. Ignoring: %s", toErrMsg(err))
return nil
case isNotFound(err):
event.Warn(ctx, ReasonUnsubscribed,
fmt.Sprintf("Subscription %q not found, skipping deletion", subs))
return nil
case err != nil:
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedUnsubscribe,
"Failed to get Pub/Sub subscription %q: %s", subs, toErrMsg(err))
}
if !assertPubSubSubscriptionOwnership(src, cfg) {
event.Warn(ctx, ReasonUnsubscribed, "Subscription %q is not owned by this source instance. "+
"Skipping deletion", subs)
return nil
}
// we don't pass cfg.Topic directly to error reporters below, because
// (*pubsub.Topic).String() panics when Topic is nil
var topicName string
if subsTopic := cfg.Topic; subsTopic != nil {
topicName = subsTopic.String()
}
err = subs.Delete(ctx)
switch {
case isDenied(err):
// it is unlikely that we recover from auth errors in the
// finalizer, so we simply record a warning event and return
event.Warn(ctx, ReasonFailedUnsubscribe,
"Access denied to Pub/Sub API. Ignoring: %s", toErrMsg(err))
return nil
case err != nil:
return failDeleteSubscriptionEvent(topicName, err)
}
event.Normal(ctx, ReasonUnsubscribed, "Unsubscribed from topic %q", topicName)
return nil
}
// makeSubscriptionResourceName returns a Pub/Sub resource name for the given subscription.
func makeSubscriptionResourceName(proj, subsID string) *v1alpha1.GCloudResourceName {
return &v1alpha1.GCloudResourceName{
Project: proj,
Collection: pubsubCollectionSubs,
Resource: subsID,
}
}
// pubsubResourceID returns a deterministic Pub/Sub resource ID matching the
// given source instance.
//
// Resource IDs aren't allowed to start with "goog" and can only contain a
// limited set of characters.
func pubsubResourceID(src commonv1alpha1.Reconcilable) string {
return src.GetNamespace() + "." + src.GetName() + "~" + sources.GoogleCloudAuditLogsSourceResource.String()
}
// assertPubSubTopicOwnership returns whether a Pub/Sub topic is owned by the
// given source.
func assertPubSubTopicOwnership(src *v1alpha1.GoogleCloudAuditLogsSource, topicCfg pubsub.TopicConfig) bool {
for k, v := range pubsubResourceLabels(src) {
if topicCfg.Labels[k] != v {
return false
}
}
return true
}
// assertPubSubSubscriptionOwnership returns whether a Pub/Sub subscription is
// owned by the given source.
func assertPubSubSubscriptionOwnership(src *v1alpha1.GoogleCloudAuditLogsSource, subsCfg pubsub.SubscriptionConfig) bool {
for k, v := range pubsubResourceLabels(src) {
if subsCfg.Labels[k] != v {
return false
}
}
return true
}
// pubsubResourceLabels returns a set of labels containing information from the
// given source instance to set on a Pub/Sub resource (topic or subscription).
//
// Labels accept lowercase characters, numbers, hyphens and underscores exclusively.
// Neither the key nor the value can exceed 63 characters.
func pubsubResourceLabels(src *v1alpha1.GoogleCloudAuditLogsSource) map[string]string {
return map[string]string{
pubsubLabelOwnerResource: strings.ReplaceAll(sources.GoogleCloudAuditLogsSourceResource.String(), ".", "-"),
pubsubLabelOwnerNamespace: src.Namespace,
pubsubLabelOwnerName: strings.ReplaceAll(src.Name, ".", "_"),
}
}
// failGetTopicEvent returns a reconciler event which indicates that a Pub/Sub
// topic could not be retrieved from the Google Cloud API.
func failGetTopicEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error getting topic %q: %s", topicName, toErrMsg(origErr))
}
// failCreateTopicEvent returns a reconciler event which indicates that a
// Pub/Sub topic could not be created via the Google Cloud API.
func failCreateTopicEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error creating topic %q: %s", topicName, toErrMsg(origErr))
}
// failDeleteTopicEvent returns a reconciler event which indicates that
// a Pub/Sub subscription could not be deleted via the Google Cloud API.
func failDeleteTopicEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedUnsubscribe,
"Error deleting topic %q: %s", topicName, toErrMsg(origErr))
}
// failGetSubscriptionEvent returns a reconciler event which indicates that a
// Pub/Sub subscription could not be retrieved from the Google Cloud API.
func failGetSubscriptionEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error getting subscription for topic %q: %s", topicName, toErrMsg(origErr))
}
// failCreateSubscriptionEvent returns a reconciler event which indicates that a
// Pub/Sub subscription could not be created via the Google Cloud API.
func failCreateSubscriptionEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error creating subscription for topic %q: %s", topicName, toErrMsg(origErr))
}
// failDeleteSubscriptionEvent returns a reconciler event which indicates that
// a Pub/Sub subscription could not be deleted via the Google Cloud API.
func failDeleteSubscriptionEvent(topicName string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedUnsubscribe,
"Error deleting subscription for topic %q: %s", topicName, toErrMsg(origErr))
}