/
subscription.go
271 lines (227 loc) · 10.2 KB
/
subscription.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
/*
Copyright 2023 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 azureservicebussource
import (
"context"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"net/http"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/controller"
"knative.dev/pkg/reconciler"
"github.com/Azure/azure-sdk-for-go/profiles/latest/servicebus/mgmt/servicebus"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
commonv1alpha1 "github.com/triggermesh/triggermesh/pkg/apis/common/v1alpha1"
"github.com/triggermesh/triggermesh/pkg/apis/sources/v1alpha1"
"github.com/triggermesh/triggermesh/pkg/reconciler/event"
"github.com/triggermesh/triggermesh/pkg/reconciler/skip"
"github.com/triggermesh/triggermesh/pkg/sources/client/azure/servicebustopics"
)
const crudTimeout = time.Second * 15
// EnsureSubscription ensures a Subscription exists with the expected configuration.
// Required permissions:
// - Microsoft.ServiceBus/namespaces/topics/subscriptions/read
// - Microsoft.ServiceBus/namespaces/topics/subscriptions/write
func EnsureSubscription(ctx context.Context, cli servicebustopics.SubscriptionsClient) error {
if skip.Skip(ctx) {
return nil
}
src := commonv1alpha1.ReconcilableFromContext(ctx)
typedSrc := src.(*v1alpha1.AzureServiceBusSource)
status := &typedSrc.Status
topic := typedSrc.Spec.TopicID.String()
subsName := subscriptionName(src)
restCtx, cancel := context.WithTimeout(ctx, crudTimeout)
defer cancel()
currentSubs, err := cli.Get(restCtx, typedSrc.Spec.TopicID.ResourceGroup, typedSrc.Spec.TopicID.Namespace,
typedSrc.Spec.TopicID.ResourceName, subsName)
switch {
case isNotFound(err):
// no-op
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.AzureReasonAPIError, "Access denied to Subscription API: "+toErrMsg(err))
return controller.NewPermanentError(failGetSubscriptionEvent(topic, err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.AzureReasonAPIError, "Cannot look up Subscription: "+toErrMsg(err))
return fmt.Errorf("%w", failGetSubscriptionEvent(topic, err))
}
if subsExists := currentSubs.ID != nil; !subsExists {
// use Azure's defaults
desiredSubs := servicebus.SBSubscription{}
restCtx, cancel = context.WithTimeout(ctx, crudTimeout)
defer cancel()
currentSubs, err = cli.CreateOrUpdate(restCtx, typedSrc.Spec.TopicID.ResourceGroup, typedSrc.Spec.TopicID.Namespace,
typedSrc.Spec.TopicID.ResourceName, subsName, desiredSubs)
switch {
// This call responds with NotFound if the topic doesn't exist.
case isNotFound(err):
// We use a generic error message in the object's status instead of the original error, because
// these API errors tend to contain a confusing message ("incoming request is not recognized as
// a namespace policy put request") and unique elements (timestamp and correlation ID) which we
// don't want to cause inifinite loops of reconciliation.
status.MarkNotSubscribed(v1alpha1.AzureReasonAPIError, "Topic does not exist")
return controller.NewPermanentError(failSubscribeEvent(topic, err))
case isDenied(err):
status.MarkNotSubscribed(v1alpha1.AzureReasonAPIError, "Access denied to Subscription API: "+toErrMsg(err))
return controller.NewPermanentError(failSubscribeEvent(topic, err))
case err != nil:
status.MarkNotSubscribed(v1alpha1.AzureReasonAPIError, "Cannot subscribe to Topic: "+toErrMsg(err))
return fmt.Errorf("%w", failSubscribeEvent(topic, err))
}
}
if !status.GetCondition(v1alpha1.AzureServiceBusTopicConditionSubscribed).IsTrue() {
event.Normal(ctx, ReasonSubscribed, "Created Subscription %q for Topic %q", subsName, topic)
}
// it is essential that we propagate the subscription's resource ID
// here, otherwise BuildAdapter() won't be able to configure the
// Service Bus adapter properly
subscriptionResID, err := parseSubscriptionResID(*currentSubs.ID)
if err != nil {
return fmt.Errorf("converting resource ID string to structured resource ID: %w", err)
}
status.SubscriptionID = subscriptionResID
status.MarkSubscribed()
return nil
}
// EnsureNoSubscription ensures the Subscription is removed.
// Required permissions:
// - Microsoft.ServiceBus/namespaces/topics/subscriptions/delete
func EnsureNoSubscription(ctx context.Context, cli servicebustopics.SubscriptionsClient) reconciler.Event {
if skip.Skip(ctx) {
return nil
}
src := commonv1alpha1.ReconcilableFromContext(ctx)
typedSrc := src.(*v1alpha1.AzureServiceBusSource)
topic := typedSrc.Spec.TopicID.String()
subsName := subscriptionName(src)
restCtx, cancel := context.WithTimeout(ctx, crudTimeout)
defer cancel()
_, err := cli.Delete(restCtx, typedSrc.Spec.TopicID.ResourceGroup, typedSrc.Spec.TopicID.Namespace,
typedSrc.Spec.TopicID.ResourceName, subsName)
switch {
case isNotFound(err):
event.Warn(ctx, ReasonUnsubscribed, "Subscription not found, skipping deletion")
return nil
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 Subscription API. Ignoring: %s", toErrMsg(err))
return nil
case err != nil:
return failUnsubscribeEvent(topic, err)
}
event.Normal(ctx, ReasonUnsubscribed, "Deleted Subscription %q for Topic %q",
subsName, topic)
return nil
}
// parseSubscriptionResID parses the given Subscription resource ID string to a
// structured resource ID.
func parseSubscriptionResID(resIDStr string) (*v1alpha1.AzureResourceID, error) {
resID := &v1alpha1.AzureResourceID{}
err := json.Unmarshal([]byte(strconv.Quote(resIDStr)), resID)
if err != nil {
return nil, fmt.Errorf("deserializing resource ID string: %w", err)
}
return resID, nil
}
// toErrMsg returns the given error as a string.
// If the error is an Azure API error, the error message is sanitized while
// still preserving the concatenation of all nested levels of errors.
//
// Used to remove clutter from errors before writing them to status conditions.
func toErrMsg(err error) string {
return recursErrMsg("", err)
}
// recursErrMsg concatenates the messages of deeply nested API errors recursively.
func recursErrMsg(errMsg string, err error) string {
if errMsg != "" {
errMsg += ": "
}
switch tErr := err.(type) {
case autorest.DetailedError:
return recursErrMsg(errMsg+tErr.Message, tErr.Original)
case *azure.RequestError:
if tErr.DetailedError.Original != nil {
return recursErrMsg(errMsg+tErr.DetailedError.Message, tErr.DetailedError.Original)
}
if tErr.ServiceError != nil {
return errMsg + tErr.ServiceError.Message
}
case adal.TokenRefreshError:
// This type of error is returned when the OAuth authentication with Azure Active Directory fails, often
// due to an invalid or expired secret.
//
// The associated message is typically opaque and contains elements that are unique to each request
// (trace/correlation IDs, timestamps), which causes an infinite loop of reconciliation if propagated to
// the object's status conditions.
// Instead of resorting to over-engineered error parsing techniques to get around the verbosity of the
// message, we simply return a short and generic error description.
return errMsg + "failed to refresh token: the provided secret is either invalid or expired"
}
return errMsg + err.Error()
}
// isNotFound returns whether the given error indicates that some Azure
// resource was not found.
func isNotFound(err error) bool {
if dErr := (autorest.DetailedError{}); errors.As(err, &dErr) {
return dErr.StatusCode == http.StatusNotFound
}
return false
}
// isDenied returns whether the given error indicates that a request to the
// Azure API could not be authorized.
// This category of issues is unrecoverable without user intervention.
func isDenied(err error) bool {
if dErr := (autorest.DetailedError{}); errors.As(err, &dErr) {
if code, ok := dErr.StatusCode.(int); ok {
return code == http.StatusUnauthorized || code == http.StatusForbidden
}
}
return false
}
// subscriptionName returns a deterministic Subscription name matching the
// given source instance.
//
// The generated name must match the regexp /[A-Za-z0-9][\w.-]{0,49}/, which
// doesn't give us a lot of characters for indicating what component owns the
// Subscription. Therefore, we compute the CRC32 checksum of the source's
// name/namespace (8 characters) and make it part of the name.
func subscriptionName(src commonv1alpha1.Reconcilable) string {
nsNameChecksum := crc32.ChecksumIEEE([]byte(src.GetNamespace() + "/" + src.GetName()))
return "io.triggermesh.azureservicebussources-" + strconv.FormatUint(uint64(nsNameChecksum), 10)
}
// failGetSubscriptionEvent returns a reconciler event which indicates that a
// Subscription for the given Topic could not be retrieved from the Azure API.
func failGetSubscriptionEvent(topicID string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error getting Subscription for Topic %q: %s", topicID, toErrMsg(origErr))
}
// failSubscribeEvent returns a reconciler event which indicates that a
// Subscription for the given Topic could not be created via the Azure API.
func failSubscribeEvent(topicID string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error creating Subscription for Topic %q: %s", topicID, toErrMsg(origErr))
}
// failUnsubscribeEvent returns a reconciler event which indicates that a
// Subscription for the given Topic could not be deleted via the Azure API.
func failUnsubscribeEvent(topicID string, origErr error) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeWarning, ReasonFailedSubscribe,
"Error deleting Subscription for Topic %q: %s", topicID, toErrMsg(origErr))
}