forked from google/knative-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auditlogs.go
349 lines (303 loc) · 12.2 KB
/
auditlogs.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
/*
Copyright 2019 Google LLC.
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 auditlogs
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/knative-gcp/pkg/apis/events/v1alpha1"
listers "github.com/google/knative-gcp/pkg/client/listers/events/v1alpha1"
"github.com/google/knative-gcp/pkg/reconciler/events/auditlogs/resources"
pubsubreconciler "github.com/google/knative-gcp/pkg/reconciler/pubsub"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"cloud.google.com/go/logging/logadmin"
glogadmin "github.com/google/knative-gcp/pkg/gclient/logging/logadmin"
gpubsub "github.com/google/knative-gcp/pkg/gclient/pubsub"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
)
const (
finalizerName = controllerAgentName
resourceGroup = "cloudauditlogssources.events.cloud.google.com"
publisherRole = "roles/pubsub.publisher"
)
type Reconciler struct {
*pubsubreconciler.PubSubBase
auditLogsSourceLister listers.CloudAuditLogsSourceLister
logadminClientProvider glogadmin.CreateFn
pubsubClientProvider gpubsub.CreateFn
}
// Check that we implement the controller.Reconciler interface.
var _ controller.Reconciler = (*Reconciler)(nil)
// Reconcile compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the CloudAuditLogsSource resource
// with the current status of the resource.
func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
logging.FromContext(ctx).Desugar().Error("Invalid resource key")
return nil
}
// Get the CloudAuditLogsSource resource with this namespace/name
original, err := c.auditLogsSourceLister.CloudAuditLogsSources(namespace).Get(name)
if apierrs.IsNotFound(err) {
// The CloudAuditLogsSource resource may no longer exist, in which case we stop processing.
logging.FromContext(ctx).Desugar().Error("CloudAuditLogsSource in work queue no longer exists")
return nil
} else if err != nil {
return err
}
// Don't modify the informers copy
s := original.DeepCopy()
reconcileErr := c.reconcile(ctx, s)
// If no error is returned, mark the observed generation.
if reconcileErr == nil {
s.Status.ObservedGeneration = s.Generation
}
if _, updated, err := c.updateFinalizers(ctx, s); err != nil {
logging.FromContext(ctx).Desugar().Warn("Failed to update CloudAuditLogsSource finalizers", zap.Error(err))
c.Recorder.Eventf(s, corev1.EventTypeWarning, "UpdateFailed", "Failed to update finalizers for CloudAuditLogsSource %q: %v", s.Name, err)
return err
} else if updated {
c.Recorder.Eventf(s, corev1.EventTypeNormal, "Updated", "Updated CloudAuditLogsSource %q finalizers", s.Name)
}
// If we didn't change anything then don't call updateStatus.
// This is important because the copy we loaded from the
// informer's cache may be stale and we don't want to
// overwrite a prior update to status with this stale state.
if !equality.Semantic.DeepEqual(original.Status, s.Status) {
if err := c.updateStatus(ctx, original, s); err != nil {
c.Logger.Warn("Failed to update CloudAuditLogsSource status", zap.Error(err))
c.Recorder.Eventf(s, corev1.EventTypeWarning, "UpdateFailed",
"Failed to update status for CloudAuditLogsSource %q: %v", s.Name, err)
return err
} else if reconcileErr == nil {
c.Recorder.Eventf(s, corev1.EventTypeNormal, "Updated", "Updated CloudAuditLogsSource %q", s.Name)
}
}
if reconcileErr != nil {
c.Recorder.Event(s, corev1.EventTypeWarning, "InternalError", reconcileErr.Error())
}
return reconcileErr
}
func (c *Reconciler) reconcile(ctx context.Context, s *v1alpha1.CloudAuditLogsSource) error {
ctx = logging.WithLogger(ctx, c.Logger.With(zap.Any("auditlogsource", s)))
s.Status.InitializeConditions()
// See if the source has been deleted.
if s.DeletionTimestamp != nil {
if err := c.deleteSink(ctx, s); err != nil {
s.Status.MarkSinkNotReady("SinkDeleteFailed", "Failed to delete Stackdriver sink: %s", err.Error())
return err
}
s.Status.MarkSinkNotReady("SinkDeleted", "Successfully deleted Stackdriver sink: %s", s.Status.StackdriverSink)
if err := c.PubSubBase.DeletePubSub(ctx, s); err != nil {
return err
}
s.Status.StackdriverSink = ""
c.removeFinalizer(s)
return nil
}
// Ensure the finalizer's there, since we're about to attempt
// to change external state with the topic, so we need to
// clean it up.
c.addFinalizer(s)
topic := resources.GenerateTopicName(s)
t, ps, err := c.PubSubBase.ReconcilePubSub(ctx, s, topic, resourceGroup)
if err != nil {
return err
}
c.Logger.Debugf("Reconciled: PubSub: %+v PullSubscription: %+v", t, ps)
sink, err := c.reconcileSink(ctx, s)
if err != nil {
return err
}
s.Status.StackdriverSink = sink
s.Status.MarkSinkReady()
c.Logger.Debugf("Reconciled Stackdriver sink: %+v", sink)
return nil
}
func (c *Reconciler) reconcileSink(ctx context.Context, s *v1alpha1.CloudAuditLogsSource) (string, error) {
sink, err := c.ensureSinkCreated(ctx, s)
if err != nil {
s.Status.MarkSinkNotReady("SinkCreateFailed", "failed to ensure creation of logging sink: %v", err)
return "", err
}
err = c.ensureSinkIsPublisher(ctx, s, sink)
if err != nil {
s.Status.MarkSinkNotReady("SinkNotPublisher", "failed to ensure sink has pubsub.publisher permission on source topic: %v", err)
return "", err
}
return sink.ID, nil
}
func (c *Reconciler) ensureSinkCreated(ctx context.Context, s *v1alpha1.CloudAuditLogsSource) (*logadmin.Sink, error) {
sinkID := s.Status.StackdriverSink
if sinkID == "" {
sinkID = resources.GenerateSinkName(s)
}
logadminClient, err := c.logadminClientProvider(ctx, s.Status.ProjectID)
if err != nil {
logging.FromContext(ctx).Desugar().Error("Failed to create LogAdmin client", zap.Error(err))
return nil, err
}
sink, err := logadminClient.Sink(ctx, sinkID)
if status.Code(err) == codes.NotFound {
filterBuilder := resources.FilterBuilder{}
filterBuilder.WithServiceName(s.Spec.ServiceName).WithMethodName(s.Spec.MethodName)
if s.Spec.ResourceName != "" {
filterBuilder.WithResourceName(s.Spec.ResourceName)
}
sink = &logadmin.Sink{
ID: sinkID,
Destination: resources.GenerateTopicResourceName(s),
Filter: filterBuilder.GetFilterQuery(),
}
sink, err = logadminClient.CreateSinkOpt(ctx, sink, logadmin.SinkOptions{UniqueWriterIdentity: true})
// Handle AlreadyExists in-case of a race between another create call.
if status.Code(err) == codes.AlreadyExists {
sink, err = logadminClient.Sink(ctx, sinkID)
}
}
return sink, err
}
// Ensures that the sink has been granted the pubsub.publisher role on the source topic.
func (c *Reconciler) ensureSinkIsPublisher(ctx context.Context, s *v1alpha1.CloudAuditLogsSource, sink *logadmin.Sink) error {
pubsubClient, err := c.pubsubClientProvider(ctx, s.Status.ProjectID)
if err != nil {
logging.FromContext(ctx).Desugar().Error("Failed to create PubSub client", zap.Error(err))
return err
}
topicIam := pubsubClient.Topic(s.Status.TopicID).IAM()
topicPolicy, err := topicIam.Policy(ctx)
if err != nil {
return err
}
if !topicPolicy.HasRole(sink.WriterIdentity, publisherRole) {
topicPolicy.Add(sink.WriterIdentity, publisherRole)
if err = topicIam.SetPolicy(ctx, topicPolicy); err != nil {
return err
}
logging.FromContext(ctx).Desugar().Debug(
"Granted the Stackdriver Sink writer identity roles/pubsub.publisher on PubSub Topic.",
zap.String("writerIdentity", sink.WriterIdentity),
zap.String("topicID", s.Status.TopicID))
}
return nil
}
// deleteSink looks at status.SinkID and if non-empty will delete the
// previously created stackdriver sink.
func (c *Reconciler) deleteSink(ctx context.Context, s *v1alpha1.CloudAuditLogsSource) error {
if s.Status.StackdriverSink == "" {
return nil
}
logadminClient, err := c.logadminClientProvider(ctx, s.Status.ProjectID)
if err != nil {
logging.FromContext(ctx).Desugar().Error("Failed to create LogAdmin client", zap.Error(err))
return err
}
if err = logadminClient.DeleteSink(ctx, s.Status.StackdriverSink); status.Code(err) != codes.NotFound {
return err
}
return nil
}
func (c *Reconciler) addFinalizer(s *v1alpha1.CloudAuditLogsSource) {
finalizers := sets.NewString(s.Finalizers...)
finalizers.Insert(finalizerName)
s.Finalizers = finalizers.List()
}
func (c *Reconciler) removeFinalizer(s *v1alpha1.CloudAuditLogsSource) {
finalizers := sets.NewString(s.Finalizers...)
finalizers.Delete(finalizerName)
s.Finalizers = finalizers.List()
}
func (c *Reconciler) updateStatus(ctx context.Context, original *v1alpha1.CloudAuditLogsSource, desired *v1alpha1.CloudAuditLogsSource) error {
existing := original.DeepCopy()
return reconciler.RetryUpdateConflicts(func(attempts int) (err error) {
// The first iteration tries to use the informer's state, subsequent attempts fetch the latest state via API.
if attempts > 0 {
existing, err = c.RunClientSet.EventsV1alpha1().CloudAuditLogsSources(desired.Namespace).Get(desired.Name, metav1.GetOptions{})
if err != nil {
return err
}
}
// If there's nothing to update, just return.
if equality.Semantic.DeepEqual(existing.Status, desired.Status) {
return nil
}
becomesReady := desired.Status.IsReady() && !existing.Status.IsReady()
existing.Status = desired.Status
_, err = c.RunClientSet.EventsV1alpha1().CloudAuditLogsSources(desired.Namespace).UpdateStatus(existing)
if err == nil && becomesReady {
duration := time.Since(existing.ObjectMeta.CreationTimestamp.Time)
logging.FromContext(ctx).Desugar().Info("CloudAuditLogsSource became ready", zap.Any("after", duration))
c.Recorder.Event(existing, corev1.EventTypeNormal, "ReadinessChanged", fmt.Sprintf("CloudAuditLogsSource %q became ready", existing.Name))
if err := c.StatsReporter.ReportReady("CloudAuditLogsSource", existing.Namespace, existing.Name, duration); err != nil {
logging.FromContext(ctx).Infof("failed to record ready for CloudAuditLogsSource, %v", err)
}
}
return err
})
}
// updateFinalizers is a generic method for future compatibility with a
// reconciler SDK.
func (r *Reconciler) updateFinalizers(ctx context.Context, desired *v1alpha1.CloudAuditLogsSource) (*v1alpha1.CloudAuditLogsSource, bool, error) {
s, err := r.auditLogsSourceLister.CloudAuditLogsSources(desired.Namespace).Get(desired.Name)
if err != nil {
return nil, false, err
}
// Don't modify the informers copy.
existing := s.DeepCopy()
var finalizers []string
// If there's nothing to update, just return.
existingFinalizers := sets.NewString(existing.Finalizers...)
desiredFinalizers := sets.NewString(desired.Finalizers...)
if desiredFinalizers.Has(finalizerName) {
if existingFinalizers.Has(finalizerName) {
// Nothing to do.
return desired, false, nil
}
// Add the finalizer.
finalizers = append(existing.Finalizers, finalizerName)
} else {
if !existingFinalizers.Has(finalizerName) {
// Nothing to do.
return desired, false, nil
}
// Remove the finalizer.
existingFinalizers.Delete(finalizerName)
finalizers = existingFinalizers.List()
}
mergePatch := map[string]interface{}{
"metadata": map[string]interface{}{
"finalizers": finalizers,
"resourceVersion": existing.ResourceVersion,
},
}
patch, err := json.Marshal(mergePatch)
if err != nil {
return desired, false, err
}
update, err := r.RunClientSet.EventsV1alpha1().CloudAuditLogsSources(existing.Namespace).Patch(existing.Name, types.MergePatchType, patch)
return update, true, err
}