This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch custos-integration
in repository https://gitbox.apache.org/repos/asf/airavata-data-catalog.git
The following commit(s) were added to refs/heads/custos-integration by this
push:
new 381a452 Checking if user already exists
381a452 is described below
commit 381a4520bdca34af1d597640cf9fd9eee6eb8ab5
Author: Marcus Christie <[email protected]>
AuthorDate: Fri Apr 28 16:51:40 2023 -0400
Checking if user already exists
Same for tenant as well, with initializing the tenant if it is new
---
.../api/sharing/SharingManagerImpl.java | 101 ++++++++++++++-------
1 file changed, 66 insertions(+), 35 deletions(-)
diff --git
a/data-catalog-api/server/src/main/java/org/apache/airavata/datacatalog/api/sharing/SharingManagerImpl.java
b/data-catalog-api/server/src/main/java/org/apache/airavata/datacatalog/api/sharing/SharingManagerImpl.java
index 5c179c4..0ec0f80 100644
---
a/data-catalog-api/server/src/main/java/org/apache/airavata/datacatalog/api/sharing/SharingManagerImpl.java
+++
b/data-catalog-api/server/src/main/java/org/apache/airavata/datacatalog/api/sharing/SharingManagerImpl.java
@@ -1,6 +1,13 @@
package org.apache.airavata.datacatalog.api.sharing;
-import jakarta.annotation.PostConstruct;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
import org.apache.airavata.datacatalog.api.DataProduct;
import org.apache.airavata.datacatalog.api.GroupInfo;
import org.apache.airavata.datacatalog.api.Permission;
@@ -9,6 +16,7 @@ import
org.apache.airavata.datacatalog.api.exception.SharingException;
import org.apache.airavata.datacatalog.api.model.TenantEntity;
import org.apache.airavata.datacatalog.api.model.UserEntity;
import org.apache.airavata.datacatalog.api.repository.TenantRepository;
+import org.apache.airavata.datacatalog.api.repository.UserRepository;
import org.apache.custos.clients.CustosClientProvider;
import org.apache.custos.iam.service.FindUsersResponse;
import org.apache.custos.iam.service.UserRepresentation;
@@ -25,10 +33,7 @@ import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
+import jakarta.annotation.PostConstruct;
@Component("custosSharingManager")
public class SharingManagerImpl implements SharingManager {
@@ -43,18 +48,24 @@ public class SharingManagerImpl implements SharingManager {
@Autowired
TenantRepository tenantRepository;
+ @Autowired
+ UserRepository userRepository;
CustosClientProvider custosClientProvider;
-
private static final String PUBLIC_ACCESS_GROUP = "public_access_group";
- @PostConstruct
- public void initializeTenants(@Value("${identity.server.hostname}") String
hostname,
- @Value("${identity.server.port}") int port,
- @Value("${identity.server.clientId}") String
clientId,
- @Value("${identity.server.clientSec}")
String clientSec) throws SharingException {
+ @Value("${identity.server.hostname}")
+ String hostname;
+ @Value("${identity.server.port}")
+ int port;
+ @Value("${identity.server.clientId}")
+ String clientId;
+ @Value("${identity.server.clientSec}")
+ String clientSec;
+ @PostConstruct
+ public void initializeTenants() throws SharingException {
logger.info("Initializing all tenants");
List<TenantEntity> tenants = tenantRepository.findAll();
@@ -62,7 +73,6 @@ public class SharingManagerImpl implements SharingManager {
this.initialize(tenant.getExternalId());
}
-
logger.info("Initializing Custos client provider");
custosClientProvider = new CustosClientProvider.Builder()
.setServerHost(hostname)
@@ -70,7 +80,6 @@ public class SharingManagerImpl implements SharingManager {
.setClientId(clientId)
.setClientSec(clientSec).build();
-
}
@Override
@@ -92,8 +101,11 @@ public class SharingManagerImpl implements SharingManager {
throw new SharingException(e);
}
+ Set<Permission> allPermissions = new
HashSet<>(Arrays.asList(Permission.values()));
+ allPermissions.remove(Permission.UNRECOGNIZED);
+
// Create permission types for all permissions
- for (Permission permission : Permission.values()) {
+ for (Permission permission : allPermissions) {
PermissionType permissionType = PermissionType.newBuilder()
.setId(permission.name())
@@ -113,26 +125,32 @@ public class SharingManagerImpl implements SharingManager
{
@Override
public UserEntity resolveUser(UserInfo userInfo) throws SharingException {
- try (UserManagementClient userManagementClient =
custosClientProvider.getUserManagementClient()) {
- FindUsersResponse findUsersResponse =
userManagementClient.findUser(userInfo.getTenantId(),
- userInfo.getUserId(), null, null, null, 0, 1);
- if (!findUsersResponse.getUsersList().isEmpty()) {
- UserRepresentation userProfile =
findUsersResponse.getUsersList().get(0);
- TenantEntity tenantEntity = new TenantEntity();
- tenantEntity.setExternalId(userInfo.getTenantId());
-
- UserEntity userEntity = new UserEntity();
- userEntity.setExternalId(userProfile.getId());
- userEntity.setName(userProfile.getUsername());
- userEntity.setTenant(tenantEntity);
- return userEntity;
- } else {
- throw new SharingException("User " + userInfo.getUserId() + "
in tenant "
- + userInfo.getTenantId() + " not found in Identity
Sever ");
+
+ Optional<UserEntity> maybeUserEntity =
userRepository.findByExternalIdAndTenant_ExternalId(userInfo.getUserId(),
+ userInfo.getTenantId());
+ if (maybeUserEntity.isPresent()) {
+ return maybeUserEntity.get();
+ } else {
+ try (UserManagementClient userManagementClient =
custosClientProvider.getUserManagementClient()) {
+ FindUsersResponse findUsersResponse =
userManagementClient.findUser(userInfo.getTenantId(),
+ userInfo.getUserId(), null, null, null, 0, 1);
+ if (!findUsersResponse.getUsersList().isEmpty()) {
+ UserRepresentation userProfile =
findUsersResponse.getUsersList().get(0);
+ TenantEntity tenantEntity = resolveTenant(userInfo);
+
+ UserEntity userEntity = new UserEntity();
+ userEntity.setExternalId(userProfile.getId());
+ userEntity.setName(userProfile.getUsername());
+ userEntity.setTenant(tenantEntity);
+ return userRepository.save(userEntity);
+ } else {
+ throw new SharingException("User " + userInfo.getUserId()
+ " in tenant "
+ + userInfo.getTenantId() + " not found in Identity
Sever ");
+ }
+ } catch (IOException e) {
+ throw new SharingException("Error occurred while resolving
user " + userInfo.getUserId()
+ + " tenant " + userInfo.getTenantId(), e);
}
- } catch (IOException e) {
- throw new SharingException("Error occurred while resolving user "
+ userInfo.getUserId()
- + " tenant " + userInfo.getTenantId(), e);
}
}
@@ -156,7 +174,7 @@ public class SharingManagerImpl implements SharingManager {
@Override
public void grantPermissionToUser(UserInfo userInfo, DataProduct
dataProduct, Permission permission,
- UserInfo sharedByUser)
+ UserInfo sharedByUser)
throws SharingException {
List<String> userIds = new ArrayList<>();
@@ -193,7 +211,7 @@ public class SharingManagerImpl implements SharingManager {
@Override
public void grantPermissionToGroup(GroupInfo groupInfo, DataProduct
dataProduct, Permission permission,
- UserInfo sharedByUser)
+ UserInfo sharedByUser)
throws SharingException {
List<String> userIds = new ArrayList<>();
@@ -274,4 +292,17 @@ public class SharingManagerImpl implements SharingManager {
}
}
+ private TenantEntity resolveTenant(UserInfo userInfo) throws
SharingException {
+ Optional<TenantEntity> maybeTenantEntity =
tenantRepository.findByExternalId(userInfo.getTenantId());
+ if (maybeTenantEntity.isPresent()) {
+ return maybeTenantEntity.get();
+ } else {
+ TenantEntity newTenantEntity = new TenantEntity();
+ newTenantEntity.setExternalId(userInfo.getTenantId());
+ newTenantEntity.setName(userInfo.getTenantId());
+ newTenantEntity = tenantRepository.save(newTenantEntity);
+ initialize(newTenantEntity.getExternalId());
+ return newTenantEntity;
+ }
+ }
}