-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathauth_test.go
More file actions
157 lines (152 loc) · 3.6 KB
/
auth_test.go
File metadata and controls
157 lines (152 loc) · 3.6 KB
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
// Copyright 2026 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
package adminapi
import (
"testing"
"github.com/redpanda-data/common-go/rpadmin"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/stretchr/testify/require"
)
func TestGetAuth(t *testing.T) {
tests := []struct {
name string
profile *config.RpkProfile
wantTyp rpadmin.Auth
wantErr string
}{
{
name: "no SASL returns NopAuth",
profile: &config.RpkProfile{},
wantTyp: &rpadmin.NopAuth{},
},
{
name: "SCRAM-SHA-256 returns BasicAuth",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
User: "admin",
Password: "secret",
Mechanism: "SCRAM-SHA-256",
},
},
},
wantTyp: &rpadmin.BasicAuth{Username: "admin", Password: "secret"},
},
{
name: "OAUTHBEARER with token prefix returns BearerToken",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
Password: "token:my-jwt-token",
Mechanism: "OAUTHBEARER",
},
},
},
wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"},
},
{
name: "OAUTHBEARER with raw token returns BearerToken",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
Password: "my-jwt-token",
Mechanism: "OAUTHBEARER",
},
},
},
wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"},
},
{
name: "OAUTHBEARER case-insensitive",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
Password: "my-jwt-token",
Mechanism: "oauthbearer",
},
},
},
wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"},
},
{
name: "OAUTHBEARER with empty password errors",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
Mechanism: "OAUTHBEARER",
},
},
},
wantErr: "OAUTHBEARER requires a token",
},
{
name: "OAUTHBEARER with token: prefix only errors",
profile: &config.RpkProfile{
KafkaAPI: config.RpkKafkaAPI{
SASL: &config.SASL{
Password: "token:",
Mechanism: "OAUTHBEARER",
},
},
},
wantErr: "OAUTHBEARER requires a token",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAuth(tt.profile)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
return
}
require.NoError(t, err)
require.IsType(t, tt.wantTyp, got)
require.Equal(t, tt.wantTyp, got)
})
}
}
func TestOAuthBearerToken(t *testing.T) {
tests := []struct {
name string
password string
want string
}{
{
name: "token prefix stripped",
password: "token:eyJhbGciOiJSUzI1NiJ9.payload.sig",
want: "eyJhbGciOiJSUzI1NiJ9.payload.sig",
},
{
name: "raw token returned as-is",
password: "eyJhbGciOiJSUzI1NiJ9.payload.sig",
want: "eyJhbGciOiJSUzI1NiJ9.payload.sig",
},
{
name: "empty password returns empty",
password: "",
want: "",
},
{
name: "token prefix only returns empty",
password: "token:",
want: "",
},
{
name: "token prefix is case-sensitive",
password: "Token:abc",
want: "Token:abc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := OAuthBearerToken(tt.password)
require.Equal(t, tt.want, got)
})
}
}