|
| 1 | +// Copyright 2026 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +package adminapi |
| 11 | + |
| 12 | +import ( |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/redpanda-data/common-go/rpadmin" |
| 16 | + "github.com/redpanda-data/redpanda/src/go/rpk/pkg/config" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGetAuth(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + profile *config.RpkProfile |
| 24 | + wantTyp rpadmin.Auth |
| 25 | + wantErr string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "no SASL returns NopAuth", |
| 29 | + profile: &config.RpkProfile{}, |
| 30 | + wantTyp: &rpadmin.NopAuth{}, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "SCRAM-SHA-256 returns BasicAuth", |
| 34 | + profile: &config.RpkProfile{ |
| 35 | + KafkaAPI: config.RpkKafkaAPI{ |
| 36 | + SASL: &config.SASL{ |
| 37 | + User: "admin", |
| 38 | + Password: "secret", |
| 39 | + Mechanism: "SCRAM-SHA-256", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + wantTyp: &rpadmin.BasicAuth{Username: "admin", Password: "secret"}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "OAUTHBEARER with token prefix returns BearerToken", |
| 47 | + profile: &config.RpkProfile{ |
| 48 | + KafkaAPI: config.RpkKafkaAPI{ |
| 49 | + SASL: &config.SASL{ |
| 50 | + Password: "token:my-jwt-token", |
| 51 | + Mechanism: "OAUTHBEARER", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "OAUTHBEARER with raw token returns BearerToken", |
| 59 | + profile: &config.RpkProfile{ |
| 60 | + KafkaAPI: config.RpkKafkaAPI{ |
| 61 | + SASL: &config.SASL{ |
| 62 | + Password: "my-jwt-token", |
| 63 | + Mechanism: "OAUTHBEARER", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "OAUTHBEARER case-insensitive", |
| 71 | + profile: &config.RpkProfile{ |
| 72 | + KafkaAPI: config.RpkKafkaAPI{ |
| 73 | + SASL: &config.SASL{ |
| 74 | + Password: "my-jwt-token", |
| 75 | + Mechanism: "oauthbearer", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + wantTyp: &rpadmin.BearerToken{Token: "my-jwt-token"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "OAUTHBEARER with empty password errors", |
| 83 | + profile: &config.RpkProfile{ |
| 84 | + KafkaAPI: config.RpkKafkaAPI{ |
| 85 | + SASL: &config.SASL{ |
| 86 | + Mechanism: "OAUTHBEARER", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + wantErr: "OAUTHBEARER requires a token", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "OAUTHBEARER with token: prefix only errors", |
| 94 | + profile: &config.RpkProfile{ |
| 95 | + KafkaAPI: config.RpkKafkaAPI{ |
| 96 | + SASL: &config.SASL{ |
| 97 | + Password: "token:", |
| 98 | + Mechanism: "OAUTHBEARER", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + wantErr: "OAUTHBEARER requires a token", |
| 103 | + }, |
| 104 | + } |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + got, err := GetAuth(tt.profile) |
| 108 | + if tt.wantErr != "" { |
| 109 | + require.ErrorContains(t, err, tt.wantErr) |
| 110 | + return |
| 111 | + } |
| 112 | + require.NoError(t, err) |
| 113 | + require.IsType(t, tt.wantTyp, got) |
| 114 | + require.Equal(t, tt.wantTyp, got) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestOAuthBearerToken(t *testing.T) { |
| 120 | + tests := []struct { |
| 121 | + name string |
| 122 | + password string |
| 123 | + want string |
| 124 | + }{ |
| 125 | + { |
| 126 | + name: "token prefix stripped", |
| 127 | + password: "token:eyJhbGciOiJSUzI1NiJ9.payload.sig", |
| 128 | + want: "eyJhbGciOiJSUzI1NiJ9.payload.sig", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "raw token returned as-is", |
| 132 | + password: "eyJhbGciOiJSUzI1NiJ9.payload.sig", |
| 133 | + want: "eyJhbGciOiJSUzI1NiJ9.payload.sig", |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "empty password returns empty", |
| 137 | + password: "", |
| 138 | + want: "", |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "token prefix only returns empty", |
| 142 | + password: "token:", |
| 143 | + want: "", |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "token prefix is case-sensitive", |
| 147 | + password: "Token:abc", |
| 148 | + want: "Token:abc", |
| 149 | + }, |
| 150 | + } |
| 151 | + for _, tt := range tests { |
| 152 | + t.Run(tt.name, func(t *testing.T) { |
| 153 | + got := OAuthBearerToken(tt.password) |
| 154 | + require.Equal(t, tt.want, got) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments