-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathSimpleAuthTest.java
388 lines (326 loc) · 17 KB
/
SimpleAuthTest.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright 2021 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apicurio.registry.auth;
import io.apicurio.common.apps.config.Info;
import io.apicurio.registry.AbstractResourceTestBase;
import io.apicurio.registry.rest.client.AdminClient;
import io.apicurio.registry.rest.client.RegistryClient;
import io.apicurio.registry.rest.client.exception.ArtifactNotFoundException;
import io.apicurio.registry.rest.v2.beans.ArtifactMetaData;
import io.apicurio.registry.rest.v2.beans.ArtifactOwner;
import io.apicurio.registry.rest.v2.beans.EditableMetaData;
import io.apicurio.registry.rest.v2.beans.IfExists;
import io.apicurio.registry.rest.v2.beans.Rule;
import io.apicurio.registry.rest.v2.beans.UserInfo;
import io.apicurio.registry.rules.compatibility.CompatibilityLevel;
import io.apicurio.registry.rules.validity.ValidityLevel;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.types.RuleType;
import io.apicurio.registry.utils.IoUtil;
import io.apicurio.registry.utils.tests.ApicurioTestTags;
import io.apicurio.registry.utils.tests.AuthTestProfile;
import io.apicurio.registry.utils.tests.JWKSMockServer;
import io.apicurio.registry.utils.tests.TestUtils;
import io.apicurio.rest.client.auth.Auth;
import io.apicurio.rest.client.auth.BasicAuth;
import io.apicurio.rest.client.auth.OidcAuth;
import io.apicurio.rest.client.auth.exception.AuthErrorHandler;
import io.apicurio.rest.client.auth.exception.ForbiddenException;
import io.apicurio.rest.client.auth.exception.NotAuthorizedException;
import io.apicurio.rest.client.spi.ApicurioHttpClient;
import io.apicurio.rest.client.spi.ApicurioHttpClientFactory;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Fabian Martinez
*/
@QuarkusTest
@TestProfile(AuthTestProfile.class)
@Tag(ApicurioTestTags.SLOW)
public class SimpleAuthTest extends AbstractResourceTestBase {
private static final String ARTIFACT_CONTENT = "{\"name\":\"redhat\"}";
@ConfigProperty(name = "registry.auth.token.endpoint")
@Info(category = "auth", description = "Auth token endpoint", availableSince = "2.1.0.Final")
String authServerUrlConfigured;
final String groupId = "authTestGroupId";
ApicurioHttpClient httpClient;
/**
* @see io.apicurio.registry.AbstractResourceTestBase#createRestClientV2()
*/
@Override
protected RegistryClient createRestClientV2() {
httpClient = ApicurioHttpClientFactory.create(authServerUrlConfigured, new AuthErrorHandler());
Auth auth = new OidcAuth(httpClient, JWKSMockServer.ADMIN_CLIENT_ID, "test1");
return this.createClient(auth);
}
@Override
protected AdminClient createAdminClientV2() {
httpClient = ApicurioHttpClientFactory.create(authServerUrlConfigured, new AuthErrorHandler());
Auth auth = new OidcAuth(httpClient, JWKSMockServer.ADMIN_CLIENT_ID, "test1");
return this.createAdminClient(auth);
}
@Test
public void testWrongCreds() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.WRONG_CREDS_CLIENT_ID, "test55");
RegistryClient client = createClient(auth);
Assertions.assertThrows(NotAuthorizedException.class, () -> {
client.listArtifactsInGroup(groupId);
});
}
@Test
public void testNoCreds() throws Exception {
RegistryClient clientNoAuth = createClient(null);
Assertions.assertThrows(NotAuthorizedException.class, () -> {
clientNoAuth.listArtifactsInGroup(groupId);
});
}
@Test
public void testReadOnly() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.READONLY_CLIENT_ID, "test1");
RegistryClient client = createClient(auth);
String artifactId = TestUtils.generateArtifactId();
client.listArtifactsInGroup(groupId);
Assertions.assertThrows(ArtifactNotFoundException.class, () -> client.getArtifactMetaData(groupId, artifactId));
Assertions.assertThrows(ArtifactNotFoundException.class, () -> client.getLatestArtifact("abc", artifactId));
Assertions.assertThrows(ForbiddenException.class, () -> {
client.createArtifact("testReadOnly", artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
});
{
Auth devAuth = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient devClient = createClient(devAuth);
ArtifactMetaData meta = devClient.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
TestUtils.retry(() -> devClient.getArtifactMetaData(groupId, meta.getId()));
}
assertNotNull(client.getLatestArtifact(groupId, artifactId));
UserInfo userInfo = client.getCurrentUserInfo();
assertNotNull(userInfo);
Assertions.assertEquals("readonly-client", userInfo.getUsername());
Assertions.assertFalse(userInfo.getAdmin());
Assertions.assertFalse(userInfo.getDeveloper());
Assertions.assertTrue(userInfo.getViewer());
}
@Test
public void testDevRole() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient client = createClient(auth);
String artifactId = TestUtils.generateArtifactId();
try {
client.listArtifactsInGroup(groupId);
client.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
TestUtils.retry(() -> client.getArtifactMetaData(groupId, artifactId));
assertNotNull(client.getLatestArtifact(groupId, artifactId));
Rule ruleConfig = new Rule();
ruleConfig.setType(RuleType.VALIDITY);
ruleConfig.setConfig(ValidityLevel.NONE.name());
client.createArtifactRule(groupId, artifactId, ruleConfig);
Assertions.assertThrows(ForbiddenException.class, () -> {
client.createGlobalRule(ruleConfig);
});
UserInfo userInfo = client.getCurrentUserInfo();
assertNotNull(userInfo);
Assertions.assertEquals("developer-client", userInfo.getUsername());
Assertions.assertFalse(userInfo.getAdmin());
Assertions.assertTrue(userInfo.getDeveloper());
Assertions.assertFalse(userInfo.getViewer());
} finally {
client.deleteArtifact(groupId, artifactId);
}
}
@Test
public void testAdminRole() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.ADMIN_CLIENT_ID, "test1");
RegistryClient client = createClient(auth);
AdminClient adminClient = createAdminClient(auth);
String artifactId = TestUtils.generateArtifactId();
try {
client.listArtifactsInGroup(groupId);
client.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
TestUtils.retry(() -> client.getArtifactMetaData(groupId, artifactId));
assertNotNull(client.getLatestArtifact(groupId, artifactId));
Rule ruleConfig = new Rule();
ruleConfig.setType(RuleType.VALIDITY);
ruleConfig.setConfig(ValidityLevel.NONE.name());
client.createArtifactRule(groupId, artifactId, ruleConfig);
adminClient.createGlobalRule(ruleConfig);
UserInfo userInfo = client.getCurrentUserInfo();
assertNotNull(userInfo);
Assertions.assertEquals("admin-client", userInfo.getUsername());
Assertions.assertTrue(userInfo.getAdmin());
Assertions.assertFalse(userInfo.getDeveloper());
Assertions.assertFalse(userInfo.getViewer());
} finally {
client.deleteArtifact(groupId, artifactId);
}
}
@Test
public void testAdminRoleBasicAuth() throws Exception {
Auth auth = new BasicAuth(JWKSMockServer.BASIC_USER, JWKSMockServer.BASIC_PASSWORD);
RegistryClient client = createClient(auth);
AdminClient adminClient = createAdminClient(auth);
String artifactId = TestUtils.generateArtifactId();
try {
client.listArtifactsInGroup(groupId);
client.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
TestUtils.retry(() -> client.getArtifactMetaData(groupId, artifactId));
assertNotNull(client.getLatestArtifact(groupId, artifactId));
Rule ruleConfig = new Rule();
ruleConfig.setType(RuleType.VALIDITY);
ruleConfig.setConfig(ValidityLevel.NONE.name());
client.createArtifactRule(groupId, artifactId, ruleConfig);
adminClient.createGlobalRule(ruleConfig);
} finally {
client.deleteArtifact(groupId, artifactId);
}
}
@Test
public void testAdminRoleBasicAuthWrongCreds() throws Exception {
Auth auth = new BasicAuth(JWKSMockServer.WRONG_CREDS_CLIENT_ID, UUID.randomUUID().toString());
RegistryClient client = createClient(auth);
String artifactId = TestUtils.generateArtifactId();
Assertions.assertThrows(NotAuthorizedException.class, () -> {
client.listArtifactsInGroup(groupId);
});
Assertions.assertThrows(NotAuthorizedException.class, () -> {
client.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
});
}
@Test
public void testOwnerOnlyAuthorization() throws Exception {
Auth authDev = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient clientDev = createClient(authDev);
Auth authAdmin = new OidcAuth(httpClient, JWKSMockServer.ADMIN_CLIENT_ID, "test1");
RegistryClient clientAdmin = createClient(authAdmin);
// Admin user will create an artifact
String artifactId = TestUtils.generateArtifactId();
clientAdmin.createArtifact(groupId, artifactId, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
EditableMetaData updatedMetaData = new EditableMetaData();
updatedMetaData.setName("Updated Name");
// Dev user cannot edit the same artifact because Dev user is not the owner
Assertions.assertThrows(ForbiddenException.class, () -> {
clientDev.updateArtifactMetaData(groupId, artifactId, updatedMetaData);
});
// Dev user cannot update with ifExists the same artifact because Dev user is not the owner
Assertions.assertThrows(ForbiddenException.class, () -> {
clientDev.createArtifact(groupId, artifactId, ArtifactType.JSON, IfExists.RETURN_OR_UPDATE, new ByteArrayInputStream("{fffff}".getBytes()));
});
// But the admin user CAN make the change.
clientAdmin.updateArtifactMetaData(groupId, artifactId, updatedMetaData);
// Now the Dev user will create an artifact
String artifactId2 = TestUtils.generateArtifactId();
clientDev.createArtifact(groupId, artifactId2, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
// And the Admin user will modify it (allowed because it's the Admin user)
Rule rule = new Rule();
rule.setType(RuleType.COMPATIBILITY);
rule.setConfig(CompatibilityLevel.BACKWARD.name());
clientAdmin.createArtifactRule(groupId, artifactId2, rule);
}
@Test
public void testGetArtifactOwner() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient client = createClient(auth);
//Preparation
final String groupId = "testGetArtifactOwner";
final String artifactId = generateArtifactId();
final String version = "1";
final String name = "testGetArtifactOwnerName";
final String description = "testGetArtifactOwnerDescription";
//Execution
final InputStream stream = IoUtil.toStream(ARTIFACT_CONTENT.getBytes(StandardCharsets.UTF_8));
final ArtifactMetaData created = client.createArtifact(groupId, artifactId, version, ArtifactType.JSON, IfExists.FAIL, false, name, description, stream);
//Assertions
assertNotNull(created);
assertEquals(groupId, created.getGroupId());
assertEquals(artifactId, created.getId());
assertEquals(version, created.getVersion());
assertEquals("developer-client", created.getCreatedBy());
//Get the artifact owner via the REST API and verify it
ArtifactOwner owner = client.getArtifactOwner(groupId, artifactId);
assertEquals("developer-client", owner.getOwner());
}
@Test
public void testUpdateArtifactOwner() throws Exception {
Auth auth = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient client = createClient(auth);
//Preparation
final String groupId = "testUpdateArtifactOwner";
final String artifactId = generateArtifactId();
final String version = "1.0";
final String name = "testUpdateArtifactOwnerName";
final String description = "testUpdateArtifactOwnerDescription";
//Execution
final InputStream stream = IoUtil.toStream(ARTIFACT_CONTENT.getBytes(StandardCharsets.UTF_8));
final ArtifactMetaData created = client.createArtifact(groupId, artifactId, version, ArtifactType.JSON, IfExists.FAIL, false, name, description, stream);
//Assertions
assertNotNull(created);
assertEquals(groupId, created.getGroupId());
assertEquals(artifactId, created.getId());
assertEquals(version, created.getVersion());
assertEquals("developer-client", created.getCreatedBy());
//Get the artifact owner via the REST API and verify it
ArtifactOwner owner = client.getArtifactOwner(groupId, artifactId);
assertEquals("developer-client", owner.getOwner());
//Update the owner
owner = new ArtifactOwner();
owner.setOwner("developer-2-client");
client.updateArtifactOwner(groupId, artifactId, owner);
//Check that the update worked
owner = client.getArtifactOwner(groupId, artifactId);
assertEquals("developer-2-client", owner.getOwner());
}
@Test
public void testUpdateArtifactOwnerOnlyByOwner() throws Exception {
Auth auth_dev1 = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
RegistryClient client_dev1 = createClient(auth_dev1);
Auth auth_dev2 = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_2_CLIENT_ID, "test1");
RegistryClient client_dev2 = createClient(auth_dev2);
//Preparation
final String groupId = "testUpdateArtifactOwnerOnlyByOwner";
final String artifactId = generateArtifactId();
final String version = "1.0";
final String name = "testUpdateArtifactOwnerOnlyByOwnerName";
final String description = "testUpdateArtifactOwnerOnlyByOwnerDescription";
//Execution
final InputStream stream = IoUtil.toStream(ARTIFACT_CONTENT.getBytes(StandardCharsets.UTF_8));
final ArtifactMetaData created = client_dev1.createArtifact(groupId, artifactId, version, ArtifactType.JSON, IfExists.FAIL, false, name, description, stream);
//Assertions
assertNotNull(created);
assertEquals(groupId, created.getGroupId());
assertEquals(artifactId, created.getId());
assertEquals(version, created.getVersion());
assertEquals("developer-client", created.getCreatedBy());
//Get the artifact owner via the REST API and verify it
ArtifactOwner owner = client_dev1.getArtifactOwner(groupId, artifactId);
assertEquals("developer-client", owner.getOwner());
//Try to update the owner by dev2 (should fail)
assertThrows(ForbiddenException.class, () -> {
ArtifactOwner newOwner = new ArtifactOwner();
newOwner.setOwner("developer-2-client");
client_dev2.updateArtifactOwner(groupId, artifactId, newOwner);
});
//Should still be the original owner
owner = client_dev1.getArtifactOwner(groupId, artifactId);
assertEquals("developer-client", owner.getOwner());
}
}