Changesets · Branches · Tags · Compare
| Changeset | e7ba3fa407b87aa851796b8f154ead251b595ae4766e58c245bc15b031bbfed1 |
|---|---|
| Author | Enrico Risa <enrico.risa@gmail.com> · 2024-07-09 13:17 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-09 13:17 +02:00 |
| Parents | 421ed927c56e |
19 files changed
modified extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreDefaultServicesExtension.java +7 −1
| @@ -14,7 +14,9 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.api; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.api.auth.ApiAuthenticationProviderRegistryImpl; | |
| 17 | 18 | import org.eclipse.edc.api.auth.ApiAuthenticationRegistryImpl; |
| 19 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 18 | 20 | import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry; |
| 19 | 21 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 20 | 22 | import org.eclipse.edc.runtime.metamodel.annotation.Provider; |
| @@ -37,5 +39,9 @@ | ||
| 37 | 39 | public ApiAuthenticationRegistry apiAuthenticationRegistry() { |
| 38 | 40 | return new ApiAuthenticationRegistryImpl(); |
| 39 | 41 | } |
| 40 | - | |
| 42 | + | |
| 43 | + @Provider | |
| 44 | + public ApiAuthenticationProviderRegistry apiAuthenticationProviderRegistry() { | |
| 45 | + return new ApiAuthenticationProviderRegistryImpl(); | |
| 46 | + } | |
| 41 | 47 | } |
added extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/auth/ApiAuthenticationProviderRegistryImpl.java +35 −0
| @@ -0,0 +1,35 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 18 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 19 | + | |
| 20 | +import java.util.HashMap; | |
| 21 | +import java.util.Map; | |
| 22 | + | |
| 23 | +public class ApiAuthenticationProviderRegistryImpl implements ApiAuthenticationProviderRegistry { | |
| 24 | + private final Map<String, ApiAuthenticationProvider> providers = new HashMap<>(); | |
| 25 | + | |
| 26 | + @Override | |
| 27 | + public void register(String type, ApiAuthenticationProvider provider) { | |
| 28 | + providers.put(type, provider); | |
| 29 | + } | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + public ApiAuthenticationProvider resolve(String type) { | |
| 33 | + return providers.get(type); | |
| 34 | + } | |
| 35 | +} |
added extensions/common/api/api-core/src/test/java/org/eclipse/edc/api/auth/ApiAuthenticationProviderRegistryImplTest.java +37 −0
| @@ -0,0 +1,37 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 18 | +import org.junit.jupiter.api.Test; | |
| 19 | + | |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 21 | +import static org.mockito.Mockito.mock; | |
| 22 | + | |
| 23 | +class ApiAuthenticationProviderRegistryImplTest { | |
| 24 | + | |
| 25 | + private final ApiAuthenticationProviderRegistryImpl registry = new ApiAuthenticationProviderRegistryImpl(); | |
| 26 | + | |
| 27 | + @Test | |
| 28 | + void shouldResolveRegisteredProvider() { | |
| 29 | + ApiAuthenticationProvider provider = mock(); | |
| 30 | + registry.register("authType", provider); | |
| 31 | + | |
| 32 | + var actual = registry.resolve("authType"); | |
| 33 | + | |
| 34 | + assertThat(actual).isSameAs(provider); | |
| 35 | + } | |
| 36 | + | |
| 37 | +} |
added extensions/common/auth/auth-configuration/README.md +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +# Authentication Configuration | |
| 2 | + | |
| 3 | +This extension allows to secure a set of APIs grouped by a web context. It inspects | |
| 4 | +all `web.http.<context>` and if the authentication is configured it applies the `AuthenticationRequestFilter` | |
| 5 | +to the `<context>` with the chosen `AuthenticationService`. The chosen `AuthenticationService` is currently registered | |
| 6 | +in the `ApiAuthenticationRegistry`. This will be removed once the `ApiAuthenticationRegistry` will be refactored out. | |
| 7 | + | |
| 8 | +## Configuration | |
| 9 | + | |
| 10 | +| Key | Description | Mandatory | | |
| 11 | +|:--------------------------------|:-------------------------------------------------------------------------------------------|-----------| | |
| 12 | +| web.http.<context>.auth.type | The type of authentication to apply to the `<context>` | | | |
| 13 | +| web.http.<context>.auth.context | Override the name of the context in the `ApiAuthenticationRegistry` instead of `<context>` | | | |
| 14 | + | |
| 15 | +Depending on the `web.http.<context>.auth.type` chosen, additional properties might be required in order to configure | |
| 16 | +the `AuthenticationService`. | |
| 17 | + | |
| 18 | +Example of a complete configuration for a custom context with token based authentication | |
| 19 | + | |
| 20 | +```properties | |
| 21 | +web.http.custom.path=/custom | |
| 22 | +web.http.custom.port=8081 | |
| 23 | +web.http.custom.auth.type=tokenbased | |
| 24 | +web.http.custom.auth.key=apiKey | |
| 25 | +``` | |
| No newline at the end of the file |
added extensions/common/auth/auth-configuration/build.gradle.kts +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +plugins { | |
| 16 | + `java-library` | |
| 17 | +} | |
| 18 | + | |
| 19 | +dependencies { | |
| 20 | + api(project(":spi:common:auth-spi")) | |
| 21 | + | |
| 22 | + testImplementation(project(":core:common:junit")) | |
| 23 | +} | |
| 24 | + | |
| 25 | + |
added extensions/common/auth/auth-configuration/src/main/java/org/eclipse/edc/api/auth/configuration/ApiAuthenticationConfigurationExtension.java +99 −0
| @@ -0,0 +1,99 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth.configuration; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.api.auth.spi.AuthenticationRequestFilter; | |
| 18 | +import org.eclipse.edc.api.auth.spi.AuthenticationService; | |
| 19 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 20 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry; | |
| 21 | +import org.eclipse.edc.runtime.metamodel.annotation.Extension; | |
| 22 | +import org.eclipse.edc.runtime.metamodel.annotation.Inject; | |
| 23 | +import org.eclipse.edc.runtime.metamodel.annotation.Setting; | |
| 24 | +import org.eclipse.edc.spi.EdcException; | |
| 25 | +import org.eclipse.edc.spi.result.Result; | |
| 26 | +import org.eclipse.edc.spi.system.ServiceExtension; | |
| 27 | +import org.eclipse.edc.spi.system.ServiceExtensionContext; | |
| 28 | +import org.eclipse.edc.spi.system.configuration.Config; | |
| 29 | +import org.eclipse.edc.web.spi.WebService; | |
| 30 | + | |
| 31 | +import java.util.HashMap; | |
| 32 | +import java.util.Map; | |
| 33 | +import java.util.Optional; | |
| 34 | +import java.util.stream.Collectors; | |
| 35 | + | |
| 36 | +import static org.eclipse.edc.api.auth.configuration.ApiAuthenticationConfigurationExtension.NAME; | |
| 37 | +import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX; | |
| 38 | + | |
| 39 | +@Extension(NAME) | |
| 40 | +public class ApiAuthenticationConfigurationExtension implements ServiceExtension { | |
| 41 | + | |
| 42 | + public static final String NAME = "Api Authentication Configuration Extension"; | |
| 43 | + | |
| 44 | + public static final String AUTH_KEY = "auth"; | |
| 45 | + public static final String CONFIG_ALIAS = WEB_HTTP_PREFIX + ".<context>." + AUTH_KEY + "."; | |
| 46 | + | |
| 47 | + @Setting(context = CONFIG_ALIAS, value = "The type of the authentication provider.", required = true) | |
| 48 | + public static final String TYPE_KEY = "type"; | |
| 49 | + | |
| 50 | + @Setting(context = CONFIG_ALIAS, value = "The api context where to apply the authentication. Default to the web <context>") | |
| 51 | + public static final String CONTEXT_KEY = "context"; | |
| 52 | + | |
| 53 | + private Map<String, Config> authConfiguration = new HashMap<>(); | |
| 54 | + | |
| 55 | + @Inject | |
| 56 | + private ApiAuthenticationProviderRegistry providerRegistry; | |
| 57 | + | |
| 58 | + @Inject | |
| 59 | + private ApiAuthenticationRegistry authenticationRegistry; | |
| 60 | + | |
| 61 | + @Inject | |
| 62 | + private WebService webService; | |
| 63 | + | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public String name() { | |
| 67 | + return NAME; | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public void initialize(ServiceExtensionContext context) { | |
| 72 | + authConfiguration = context.getConfig(WEB_HTTP_PREFIX) | |
| 73 | + .partition().filter(config -> config.getString(AUTH_KEY + "." + TYPE_KEY, null) != null) | |
| 74 | + .map(cfg -> Map.entry(cfg.currentNode(), cfg.getConfig(AUTH_KEY))) | |
| 75 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + public void prepare() { | |
| 80 | + for (var entry : authConfiguration.entrySet()) { | |
| 81 | + var webContext = entry.getKey(); | |
| 82 | + var apiContext = Optional.ofNullable(entry.getValue().getString(CONTEXT_KEY, null)).orElse(webContext); | |
| 83 | + var serviceResult = configureService(entry.getValue()); | |
| 84 | + if (serviceResult.failed()) { | |
| 85 | + throw new EdcException("Failed to configure authentication for context %s: %s".formatted(entry.getKey(), serviceResult.getFailureDetail())); | |
| 86 | + } | |
| 87 | + authenticationRegistry.register(apiContext, serviceResult.getContent()); | |
| 88 | + var authenticationFilter = new AuthenticationRequestFilter(authenticationRegistry, apiContext); | |
| 89 | + webService.registerResource(entry.getKey(), authenticationFilter); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + private Result<AuthenticationService> configureService(Config config) { | |
| 94 | + var type = config.getString(TYPE_KEY); | |
| 95 | + return Optional.ofNullable(providerRegistry.resolve(type)) | |
| 96 | + .map(provider -> provider.provide(config)) | |
| 97 | + .orElseGet(() -> Result.failure("Authentication provider for type %s not found".formatted(type))); | |
| 98 | + } | |
| 99 | +} |
added extensions/common/auth/auth-configuration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension +15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# | |
| 2 | +# Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | +# | |
| 4 | +# This program and the accompanying materials are made available under the | |
| 5 | +# terms of the Apache License, Version 2.0 which is available at | |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | +# | |
| 8 | +# SPDX-License-Identifier: Apache-2.0 | |
| 9 | +# | |
| 10 | +# Contributors: | |
| 11 | +# Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | +# | |
| 13 | +# | |
| 14 | + | |
| 15 | +org.eclipse.edc.api.auth.configuration.ApiAuthenticationConfigurationExtension |
added extensions/common/auth/auth-configuration/src/test/java/org/eclipse/edc/api/auth/configuration/ApiAuthenticationConfigurationExtensionTest.java +132 −0
| @@ -0,0 +1,132 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth.configuration; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 18 | +import org.eclipse.edc.api.auth.spi.AuthenticationService; | |
| 19 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 20 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry; | |
| 21 | +import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | |
| 22 | +import org.eclipse.edc.spi.EdcException; | |
| 23 | +import org.eclipse.edc.spi.monitor.Monitor; | |
| 24 | +import org.eclipse.edc.spi.result.Result; | |
| 25 | +import org.eclipse.edc.spi.system.ServiceExtensionContext; | |
| 26 | +import org.eclipse.edc.spi.system.configuration.ConfigFactory; | |
| 27 | +import org.junit.jupiter.api.BeforeEach; | |
| 28 | +import org.junit.jupiter.api.Test; | |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; | |
| 30 | + | |
| 31 | +import java.util.Map; | |
| 32 | + | |
| 33 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
| 34 | +import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX; | |
| 35 | +import static org.mockito.ArgumentMatchers.any; | |
| 36 | +import static org.mockito.ArgumentMatchers.anyString; | |
| 37 | +import static org.mockito.Mockito.mock; | |
| 38 | +import static org.mockito.Mockito.verify; | |
| 39 | +import static org.mockito.Mockito.verifyNoMoreInteractions; | |
| 40 | +import static org.mockito.Mockito.when; | |
| 41 | + | |
| 42 | +@ExtendWith(DependencyInjectionExtension.class) | |
| 43 | +class ApiAuthenticationConfigurationExtensionTest { | |
| 44 | + | |
| 45 | + private final Monitor monitor = mock(Monitor.class); | |
| 46 | + private final ApiAuthenticationRegistry authenticationRegistry = mock(); | |
| 47 | + private final ApiAuthenticationProviderRegistry providerRegistry = mock(); | |
| 48 | + | |
| 49 | + @BeforeEach | |
| 50 | + void setUp(ServiceExtensionContext context) { | |
| 51 | + when(monitor.withPrefix(anyString())).thenReturn(monitor); | |
| 52 | + when(context.getMonitor()).thenReturn(monitor); | |
| 53 | + context.registerService(ApiAuthenticationRegistry.class, authenticationRegistry); | |
| 54 | + context.registerService(ApiAuthenticationProviderRegistry.class, providerRegistry); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test | |
| 58 | + void prepare(ApiAuthenticationConfigurationExtension extension, ServiceExtensionContext context) { | |
| 59 | + var authType = "testAuth"; | |
| 60 | + var config = ConfigFactory.fromMap(Map.of("test.auth.type", authType, "test.auth.custom", "custom")); | |
| 61 | + var provider = mock(ApiAuthenticationProvider.class); | |
| 62 | + var authentication = mock(AuthenticationService.class); | |
| 63 | + when(context.getConfig(WEB_HTTP_PREFIX)).thenReturn(config); | |
| 64 | + when(providerRegistry.resolve(authType)).thenReturn(provider); | |
| 65 | + when(provider.provide(any())).thenReturn(Result.success(authentication)); | |
| 66 | + | |
| 67 | + extension.initialize(context); | |
| 68 | + extension.prepare(); | |
| 69 | + | |
| 70 | + verify(providerRegistry).resolve(authType); | |
| 71 | + verify(provider).provide(any()); | |
| 72 | + verify(authenticationRegistry).register("test", authentication); | |
| 73 | + | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test | |
| 77 | + void prepare_whenEmptyConfig(ApiAuthenticationConfigurationExtension extension, ServiceExtensionContext context) { | |
| 78 | + var authType = "testAuth"; | |
| 79 | + var config = ConfigFactory.fromMap(Map.of()); | |
| 80 | + var provider = mock(ApiAuthenticationProvider.class); | |
| 81 | + var authentication = mock(AuthenticationService.class); | |
| 82 | + when(context.getConfig(WEB_HTTP_PREFIX)).thenReturn(config); | |
| 83 | + when(providerRegistry.resolve(authType)).thenReturn(provider); | |
| 84 | + when(provider.provide(any())).thenReturn(Result.success(authentication)); | |
| 85 | + | |
| 86 | + extension.initialize(context); | |
| 87 | + extension.prepare(); | |
| 88 | + | |
| 89 | + verifyNoMoreInteractions(providerRegistry); | |
| 90 | + verifyNoMoreInteractions(provider); | |
| 91 | + verifyNoMoreInteractions(authenticationRegistry); | |
| 92 | + | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Test | |
| 96 | + void prepare_whenNoProvider(ApiAuthenticationConfigurationExtension extension, ServiceExtensionContext context) { | |
| 97 | + var authType = "testAuth"; | |
| 98 | + var config = ConfigFactory.fromMap(Map.of("test.auth.type", authType, "test.auth.custom", "custom")); | |
| 99 | + var provider = mock(ApiAuthenticationProvider.class); | |
| 100 | + when(context.getConfig(WEB_HTTP_PREFIX)).thenReturn(config); | |
| 101 | + when(providerRegistry.resolve(authType)).thenReturn(null); | |
| 102 | + | |
| 103 | + extension.initialize(context); | |
| 104 | + | |
| 105 | + assertThatThrownBy(extension::prepare).isInstanceOf(EdcException.class).hasMessageContaining(authType); | |
| 106 | + | |
| 107 | + verify(providerRegistry).resolve(authType); | |
| 108 | + verifyNoMoreInteractions(provider); | |
| 109 | + verifyNoMoreInteractions(authenticationRegistry); | |
| 110 | + | |
| 111 | + } | |
| 112 | + | |
| 113 | + @Test | |
| 114 | + void prepare_whenProviderFails(ApiAuthenticationConfigurationExtension extension, ServiceExtensionContext context) { | |
| 115 | + var authType = "testAuth"; | |
| 116 | + var config = ConfigFactory.fromMap(Map.of("test.auth.type", authType, "test.auth.custom", "custom")); | |
| 117 | + var provider = mock(ApiAuthenticationProvider.class); | |
| 118 | + when(context.getConfig(WEB_HTTP_PREFIX)).thenReturn(config); | |
| 119 | + when(providerRegistry.resolve(authType)).thenReturn(provider); | |
| 120 | + when(provider.provide(any())).thenReturn(Result.failure("Auth failure")); | |
| 121 | + | |
| 122 | + extension.initialize(context); | |
| 123 | + | |
| 124 | + assertThatThrownBy(extension::prepare).isInstanceOf(EdcException.class).hasMessageContaining("Auth failure"); | |
| 125 | + | |
| 126 | + verify(providerRegistry).resolve(authType); | |
| 127 | + verify(provider).provide(any()); | |
| 128 | + verifyNoMoreInteractions(authenticationRegistry); | |
| 129 | + | |
| 130 | + } | |
| 131 | + | |
| 132 | +} | |
| No newline at the end of the file |
added extensions/common/auth/auth-configuration/src/test/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension +15 −0
| @@ -0,0 +1,15 @@ | ||
| 1 | +# | |
| 2 | +# Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | +# | |
| 4 | +# This program and the accompanying materials are made available under the | |
| 5 | +# terms of the Apache License, Version 2.0 which is available at | |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | +# | |
| 8 | +# SPDX-License-Identifier: Apache-2.0 | |
| 9 | +# | |
| 10 | +# Contributors: | |
| 11 | +# Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | +# | |
| 13 | +# | |
| 14 | + | |
| 15 | +org.eclipse.edc.api.auth.configuration.ApiAuthenticationConfigurationExtension |
modified extensions/common/auth/auth-delegated/src/main/java/org/eclipse/edc/api/auth/delegated/DelegatedAuthenticationExtension.java +29 −0
| @@ -14,13 +14,19 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.api.auth.delegated; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 18 | +import org.eclipse.edc.api.auth.spi.AuthenticationService; | |
| 19 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 17 | 20 | import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry; |
| 18 | 21 | import org.eclipse.edc.keys.spi.KeyParserRegistry; |
| 19 | 22 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 20 | 23 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 21 | 24 | import org.eclipse.edc.runtime.metamodel.annotation.Setting; |
| 25 | +import org.eclipse.edc.spi.monitor.Monitor; | |
| 26 | +import org.eclipse.edc.spi.result.Result; | |
| 22 | 27 | import org.eclipse.edc.spi.system.ServiceExtension; |
| 23 | 28 | import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 29 | +import org.eclipse.edc.spi.system.configuration.Config; | |
| 24 | 30 | import org.eclipse.edc.token.rules.ExpirationIssuedAtValidationRule; |
| 25 | 31 | import org.eclipse.edc.token.rules.NotBeforeValidationRule; |
| 26 | 32 | import org.eclipse.edc.token.spi.TokenValidationRulesRegistry; |
| @@ -30,9 +36,11 @@ | ||
| 30 | 36 | |
| 31 | 37 | import static com.nimbusds.jose.jwk.source.JWKSourceBuilder.DEFAULT_CACHE_TIME_TO_LIVE; |
| 32 | 38 | import static org.eclipse.edc.api.auth.delegated.DelegatedAuthenticationService.MANAGEMENT_API_CONTEXT; |
| 39 | +import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX; | |
| 33 | 40 | |
| 34 | 41 | /** |
| 35 | 42 | * Extension that registers an AuthenticationService that delegates authentication and authorization to a third-party IdP |
| 43 | + * and register an {@link ApiAuthenticationProvider} under the type called delegated | |
| 36 | 44 | */ |
| 37 | 45 | @Extension(value = DelegatedAuthenticationExtension.NAME) |
| 38 | 46 | public class DelegatedAuthenticationExtension implements ServiceExtension { |
| @@ -40,14 +48,25 @@ | ||
| 40 | 48 | public static final int DEFAULT_VALIDATION_TOLERANCE = 5_000; |
| 41 | 49 | public static final String NAME = "Delegating Authentication Service Extension"; |
| 42 | 50 | @Setting(value = "Duration (in ms) that the internal key cache is valid", type = "Long", defaultValue = "" + DEFAULT_CACHE_TIME_TO_LIVE) |
| 51 | + @Deprecated(since = "0.7.1") | |
| 43 | 52 | public static final String AUTH_SETTING_CACHE_VALIDITY_MS = "edc.api.auth.dac.cache.validity"; |
| 53 | + @Deprecated(since = "0.7.1") | |
| 44 | 54 | @Setting(value = "URL where the third-party IdP's public key(s) can be resolved") |
| 45 | 55 | public static final String AUTH_SETTING_KEY_URL = "edc.api.auth.dac.key.url"; |
| 56 | + public static final String AUTH_KEY = "auth"; | |
| 57 | + public static final String CONFIG_ALIAS = WEB_HTTP_PREFIX + ".<context>." + AUTH_KEY + "."; | |
| 58 | + @Setting(context = CONFIG_ALIAS, value = "URL where the third-party IdP's public key(s) can be resolved for the configured <context>") | |
| 59 | + public static final String AUTH_KEY_URL = "dac.key.url"; | |
| 60 | + @Setting(context = CONFIG_ALIAS, value = "Duration (in ms) that the internal key cache is valid for the configured <context>", type = "Long", defaultValue = "" + DEFAULT_CACHE_TIME_TO_LIVE) | |
| 61 | + public static final String AUTH_CACHE_VALIDITY_MS = "dac.cache.validity"; | |
| 62 | + public static final String DELEGATED_TYPE = "delegated"; | |
| 46 | 63 | @Setting(value = "Default token validation time tolerance (in ms), e.g. for nbf or exp claims", defaultValue = "" + DEFAULT_VALIDATION_TOLERANCE) |
| 47 | 64 | private static final String AUTH_SETTING_VALIDATION_TOLERANCE_MS = "edc.api.auth.dac.validation.tolerance"; |
| 48 | 65 | @Inject |
| 49 | 66 | private ApiAuthenticationRegistry authenticationRegistry; |
| 50 | 67 | @Inject |
| 68 | + private ApiAuthenticationProviderRegistry providerRegistry; | |
| 69 | + @Inject | |
| 51 | 70 | private TokenValidationRulesRegistry tokenValidationRulesRegistry; |
| 52 | 71 | @Inject |
| 53 | 72 | private KeyParserRegistry keyParserRegistry; |
| @@ -81,5 +100,15 @@ | ||
| 81 | 100 | |
| 82 | 101 | // always register - this would potentially overwrite other services |
| 83 | 102 | authenticationRegistry.register("management-api", new DelegatedAuthenticationService(resolver, monitor, tokenValidationService, tokenValidationRulesRegistry)); |
| 103 | + | |
| 104 | + providerRegistry.register(DELEGATED_TYPE, (cfg) -> delegatedProvider(monitor, cfg)); | |
| 105 | + } | |
| 106 | + | |
| 107 | + public Result<AuthenticationService> delegatedProvider(Monitor monitor, Config config) { | |
| 108 | + var keyUrl = config.getString(AUTH_KEY_URL); | |
| 109 | + var cacheValidityMs = config.getLong(AUTH_CACHE_VALIDITY_MS, DEFAULT_CACHE_TIME_TO_LIVE); | |
| 110 | + var resolver = JwksPublicKeyResolver.create(keyParserRegistry, keyUrl, monitor, cacheValidityMs); | |
| 111 | + | |
| 112 | + return Result.success(new DelegatedAuthenticationService(resolver, monitor, tokenValidationService, tokenValidationRulesRegistry)); | |
| 84 | 113 | } |
| 85 | 114 | } |
modified extensions/common/auth/auth-delegated/src/test/java/org/eclipse/edc/api/auth/delegated/DelegatedAuthenticationExtensionTest.java +20 −0
| @@ -23,7 +23,11 @@ | ||
| 23 | 23 | import org.junit.jupiter.api.Test; |
| 24 | 24 | import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | 25 | |
| 26 | +import static com.nimbusds.jose.jwk.source.JWKSourceBuilder.DEFAULT_CACHE_TIME_TO_LIVE; | |
| 27 | +import static org.eclipse.edc.api.auth.delegated.DelegatedAuthenticationExtension.AUTH_CACHE_VALIDITY_MS; | |
| 28 | +import static org.eclipse.edc.api.auth.delegated.DelegatedAuthenticationExtension.AUTH_KEY_URL; | |
| 26 | 29 | import static org.eclipse.edc.api.auth.delegated.DelegatedAuthenticationExtension.AUTH_SETTING_KEY_URL; |
| 30 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 27 | 31 | import static org.mockito.ArgumentMatchers.anyString; |
| 28 | 32 | import static org.mockito.ArgumentMatchers.eq; |
| 29 | 33 | import static org.mockito.ArgumentMatchers.isA; |
| @@ -65,4 +69,20 @@ | ||
| 65 | 69 | verify(monitor).warning("The '%s' setting was not provided, so the DelegatedAuthenticationService will NOT be registered. In this case, the TokenBasedAuthenticationService usually acts as fallback.".formatted(AUTH_SETTING_KEY_URL)); |
| 66 | 70 | verify(registry, never()).register(eq("management-api"), isA(DelegatedAuthenticationService.class)); |
| 67 | 71 | } |
| 72 | + | |
| 73 | + @Test | |
| 74 | + public void delegatedProvider(DelegatedAuthenticationExtension extension) { | |
| 75 | + | |
| 76 | + var config = mock(Config.class); | |
| 77 | + | |
| 78 | + when(config.getString(AUTH_KEY_URL)).thenReturn("http://url"); | |
| 79 | + | |
| 80 | + assertThat(extension.delegatedProvider(monitor, config)) | |
| 81 | + .isSucceeded().isInstanceOf(DelegatedAuthenticationService.class); | |
| 82 | + | |
| 83 | + | |
| 84 | + verify(config).getString(AUTH_KEY_URL); | |
| 85 | + verify(config).getLong(AUTH_CACHE_VALIDITY_MS, DEFAULT_CACHE_TIME_TO_LIVE); | |
| 86 | + | |
| 87 | + } | |
| 68 | 88 | } |
| No newline at the end of the file |
modified extensions/common/auth/auth-tokenbased/src/main/java/org/eclipse/edc/api/auth/token/TokenBasedAuthenticationExtension.java +32 −2
| @@ -16,34 +16,54 @@ | ||
| 16 | 16 | |
| 17 | 17 | package org.eclipse.edc.api.auth.token; |
| 18 | 18 | |
| 19 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 20 | +import org.eclipse.edc.api.auth.spi.AuthenticationService; | |
| 21 | +import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry; | |
| 19 | 22 | import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry; |
| 20 | 23 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 21 | 24 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 22 | 25 | import org.eclipse.edc.runtime.metamodel.annotation.Setting; |
| 26 | +import org.eclipse.edc.spi.result.Result; | |
| 23 | 27 | import org.eclipse.edc.spi.security.Vault; |
| 24 | 28 | import org.eclipse.edc.spi.system.ServiceExtension; |
| 25 | 29 | import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 30 | +import org.eclipse.edc.spi.system.configuration.Config; | |
| 26 | 31 | |
| 27 | 32 | import java.util.Optional; |
| 28 | 33 | import java.util.UUID; |
| 29 | 34 | |
| 35 | +import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX; | |
| 36 | + | |
| 30 | 37 | /** |
| 31 | - * Extension that registers an AuthenticationService that uses API Keys | |
| 38 | + * Extension that registers an AuthenticationService that uses API Keys and register | |
| 39 | + * an {@link ApiAuthenticationProvider} under the type called tokenbased | |
| 32 | 40 | */ |
| 33 | 41 | @Extension(value = TokenBasedAuthenticationExtension.NAME) |
| 34 | 42 | public class TokenBasedAuthenticationExtension implements ServiceExtension { |
| 35 | 43 | |
| 36 | 44 | public static final String NAME = "Static token API Authentication"; |
| 45 | + public static final String AUTH_KEY = "auth"; | |
| 46 | + | |
| 47 | + public static final String CONFIG_ALIAS = WEB_HTTP_PREFIX + ".<context>." + AUTH_KEY + "."; | |
| 48 | + @Setting(context = CONFIG_ALIAS, value = "The api key to use for the <context>") | |
| 49 | + public static final String AUTH_API_KEY = "key"; | |
| 50 | + @Setting(context = CONFIG_ALIAS, value = "The vault api key alias to use for the <context>") | |
| 51 | + public static final String AUTH_API_KEY_ALIAS = "key.alias"; | |
| 52 | + public static final String TOKENBASED_TYPE = "tokenbased"; | |
| 37 | 53 | @Setting |
| 54 | + @Deprecated(since = "0.7.1") | |
| 38 | 55 | private static final String AUTH_SETTING_APIKEY = "edc.api.auth.key"; |
| 39 | 56 | @Setting |
| 57 | + @Deprecated(since = "0.7.1") | |
| 40 | 58 | private static final String AUTH_SETTING_APIKEY_ALIAS = "edc.api.auth.key.alias"; |
| 41 | - | |
| 42 | 59 | @Inject |
| 43 | 60 | private Vault vault; |
| 44 | 61 | @Inject |
| 45 | 62 | private ApiAuthenticationRegistry authenticationRegistry; |
| 46 | 63 | |
| 64 | + @Inject | |
| 65 | + private ApiAuthenticationProviderRegistry providerRegistry; | |
| 66 | + | |
| 47 | 67 | @Override |
| 48 | 68 | public String name() { |
| 49 | 69 | return NAME; |
| @@ -59,5 +79,15 @@ | ||
| 59 | 79 | if (!authenticationRegistry.hasService("management-api")) { |
| 60 | 80 | authenticationRegistry.register("management-api", new TokenBasedAuthenticationService(apiKey)); |
| 61 | 81 | } |
| 82 | + | |
| 83 | + providerRegistry.register(TOKENBASED_TYPE, this::tokenBasedProvider); | |
| 84 | + } | |
| 85 | + | |
| 86 | + public Result<AuthenticationService> tokenBasedProvider(Config config) { | |
| 87 | + var apiKey = Optional.ofNullable(config.getString(AUTH_API_KEY_ALIAS, null)) | |
| 88 | + .map(alias -> vault.resolveSecret(alias)) | |
| 89 | + .orElseGet(() -> config.getString(AUTH_API_KEY)); | |
| 90 | + | |
| 91 | + return Result.success(new TokenBasedAuthenticationService(apiKey)); | |
| 62 | 92 | } |
| 63 | 93 | } |
modified extensions/common/auth/auth-tokenbased/src/test/java/org/eclipse/edc/api/auth/token/TokenBasedAuthenticationExtensionTest.java +32 −0
| @@ -18,10 +18,14 @@ | ||
| 18 | 18 | import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; |
| 19 | 19 | import org.eclipse.edc.spi.security.Vault; |
| 20 | 20 | import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 21 | +import org.eclipse.edc.spi.system.configuration.Config; | |
| 21 | 22 | import org.junit.jupiter.api.BeforeEach; |
| 22 | 23 | import org.junit.jupiter.api.Test; |
| 23 | 24 | import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | 25 | |
| 26 | +import static org.eclipse.edc.api.auth.token.TokenBasedAuthenticationExtension.AUTH_API_KEY; | |
| 27 | +import static org.eclipse.edc.api.auth.token.TokenBasedAuthenticationExtension.AUTH_API_KEY_ALIAS; | |
| 28 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 25 | 29 | import static org.mockito.ArgumentMatchers.isA; |
| 26 | 30 | import static org.mockito.Mockito.anyString; |
| 27 | 31 | import static org.mockito.Mockito.eq; |
| @@ -29,6 +33,7 @@ | ||
| 29 | 33 | import static org.mockito.Mockito.mock; |
| 30 | 34 | import static org.mockito.Mockito.never; |
| 31 | 35 | import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.verifyNoMoreInteractions; | |
| 32 | 37 | import static org.mockito.Mockito.when; |
| 33 | 38 | |
| 34 | 39 | @ExtendWith(DependencyInjectionExtension.class) |
| @@ -83,4 +88,31 @@ | ||
| 83 | 88 | verify(apiAuthenticationRegistry).register(eq("management-api"), isA(TokenBasedAuthenticationService.class)); |
| 84 | 89 | } |
| 85 | 90 | |
| 91 | + @Test | |
| 92 | + public void tokenBasedProvider(TokenBasedAuthenticationExtension extension) { | |
| 93 | + var config = mock(Config.class); | |
| 94 | + when(config.getString(AUTH_API_KEY)).thenReturn("key"); | |
| 95 | + | |
| 96 | + assertThat(extension.tokenBasedProvider(config)) | |
| 97 | + .isSucceeded().isInstanceOf(TokenBasedAuthenticationService.class); | |
| 98 | + | |
| 99 | + verifyNoMoreInteractions(vault); | |
| 100 | + | |
| 101 | + } | |
| 102 | + | |
| 103 | + @Test | |
| 104 | + public void tokenBasedProvider_withAlias(TokenBasedAuthenticationExtension extension) { | |
| 105 | + | |
| 106 | + var config = mock(Config.class); | |
| 107 | + | |
| 108 | + when(config.getString(AUTH_API_KEY_ALIAS, null)).thenReturn("alias"); | |
| 109 | + when(vault.resolveSecret("alias")).thenReturn("key"); | |
| 110 | + | |
| 111 | + assertThat(extension.tokenBasedProvider(config)) | |
| 112 | + .isSucceeded().isInstanceOf(TokenBasedAuthenticationService.class); | |
| 113 | + | |
| 114 | + verify(vault).resolveSecret("alias"); | |
| 115 | + | |
| 116 | + | |
| 117 | + } | |
| 86 | 118 | } |
modified extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyConfiguration.java +2 −1
| @@ -23,9 +23,10 @@ | ||
| 23 | 23 | import java.util.Set; |
| 24 | 24 | import java.util.stream.Collectors; |
| 25 | 25 | |
| 26 | +import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX; | |
| 27 | + | |
| 26 | 28 | public class JettyConfiguration { |
| 27 | 29 | |
| 28 | - public static final String WEB_HTTP_PREFIX = "web.http"; | |
| 29 | 30 | public static final String DEFAULT_PATH = "/api"; |
| 30 | 31 | public static final String DEFAULT_CONTEXT_NAME = "default"; |
| 31 | 32 | public static final int DEFAULT_PORT = 8181; |
modified settings.gradle.kts +1 −0
| @@ -105,6 +105,7 @@ | ||
| 105 | 105 | include(":extensions:common:auth:auth-basic") |
| 106 | 106 | include(":extensions:common:auth:auth-tokenbased") |
| 107 | 107 | include(":extensions:common:auth:auth-delegated") |
| 108 | +include(":extensions:common:auth:auth-configuration") | |
| 108 | 109 | include(":extensions:common:crypto:ldp-verifiable-credentials") |
| 109 | 110 | include(":extensions:common:crypto:jwt-verifiable-credentials") |
| 110 | 111 | include(":extensions:common:crypto:lib:jws2020-lib") |
added spi/common/auth-spi/src/main/java/org/eclipse/edc/api/auth/spi/ApiAuthenticationProvider.java +32 −0
| @@ -0,0 +1,32 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth.spi; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint; | |
| 18 | +import org.eclipse.edc.spi.result.Result; | |
| 19 | +import org.eclipse.edc.spi.system.configuration.Config; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Provider for {@link AuthenticationService} with a configuration | |
| 23 | + */ | |
| 24 | +@FunctionalInterface | |
| 25 | +@ExtensionPoint | |
| 26 | +public interface ApiAuthenticationProvider { | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Returns an {@link AuthenticationService} given the input configuration | |
| 30 | + */ | |
| 31 | + Result<AuthenticationService> provide(Config config); | |
| 32 | +} |
added spi/common/auth-spi/src/main/java/org/eclipse/edc/api/auth/spi/registry/ApiAuthenticationProviderRegistry.java +40 −0
| @@ -0,0 +1,40 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.api.auth.spi.registry; | |
| 16 | + | |
| 17 | +import org.eclipse.edc.api.auth.spi.ApiAuthenticationProvider; | |
| 18 | +import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Registry for {@link ApiAuthenticationProvider} | |
| 22 | + */ | |
| 23 | +@ExtensionPoint | |
| 24 | +public interface ApiAuthenticationProviderRegistry { | |
| 25 | + /** | |
| 26 | + * Register an {@link ApiAuthenticationProvider} for the specified type | |
| 27 | + * | |
| 28 | + * @param type the type | |
| 29 | + * @param provider the auth provider | |
| 30 | + */ | |
| 31 | + void register(String type, ApiAuthenticationProvider provider); | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Resolve an {@link ApiAuthenticationProvider} for the specified type | |
| 35 | + * | |
| 36 | + * @param type the type | |
| 37 | + * @return The {@link ApiAuthenticationProvider} if found, otherwise null | |
| 38 | + */ | |
| 39 | + ApiAuthenticationProvider resolve(String type); | |
| 40 | +} |
modified spi/common/web-spi/src/main/java/org/eclipse/edc/web/spi/configuration/WebServiceConfigurer.java +4 −2
| @@ -25,12 +25,14 @@ | ||
| 25 | 25 | @ExtensionPoint |
| 26 | 26 | public interface WebServiceConfigurer { |
| 27 | 27 | |
| 28 | + String WEB_HTTP_PREFIX = "web.http"; | |
| 29 | + | |
| 28 | 30 | /** |
| 29 | 31 | * Build the configuration for an API |
| 30 | 32 | * |
| 31 | - * @param config The context configuration | |
| 33 | + * @param config The context configuration | |
| 32 | 34 | * @param webServer The WebServer |
| 33 | - * @param settings WebService settings | |
| 35 | + * @param settings WebService settings | |
| 34 | 36 | * @return The final webservice configuration |
| 35 | 37 | */ |
| 36 | 38 | WebServiceConfiguration configure(Config config, WebServer webServer, WebServiceSettings settings); |
added system-tests/management-api/management-api-test-runner/src/test/java/org/eclipse/edc/test/e2e/managementapi/AuthConfigurationApiEndToEndTest.java +107 −0
| @@ -0,0 +1,107 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.test.e2e.managementapi; | |
| 16 | + | |
| 17 | +import io.restassured.http.ContentType; | |
| 18 | +import org.eclipse.edc.junit.annotations.EndToEndTest; | |
| 19 | +import org.eclipse.edc.junit.extensions.EmbeddedRuntime; | |
| 20 | +import org.junit.jupiter.api.Nested; | |
| 21 | +import org.junit.jupiter.api.Test; | |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; | |
| 23 | + | |
| 24 | +import java.util.HashMap; | |
| 25 | + | |
| 26 | +import static jakarta.json.Json.createArrayBuilder; | |
| 27 | +import static jakarta.json.Json.createObjectBuilder; | |
| 28 | +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT; | |
| 29 | +import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE; | |
| 30 | +import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX; | |
| 31 | +import static org.eclipse.edc.util.io.Ports.getFreePort; | |
| 32 | +import static org.hamcrest.Matchers.is; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * Auth configuration end-to-end tests | |
| 36 | + */ | |
| 37 | +public class AuthConfigurationApiEndToEndTest { | |
| 38 | + | |
| 39 | + private static final String API_KEY = "apiKey"; | |
| 40 | + | |
| 41 | + abstract static class Tests { | |
| 42 | + | |
| 43 | + | |
| 44 | + @Test | |
| 45 | + void queryAsset(ManagementEndToEndTestContext context) { | |
| 46 | + | |
| 47 | + var query = createObjectBuilder() | |
| 48 | + .add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE)) | |
| 49 | + .add("filterExpression", createArrayBuilder()).build(); | |
| 50 | + | |
| 51 | + context.baseRequest() | |
| 52 | + .header("x-api-key", API_KEY) | |
| 53 | + .contentType(ContentType.JSON) | |
| 54 | + .body(query) | |
| 55 | + .post("/v3/assets/request") | |
| 56 | + .then() | |
| 57 | + .log().ifError() | |
| 58 | + .statusCode(200) | |
| 59 | + .body("size()", is(0)); | |
| 60 | + } | |
| 61 | + | |
| 62 | + | |
| 63 | + } | |
| 64 | + | |
| 65 | + static class AuthExtension extends ManagementEndToEndExtension { | |
| 66 | + | |
| 67 | + protected AuthExtension() { | |
| 68 | + super(context()); | |
| 69 | + } | |
| 70 | + | |
| 71 | + private static ManagementEndToEndTestContext context() { | |
| 72 | + var managementPort = getFreePort(); | |
| 73 | + var protocolPort = getFreePort(); | |
| 74 | + | |
| 75 | + var runtime = new EmbeddedRuntime( | |
| 76 | + "control-plane", | |
| 77 | + new HashMap<>() { | |
| 78 | + { | |
| 79 | + put("web.http.path", "/"); | |
| 80 | + put("web.http.port", String.valueOf(getFreePort())); | |
| 81 | + put("web.http.protocol.path", "/protocol"); | |
| 82 | + put("web.http.protocol.port", String.valueOf(protocolPort)); | |
| 83 | + put("web.http.control.port", String.valueOf(getFreePort())); | |
| 84 | + put("edc.dsp.callback.address", "http://localhost:" + protocolPort + "/protocol"); | |
| 85 | + put("web.http.management.path", "/management"); | |
| 86 | + put("web.http.management.port", String.valueOf(managementPort)); | |
| 87 | + put("web.http.management.auth.type", "tokenbased"); | |
| 88 | + put("web.http.management.auth.key", API_KEY); | |
| 89 | + put("web.http.management.auth.context", "management-api"); | |
| 90 | + } | |
| 91 | + }, | |
| 92 | + ":system-tests:management-api:management-api-test-runtime", | |
| 93 | + ":extensions:common:auth:auth-configuration", | |
| 94 | + ":extensions:common:auth:auth-tokenbased" | |
| 95 | + ); | |
| 96 | + | |
| 97 | + return new ManagementEndToEndTestContext(runtime, managementPort, protocolPort); | |
| 98 | + } | |
| 99 | + | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Nested | |
| 103 | + @EndToEndTest | |
| 104 | + @ExtendWith(AuthExtension.class) | |
| 105 | + class InMemory extends Tests { | |
| 106 | + } | |
| 107 | +} |