Skip to content

Commit

Permalink
Implements authorization for ConnectedAasManager (eclipse-basyx#307)
Browse files Browse the repository at this point in the history
* Implements authorization for ConnectedAasManager

- Implements authorization for ConnectedAasRepository
- Implements authorization for ConnectedAasService
- Implements authorization for ConnectedSubmodelRepository
- Implements authorization for ConnectedSubmodelService

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Refactors code

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Refactors BaSyx-realm.json

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Fixes compilation error in Registries

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Fixes issues in AAS Repo

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Refactors code

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Adds keycloak fix uri

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Refactors code

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Addressed review remarks and minor refactoring

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Refactors code

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Minor refactoring

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Addressed review remarks

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Minor refactoring

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

---------

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
  • Loading branch information
mdanish98 authored Jun 13, 2024
1 parent 127c2f1 commit 6b290bc
Show file tree
Hide file tree
Showing 86 changed files with 8,124 additions and 180 deletions.
28 changes: 28 additions & 0 deletions basyx.aasenvironment/basyx.aasenvironment-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,33 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasservice-client</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasservice-core</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasenvironment-feature-authorization</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.http</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasenvironment.client;

import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.EndpointResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.DescriptorResolverManager;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.authorization.AuthorizedAasDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.authorization.AuthorizedSubmodelDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasregistry.main.client.AuthorizedConnectedAasRegistry;
import org.eclipse.digitaltwin.basyx.aasrepository.client.AuthorizedConnectedAasRepository;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.AuthorizedConnectedSubmodelRegistry;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelrepository.client.AuthorizedConnectedSubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;

/**
* Authorized client component for executing consolidated Repository and Registry requests
*
* @author danish
*
*/
public class AuthorizedConnectedAasManager extends ConnectedAasManager {

public AuthorizedConnectedAasManager(AuthorizedConnectedAasRegistry authorizedAasRegistryApi, AuthorizedConnectedAasRepository authorizedAasRepository, AuthorizedConnectedSubmodelRegistry authorizedSubmodelRegistryApi, AuthorizedConnectedSubmodelRepository authorizedSubmodelRepository) {
super(authorizedAasRegistryApi, authorizedAasRepository, authorizedSubmodelRegistryApi, authorizedSubmodelRepository, getAuthorizedResolver(authorizedAasRepository.getTokenManager(), authorizedSubmodelRepository.getTokenManager()));
}

private static DescriptorResolverManager getAuthorizedResolver(TokenManager authorizedAasRepoTokenManager, TokenManager authorizedSubmodelRepoTokenManager) {
DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> aasDescriptorResolver = new AuthorizedAasDescriptorResolver(new EndpointResolver(), authorizedAasRepoTokenManager);
DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> smDescriptorResolver = new AuthorizedSubmodelDescriptorResolver(new EndpointResolver(), authorizedSubmodelRepoTokenManager);

return new DescriptorResolverManager(aasDescriptorResolver, smDescriptorResolver);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@
import org.eclipse.digitaltwin.basyx.aasenvironment.client.exceptions.NoValidEndpointFoundException;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.exceptions.RegistryHttpRequestException;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.AasDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.DescriptorResolverManager;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.EndpointResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.SubmodelDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasregistry.client.api.RegistryAndDiscoveryInterfaceApi;
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.AasDescriptorFactory;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
Expand All @@ -53,7 +56,7 @@
/**
* Client component for executing consolidated Repository and Registry requests
*
* @author mateusmolina, jungjan
* @author mateusmolina, jungjan, danish
*
*/
public class ConnectedAasManager {
Expand All @@ -64,10 +67,10 @@ public class ConnectedAasManager {
private final RegistryAndDiscoveryInterfaceApi aasRegistryApi;
private final SubmodelRegistryApi smRegistryApi;

private final AasDescriptorResolver aasDescriptorResolver;
private final DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> aasDescriptorResolver;
private final AasDescriptorFactory aasDescriptorFactory;

private final SubmodelDescriptorResolver smDescriptorResolver;
private final DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> smDescriptorResolver;
private final SubmodelDescriptorFactory smDescriptorFactory;

/**
Expand All @@ -79,38 +82,37 @@ public class ConnectedAasManager {
* @param submodelBaseRepositoryUrl
*/
public ConnectedAasManager(String aasRegistryBaseUrl, String aasRepositoryBaseUrl, String submodelRegistryBaseUrl, String submodelBaseRepositoryUrl) {
this(new RegistryAndDiscoveryInterfaceApi(aasRegistryBaseUrl), new ConnectedAasRepository(aasRepositoryBaseUrl), aasRepositoryBaseUrl, new SubmodelRegistryApi(submodelRegistryBaseUrl),
new ConnectedSubmodelRepository(submodelBaseRepositoryUrl), submodelBaseRepositoryUrl);
this(new RegistryAndDiscoveryInterfaceApi(aasRegistryBaseUrl), new ConnectedAasRepository(aasRepositoryBaseUrl), new SubmodelRegistryApi(submodelRegistryBaseUrl),
new ConnectedSubmodelRepository(submodelBaseRepositoryUrl), getResolver());
}

ConnectedAasManager(RegistryAndDiscoveryInterfaceApi aasRegistryApi, ConnectedAasRepository aasRepository, String aasRepositoryBaseUrl, SubmodelRegistryApi smRegistryApi, ConnectedSubmodelRepository smRepository,
String submodelBaseRepositoryUrl) {

ConnectedAasManager(RegistryAndDiscoveryInterfaceApi aasRegistryApi, ConnectedAasRepository aasRepository, SubmodelRegistryApi smRegistryApi, ConnectedSubmodelRepository smRepository, DescriptorResolverManager resolver) {
this.aasRepository = aasRepository;
this.aasRegistryApi = aasRegistryApi;
this.smRepository = smRepository;
this.smRegistryApi = smRegistryApi;
this.aasDescriptorResolver = ConnectedAasManagerHelper.buildAasDescriptorResolver();
this.aasDescriptorFactory = ConnectedAasManagerHelper.buildAasDescriptorFactory(aasRepositoryBaseUrl);
this.smDescriptorResolver = ConnectedAasManagerHelper.buildSubmodelDescriptorResolver();
this.smDescriptorFactory = ConnectedAasManagerHelper.buildSmDescriptorFactory(submodelBaseRepositoryUrl);
this.aasDescriptorResolver = resolver.getAasDescriptorResolver();
this.aasDescriptorFactory = ConnectedAasManagerHelper.buildAasDescriptorFactory(aasRepository.getBaseUrl());
this.smDescriptorResolver = resolver.getSubmodelDescriptorResolver();
this.smDescriptorFactory = ConnectedAasManagerHelper.buildSmDescriptorFactory(smRepository.getBaseUrl());
}

/**
* Retrieves a ConnectedAasService in an AAS registry by its identifier.
*
* @param identifier
* The identifier of the AAS to retrieve.
* @return The retrieved ConnectedAasService object.
*/
public ConnectedAasService getAas(String identifier) throws NoValidEndpointFoundException {
public ConnectedAasService getAasService(String identifier) throws NoValidEndpointFoundException {
AssetAdministrationShellDescriptor descriptor;

try {
descriptor = aasRegistryApi.getAssetAdministrationShellDescriptorById(identifier);
} catch (Exception e) {
throw new RegistryHttpRequestException(identifier, e);
}
return aasDescriptorResolver.resolveAasDescriptor(descriptor);
return aasDescriptorResolver.resolveDescriptor(descriptor);
}

/**
Expand All @@ -121,7 +123,7 @@ public ConnectedAasService getAas(String identifier) throws NoValidEndpointFound
* The identifier of the submodel to retrieve.
* @return The retrieved ConnectedSubmodelService object.
*/
public ConnectedSubmodelService getSubmodel(String identifier) {
public ConnectedSubmodelService getSubmodelService(String identifier) {
SubmodelDescriptor descriptor;

try {
Expand All @@ -130,7 +132,7 @@ public ConnectedSubmodelService getSubmodel(String identifier) {
throw new RegistryHttpRequestException(identifier, e);
}

return smDescriptorResolver.resolveSubmodelDescriptor(descriptor);
return smDescriptorResolver.resolveDescriptor(descriptor);
}

/**
Expand All @@ -141,11 +143,11 @@ public ConnectedSubmodelService getSubmodel(String identifier) {
* @return The retrieved Submodel object.
*/
public List<ConnectedSubmodelService> getAllSubmodels(String shellIdentifier) {
AssetAdministrationShell shell = getAas(shellIdentifier).getAAS();
AssetAdministrationShell shell = getAasService(shellIdentifier).getAAS();
List<Reference> submodelReferences = shell.getSubmodels();
return submodelReferences.parallelStream()
.map(this::extractSubmodelIdentifierFromReference)
.map(this::getSubmodel)
.map(this::getSubmodelService)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -247,5 +249,12 @@ private Key extractSubmodelKeyFromReference(Reference submodelReference) {
return submodelReference.getKeys()
.get(0);
}

private static DescriptorResolverManager getResolver() {
DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> aasDescriptorResolver = new AasDescriptorResolver(new EndpointResolver());
DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> smDescriptorResolver = new SubmodelDescriptorResolver(new EndpointResolver());

return new DescriptorResolverManager(aasDescriptorResolver, smDescriptorResolver);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
package org.eclipse.digitaltwin.basyx.aasenvironment.client;


import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.AasDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.EndpointResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.SubmodelDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.AasDescriptorFactory;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.mapper.AttributeMapper;
import org.eclipse.digitaltwin.basyx.http.Aas4JHTTPSerializationExtension;
Expand All @@ -41,7 +38,7 @@
/**
* Provides builder methods for {@link ConnectedAasManager} dependencies
*
* @author mateusmolina
* @author mateusmolina, danish
*
*/
class ConnectedAasManagerHelper {
Expand All @@ -58,14 +55,6 @@ static ObjectMapper buildObjectMapper() {
return builder.build();
}

static AasDescriptorResolver buildAasDescriptorResolver() {
return new AasDescriptorResolver(new EndpointResolver());
}

static SubmodelDescriptorResolver buildSubmodelDescriptorResolver() {
return new SubmodelDescriptorResolver(new EndpointResolver());
}

static AasDescriptorFactory buildAasDescriptorFactory(String aasRepositoryBaseUrl) {
AttributeMapper attributeMapper = new AttributeMapper(objectMapper);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.Endpoint;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;

/**
* Resolves an AasDescriptor into an AssetAdministrationShell
* Resolves an AasDescriptor into a {@link ConnectedAasService}
*
* @author mateusmolina
* @author mateusmolina, danish
*
*/
public class AasDescriptorResolver {
public class AasDescriptorResolver implements DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> {

private final EndpointResolver endpointResolver;

Expand All @@ -50,20 +51,20 @@ public class AasDescriptorResolver {
public AasDescriptorResolver(EndpointResolver endpointResolver) {
this.endpointResolver = endpointResolver;
}

/**
* Resolves an AASDescriptor to a ConnectedAasService
*
* @param aasDescriptor
* @return
*/
public ConnectedAasService resolveAasDescriptor(AssetAdministrationShellDescriptor aasDescriptor) {
public ConnectedAasService resolveDescriptor(AssetAdministrationShellDescriptor aasDescriptor) {
String endpoint = endpointResolver.resolveFirst(aasDescriptor.getEndpoints(), AasDescriptorResolver::parseEndpoint);

return new ConnectedAasService(endpoint);
}

private static Optional<URI> parseEndpoint(Endpoint endpoint) {
public static Optional<URI> parseEndpoint(Endpoint endpoint) {
try {
if (endpoint == null || endpoint.getProtocolInformation() == null || endpoint.getProtocolInformation()
.getHref() == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers;

import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;

/**
* A helper class for accumulating various {@link DescriptorResolver}s
*
* @author danish
*/
public class DescriptorResolverManager {

private DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> aasDescriptorResolver;
private DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> submodelDescriptorResolver;

public DescriptorResolverManager(DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> aasDescriptorResolver, DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> submodelDescriptorResolver) {
super();
this.aasDescriptorResolver = aasDescriptorResolver;
this.submodelDescriptorResolver = submodelDescriptorResolver;
}

public DescriptorResolver<AssetAdministrationShellDescriptor, ConnectedAasService> getAasDescriptorResolver() {
return aasDescriptorResolver;
}

public DescriptorResolver<SubmodelDescriptor, ConnectedSubmodelService> getSubmodelDescriptorResolver() {
return submodelDescriptorResolver;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private boolean isURIWorking(URI uri) {
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
return (200 <= responseCode && responseCode <= 401);
} catch (Exception e) {
return false;
} finally {
Expand Down
Loading

0 comments on commit 6b290bc

Please sign in to comment.