-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproxy_authenticator_test.go
More file actions
204 lines (198 loc) · 5.51 KB
/
Copy pathproxy_authenticator_test.go
File metadata and controls
204 lines (198 loc) · 5.51 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
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
package pivnet_test
import (
"strings"
"testing"
pivnet "github.com/pivotal-cf/go-pivnet/v9"
)
func TestNewProxyAuthenticator(t *testing.T) {
tests := []struct {
name string
config pivnet.ProxyAuthConfig
expectError bool
errorContains string
expectAuthType string // Expected type of authenticator
}{
// Basic Authentication - Success Cases
{
name: "basic auth with valid credentials",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeBasic,
Username: "testuser",
Password: "testpass",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: false,
expectAuthType: "BasicProxyAuth",
},
{
name: "basic auth with empty username and password",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeBasic,
Username: "",
Password: "",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: false,
expectAuthType: "BasicProxyAuth",
},
{
name: "basic auth with special characters in password",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeBasic,
Username: "user",
Password: "p@ssw0rd!#$%",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: false,
expectAuthType: "BasicProxyAuth",
},
// SPNEGO Authentication - Error Cases (can't test success without real Kerberos, will handle this in integration tests)
{
name: "spnego with empty username",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "",
Password: "password",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
errorContains: "username, password, and proxyURL are required",
},
{
name: "spnego with empty password",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
errorContains: "username, password, and proxyURL are required",
},
{
name: "spnego with empty proxy URL",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "",
},
expectError: true,
errorContains: "username, password, and proxyURL are required",
},
{
name: "spnego with invalid proxy URL scheme",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "ftp://proxy.example.com:8080",
},
expectError: true,
errorContains: "proxy URL must start with http:// or https://",
},
{
name: "spnego with proxy URL without scheme",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "proxy.example.com:8080",
},
expectError: true,
errorContains: "proxy URL must start with http:// or https://",
},
{
name: "spnego with path traversal in krb5 config",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "http://proxy.example.com:8080",
Krb5Config: "/etc/../../../etc/passwd",
},
expectError: true,
errorContains: "krb5 config path contains invalid path traversal",
},
{
name: "spnego with relative path traversal in krb5 config",
config: pivnet.ProxyAuthConfig{
AuthType: pivnet.ProxyAuthTypeSPNEGO,
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "http://proxy.example.com:8080",
Krb5Config: "../krb5.conf",
},
expectError: true,
errorContains: "krb5 config path contains invalid path traversal",
},
// General Error Cases
{
name: "empty auth type",
config: pivnet.ProxyAuthConfig{
AuthType: "",
Username: "user",
Password: "pass",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
errorContains: "proxy authentication type cannot be empty",
},
{
name: "unsupported auth type",
config: pivnet.ProxyAuthConfig{
AuthType: "digest",
Username: "user",
Password: "pass",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
errorContains: "unsupported proxy authentication type",
},
{
name: "case insensitive basic auth type",
config: pivnet.ProxyAuthConfig{
AuthType: "BASIC",
Username: "testuser",
Password: "testpass",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
},
{
name: "case insensitive spnego auth type (will fail on krb5 config)",
config: pivnet.ProxyAuthConfig{
AuthType: "SPNEGO",
Username: "user@REALM.COM",
Password: "password",
ProxyURL: "http://proxy.example.com:8080",
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
authenticator, err := pivnet.NewProxyAuthenticator(tt.config)
if tt.expectError {
if err == nil {
t.Errorf("expected error containing '%s', but got no error", tt.errorContains)
return
}
if !strings.Contains(err.Error(), tt.errorContains) {
t.Errorf("expected error containing '%s', but got: %v", tt.errorContains, err)
}
if authenticator != nil {
t.Errorf("expected nil authenticator on error, but got: %v", authenticator)
}
} else {
if err != nil {
t.Errorf("expected no error, but got: %v", err)
return
}
if authenticator == nil {
t.Errorf("expected authenticator to be non-nil")
}
}
})
}
}