From c090c8a202da6ae30e34ecd19f14fe7e679cdb19 Mon Sep 17 00:00:00 2001 From: MDeLuise <66636702+MDeLuise@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:30:54 +0200 Subject: [PATCH] fix(api): throw `KapuaIllegalArgument` for non-KapuaConfigurableService pid Update `ServiceConfigurations` methods to throw `KapuaIllegalArgument` when the provided pid is not a `KapuaConfigurableService`. --- .../v1/resources/ServiceConfigurations.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java index 957bb4db818..ff2647cd27a 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java @@ -29,6 +29,7 @@ import org.eclipse.kapua.KapuaEntityNotFoundException; import org.eclipse.kapua.KapuaException; +import org.eclipse.kapua.KapuaIllegalArgumentException; import org.eclipse.kapua.app.api.core.model.ScopeId; import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource; import org.eclipse.kapua.commons.configuration.metatype.EmptyTocd; @@ -86,10 +87,11 @@ public Response update( } for (ServiceComponentConfiguration serviceComponentConfiguration : serviceConfiguration.getComponentConfigurations()) { Class configurableServiceClass = (Class) Class.forName(serviceComponentConfiguration.getId()).asSubclass(KapuaService.class); - if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { - KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); - configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties()); + if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { + throw new KapuaIllegalArgumentException("serviceComponentConfiguration.id", serviceComponentConfiguration.getId()); } + KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); + configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties()); } return Response.noContent().build(); } @@ -102,17 +104,18 @@ public ServiceComponentConfiguration getComponent( @PathParam("serviceId") String serviceId ) throws KapuaException, ClassNotFoundException { Class configurableServiceClass = (Class) Class.forName(serviceId).asSubclass(KapuaService.class); - if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { - KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); - KapuaTocd metadata = configurableService.getConfigMetadata(scopeId); - Map values = configurableService.getConfigValues(scopeId); - if (metadata != null && !(metadata instanceof EmptyTocd)) { - ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId()); - serviceComponentConfiguration.setDefinition(metadata); - serviceComponentConfiguration.setName(metadata.getName()); - serviceComponentConfiguration.setProperties(values); - return serviceComponentConfiguration; - } + if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { + throw new KapuaIllegalArgumentException("service.pid", serviceId); + } + KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); + KapuaTocd metadata = configurableService.getConfigMetadata(scopeId); + Map values = configurableService.getConfigValues(scopeId); + if (metadata != null && !(metadata instanceof EmptyTocd)) { + ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId()); + serviceComponentConfiguration.setDefinition(metadata); + serviceComponentConfiguration.setName(metadata.getName()); + serviceComponentConfiguration.setProperties(values); + return serviceComponentConfiguration; } return null; } @@ -131,10 +134,11 @@ public Response updateComponent( throw new KapuaEntityNotFoundException(Account.TYPE, scopeId); } Class configurableServiceClass = (Class) Class.forName(serviceId).asSubclass(KapuaService.class); - if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { - KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); - configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties()); + if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) { + throw new KapuaIllegalArgumentException("service.pid", serviceId); } + KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass); + configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties()); return Response.noContent().build(); }