Added delete operation

This commit is contained in:
Giancarlo Panichi 2021-10-14 17:19:55 +02:00
parent 8fb3eb9a9d
commit e7a98ef0aa
10 changed files with 251 additions and 117 deletions

46
pom.xml
View File

@ -17,12 +17,27 @@
<description> <description>
Requesting Party Token Portlet Requesting Party Token Portlet
</description> </description>
<scm> <scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection> <connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection> <developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url> <url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
</scm> </scm>
<developers>
<developer>
<name>Giancarlo Panichi</name>
<email>g.panichi@isti.cnr.it</email>
<organization>CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"</organization>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
</developer>
</developers>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
@ -147,37 +162,21 @@
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId> <artifactId>ic-client</artifactId>
<version>1.0.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId> <artifactId>common-scope-maps</artifactId>
<version>1.1.0</version>
</dependency> </dependency>
<!-- <!-- <dependency> <groupId>org.gcube.resourcemanagement</groupId> <artifactId>resourcemanager-client</artifactId>
<dependency> <version>[1.0.0, 2.0.0-SNAPSHOT)</version> <scope>compile</scope> </dependency> -->
<groupId>org.gcube.resourcemanagement</groupId>
<artifactId>resourcemanager-client</artifactId>
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency> -->
<dependency> <dependency>
<groupId>org.gcube.resources</groupId> <groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId> <artifactId>registry-publisher</artifactId>
<version>1.3.1</version>
</dependency> </dependency>
<!-- <!-- <dependency> <groupId>org.gcube.resourcemanagement</groupId> <artifactId>ghn-manager-client</artifactId>
<dependency> <version>[1.1.0, 2.0.0-SNAPSHOT)</version> <scope>compile</scope> </dependency> -->
<groupId>org.gcube.resourcemanagement</groupId> <!-- <dependency> <groupId>org.gcube.resources</groupId> <artifactId>common-gcore-resources</artifactId>
<artifactId>ghn-manager-client</artifactId>
<version>[1.1.0, 2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency> -->
<!--
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
</dependency> --> </dependency> -->
<!-- Common Encryption --> <!-- Common Encryption -->
@ -263,13 +262,16 @@
<target>${maven.compiler.target}</target> <target>${maven.compiler.target}</target>
</configuration> </configuration>
</plugin> </plugin>
<!--
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
<version>2.5</version> <version>2.5</version>
<configuration> <configuration>
<encoding>${project.build.resources.encoding}</encoding> <encoding>${project.build.resources.encoding}</encoding>
</configuration> </configuration>
</plugin> </plugin> -->
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -20,7 +20,7 @@ import org.gcube.portlets.admin.systemservicedefinition.definition.DefClientIdCo
import org.gcube.portlets.admin.systemservicedefinition.definition.DefDescriptionComparator; import org.gcube.portlets.admin.systemservicedefinition.definition.DefDescriptionComparator;
import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem; import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem;
import org.gcube.portlets.admin.systemservicedefinition.event.CreateSSDEvent; import org.gcube.portlets.admin.systemservicedefinition.event.CreateSSDEvent;
import org.gcube.portlets.admin.systemservicedefinition.event.CreateSSDEventPublisher; import org.gcube.portlets.admin.systemservicedefinition.event.DeleteSSDEvent;
import org.gcube.portlets.admin.systemservicedefinition.is.InformationSystemUtils; import org.gcube.portlets.admin.systemservicedefinition.is.InformationSystemUtils;
import org.gcube.portlets.admin.systemservicedefinition.shared.Constants; import org.gcube.portlets.admin.systemservicedefinition.shared.Constants;
import org.gcube.portlets.admin.systemservicedefinition.util.UserCredentials; import org.gcube.portlets.admin.systemservicedefinition.util.UserCredentials;
@ -209,7 +209,7 @@ public class SystemServiceDefinition extends MVCPortlet {
*/ */
try { try {
itemsList = InformationSystemUtils.retrieveSSD(Constants.SYSTEM_SERVICE_CATEGORY, itemsList = InformationSystemUtils.retrieveSSD(Constants.SYSTEM_SERVICE_CATEGORY,
userCredentials.getCurrentScope(), userCredentials.getAccessToken()); userCredentials.getCurrentScope());
} catch (Exception e) { } catch (Exception e) {
logger.error("Error retrieving System Services from IS. {}", e.getLocalizedMessage(), e); logger.error("Error retrieving System Services from IS. {}", e.getLocalizedMessage(), e);
throw new PortletException("Error retrieving System Services from IS. " + e.getLocalizedMessage(), e); throw new PortletException("Error retrieving System Services from IS. " + e.getLocalizedMessage(), e);
@ -364,8 +364,8 @@ public class SystemServiceDefinition extends MVCPortlet {
String secret = UUID.randomUUID().toString(); String secret = UUID.randomUUID().toString();
DefinitionItem definitionItem = new DefinitionItem(clientId, description, username, secret); DefinitionItem definitionItem = new DefinitionItem(clientId, description, username, secret);
InformationSystemUtils.publishSSD(definitionItem, Constants.SYSTEM_SERVICE_CATEGORY, InformationSystemUtils.publishSSD(definitionItem, Constants.SYSTEM_SERVICE_CATEGORY,
userCredentials.getCurrentScope(), userCredentials.getAccessToken()); userCredentials.getCurrentScope());
// publishCreateSSDEvent(definitionItem); publishCreateSSDEvent(definitionItem);
SessionMessages.add(actionRequest, "success-definition-created"); SessionMessages.add(actionRequest, "success-definition-created");
actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp"); actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp");
} catch (Exception e) { } catch (Exception e) {
@ -393,17 +393,7 @@ public class SystemServiceDefinition extends MVCPortlet {
} }
private void publishCreateSSDEvent(DefinitionItem definitionItem) {
logger.debug("Trying to send the event to create System Service");
CreateSSDEvent toSend = new CreateSSDEvent(definitionItem);
new CreateSSDEventPublisher().publish(toSend);
logger.info("Done send event " + toSend.getName() + " for " + toSend.getUser());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
public void editDefinition(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { public void editDefinition(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -466,30 +456,61 @@ public class SystemServiceDefinition extends MVCPortlet {
paramsInfo(actionRequest); paramsInfo(actionRequest);
logger.debug("Delete Request");
String clientId = ParamUtil.getString(actionRequest, "clientId");
if (clientId == null || clientId.isEmpty()) {
logger.error("Invalid client id: " + clientId);
SessionErrors.add(actionRequest, "error-invalid-clientId");
SessionMessages.add(actionRequest,
PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
PortalUtil.copyRequestParameters(actionRequest, actionResponse);
actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp");
return;
}
try {
DefinitionItem definitionItem=new DefinitionItem(clientId);
InformationSystemUtils.deleteSSD(definitionItem, Constants.SYSTEM_SERVICE_CATEGORY,
userCredentials.getCurrentScope());
publishDeleteSSDEvent(definitionItem);
SessionMessages.add(actionRequest, "success-definition-deleted"); SessionMessages.add(actionRequest, "success-definition-deleted");
actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp"); actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp");
/* } catch (Exception e) {
* SessionErrors.add(actionRequest, "error-delete-definition"); logger.error(e.getLocalizedMessage(),e);
* SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionErrors.add(actionRequest, "error-delete-definition");
* SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE); SessionMessages.add(actionRequest,
* actionResponse.setRenderParameter("jspPage", PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
* "/html/systemservicedefinition/view.jsp"); actionResponse.setRenderParameter("jspPage", "/html/systemservicedefinition/view.jsp");
*/ }
/* }
* String portletName = (String)actionRequest.getAttribute(WebKeys.PORTLET_ID);
* ThemeDisplay themeDisplay =
* (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
*
* PortletURL redirectURL =
* PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest),
* portletName,themeDisplay.getLayout().getPlid(), PortletRequest.RENDER_PHASE);
* redirectURL.setParameter("jspPage", "/registration.jsp");
* SessionErrors.add(actionRequest, "WRONG_FIRST_NAME_ERROR");
* actionResponse.sendRedirect(redirectURL.toString());
*/
private void publishCreateSSDEvent(DefinitionItem definitionItem) {
logger.debug("Trying to send the event to create System Service");
CreateSSDEvent toCreate = new CreateSSDEvent(definitionItem);
toCreate.publisher.publish(toCreate);
logger.info("Done send create event " + toCreate.getName() + " for " + toCreate.getUser());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
private void publishDeleteSSDEvent(DefinitionItem definitionItem) {
logger.debug("Trying to send the event to delete System Service");
DeleteSSDEvent toDelete = new DeleteSSDEvent(definitionItem);
toDelete.publisher.publish(toDelete);
logger.info("Done send delete event " + toDelete.getName() + " for " + toDelete.getUser());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
} }
/* /*

View File

@ -16,6 +16,10 @@ public class DefinitionItem implements Serializable {
private String username; private String username;
private String secret; private String secret;
public DefinitionItem(String clientId) {
super();
this.clientId = clientId;
}
public DefinitionItem(String clientId, String description, String username, String secret) { public DefinitionItem(String clientId, String description, String username, String secret) {
super(); super();

View File

@ -1,5 +1,6 @@
package org.gcube.portlets.admin.systemservicedefinition.event; package org.gcube.portlets.admin.systemservicedefinition.event;
import org.gcube.portal.event.publisher.lr62.AbstractLR62EventPublisher;
import org.gcube.portal.event.publisher.lr62.PortalEvent; import org.gcube.portal.event.publisher.lr62.PortalEvent;
import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem; import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem;
@ -12,6 +13,14 @@ public class CreateSSDEvent extends PortalEvent {
private static final long serialVersionUID = 1499288552188273747L; private static final long serialVersionUID = 1499288552188273747L;
public class CreateSSDEventPublisher extends AbstractLR62EventPublisher {
public CreateSSDEventPublisher() {
super();
}
}
public CreateSSDEventPublisher publisher;
public static final String NAME = "create_system_service"; public static final String NAME = "create_system_service";
public static final String CLIENT_ID_ENTRY = "client_id"; public static final String CLIENT_ID_ENTRY = "client_id";
public static final String DESCRIPTION_ENTRY = "description"; public static final String DESCRIPTION_ENTRY = "description";
@ -19,6 +28,7 @@ public class CreateSSDEvent extends PortalEvent {
public CreateSSDEvent(DefinitionItem definitionItem) { public CreateSSDEvent(DefinitionItem definitionItem) {
super(NAME); super(NAME);
publisher = new CreateSSDEventPublisher();
setClientId(definitionItem.getClientId()); setClientId(definitionItem.getClientId());
setDescription(definitionItem.getDescription()); setDescription(definitionItem.getDescription());
setClientSecret(definitionItem.getSecret()); setClientSecret(definitionItem.getSecret());

View File

@ -1,9 +0,0 @@
package org.gcube.portlets.admin.systemservicedefinition.event;
import org.gcube.portal.event.publisher.lr62.AbstractLR62EventPublisher;
public class CreateSSDEventPublisher extends AbstractLR62EventPublisher {
public CreateSSDEventPublisher() {
super();
}
}

View File

@ -0,0 +1,41 @@
package org.gcube.portlets.admin.systemservicedefinition.event;
import org.gcube.portal.event.publisher.lr62.AbstractLR62EventPublisher;
import org.gcube.portal.event.publisher.lr62.PortalEvent;
import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem;
/**
*
* @author Giancarlo Panichi
*
*/
public class DeleteSSDEvent extends PortalEvent {
private static final long serialVersionUID = 1499288552188273747L;
public class DeleteSSDEventPublisher extends AbstractLR62EventPublisher {
public DeleteSSDEventPublisher() {
super();
}
}
public DeleteSSDEventPublisher publisher;
public static final String NAME = "delete_system_service";
public static final String CLIENT_ID_ENTRY = "client_id";
public DeleteSSDEvent(DefinitionItem definitionItem) {
super(NAME);
publisher = new DeleteSSDEventPublisher();
setClientId(definitionItem.getClientId());
}
public void setClientId(String clientId) {
set(CLIENT_ID_ENTRY, clientId);
}
public String getClientId() {
return (String) get(CLIENT_ID_ENTRY);
}
}

View File

@ -26,8 +26,7 @@ public class InformationSystemUtils {
private static Logger logger = LoggerFactory.getLogger(InformationSystemUtils.class); private static Logger logger = LoggerFactory.getLogger(InformationSystemUtils.class);
public static IAMService retrieveIAMService(String scope) throws Exception {
public static IAMService retrieveIAMService(String scope, String token) throws Exception {
try { try {
logger.debug("Retrieve IAM Service on IS"); logger.debug("Retrieve IAM Service on IS");
@ -35,8 +34,10 @@ public class InformationSystemUtils {
if (scope == null || scope.isEmpty()) if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope); throw new Exception("Invalid scope: " + scope);
if (token == null || token.isEmpty()) /*
throw new Exception("Invalid token: " + scope); * if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* scope);
*/
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
// AccessTokenProvider.instance.set(token); // AccessTokenProvider.instance.set(token);
@ -67,7 +68,7 @@ public class InformationSystemUtils {
} }
} }
public static ArrayList<DefinitionItem> retrieveSSD(String category, String scope, String token) throws Exception { public static ArrayList<DefinitionItem> retrieveSSD(String category, String scope) throws Exception {
try { try {
logger.debug("Retrieve System Services Definition on IS"); logger.debug("Retrieve System Services Definition on IS");
@ -75,8 +76,10 @@ public class InformationSystemUtils {
if (scope == null || scope.isEmpty()) if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope); throw new Exception("Invalid scope: " + scope);
if (token == null || token.isEmpty()) /*
throw new Exception("Invalid token: " + scope); * if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* scope);
*/
ScopeProvider.instance.set(scope); ScopeProvider.instance.set(scope);
// AccessTokenProvider.instance.set(token); // AccessTokenProvider.instance.set(token);
@ -116,8 +119,7 @@ public class InformationSystemUtils {
} }
} }
public static void publishSSD(DefinitionItem definitionItem, String category, String scope, String token) public static void publishSSD(DefinitionItem definitionItem, String category, String scope) throws Exception {
throws Exception {
if (definitionItem == null) if (definitionItem == null)
throw new Exception("Invalid definition: " + definitionItem); throw new Exception("Invalid definition: " + definitionItem);
@ -127,11 +129,12 @@ public class InformationSystemUtils {
if (scope == null || scope.isEmpty()) if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope); throw new Exception("Invalid scope: " + scope);
if (token == null || token.isEmpty()) /*
throw new Exception("Invalid token: " + token); * if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* token);
*/
IAMService iamService = retrieveIAMService(scope);
IAMService iamService=retrieveIAMService(scope, token);
ServiceEndpoint toPublish = new ServiceEndpoint(); ServiceEndpoint toPublish = new ServiceEndpoint();
logger.debug("Resource Type: {}", toPublish.type()); logger.debug("Resource Type: {}", toPublish.type());
@ -159,18 +162,17 @@ public class InformationSystemUtils {
toPublish.profile().platform().revisionVersion((short) 0); toPublish.profile().platform().revisionVersion((short) 0);
toPublish.profile().platform().buildVersion((short) 0); toPublish.profile().platform().buildVersion((short) 0);
AccessPoint accessPoint = new AccessPoint(); AccessPoint accessPoint = new AccessPoint();
// String encryptedPassword = String encryptedPassword = StringEncrypter.getEncrypter().encrypt(definitionItem.getSecret());
// StringEncrypter.getEncrypter().encrypt(definitionItem.getSecret());
accessPoint.name(definitionItem.getClientId()); accessPoint.name(definitionItem.getClientId());
accessPoint.description("Keycloak client credentials"); accessPoint.description("Keycloak client credentials");
accessPoint.address(iamService.getAddress()); accessPoint.address(iamService.getAddress());
accessPoint.credentials(definitionItem.getSecret(), definitionItem.getUsername()); accessPoint.credentials(encryptedPassword, definitionItem.getUsername());
toPublish.profile().accessPoints().add(accessPoint); toPublish.profile().accessPoints().add(accessPoint);
logger.debug("Request publish: {}", toPublish); logger.debug("Request publish: {}", toPublish);
try { try {
// AccessTokenProvider.instance.set(token); // AccessTokenProvider.instance.set(token);
@ -191,6 +193,49 @@ public class InformationSystemUtils {
logger.debug("Published on IS"); logger.debug("Published on IS");
} }
public static void deleteSSD(DefinitionItem definitionItem, String category, String scope) throws Exception {
if (definitionItem == null)
throw new Exception("Invalid definition: " + definitionItem);
if (category == null || category.isEmpty())
throw new Exception("Invalid category: " + category);
if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope);
ScopeProvider.instance.set(scope);
// AccessTokenProvider.instance.set(token);
// SecurityTokenProvider.instance.set(token);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'")
.addCondition("$resource/Profile/Name/text() eq '" + definitionItem.getClientId() + "'");
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
if (resources != null && !resources.isEmpty()) {
ServiceEndpoint toDelete=resources.get(0);
logger.debug("Requested delete: {}", toDelete);
try {
RegistryPublisher publisher = RegistryPublisherFactory.create();
publisher.remove(toDelete);
} catch (Exception e) {
logger.error("Error publishing the ssd on IS: {}", e.getLocalizedMessage(), e);
throw e;
} }
logger.debug("Deleted on IS");
} else {
String error="No resources found with name: "+definitionItem.getClientId();
logger.error(error);
throw new Exception(error);
}
}
}

View File

@ -12,6 +12,7 @@ public class Constants {
public static final String DEFAULT_SCOPE = "/gcube"; public static final String DEFAULT_SCOPE = "/gcube";
public static final String DEFAULT_TOKEN = ""; public static final String DEFAULT_TOKEN = "";
public static final String DEFAULT_ROLE = "OrganizationMember"; public static final String DEFAULT_ROLE = "OrganizationMember";
// Session // Session

View File

@ -24,8 +24,7 @@ public class ISTest {
public void findSSD() { public void findSSD() {
try { try {
InformationSystemUtils.retrieveSSD(Constants.SYSTEM_SERVICE_CATEGORY, Constants.DEFAULT_SCOPE, InformationSystemUtils.retrieveSSD(Constants.SYSTEM_SERVICE_CATEGORY, Constants.DEFAULT_SCOPE);
Constants.DEFAULT_TOKEN);
assertTrue(true); assertTrue(true);
} catch (Exception e) { } catch (Exception e) {
@ -41,8 +40,8 @@ public class ISTest {
try { try {
DefinitionItem definitionItem = new DefinitionItem("sample-client-name", "sample-client-description", DefinitionItem definitionItem = new DefinitionItem("sample-client-name", "sample-client-description",
"sample-client-description", "xxxxx-xxxx-xxx-xxxx"); "sample-client-description", "xxxxx-xxxx-xxx-xxxx");
InformationSystemUtils.publishSSD(definitionItem,Constants.SYSTEM_SERVICE_CATEGORY, Constants.DEFAULT_SCOPE, InformationSystemUtils.publishSSD(definitionItem, Constants.SYSTEM_SERVICE_CATEGORY,
Constants.DEFAULT_TOKEN); Constants.DEFAULT_SCOPE);
assertTrue(true); assertTrue(true);
} catch (Exception e) { } catch (Exception e) {
@ -52,4 +51,23 @@ public class ISTest {
} }
@Ignore
public void deleteSSD() {
try {
DefinitionItem definitionItem = new DefinitionItem("sample-client-name", "sample-client-description",
"sample-client-description", "xxxxx-xxxx-xxx-xxxx");
InformationSystemUtils.deleteSSD(definitionItem, Constants.SYSTEM_SERVICE_CATEGORY,
Constants.DEFAULT_SCOPE);
assertTrue(true);
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
assertTrue("Error deleting the resource!", false);
}
}
} }

View File

@ -7,3 +7,4 @@
/howto.txt /howto.txt
/log4j.properties /log4j.properties
/symm.key /symm.key
/.~lock.symm.key#