Skip to content
Permalink
Browse files
Merge pull request #48 from isururanawaka/sharing_service_impl
Auto sharing of resource with admin group
  • Loading branch information
isururanawaka committed Sep 1, 2021
2 parents 5c23e0e + d4bed54 commit 9b6318c4e9e63a36a418bfd425b79800e88afa06
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 15 deletions.
@@ -20,6 +20,8 @@ public void setMessageFilter(FilterConfig messageFilter) {
this.messageFilter = messageFilter;
}

public TenantConfigs tenantConfigs;

public Configuration() {

}
@@ -60,6 +62,14 @@ public void setOutboundEventProcessor(OutboundEventProcessorConfig outboundEvent
this.outboundEventProcessor = outboundEventProcessor;
}

public TenantConfigs getTenantConfigs() {
return tenantConfigs;
}

public void setTenantConfigs(TenantConfigs tenantConfigs) {
this.tenantConfigs = tenantConfigs;
}

public static class Consumer {

private String brokerURL;
@@ -219,4 +229,36 @@ public void setMftPort(int mftPort) {
}


public static class TenantConfigs {

private String tenantId;
private String userGroup;
private String adminGroup;

public String getTenantId() {
return tenantId;
}

public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public String getUserGroup() {
return userGroup;
}

public void setUserGroup(String userGroup) {
this.userGroup = userGroup;
}

public String getAdminGroup() {
return adminGroup;
}

public void setAdminGroup(String adminGroup) {
this.adminGroup = adminGroup;
}
}


}
@@ -7,6 +7,7 @@
import org.apache.airavata.datalake.drms.DRMSServiceAuthToken;
import org.apache.airavata.datalake.drms.resource.GenericResource;
import org.apache.airavata.datalake.drms.sharing.DRMSSharingServiceGrpc;
import org.apache.airavata.datalake.drms.sharing.ShareEntityWithGroupRequest;
import org.apache.airavata.datalake.drms.sharing.ShareEntityWithUserRequest;
import org.apache.airavata.datalake.drms.storage.*;
import org.apache.airavata.datalake.orchestrator.Configuration;
@@ -77,6 +78,29 @@ public void shareWithUser(String authToken, String tenantId, String admin, Strin

}


public void shareWithGroup(String authToken, String tenantId, String admin, String groupId, String resourceId,
String permission) throws Exception {

DRMSServiceAuthToken serviceAuthToken = DRMSServiceAuthToken.newBuilder()
.setAccessToken(authToken)
.setAuthCredentialType(AuthCredentialType.AGENT_ACCOUNT_CREDENTIAL)
.setAuthenticatedUser(AuthenticatedUser.newBuilder()
.setUsername(admin)
.setTenantId(tenantId)
.build())
.build();

ShareEntityWithGroupRequest.Builder shareBuilder = ShareEntityWithGroupRequest.newBuilder()
.setAuthToken(serviceAuthToken)
.setEntityId(resourceId)
.setSharedGroupId(groupId)
.setPermissionId(permission);

this.sharingServiceBlockingStub.shareEntityWithGroup(shareBuilder.build());

}

public Optional<TransferMapping> getActiveTransferMapping(String authToken, String tenantId,
String user, String hostName) throws Exception {

@@ -57,16 +57,17 @@ public OrchestratorEventProcessor(Configuration configuration, NotificationEvent
this.configuration = configuration;
}

private List<GenericResource> createResourceRecursively(String storageId, String basePath, String resourcePath, String resourceType, String user)
throws Exception{
private List<GenericResource> createResourceRecursively(String storageId, String basePath,
String resourcePath, String resourceType, String user)
throws Exception {

List<GenericResource> resourceList = new ArrayList<>();

String parentType = "Storage";

String[] splitted = resourcePath.substring(basePath.length()).split("/");

String currentPath = basePath.endsWith("/")? basePath.substring(0, basePath.length() -1): basePath;
String currentPath = basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) : basePath;
String parentId = storageId;
for (int i = 0; i < splitted.length - 1; i++) {
String resourceName = splitted[i];
@@ -86,13 +87,13 @@ private List<GenericResource> createResourceRecursively(String storageId, String
}
}

currentPath = currentPath + "/" + splitted[splitted.length -1];
currentPath = currentPath + "/" + splitted[splitted.length - 1];

Optional<GenericResource> optionalGenericResource =
this.drmsConnector.createResource(notificationEvent.getAuthToken(),
notificationEvent.getTenantId(),
Utils.getId(storageId + ":" + currentPath),
splitted[splitted.length -1], currentPath,
splitted[splitted.length - 1], currentPath,
parentId, resourceType, parentType, user);

if (optionalGenericResource.isPresent()) {
@@ -106,11 +107,21 @@ private List<GenericResource> createResourceRecursively(String storageId, String
}


private void shareResources(List<GenericResource> resourceList, String admin, String user, String permission) throws Exception {
private void shareResourcesWithUsers(List<GenericResource> resourceList, String admin, String user, String permission) throws Exception {
for (GenericResource resource : resourceList) {
logger.info("Sharing resource {} with path {} with user {}",
resource.getResourceId(), resource.getResourcePath(), user);
this.drmsConnector.shareWithUser(notificationEvent.getAuthToken(), notificationEvent.getTenantId(), admin, user, resource.getResourceId(), permission);
this.drmsConnector.shareWithUser(notificationEvent.getAuthToken(), notificationEvent.getTenantId(),
admin, user, resource.getResourceId(), permission);
}
}

private void shareResourcesWithGroups(List<GenericResource> resourceList, String admin, String group, String permission) throws Exception {
for (GenericResource resource : resourceList) {
logger.info("Sharing resource {} with path {} with group {}",
resource.getResourceId(), resource.getResourcePath(), group);
this.drmsConnector.shareWithGroup(notificationEvent.getAuthToken(), notificationEvent.getTenantId(),
admin, group, resource.getResourceId(), permission);
}
}

@@ -134,7 +145,7 @@ public void run() {
String owner = splitted[1].split("_")[0];

Map<String, String> ownerRules = new HashMap<>();
ownerRules.put(adminUser, "ADMIN");
ownerRules.put(adminUser, "VIEWER");
ownerRules.put(splitted[1], "OWNER");

Optional<TransferMapping> optionalTransferMapping = drmsConnector.getActiveTransferMapping(
@@ -159,9 +170,18 @@ public void run() {
notificationEvent.getResourcePath(),
"COLLECTION", adminUser);

shareResources(Collections.singletonList(resourceList.get(resourceList.size() -1)), adminUser, owner, "ADMIN");
shareResourcesWithUsers(Collections.singletonList(resourceList.get(resourceList.size() - 1)),
adminUser, owner, "VIEWER");

shareResourcesWithGroups(Collections.singletonList(resourceList.get(resourceList.size() - 1)), adminUser,
configuration.getTenantConfigs().getAdminGroup(),
"EDITOR");

// shareResourcesWithGroups(Collections.singletonList(resourceList.get(resourceList.size() - 1)), adminUser,
// configuration.getTenantConfigs().getUserGroup(),
// "VIEWER");

GenericResource resourceObj = resourceList.get(resourceList.size() -1);
GenericResource resourceObj = resourceList.get(resourceList.size() - 1);

Optional<AnyStoragePreference> sourceSPOp = this.drmsConnector.getStoragePreference(
notificationEvent.getAuthToken(), adminUser,
@@ -207,7 +227,7 @@ public void run() {
.setMftAuthorizationToken(mftAuth)
.setResourceId(resourceObj.getResourceId());

switch (sourceSP.getStorageCase()){
switch (sourceSP.getStorageCase()) {
case SSH_STORAGE_PREFERENCE:
resourceMetadataReq.setResourceType("SCP");
resourceMetadataReq.setResourceToken(sourceSP.getSshStoragePreference().getCredentialToken());
@@ -227,7 +247,7 @@ public void run() {
logger.info("Registering file {} for source storage {}", fileMetadata.getResourcePath(), sourceStorageId);
resourceList = createResourceRecursively(sourceStorageId, notificationEvent.getBasePath(),
fileMetadata.getResourcePath(), "FILE", adminUser);
GenericResource fileResource = resourceList.get(resourceList.size() -1);
GenericResource fileResource = resourceList.get(resourceList.size() - 1);

resourceIDsToProcess.add(fileResource.getResourceId());
}
@@ -244,7 +264,7 @@ public void run() {
resourceList = createResourceRecursively(destinationStorageId, notificationEvent.getBasePath(),
notificationEvent.getResourcePath(), "FILE", adminUser);

GenericResource destinationResource = resourceList.get(resourceList.size() -1);
GenericResource destinationResource = resourceList.get(resourceList.size() - 1);

System.out.println(destinationResource);

@@ -34,7 +34,7 @@ public void shareEntityWithUser(ShareEntityWithUserRequest request, StreamObserv

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId(request.getPermissionId()).build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest
@@ -88,7 +88,7 @@ public void shareEntityWithGroup(ShareEntityWithGroupRequest request, StreamObse

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId(request.getPermissionId()).build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest

0 comments on commit 9b6318c

Please sign in to comment.