Skip to content

Commit 5b654fb

Browse files
removing parse url from the get rabbitmqUrl function so usernames can have special characters needed i.e when pulled from an active directory and the url does not fail to parse (#1259)
Co-authored-by: Gabriel Freites <[email protected]>
1 parent 76794c0 commit 5b654fb

File tree

7 files changed

+32
-30
lines changed

7 files changed

+32
-30
lines changed

pkg/rabbit/exchange.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package rabbit
1818

1919
import (
20-
"net/url"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
"knative.dev/eventing-rabbitmq/pkg/apis/sources/v1alpha1"
2422
"knative.dev/pkg/kmeta"
@@ -33,7 +31,7 @@ type ExchangeArgs struct {
3331
Namespace string
3432
RabbitMQVhost string
3533
RabbitmqClusterReference *rabbitv1beta1.RabbitmqClusterReference
36-
RabbitMQURL *url.URL
34+
RabbitMQURL string
3735
Broker *eventingv1.Broker
3836
Trigger *eventingv1.Trigger
3937
Source *v1alpha1.RabbitmqSource

pkg/rabbit/secret_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"knative.dev/eventing-rabbitmq/pkg/apis/sources/v1alpha1"
2626
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
2727

28-
"knative.dev/pkg/apis"
2928
"knative.dev/pkg/kmeta"
3029
_ "knative.dev/pkg/system/testing"
3130
)
@@ -40,11 +39,6 @@ const (
4039

4140
func TestMakeSecret(t *testing.T) {
4241
var TrueValue = true
43-
url, err := apis.ParseURL(testRabbitURL)
44-
if err != nil {
45-
t.Errorf("Failed to parse the test URL: %s", err)
46-
}
47-
4842
for _, tt := range []struct {
4943
name string
5044
args *ExchangeArgs
@@ -54,7 +48,7 @@ func TestMakeSecret(t *testing.T) {
5448
name: "test broker secret name",
5549
args: &ExchangeArgs{
5650
Broker: &eventingv1.Broker{ObjectMeta: metav1.ObjectMeta{Name: brokerName, Namespace: ns}},
57-
RabbitMQURL: url.URL(),
51+
RabbitMQURL: testRabbitURL,
5852
},
5953
want: &corev1.Secret{
6054
ObjectMeta: metav1.ObjectMeta{
@@ -79,7 +73,7 @@ func TestMakeSecret(t *testing.T) {
7973
name: "test source secret name",
8074
args: &ExchangeArgs{
8175
Source: &v1alpha1.RabbitmqSource{ObjectMeta: metav1.ObjectMeta{Name: sourceName, Namespace: ns}},
82-
RabbitMQURL: url.URL(),
76+
RabbitMQURL: testRabbitURL,
8377
},
8478
want: &corev1.Secret{
8579
ObjectMeta: metav1.ObjectMeta{
@@ -115,7 +109,7 @@ func TestMakeSecret(t *testing.T) {
115109
owner = tt.args.Source
116110
name = tt.args.Source.Name
117111
}
118-
got := MakeSecret(name, typeString, ns, tt.args.RabbitMQURL.String(), owner)
112+
got := MakeSecret(name, typeString, ns, tt.args.RabbitMQURL, owner)
119113
if diff := cmp.Diff(tt.want, got); diff != "" {
120114
t.Error("unexpected diff (-want, +got) = ", diff)
121115
}

pkg/rabbit/service.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"net/url"
2423
"strings"
2524

2625
"go.uber.org/zap"
@@ -254,25 +253,25 @@ func isReady(conditions []rabbitv1beta1.Condition) bool {
254253
return numConditions == 0
255254
}
256255

257-
func (r *Rabbit) RabbitMQURL(ctx context.Context, clusterRef *rabbitv1beta1.RabbitmqClusterReference) (*url.URL, error) {
256+
func (r *Rabbit) RabbitMQURL(ctx context.Context, clusterRef *rabbitv1beta1.RabbitmqClusterReference) (string, error) {
258257
protocol := []byte("amqp")
259258
if clusterRef.ConnectionSecret != nil {
260259
s, err := r.kubeClientSet.CoreV1().Secrets(clusterRef.Namespace).Get(ctx, clusterRef.ConnectionSecret.Name, metav1.GetOptions{})
261260
if err != nil {
262-
return nil, err
261+
return "", err
263262
}
264263
uri, ok := s.Data["uri"]
265264
if !ok {
266-
return nil, fmt.Errorf("rabbit Secret missing key uri")
265+
return "", fmt.Errorf("rabbit Secret missing key uri")
267266
}
268267
uriString := string(uri)
269268
password, ok := s.Data["password"]
270269
if !ok {
271-
return nil, fmt.Errorf("rabbit Secret missing key password")
270+
return "", fmt.Errorf("rabbit Secret missing key password")
272271
}
273272
username, ok := s.Data["username"]
274273
if !ok {
275-
return nil, fmt.Errorf("rabbit Secret missing key username")
274+
return "", fmt.Errorf("rabbit Secret missing key username")
276275
}
277276
port, ok := s.Data["port"]
278277
if !ok {
@@ -286,31 +285,31 @@ func (r *Rabbit) RabbitMQURL(ctx context.Context, clusterRef *rabbitv1beta1.Rabb
286285
}
287286
uriString = strings.TrimPrefix(uriString, prefix)
288287
splittedUri := strings.Split(uriString, ":")
289-
return url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", protocol, username, password, splittedUri[0], port))
288+
return fmt.Sprintf("%s://%s:%s@%s:%s", protocol, username, password, splittedUri[0], port), nil
290289
}
291290

292291
rab, err := r.getClusterFromReference(ctx, clusterRef)
293292
if err != nil {
294-
return nil, err
293+
return "", err
295294
}
296295

297296
if rab.Status.DefaultUser == nil || rab.Status.DefaultUser.SecretReference == nil || rab.Status.DefaultUser.ServiceReference == nil {
298-
return nil, fmt.Errorf("rabbit \"%s/%s\" not ready", rab.Namespace, rab.Name)
297+
return "", fmt.Errorf("rabbit \"%s/%s\" not ready", rab.Namespace, rab.Name)
299298
}
300299

301300
_ = rab.Status.DefaultUser.SecretReference
302301

303302
s, err := r.kubeClientSet.CoreV1().Secrets(rab.Status.DefaultUser.SecretReference.Namespace).Get(ctx, rab.Status.DefaultUser.SecretReference.Name, metav1.GetOptions{})
304303
if err != nil {
305-
return nil, err
304+
return "", err
306305
}
307306
password, ok := s.Data[rab.Status.DefaultUser.SecretReference.Keys["password"]]
308307
if !ok {
309-
return nil, fmt.Errorf("rabbit Secret missing key %s", rab.Status.DefaultUser.SecretReference.Keys["password"])
308+
return "", fmt.Errorf("rabbit Secret missing key %s", rab.Status.DefaultUser.SecretReference.Keys["password"])
310309
}
311310
username, ok := s.Data[rab.Status.DefaultUser.SecretReference.Keys["username"]]
312311
if !ok {
313-
return nil, fmt.Errorf("rabbit Secret missing key %s", rab.Status.DefaultUser.SecretReference.Keys["username"])
312+
return "", fmt.Errorf("rabbit Secret missing key %s", rab.Status.DefaultUser.SecretReference.Keys["username"])
314313
}
315314
port, ok := s.Data["port"]
316315
if !ok {
@@ -320,7 +319,7 @@ func (r *Rabbit) RabbitMQURL(ctx context.Context, clusterRef *rabbitv1beta1.Rabb
320319
protocol = []byte("amqps")
321320
}
322321
host := network.GetServiceHostname(rab.Status.DefaultUser.ServiceReference.Name, rab.Status.DefaultUser.ServiceReference.Namespace)
323-
return url.Parse(fmt.Sprintf("%s://%s:%s@%s:%s", protocol, username, password, host, port))
322+
return fmt.Sprintf("%s://%s:%s@%s:%s", protocol, username, password, host, port), nil
324323
}
325324

326325
func (r *Rabbit) GetRabbitMQCASecret(ctx context.Context, clusterRef *rabbitv1beta1.RabbitmqClusterReference) (string, error) {

pkg/rabbit/service_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ func Test_RabbitMQURL(t *testing.T) {
125125
secretData: map[string][]byte{"uri": []byte("https://test-uri"), "password": []byte("1234"), "username": []byte("test"), "port": []byte("1234")},
126126
wantUrl: "amqps://test:1234@test-uri:1234",
127127
wantErr: false,
128+
}, {
129+
name: "username with /",
130+
conSecret: true,
131+
secretData: map[string][]byte{"uri": []byte("https://test-uri:5678"), "password": []byte("1234"), "username": []byte("my//domain/test"), "port": []byte("1234")},
132+
wantUrl: "amqps://my//domain/test:1234@test-uri:1234",
133+
wantErr: false,
134+
}, {
135+
name: "username with \\",
136+
conSecret: true,
137+
secretData: map[string][]byte{"uri": []byte("https://test-uri:5678"), "password": []byte("1234"), "username": []byte("mydomain\\test"), "port": []byte("1234")},
138+
wantUrl: "amqps://mydomain\\test:1234@test-uri:1234",
139+
wantErr: false,
128140
}} {
129141
t.Run(tt.name, func(t *testing.T) {
130142
tt := tt
@@ -149,7 +161,7 @@ func Test_RabbitMQURL(t *testing.T) {
149161
gotUrl, err := r.RabbitMQURL(ctx, cr)
150162
if (err != nil && !tt.wantErr) || (err == nil && tt.wantErr) {
151163
t.Errorf("unexpected error checking conditions want: %v, got: %v", tt.wantErr, err)
152-
} else if !tt.wantErr && gotUrl.String() != tt.wantUrl {
164+
} else if !tt.wantErr && gotUrl != tt.wantUrl {
153165
t.Errorf("got wrong url want: %s, got: %s", tt.wantUrl, gotUrl)
154166
}
155167
})

pkg/rabbit/types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package rabbit
1818

1919
import (
2020
"context"
21-
"net/url"
2221

2322
rabbitv1beta1 "knative.dev/eventing-rabbitmq/third_party/pkg/apis/rabbitmq.com/v1beta1"
2423
)
@@ -29,7 +28,7 @@ type Result struct {
2928
}
3029

3130
type Service interface {
32-
RabbitMQURL(context.Context, *rabbitv1beta1.RabbitmqClusterReference) (*url.URL, error)
31+
RabbitMQURL(context.Context, *rabbitv1beta1.RabbitmqClusterReference) (string, error)
3332
ReconcileExchange(context.Context, *ExchangeArgs) (Result, error)
3433
ReconcileQueue(context.Context, *QueueArgs) (Result, error)
3534
ReconcileBinding(context.Context, *BindingArgs) (Result, error)

pkg/reconciler/broker/broker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (r *Reconciler) reconcileRabbitResources(ctx context.Context, b *eventingv1
290290

291291
return r.reconcileCommonIngressResources(
292292
ctx,
293-
rabbit.MakeSecret(args.Broker.Name, "broker", args.Namespace, args.RabbitMQURL.String(), args.Broker),
293+
rabbit.MakeSecret(args.Broker.Name, "broker", args.Namespace, args.RabbitMQURL, args.Broker),
294294
b,
295295
args.RabbitMQVhost,
296296
)

pkg/reconciler/source/rabbitmqsource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (r *Reconciler) reconcileRabbitObjects(ctx context.Context, src *v1alpha1.R
146146
return err
147147
}
148148

149-
if err := rabbit.ReconcileSecret(ctx, r.secretLister, r.KubeClientSet, rabbit.MakeSecret(src.Name, "source", src.Namespace, rabbitmqURL.String(), src)); err != nil {
149+
if err := rabbit.ReconcileSecret(ctx, r.secretLister, r.KubeClientSet, rabbit.MakeSecret(src.Name, "source", src.Namespace, rabbitmqURL, src)); err != nil {
150150
logging.FromContext(ctx).Errorw("Problem reconciling Secret", zap.Error(err))
151151
src.Status.MarkSecretFailed("SecretFailure", "Failed to reconcile secret: %s", err)
152152
return err

0 commit comments

Comments
 (0)