-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestAuth.cs
More file actions
226 lines (191 loc) · 6.95 KB
/
TestAuth.cs
File metadata and controls
226 lines (191 loc) · 6.95 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
namespace Weaviate.Client.Tests.Integration;
using System.Net.Http;
using System.Threading.Tasks;
using Weaviate.Client;
public class TestAuth : IntegrationTests
{
const int ANON_PORT = 8080;
const int OKTA_PORT_CC = 8082;
const int OKTA_PORT_USERS = 8083;
private static async Task<(
bool IsSuccessStatusCode,
string? TokenEndpoint,
string? ClientID
)> GetOpenIdConfig(string url)
{
var response = await OAuthTokenService.GetOpenIdConfig(url);
return response;
}
private static async Task<(
bool IsSuccessStatusCode,
string? AccessToken,
int? ExpiresIn,
string? RefreshToken,
string? ClientID
)> GetAccessToken(string url, string user, string password)
{
var response = await OAuthTokenService.GetOpenIdConfig($"http://{url}/v1/");
var tokenSvc = new OAuthTokenService(
new HttpClient(),
new()
{
ClientId = response.ClientID!,
GrantType = "password",
Password = password,
Username = user,
TokenEndpoint = response.TokenEndpoint!,
Scope = "",
}
);
var _ = await tokenSvc.GetAccessTokenAsync();
if (tokenSvc.CurrentToken == null)
{
throw new InvalidOperationException("Failed to retrieve access token");
}
var currentTokenResponse = tokenSvc.CurrentToken;
return (
response.IsSuccessStatusCode,
currentTokenResponse?.AccessToken ?? "",
currentTokenResponse?.ExpiresIn ?? 0,
currentTokenResponse?.RefreshToken ?? "",
response.ClientID
);
}
private static async Task<bool> IsAuthEnabled(string url)
{
return (await GetOpenIdConfig($"http://{url}/v1/")).IsSuccessStatusCode;
}
[Fact]
public async Task TestNoAuthProvided()
{
Assert.True(await IsAuthEnabled($"localhost:{OKTA_PORT_CC}"));
await Assert.ThrowsAnyAsync<WeaviateServerException>(async () =>
{
await Connect.Local(hostname: "localhost", restPort: OKTA_PORT_CC);
});
}
[Fact]
public async Task TestAuthenticationClientCredentials_Okta()
{
string clientSecret = Environment.GetEnvironmentVariable("OKTA_CLIENT_SECRET")!;
if (string.IsNullOrEmpty(clientSecret))
{
Assert.Skip("OKTA_CLIENT_SECRET is not set");
}
Assert.True(await IsAuthEnabled($"localhost:{OKTA_PORT_CC}"));
var client = await Connect.Local(
hostname: "localhost",
restPort: OKTA_PORT_CC,
credentials: Auth.ClientCredentials(clientSecret, "some_scope"),
httpMessageHandler: _httpMessageHandler
);
await client
.Collections.List(TestContext.Current.CancellationToken)
.ToListAsync(TestContext.Current.CancellationToken);
}
[Fact]
public async Task TestAuthenticationUserPw_Okta()
{
string pw = Environment.GetEnvironmentVariable("OKTA_DUMMY_CI_PW")!;
if (string.IsNullOrEmpty(pw))
{
Assert.Skip("OKTA_DUMMY_CI_PW is not set");
}
Assert.True(await IsAuthEnabled($"localhost:{OKTA_PORT_USERS}"));
var client = await Connect.Local(
hostname: "localhost",
restPort: OKTA_PORT_USERS,
credentials: Auth.ClientPassword(
username: "test@test.de",
password: pw,
scope: "some_scope offline_access"
),
httpMessageHandler: _httpMessageHandler
);
await client
.Collections.List(TestContext.Current.CancellationToken)
.ToListAsync(TestContext.Current.CancellationToken);
}
[Fact]
public async Task TestClientWithAuthenticationWithAnonWeaviate()
{
Assert.False(await IsAuthEnabled($"localhost:{ANON_PORT}"));
var client = await Connect.Local(
hostname: "localhost",
restPort: ANON_PORT,
credentials: Auth.ClientPassword(
username: "someUser",
password: "SomePw",
scope: "some_scope"
),
httpMessageHandler: _httpMessageHandler
);
// TODO Needs a finalized way to inject a logger and check that no warnings were logged
}
[Fact]
public async Task TestAuthenticationWithBearerToken_Okta()
{
string url = $"localhost:{OKTA_PORT_USERS}";
Assert.True(await IsAuthEnabled(url));
string pw = Environment.GetEnvironmentVariable("OKTA_DUMMY_CI_PW")!;
if (string.IsNullOrEmpty(pw))
{
Assert.Skip("OKTA_DUMMY_CI_PW is not set");
}
var token = await GetAccessToken(url, "test@test.de", pw);
var auth = Auth.BearerToken(
accessToken: token.AccessToken ?? "",
expiresIn: token.ExpiresIn ?? 0,
refreshToken: token.RefreshToken ?? ""
);
var client = await Connect.Local(
hostname: "localhost",
restPort: OKTA_PORT_USERS,
credentials: auth,
httpMessageHandler: _httpMessageHandler
);
await client.Collections.Exists("something", TestContext.Current.CancellationToken);
}
[Fact]
public async Task TestAuthenticationWithBearerTokenNoRefresh()
{
string url = $"localhost:{OKTA_PORT_USERS}";
Assert.True(await IsAuthEnabled(url));
string pw = Environment.GetEnvironmentVariable("OKTA_DUMMY_CI_PW")!;
if (string.IsNullOrEmpty(pw))
{
Assert.Skip("OKTA_DUMMY_CI_PW is not set");
}
var token = await GetAccessToken(url, "test@test.de", pw);
var auth = Auth.BearerToken(
accessToken: token.AccessToken ?? "",
expiresIn: token.ExpiresIn ?? 0,
refreshToken: "" // No refresh token provided
);
var client = await Connect.Local(
hostname: "localhost",
restPort: OKTA_PORT_USERS,
credentials: auth,
httpMessageHandler: _httpMessageHandler
);
await client
.Collections.List(TestContext.Current.CancellationToken)
.ToListAsync(TestContext.Current.CancellationToken);
// TODO Needs a finalized way to inject a logger and check that no warnings were logged
}
[Fact]
public async Task TestAuthenticationFailure()
{
string clientSecret = "invalid-secret";
Assert.True(await IsAuthEnabled($"localhost:{OKTA_PORT_CC}"));
await Assert.ThrowsAsync<WeaviateAuthenticationException>(async () =>
{
await Connect.Local(
hostname: "localhost",
restPort: OKTA_PORT_CC,
credentials: Auth.ClientCredentials(clientSecret, "some_scope"),
httpMessageHandler: _httpMessageHandler
);
});
}
}