-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* SpecificAssetIds default closed: If no externalSubjectId is set, only the owner has access to this specificAssetId * make specificAssetId public with wildcardprefix: PUBLIC_READABLE: If externalsubjectId includes the prefix, the specificAssetId is visible for everyone
- Loading branch information
Showing
17 changed files
with
723 additions
and
176 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
108 changes: 108 additions & 0 deletions
108
...end/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellAccessHandler.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,108 @@ | ||
package org.eclipse.tractusx.semantics.registry.service; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.tractusx.semantics.RegistryProperties; | ||
import org.eclipse.tractusx.semantics.registry.model.Shell; | ||
import org.eclipse.tractusx.semantics.registry.model.ShellIdentifier; | ||
import org.eclipse.tractusx.semantics.registry.model.ShellIdentifierExternalSubjectReferenceKey; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/******************************************************************************** | ||
* Copyright (c) 2021-2023 Robert Bosch Manufacturing Solutions GmbH | ||
* Copyright (c) 2021-2023 Contributors to the Eclipse Foundation | ||
* | ||
* 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 | ||
********************************************************************************/ | ||
@Slf4j | ||
@Service | ||
public class ShellAccessHandler { | ||
private final String owningTenantId; | ||
private final String externalSubjectIdWildcardPrefix; | ||
private final List<String> externalSubjectIdWildcardAllowedTypes; | ||
|
||
public ShellAccessHandler( RegistryProperties registryProperties ) { | ||
this.owningTenantId = registryProperties.getIdm().getOwningTenantId(); | ||
this.externalSubjectIdWildcardPrefix = registryProperties.getExternalSubjectIdWildcardPrefix(); | ||
this.externalSubjectIdWildcardAllowedTypes = registryProperties.getExternalSubjectIdWildcardAllowedTypes(); | ||
} | ||
|
||
/** | ||
* This method filter out the shell-properties based on externalSubjectId in the specificAssetIds.<br> | ||
* 1. Condition: The owner of the shell has full access to the shell.<br> | ||
* 2. Condition: If the given @param externalSubjectId is included in one of the specificAssetIds, all shell-properties are visible. Only the list of specificAssetIds are limited to given externalSubjectId.<br> | ||
* 3. Condition: If the given @param externalSubjectId is not included in one of the specificAssetIds, only few properties are visible:idShort, submodelDescriptors | ||
* | ||
* | ||
* @param shell | ||
* @param externalSubjectId externalSubjectId/tenantId | ||
* @return filtered Shell | ||
*/ | ||
public Shell filterShellProperties( Shell shell, String externalSubjectId ) { | ||
if ( externalSubjectId.equals( owningTenantId ) ) { | ||
return shell; | ||
} | ||
|
||
Set<ShellIdentifier> filteredIdentifiers = filterSpecificAssetIdsByTenantId( shell.getIdentifiers(), externalSubjectId ); | ||
boolean hasOnlyPublicAccess = !filteredIdentifiers.stream().anyMatch( shellIdentifier -> { | ||
if ( shellIdentifier.getExternalSubjectId() == null ) { | ||
return false; | ||
} | ||
return shellIdentifier.getExternalSubjectId().getKeys().stream().anyMatch( key -> key.getValue().equals( externalSubjectId ) ); | ||
} | ||
); | ||
|
||
if ( hasOnlyPublicAccess ) { | ||
return new Shell() | ||
.withIdentifiers( filteredIdentifiers ) | ||
.withSubmodels( shell.getSubmodels() ) | ||
.withIdExternal( shell.getIdExternal() ) | ||
.withId( shell.getId() ) | ||
.withCreatedDate( shell.getCreatedDate() ); | ||
} | ||
return shell.withIdentifiers( filteredIdentifiers ); | ||
} | ||
|
||
private Set<ShellIdentifier> filterSpecificAssetIdsByTenantId( Set<ShellIdentifier> shellIdentifiers, String tenantId ) { | ||
// the owning tenant should always see all identifiers | ||
if ( tenantId.equals( owningTenantId ) ) { | ||
return shellIdentifiers; | ||
} | ||
|
||
Set<ShellIdentifier> externalSubjectIdSet = new HashSet<>(); | ||
for ( ShellIdentifier identifier : shellIdentifiers ) { | ||
if ( identifier.getExternalSubjectId() != null ) { | ||
Set<ShellIdentifierExternalSubjectReferenceKey> optionalReferenceKey = | ||
identifier.getExternalSubjectId().getKeys().stream().filter( shellIdentifierExternalSubjectReferenceKey -> | ||
// Match if externalSubjectId = tenantId | ||
shellIdentifierExternalSubjectReferenceKey.getValue().equals( tenantId ) || | ||
// or match if externalSubjectId = externalSubjectIdWildcardPrefix and key of identifier (for example manufacturerPartId) is allowing wildcard. | ||
(shellIdentifierExternalSubjectReferenceKey.getValue().equals( externalSubjectIdWildcardPrefix ) && | ||
externalSubjectIdWildcardAllowedTypes.contains( identifier.getKey() )) ).collect( Collectors.toSet() ); | ||
if ( optionalReferenceKey != null && !optionalReferenceKey.isEmpty() ) { | ||
identifier.getExternalSubjectId().setKeys( optionalReferenceKey ); | ||
externalSubjectIdSet.add( identifier ); | ||
} | ||
} | ||
} | ||
return externalSubjectIdSet; | ||
} | ||
} |
Oops, something went wrong.