Skip to content

Commit

Permalink
chore(deps): bump EDC to 0.7.1 (#68)
Browse files Browse the repository at this point in the history
* chore(deps): bump EDC to 0.7.1

* DEPENDENCIES
  • Loading branch information
paullatzelsperger authored Jul 5, 2024
1 parent d16a14f commit 7d571a6
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 198 deletions.
167 changes: 86 additions & 81 deletions DEPENDENCIES

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dev.failsafe.RetryPolicy;
import okhttp3.OkHttpClient;
import org.eclipse.edc.api.auth.spi.AuthenticationRequestFilter;
import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
import org.eclipse.edc.http.client.EdcHttpClientImpl;
import org.eclipse.edc.http.spi.EdcHttpClient;
import org.eclipse.edc.iam.did.spi.resolution.DidPublicKeyResolver;
Expand Down Expand Up @@ -76,6 +77,9 @@ public class CredentialBasedAuthenticationExtension implements ServiceExtension
@Inject
private Clock clock;

@Inject
private ApiAuthenticationRegistry registry;

private TrustedIssuerRegistry trustedIssuerRegistry;
private TypeTransformerRegistryImpl typeTransformerRegistry;

Expand All @@ -95,7 +99,9 @@ public void initialize(ServiceExtensionContext context) {
var statuslistService = new StatusList2021RevocationService(typeManager.getMapper(), validity);
var validationService = new VerifiableCredentialValidationServiceImpl(presentationVerifier, createTrustedIssuerRegistry(), statuslistService, clock);

webService.registerResource(DIRECTORY_CONTEXT, new AuthenticationRequestFilter(new CredentialBasedAuthenticationService(context.getMonitor(), typeManager.getMapper(), validationService, typeTransformerRegistry(context))));
var authService = new CredentialBasedAuthenticationService(context.getMonitor(), typeManager.getMapper(), validationService, typeTransformerRegistry(context));
registry.register(DIRECTORY_CONTEXT, authService);
webService.registerResource(DIRECTORY_CONTEXT, new AuthenticationRequestFilter(registry, DIRECTORY_CONTEXT));
}

// must provide this, so the TrustedIssuerRegistryConfigurationExtension can inject it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.eclipse.tractusx.bdrs.api.management;

import org.eclipse.edc.api.auth.spi.AuthenticationRequestFilter;
import org.eclipse.edc.api.auth.spi.AuthenticationService;
import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
import org.eclipse.edc.runtime.metamodel.annotation.BaseExtension;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class ManagementApiExtension implements ServiceExtension {
private WebService webService;

@Inject
private AuthenticationService authService;
private ApiAuthenticationRegistry registry;

@Override
public String name() {
Expand All @@ -61,7 +61,8 @@ public String name() {
@Override
public void initialize(ServiceExtensionContext context) {
webService.registerResource(CONTEXT_NAME, new ManagementApiController(store));
webService.registerResource(CONTEXT_NAME, new AuthenticationRequestFilter(authService));

webService.registerResource(CONTEXT_NAME, new AuthenticationRequestFilter(registry, CONTEXT_NAME));
}

}
Expand Down
1 change: 1 addition & 0 deletions core/core-services/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {
}

dependencies {
implementation(libs.edc.spi.auth)
implementation(libs.edc.spi.core)
implementation(libs.edc.lib.json)
implementation(project(":spi:core-spi"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.tractusx.bdrs.core;

import org.eclipse.edc.api.auth.spi.AuthenticationService;
import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class ApiAuthenticationRegistryImpl implements ApiAuthenticationRegistry {

private static final AuthenticationService ALL_PASS = headers -> true;
private final Map<String, AuthenticationService> services = new HashMap<>();

public ApiAuthenticationRegistryImpl() {
}

@Override
public void register(String context, AuthenticationService service) {
services.put(context, service);
}

@Override
public @NotNull AuthenticationService resolve(String context) {
return services.getOrDefault(context, ALL_PASS);
}

@Override
public boolean hasService(String context) {
return services.containsKey(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@

package org.eclipse.tractusx.bdrs.core;

import org.eclipse.edc.api.auth.spi.registry.ApiAuthenticationRegistry;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.runtime.metamodel.annotation.BaseExtension;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.security.Vault;
import org.eclipse.edc.spi.system.ExecutorInstrumentation;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.health.HealthCheckService;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.tractusx.bdrs.core.health.HealthCheckServiceConfiguration;
import org.eclipse.tractusx.bdrs.core.health.HealthCheckServiceImpl;
import org.eclipse.tractusx.bdrs.core.store.InMemoryDidEntryStore;
import org.eclipse.tractusx.bdrs.core.vault.InMemoryVault;
import org.eclipse.tractusx.bdrs.spi.store.DidEntryStore;

import java.time.Duration;

import static org.eclipse.tractusx.bdrs.core.BdrsCoreExtension.NAME;

/**
Expand All @@ -48,21 +44,6 @@
public class BdrsCoreExtension implements ServiceExtension {
public static final String NAME = "BDRS Core";

@Setting
public static final String LIVENESS_PERIOD_SECONDS_SETTING = "edc.core.system.health.check.liveness-period";

@Setting
public static final String STARTUP_PERIOD_SECONDS_SETTING = "edc.core.system.health.check.startup-period";

@Setting
public static final String READINESS_PERIOD_SECONDS_SETTING = "edc.core.system.health.check.readiness-period";

@Setting
public static final String THREADPOOL_SIZE_SETTING = "edc.core.system.health.check.threadpool-size";

private static final long DEFAULT_DURATION = 60;
private static final int DEFAULT_TP_SIZE = 3;

private HealthCheckServiceImpl healthCheckService;
private TypeManager typeManager;

Expand All @@ -73,21 +54,7 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var config = getHealthCheckConfig(context);
var instrumentation = new ExecutorInstrumentation() {
};
healthCheckService = new HealthCheckServiceImpl(config, instrumentation);
}

@Override
public void start() {
healthCheckService.start();
}

@Override
public void shutdown() {
healthCheckService.stop();
ServiceExtension.super.shutdown();
healthCheckService = new HealthCheckServiceImpl();
}

@Provider
Expand All @@ -113,13 +80,8 @@ public Vault defaultVault() {
return new InMemoryVault();
}

private HealthCheckServiceConfiguration getHealthCheckConfig(ServiceExtensionContext context) {
return HealthCheckServiceConfiguration.Builder.newInstance()
.livenessPeriod(Duration.ofSeconds(context.getSetting(LIVENESS_PERIOD_SECONDS_SETTING, DEFAULT_DURATION)))
.startupStatusPeriod(Duration.ofSeconds(context.getSetting(STARTUP_PERIOD_SECONDS_SETTING, DEFAULT_DURATION)))
.readinessPeriod(Duration.ofSeconds(context.getSetting(READINESS_PERIOD_SECONDS_SETTING, DEFAULT_DURATION)))
.readinessPeriod(Duration.ofSeconds(context.getSetting(READINESS_PERIOD_SECONDS_SETTING, DEFAULT_DURATION)))
.threadPoolSize(context.getSetting(THREADPOOL_SIZE_SETTING, DEFAULT_TP_SIZE))
.build();
@Provider
public ApiAuthenticationRegistry apiAuthenticationRegistry() {
return new ApiAuthenticationRegistryImpl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@

package org.eclipse.tractusx.bdrs.core.health;

import org.eclipse.edc.spi.system.ExecutorInstrumentation;
import org.eclipse.edc.spi.system.health.HealthCheckResult;
import org.eclipse.edc.spi.system.health.HealthCheckService;
import org.eclipse.edc.spi.system.health.HealthStatus;
import org.eclipse.edc.spi.system.health.LivenessProvider;
import org.eclipse.edc.spi.system.health.ReadinessProvider;
import org.eclipse.edc.spi.system.health.StartupStatusProvider;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand All @@ -44,27 +40,11 @@ public class HealthCheckServiceImpl implements HealthCheckService {
private final List<ReadinessProvider> readinessProviders;
private final List<StartupStatusProvider> startupStatusProviders;

private final Map<LivenessProvider, HealthCheckResult> cachedLivenessResults;
private final Map<ReadinessProvider, HealthCheckResult> cachedReadinessResults;
private final Map<StartupStatusProvider, HealthCheckResult> cachedStartupStatus;

private final ScheduledExecutorService executor;
private final HealthCheckServiceConfiguration configuration;

public HealthCheckServiceImpl(HealthCheckServiceConfiguration configuration,
ExecutorInstrumentation executorInstrumentation) {
this.configuration = configuration;
public HealthCheckServiceImpl() {
readinessProviders = new CopyOnWriteArrayList<>();
livenessProviders = new CopyOnWriteArrayList<>();
startupStatusProviders = new CopyOnWriteArrayList<>();

cachedLivenessResults = new ConcurrentHashMap<>();
cachedReadinessResults = new ConcurrentHashMap<>();
cachedStartupStatus = new ConcurrentHashMap<>();

executor = executorInstrumentation.instrument(
Executors.newScheduledThreadPool(configuration.getThreadPoolSize()),
HealthCheckService.class.getSimpleName());
}

@Override
Expand All @@ -84,57 +64,27 @@ public void addStartupStatusProvider(StartupStatusProvider provider) {

@Override
public HealthStatus isLive() {
return new HealthStatus(cachedLivenessResults.values());
return new HealthStatus(livenessProviders.stream().map(getSilent()).toList());
}

@Override
public HealthStatus isReady() {
return new HealthStatus(cachedReadinessResults.values());
return new HealthStatus(readinessProviders.stream().map(getSilent()).toList());
}

@Override
public HealthStatus getStartupStatus() {
return new HealthStatus(cachedStartupStatus.values());
}

@Override
public void refresh() {
executor.execute(this::queryReadiness);
executor.execute(this::queryLiveness);
executor.execute(this::queryStartupStatus);
}

public void stop() {
if (!executor.isShutdown()) {
executor.shutdownNow();
}
}

public void start() {
//todo: maybe providers should provide their desired timeout instead of a global config?
executor.scheduleAtFixedRate(this::queryReadiness, 0, configuration.getReadinessPeriod().toMillis(), TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(this::queryLiveness, 0, configuration.getLivenessPeriod().toMillis(), TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(this::queryStartupStatus, 0, configuration.getStartupStatusPeriod().toMillis(), TimeUnit.MILLISECONDS);
}

private void queryReadiness() {
readinessProviders.parallelStream().forEach(provider -> updateCache(provider, cachedReadinessResults));
}

private void queryLiveness() {
livenessProviders.parallelStream().forEach(provider -> updateCache(provider, cachedLivenessResults));
return new HealthStatus(startupStatusProviders.stream().map(getSilent()).toList());
}

private void queryStartupStatus() {
startupStatusProviders.parallelStream().forEach(provider -> updateCache(provider, cachedStartupStatus));
private @NotNull Function<Supplier<HealthCheckResult>, HealthCheckResult> getSilent() {
return supplier -> {
try {
return supplier.get();
} catch (Exception e) {
return HealthCheckResult.Builder.newInstance().component(supplier.getClass().getName()).failure(e.getMessage()).build();
}
};
}

private <T extends Supplier<HealthCheckResult>> void updateCache(T provider, Map<T, HealthCheckResult> cache) {
try {
cache.put(provider, provider.get());
} catch (Exception ex) {
cache.put(provider, HealthCheckResult.failed(ex.getMessage()));
}
}

}

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ format.version = "1.1"

[versions]
assertj = "3.25.3"
edc = "0.6.4"
edc = "0.7.1"
nimbus = "9.40"
restAssured = "5.4.0"
jupiter = "5.10.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.eclipse.edc.iam.did.spi.resolution.DidResolver;
import org.eclipse.edc.iam.did.spi.resolution.DidResolverRegistry;
import org.eclipse.edc.junit.annotations.EndToEndTest;
import org.eclipse.edc.junit.extensions.EdcRuntimeExtension;
import org.eclipse.edc.junit.extensions.EmbeddedRuntime;
import org.eclipse.edc.junit.extensions.RuntimeExtension;
import org.eclipse.edc.junit.extensions.RuntimePerMethodExtension;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.verifiablecredentials.jwt.JwtCreationUtils;
import org.eclipse.edc.web.spi.ApiErrorDetail;
Expand Down Expand Up @@ -75,16 +77,16 @@ public class DirectoryEndToEndTest {
private static final String BPN1 = "BPN12345";
private static final String DID1 = "did:web:localhost/foo";
@RegisterExtension
protected static EdcRuntimeExtension runtime = new EdcRuntimeExtension(
":system-tests:test-server",
protected static RuntimeExtension runtime = new RuntimePerMethodExtension(new EmbeddedRuntime(
"BDRS Server",
Map.of("web.http.port", String.valueOf(API_ENDPOINT.getPort()),
"web.http.management.port", String.valueOf(MANAGEMENT_ENDPOINT.getPort()),
"web.http.management.path", String.valueOf(MANAGEMENT_ENDPOINT.getPath()),
"web.http.directory.port", String.valueOf(DIRECTORY_ENDPOINT.getPort()),
"web.http.directory.path", String.valueOf(DIRECTORY_ENDPOINT.getPath()),
"edc.iam.trusted-issuer.test.id", "did:web:some-issuer",
"edc.api.auth.key", AUTH_KEY)
"edc.api.auth.key", AUTH_KEY),
":system-tests:test-server")
);
private final String issuerId = "did:web:some-issuer";
private final String holderId = "did:web:bdrs-client";
Expand Down

0 comments on commit 7d571a6

Please sign in to comment.