system-service-definition-p.../src/main/java/org/gcube/portlets/admin/systemservicedefinition/is/InformationSystemUtils.java

292 lines
9.9 KiB
Java
Raw Normal View History

2021-10-12 19:01:09 +02:00
package org.gcube.portlets.admin.systemservicedefinition.is;
2021-10-13 19:05:54 +02:00
import java.util.ArrayList;
2021-10-12 19:01:09 +02:00
import java.util.List;
2021-10-13 19:05:54 +02:00
import org.gcube.common.encryption.encrypter.StringEncrypter;
2021-10-12 19:01:09 +02:00
import org.gcube.common.resources.gcore.ServiceEndpoint;
2021-10-13 19:05:54 +02:00
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
2021-10-12 19:01:09 +02:00
import org.gcube.common.scope.api.ScopeProvider;
2021-10-13 19:05:54 +02:00
import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.portlets.admin.systemservicedefinition.definition.DefinitionItem;
2021-10-14 15:10:05 +02:00
import org.gcube.portlets.admin.systemservicedefinition.shared.Constants;
2021-10-12 19:01:09 +02:00
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-10-14 15:10:05 +02:00
/**
*
* @author Giancarlo Panichi
*
*/
2021-10-12 19:01:09 +02:00
public class InformationSystemUtils {
private static Logger logger = LoggerFactory.getLogger(InformationSystemUtils.class);
2021-10-14 17:19:55 +02:00
public static IAMService retrieveIAMService(String scope) throws Exception {
2021-10-14 15:10:05 +02:00
try {
logger.debug("Retrieve IAM Service on IS");
if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope);
2021-10-14 17:19:55 +02:00
/*
* if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* scope);
*/
2021-10-14 15:10:05 +02:00
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 '" + Constants.IAM_SERVICE_CATEGORY + "'")
.addCondition("$resource/Profile/Name/text() eq '" + Constants.IAM_SERVICE_NAME + "'");
2021-10-14 17:19:55 +02:00
2021-10-14 15:10:05 +02:00
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
2021-10-14 17:19:55 +02:00
IAMService iamService = null;
if (resources != null && !resources.isEmpty()) {
ServiceEndpoint iams = resources.get(0);
iamService = new IAMService(iams.profile().runtime().hostedOn());
2021-10-14 15:10:05 +02:00
}
2021-10-14 17:19:55 +02:00
logger.debug("IAM Services retrieved: {}", iamService);
2021-10-14 15:10:05 +02:00
return iamService;
} catch (Throwable e) {
logger.error("Error in discovery IAM Service Endpoint in scope: " + scope);
logger.error(e.getLocalizedMessage(), e);
throw e;
}
}
2021-10-15 18:43:22 +02:00
public static void checkSSDPresent(DefinitionItem definitionItem, String category, String scope)
throws Exception {
try {
if (definitionItem == null || definitionItem.getClientId() == null
|| definitionItem.getClientId().isEmpty())
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);
for(ServiceEndpoint ssd: resources) {
if (definitionItem.getClientId().compareTo(ssd.profile().name()) == 0) {
StringBuilder error=new StringBuilder();
error.append("System Service ");
error.append(definitionItem.getClientId());
error.append(" already present in the scope: ");
error.append(scope);
logger.error(error.toString());
throw new Exception(error.toString());
}
}
logger.debug("System Service not already present on IS in scope: "+scope);
return ;
} catch (Throwable e) {
logger.error(e.getLocalizedMessage(), e);
throw e;
}
}
2021-10-14 17:19:55 +02:00
public static ArrayList<DefinitionItem> retrieveSSD(String category, String scope) throws Exception {
2021-10-13 19:05:54 +02:00
2021-10-12 19:01:09 +02:00
try {
logger.debug("Retrieve System Services Definition on IS");
2021-10-15 18:43:22 +02:00
if (category == null || category.isEmpty())
throw new Exception("Invalid category: " + category);
2021-10-12 19:01:09 +02:00
if (scope == null || scope.isEmpty())
throw new Exception("Invalid scope: " + scope);
2021-10-14 17:19:55 +02:00
/*
* if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* scope);
*/
2021-10-12 19:01:09 +02:00
ScopeProvider.instance.set(scope);
2021-10-13 19:05:54 +02:00
// AccessTokenProvider.instance.set(token);
// SecurityTokenProvider.instance.set(token);
2021-10-12 19:01:09 +02:00
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
logger.debug("Retrieved: " + resources);
2021-10-13 19:05:54 +02:00
ArrayList<DefinitionItem> ssdList = new ArrayList<DefinitionItem>();
for (ServiceEndpoint se : resources) {
2021-10-14 15:10:05 +02:00
String username = null;
String password = null;
2021-10-13 19:05:54 +02:00
for (AccessPoint accessPoint : se.profile().accessPoints()) {
2021-10-14 15:10:05 +02:00
username = accessPoint.username();
2021-10-13 19:05:54 +02:00
String decryptedSecret = StringEncrypter.getEncrypter().decrypt(accessPoint.password());
2021-10-14 15:10:05 +02:00
password = decryptedSecret;
2021-10-13 19:05:54 +02:00
break;
}
2021-10-14 15:10:05 +02:00
DefinitionItem definitionItem = new DefinitionItem(se.profile().name(), se.profile().description(),
username, password);
2021-10-13 19:05:54 +02:00
ssdList.add(definitionItem);
}
logger.debug("SSDList: {}", ssdList);
return ssdList;
2021-10-12 19:01:09 +02:00
} catch (Throwable e) {
logger.error("Error in discovery System Services Endpoint in scope: " + scope);
logger.error(e.getLocalizedMessage(), e);
throw e;
}
}
2021-10-14 17:19:55 +02:00
public static void publishSSD(DefinitionItem definitionItem, String category, String scope) throws Exception {
2021-10-13 19:05:54 +02:00
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);
2021-10-14 17:19:55 +02:00
/*
* if (token == null || token.isEmpty()) throw new Exception("Invalid token: " +
* token);
*/
2021-10-15 18:43:22 +02:00
checkSSDPresent(definitionItem, category, scope);
2021-10-14 17:19:55 +02:00
IAMService iamService = retrieveIAMService(scope);
2021-10-13 19:05:54 +02:00
ServiceEndpoint toPublish = new ServiceEndpoint();
2021-10-14 17:19:55 +02:00
logger.debug("Resource Type: {}", toPublish.type());
2021-10-14 15:10:05 +02:00
/*
* List<String> scopes = new ArrayList<String>(); scopes.add(scope);
* Collection<String> col = toPublish.scopes().asCollection();
* col.addAll(scopes);
*/
2021-10-14 17:19:55 +02:00
2021-10-13 19:05:54 +02:00
toPublish.newProfile();
toPublish.profile().name(definitionItem.getClientId());
2021-10-14 15:10:05 +02:00
toPublish.profile().description(definitionItem.getDescription());
2021-10-13 19:05:54 +02:00
toPublish.profile().category(category);
2021-10-14 15:10:05 +02:00
toPublish.profile().version("1.0.0");
2021-10-14 17:19:55 +02:00
2021-10-14 15:10:05 +02:00
toPublish.profile().newRuntime();
toPublish.profile().runtime().ghnId("");
toPublish.profile().runtime().status("READY");
toPublish.profile().runtime().hostedOn("d4science.org");
2021-10-14 17:19:55 +02:00
2021-10-14 15:10:05 +02:00
toPublish.profile().newPlatform();
toPublish.profile().platform().name("d4science");
2021-10-13 19:05:54 +02:00
toPublish.profile().platform().version((short) 0);
toPublish.profile().platform().minorVersion((short) 0);
toPublish.profile().platform().revisionVersion((short) 0);
toPublish.profile().platform().buildVersion((short) 0);
2021-10-14 17:19:55 +02:00
2021-10-14 15:10:05 +02:00
AccessPoint accessPoint = new AccessPoint();
2021-10-13 19:05:54 +02:00
2021-10-14 17:19:55 +02:00
String encryptedPassword = StringEncrypter.getEncrypter().encrypt(definitionItem.getSecret());
2021-10-14 15:10:05 +02:00
accessPoint.name(definitionItem.getClientId());
accessPoint.description("Keycloak client credentials");
accessPoint.address(iamService.getAddress());
2021-10-14 17:19:55 +02:00
accessPoint.credentials(encryptedPassword, definitionItem.getUsername());
2021-10-14 15:10:05 +02:00
toPublish.profile().accessPoints().add(accessPoint);
2021-10-14 17:19:55 +02:00
2021-10-13 19:05:54 +02:00
logger.debug("Request publish: {}", toPublish);
2021-10-14 17:19:55 +02:00
2021-10-13 19:05:54 +02:00
try {
2021-10-14 17:19:55 +02:00
2021-10-13 19:05:54 +02:00
// AccessTokenProvider.instance.set(token);
2021-10-14 17:19:55 +02:00
// SecurityTokenProvider.instance.set(token);
2021-10-14 15:10:05 +02:00
ScopeProvider.instance.set(scope);
2021-10-14 17:19:55 +02:00
2021-10-13 19:05:54 +02:00
RegistryPublisher publisher = RegistryPublisherFactory.create();
String id = publisher.create(toPublish).id();
logger.debug("Created new RR sent, Got from publisher: id=" + id);
// ScopedPublisher sp=RegistryPublisherFactory.scopedPublisher();
// toPublish = sp.create(toPublish,scopes);
} catch (Exception e) {
logger.error("Error publishing the ssd on IS: {}", e.getLocalizedMessage(), e);
throw e;
}
logger.debug("Published on IS");
}
2021-10-14 15:10:05 +02:00
2021-10-14 17:19:55 +02:00
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);
2021-10-14 15:10:05 +02:00
2021-10-14 17:19:55 +02:00
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()) {
2021-10-15 18:43:22 +02:00
ServiceEndpoint toDelete = resources.get(0);
2021-10-14 17:19:55 +02:00
logger.debug("Requested delete: {}", toDelete);
2021-10-15 18:43:22 +02:00
2021-10-14 17:19:55 +02:00
try {
RegistryPublisher publisher = RegistryPublisherFactory.create();
publisher.remove(toDelete);
2021-10-15 18:43:22 +02:00
2021-10-14 17:19:55 +02:00
} catch (Exception e) {
logger.error("Error publishing the ssd on IS: {}", e.getLocalizedMessage(), e);
throw e;
}
logger.debug("Deleted on IS");
} else {
2021-10-15 18:43:22 +02:00
String error = "No resources found with name: " + definitionItem.getClientId();
2021-10-14 17:19:55 +02:00
logger.error(error);
throw new Exception(error);
}
}
}