-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Identity): add identity extractor from referringConnector
- Loading branch information
Showing
20 changed files
with
331 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...sions/cx-oauth2/src/main/java/org/eclipse/tractusx/edc/oauth2/CxParticipantExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* 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 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.oauth2; | ||
|
||
import org.eclipse.edc.runtime.metamodel.annotation.Inject; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Setting; | ||
import org.eclipse.edc.spi.agent.ParticipantAgentService; | ||
import org.eclipse.edc.spi.agent.ParticipantAgentServiceExtension; | ||
import org.eclipse.edc.spi.iam.ClaimToken; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.system.ServiceExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.eclipse.edc.spi.agent.ParticipantAgent.PARTICIPANT_IDENTITY; | ||
|
||
public class CxParticipantExtension implements ServiceExtension, ParticipantAgentServiceExtension { | ||
|
||
public static final String REFERRING_CONNECTOR_CLAIM = "referringConnector"; | ||
|
||
private static final String DEFAULT_PARTICIPANT_ID_REGEX = "[^/]+(?=/$|$)"; | ||
private static final int DEFAULT_PARTICIPANT_ID_REGEX_GROUP = 0; | ||
|
||
@Setting(value = "Data plane proxy API consumer port", defaultValue = CxParticipantExtension.PARTICIPANT_ID_REGEX) | ||
private static final String PARTICIPANT_ID_REGEX = "tx.participant.id.regex"; | ||
|
||
@Setting(value = "Data plane proxy API consumer port", defaultValue = CxParticipantExtension.PARTICIPANT_ID_REGEX) | ||
private static final String PARTICIPANT_ID_REGEX_GROUP = "tx.participant.id.regex"; | ||
@Inject | ||
ParticipantAgentService agentService; | ||
private Pattern participantRegex; | ||
|
||
private int participantRegexGroup; | ||
|
||
@Inject | ||
private Monitor monitor; | ||
|
||
@Override | ||
public void initialize(ServiceExtensionContext context) { | ||
this.participantRegex = Pattern.compile(context.getConfig().getString(PARTICIPANT_ID_REGEX, DEFAULT_PARTICIPANT_ID_REGEX)); | ||
this.participantRegexGroup = context.getConfig().getInteger(PARTICIPANT_ID_REGEX_GROUP, DEFAULT_PARTICIPANT_ID_REGEX_GROUP); | ||
|
||
agentService.register(this); | ||
} | ||
|
||
@Override | ||
public @NotNull Map<String, String> attributesFor(ClaimToken token) { | ||
var referringConnector = token.getClaim(REFERRING_CONNECTOR_CLAIM); | ||
if (referringConnector instanceof String referringConnectorUrl) { | ||
var matcher = participantRegex.matcher(referringConnectorUrl); | ||
if (matcher.find()) { | ||
var id = matcher.group(participantRegexGroup); | ||
return Map.of(PARTICIPANT_IDENTITY, id); | ||
} | ||
monitor.warning("Unable to extract the participant id from the referring connector claim"); | ||
} | ||
return Map.of(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
# | ||
|
||
org.eclipse.tractusx.edc.oauth2.CxOauth2Extension | ||
org.eclipse.tractusx.edc.oauth2.CxParticipantExtension |
101 changes: 101 additions & 0 deletions
101
...s/cx-oauth2/src/test/java/org/eclipse/tractusx/edc/oauth2/CxParticipantExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* 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 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.oauth2; | ||
|
||
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; | ||
import org.eclipse.edc.spi.agent.ParticipantAgentService; | ||
import org.eclipse.edc.spi.iam.ClaimToken; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.edc.spi.system.injection.ObjectFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.spi.agent.ParticipantAgent.PARTICIPANT_IDENTITY; | ||
import static org.eclipse.tractusx.edc.oauth2.CxParticipantExtension.REFERRING_CONNECTOR_CLAIM; | ||
import static org.mockito.ArgumentMatchers.isA; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(DependencyInjectionExtension.class) | ||
public class CxParticipantExtensionTest { | ||
|
||
CxParticipantExtension extension; | ||
|
||
ParticipantAgentService agentService = mock(ParticipantAgentService.class); | ||
|
||
ServiceExtensionContext context; | ||
|
||
@BeforeEach | ||
void setUp(ObjectFactory factory, ServiceExtensionContext context) { | ||
this.context = spy(context); | ||
context.registerService(ParticipantAgentService.class, agentService); | ||
extension = factory.constructInstance(CxParticipantExtension.class); | ||
} | ||
|
||
@Test | ||
void initialize() { | ||
extension.initialize(context); | ||
var attributes = Map.of(PARTICIPANT_IDENTITY, "BPNSOKRATES"); | ||
verify(agentService).register(isA(CxParticipantExtension.class)); | ||
var claims = ClaimToken.Builder.newInstance().claim(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/BPNSOKRATES").build(); | ||
|
||
|
||
assertThat(extension.attributesFor(claims)).containsExactlyEntriesOf(attributes); | ||
|
||
claims = ClaimToken.Builder.newInstance().claim(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/BPNSOKRATES/").build(); | ||
assertThat(extension.attributesFor(claims)).containsExactlyEntriesOf(attributes); | ||
|
||
claims = ClaimToken.Builder.newInstance().claim(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/test/path/BPNSOKRATES/").build(); | ||
assertThat(extension.attributesFor(claims)).containsExactlyEntriesOf(attributes); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@ArgumentsSource(ClaimProvider.class) | ||
void attributesFor_shouldMatchTheId(Map<String, Object> claims) { | ||
var attributes = Map.of(PARTICIPANT_IDENTITY, "BPNSOKRATES"); | ||
extension.initialize(context); | ||
var claimToken = ClaimToken.Builder.newInstance().claims(claims).build(); | ||
assertThat(extension.attributesFor(claimToken)).containsExactlyEntriesOf(attributes); | ||
} | ||
|
||
static class ClaimProvider implements ArgumentsProvider { | ||
ClaimProvider() { | ||
} | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of( | ||
Map.of(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/BPNSOKRATES"), | ||
Map.of(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/BPNSOKRATES/"), | ||
Map.of(REFERRING_CONNECTOR_CLAIM, "http://sokrates-controlplane/test/path/BPNSOKRATES"), | ||
Map.of(REFERRING_CONNECTOR_CLAIM, "https://sokrates-controlplane/test/path/BPNSOKRATES"), | ||
Map.of(REFERRING_CONNECTOR_CLAIM, "BPNSOKRATES") | ||
).map(Arguments::arguments); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.