|
| 1 | +/* |
| 2 | + * Copyright 2021 Red Hat |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.apicurio.registry.auth; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author carnalca@redhat.com |
| 21 | + */ |
| 22 | + |
| 23 | +import io.apicurio.registry.AbstractResourceTestBase; |
| 24 | +import io.apicurio.registry.rest.client.RegistryClient; |
| 25 | +import io.apicurio.registry.rest.client.RegistryClientFactory; |
| 26 | +import io.apicurio.registry.rest.v2.beans.ArtifactSearchResults; |
| 27 | +import io.apicurio.registry.types.ArtifactType; |
| 28 | +import io.apicurio.registry.utils.tests.ApicurioTestTags; |
| 29 | +import io.apicurio.registry.utils.tests.AuthTestProfileAuthenticatedReadAccess; |
| 30 | +import io.apicurio.rest.client.auth.Auth; |
| 31 | +import io.apicurio.rest.client.auth.OidcAuth; |
| 32 | +import io.apicurio.rest.client.auth.exception.ForbiddenException; |
| 33 | +import io.quarkus.test.junit.QuarkusTest; |
| 34 | +import io.quarkus.test.junit.TestProfile; |
| 35 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.Tag; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +import java.io.ByteArrayInputStream; |
| 41 | +import java.io.InputStream; |
| 42 | +import java.nio.charset.StandardCharsets; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.Optional; |
| 45 | + |
| 46 | +@QuarkusTest |
| 47 | +@TestProfile(AuthTestProfileAuthenticatedReadAccess.class) |
| 48 | +@Tag(ApicurioTestTags.DOCKER) |
| 49 | +public class AuthTestAuthenticatedReadAccess extends AbstractResourceTestBase { |
| 50 | + |
| 51 | + @ConfigProperty(name = "registry.auth.token.endpoint") |
| 52 | + String authServerUrl; |
| 53 | + |
| 54 | + String noRoleClientId = "registry-api-no-role"; |
| 55 | + String adminClientId = "registry-api"; |
| 56 | + |
| 57 | + final String groupId = getClass().getSimpleName() + "Group"; |
| 58 | + |
| 59 | + private RegistryClient createClient(Auth auth) { |
| 60 | + return RegistryClientFactory.create(registryV2ApiUrl, Collections.emptyMap(), auth); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected RegistryClient createRestClientV2() { |
| 65 | + Auth auth = new OidcAuth(authServerUrl, adminClientId, "test1", Optional.empty()); |
| 66 | + return this.createClient(auth); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testReadOperationWithNoRole() throws Exception { |
| 71 | + // Read-only operation should work with credentials but no role. |
| 72 | + |
| 73 | + Auth auth = new OidcAuth(authServerUrl, noRoleClientId, "test1", Optional.empty()); |
| 74 | + RegistryClient client = this.createClient(auth); |
| 75 | + ArtifactSearchResults results = client.searchArtifacts(groupId, null, null, null, null, null, null, null, null); |
| 76 | + Assertions.assertTrue(results.getCount() >= 0); |
| 77 | + |
| 78 | + // Write operation should fail with credentials but not role. |
| 79 | + InputStream data = new ByteArrayInputStream(("{\r\n" + |
| 80 | + " \"type\" : \"record\",\r\n" + |
| 81 | + " \"name\" : \"userInfo\",\r\n" + |
| 82 | + " \"namespace\" : \"my.example\",\r\n" + |
| 83 | + " \"fields\" : [{\"name\" : \"age\", \"type\" : \"int\"}]\r\n" + |
| 84 | + "}").getBytes(StandardCharsets.UTF_8)); |
| 85 | + Assertions.assertThrows(ForbiddenException.class, () -> { |
| 86 | + client.createArtifact(groupId, "testReadOperationWithNoRole", ArtifactType.AVRO, data); |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
0 commit comments