Elffuss BraidHub

kikocis/EDC public

feat: introduce auth configuration (#4321)

* feat: introduce auth configuration

* PR remarks
Changesete7ba3fa407b87aa851796b8f154ead251b595ae4766e58c245bc15b031bbfed1
AuthorEnrico Risa <enrico.risa@gmail.com> · 2024-07-09 13:17 +02:00
CommitterGitHub <noreply@github.com> · 2024-07-09 13:17 +02:00
Parents421ed927c56e

Browse the files at this changeset

19 files changed

modified extensions/common/api/api-core/src/main/java/org/eclipse/edc/api/ApiCoreDefaultServicesExtension.java +7 −1
@@ -14,7 +14,9 @@
1414
1515 package org.eclipse.edc.api;
1616
17+import org.eclipse.edc.api.auth.ApiAuthenticationProviderRegistryImpl;
1718 import org.eclipse.edc.api.auth.ApiAuthenticationRegistryImpl;
19+import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationProviderRegistry;
1820 import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
1921 import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2022 import org.eclipse.edc.runtime.metamodel.annotation.Provider;
@@ -37,5 +39,9 @@
3739 public ApiAuthenticationRegistry apiAuthenticationRegistry() {
3840 return new ApiAuthenticationRegistryImpl();
3941 }
40-
42+
43+ @Provider
44+ public ApiAuthenticationProviderRegistry apiAuthenticationProviderRegistry() {
45+ return new ApiAuthenticationProviderRegistryImpl();
46+ }
4147 }
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 @@
1414
1515 package org.eclipse.edc.api.auth.delegated;
1616
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;
1720 import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
1821 import org.eclipse.edc.keys.spi.KeyParserRegistry;
1922 import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2023 import org.eclipse.edc.runtime.metamodel.annotation.Inject;
2124 import org.eclipse.edc.runtime.metamodel.annotation.Setting;
25+import org.eclipse.edc.spi.monitor.Monitor;
26+import org.eclipse.edc.spi.result.Result;
2227 import org.eclipse.edc.spi.system.ServiceExtension;
2328 import org.eclipse.edc.spi.system.ServiceExtensionContext;
29+import org.eclipse.edc.spi.system.configuration.Config;
2430 import org.eclipse.edc.token.rules.ExpirationIssuedAtValidationRule;
2531 import org.eclipse.edc.token.rules.NotBeforeValidationRule;
2632 import org.eclipse.edc.token.spi.TokenValidationRulesRegistry;
@@ -30,9 +36,11 @@
3036
3137 import static com.nimbusds.jose.jwk.source.JWKSourceBuilder.DEFAULT_CACHE_TIME_TO_LIVE;
3238 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;
3340
3441 /**
3542 * 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
3644 */
3745 @Extension(value = DelegatedAuthenticationExtension.NAME)
3846 public class DelegatedAuthenticationExtension implements ServiceExtension {
@@ -40,14 +48,25 @@
4048 public static final int DEFAULT_VALIDATION_TOLERANCE = 5_000;
4149 public static final String NAME = "Delegating Authentication Service Extension";
4250 @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")
4352 public static final String AUTH_SETTING_CACHE_VALIDITY_MS = "edc.api.auth.dac.cache.validity";
53+ @Deprecated(since = "0.7.1")
4454 @Setting(value = "URL where the third-party IdP's public key(s) can be resolved")
4555 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";
4663 @Setting(value = "Default token validation time tolerance (in ms), e.g. for nbf or exp claims", defaultValue = "" + DEFAULT_VALIDATION_TOLERANCE)
4764 private static final String AUTH_SETTING_VALIDATION_TOLERANCE_MS = "edc.api.auth.dac.validation.tolerance";
4865 @Inject
4966 private ApiAuthenticationRegistry authenticationRegistry;
5067 @Inject
68+ private ApiAuthenticationProviderRegistry providerRegistry;
69+ @Inject
5170 private TokenValidationRulesRegistry tokenValidationRulesRegistry;
5271 @Inject
5372 private KeyParserRegistry keyParserRegistry;
@@ -81,5 +100,15 @@
81100
82101 // always register - this would potentially overwrite other services
83102 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));
84113 }
85114 }
modified extensions/common/auth/auth-delegated/src/test/java/org/eclipse/edc/api/auth/delegated/DelegatedAuthenticationExtensionTest.java +20 −0
@@ -23,7 +23,11 @@
2323 import org.junit.jupiter.api.Test;
2424 import org.junit.jupiter.api.extension.ExtendWith;
2525
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;
2629 import static org.eclipse.edc.api.auth.delegated.DelegatedAuthenticationExtension.AUTH_SETTING_KEY_URL;
30+import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
2731 import static org.mockito.ArgumentMatchers.anyString;
2832 import static org.mockito.ArgumentMatchers.eq;
2933 import static org.mockito.ArgumentMatchers.isA;
@@ -65,4 +69,20 @@
6569 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));
6670 verify(registry, never()).register(eq("management-api"), isA(DelegatedAuthenticationService.class));
6771 }
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+ }
6888 }
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 @@
1616
1717 package org.eclipse.edc.api.auth.token;
1818
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;
1922 import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
2023 import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2124 import org.eclipse.edc.runtime.metamodel.annotation.Inject;
2225 import org.eclipse.edc.runtime.metamodel.annotation.Setting;
26+import org.eclipse.edc.spi.result.Result;
2327 import org.eclipse.edc.spi.security.Vault;
2428 import org.eclipse.edc.spi.system.ServiceExtension;
2529 import org.eclipse.edc.spi.system.ServiceExtensionContext;
30+import org.eclipse.edc.spi.system.configuration.Config;
2631
2732 import java.util.Optional;
2833 import java.util.UUID;
2934
35+import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX;
36+
3037 /**
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
3240 */
3341 @Extension(value = TokenBasedAuthenticationExtension.NAME)
3442 public class TokenBasedAuthenticationExtension implements ServiceExtension {
3543
3644 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";
3753 @Setting
54+ @Deprecated(since = "0.7.1")
3855 private static final String AUTH_SETTING_APIKEY = "edc.api.auth.key";
3956 @Setting
57+ @Deprecated(since = "0.7.1")
4058 private static final String AUTH_SETTING_APIKEY_ALIAS = "edc.api.auth.key.alias";
41-
4259 @Inject
4360 private Vault vault;
4461 @Inject
4562 private ApiAuthenticationRegistry authenticationRegistry;
4663
64+ @Inject
65+ private ApiAuthenticationProviderRegistry providerRegistry;
66+
4767 @Override
4868 public String name() {
4969 return NAME;
@@ -59,5 +79,15 @@
5979 if (!authenticationRegistry.hasService("management-api")) {
6080 authenticationRegistry.register("management-api", new TokenBasedAuthenticationService(apiKey));
6181 }
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));
6292 }
6393 }
modified extensions/common/auth/auth-tokenbased/src/test/java/org/eclipse/edc/api/auth/token/TokenBasedAuthenticationExtensionTest.java +32 −0
@@ -18,10 +18,14 @@
1818 import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
1919 import org.eclipse.edc.spi.security.Vault;
2020 import org.eclipse.edc.spi.system.ServiceExtensionContext;
21+import org.eclipse.edc.spi.system.configuration.Config;
2122 import org.junit.jupiter.api.BeforeEach;
2223 import org.junit.jupiter.api.Test;
2324 import org.junit.jupiter.api.extension.ExtendWith;
2425
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;
2529 import static org.mockito.ArgumentMatchers.isA;
2630 import static org.mockito.Mockito.anyString;
2731 import static org.mockito.Mockito.eq;
@@ -29,6 +33,7 @@
2933 import static org.mockito.Mockito.mock;
3034 import static org.mockito.Mockito.never;
3135 import static org.mockito.Mockito.verify;
36+import static org.mockito.Mockito.verifyNoMoreInteractions;
3237 import static org.mockito.Mockito.when;
3338
3439 @ExtendWith(DependencyInjectionExtension.class)
@@ -83,4 +88,31 @@
8388 verify(apiAuthenticationRegistry).register(eq("management-api"), isA(TokenBasedAuthenticationService.class));
8489 }
8590
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+ }
86118 }
modified extensions/common/http/jetty-core/src/main/java/org/eclipse/edc/web/jetty/JettyConfiguration.java +2 −1
@@ -23,9 +23,10 @@
2323 import java.util.Set;
2424 import java.util.stream.Collectors;
2525
26+import static org.eclipse.edc.web.spi.configuration.WebServiceConfigurer.WEB_HTTP_PREFIX;
27+
2628 public class JettyConfiguration {
2729
28- public static final String WEB_HTTP_PREFIX = "web.http";
2930 public static final String DEFAULT_PATH = "/api";
3031 public static final String DEFAULT_CONTEXT_NAME = "default";
3132 public static final int DEFAULT_PORT = 8181;
modified settings.gradle.kts +1 −0
@@ -105,6 +105,7 @@
105105 include(":extensions:common:auth:auth-basic")
106106 include(":extensions:common:auth:auth-tokenbased")
107107 include(":extensions:common:auth:auth-delegated")
108+include(":extensions:common:auth:auth-configuration")
108109 include(":extensions:common:crypto:ldp-verifiable-credentials")
109110 include(":extensions:common:crypto:jwt-verifiable-credentials")
110111 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 @@
2525 @ExtensionPoint
2626 public interface WebServiceConfigurer {
2727
28+ String WEB_HTTP_PREFIX = "web.http";
29+
2830 /**
2931 * Build the configuration for an API
3032 *
31- * @param config The context configuration
33+ * @param config The context configuration
3234 * @param webServer The WebServer
33- * @param settings WebService settings
35+ * @param settings WebService settings
3436 * @return The final webservice configuration
3537 */
3638 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+}