From 7b388c6547a5dc8f980ddd7fd504d07952110efa Mon Sep 17 00:00:00 2001 From: Guillaume Cusnieux Date: Fri, 20 Dec 2024 15:08:53 +0100 Subject: [PATCH] feat: delete client registration provider linked to env --- .../ClientRegistrationProviderRepository.java | 10 +++++++ ...cClientRegistrationProviderRepository.java | 29 +++++++++++++++++++ ...oClientRegistrationProviderRepository.java | 17 +++++++++++ ...ntRegistrationProviderMongoRepository.java | 3 ++ ...entRegistrationProviderRepositoryTest.java | 17 +++++++++++ .../clientRegistrationProviders.json | 28 ++++++++++++++++++ .../DeleteEnvironmentCommandHandler.java | 5 ++++ .../handler/DeleteCommandHandlerTest.java | 3 +- .../DeleteEnvironmentCommandHandlerTest.java | 6 ++++ 9 files changed, 116 insertions(+), 2 deletions(-) diff --git a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ClientRegistrationProviderRepository.java b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ClientRegistrationProviderRepository.java index 03be5154aba..2a4dca31355 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ClientRegistrationProviderRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ClientRegistrationProviderRepository.java @@ -17,6 +17,7 @@ import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.management.model.ClientRegistrationProvider; +import java.util.List; import java.util.Set; /** @@ -38,4 +39,13 @@ public interface ClientRegistrationProviderRepository extends CrudRepository findAllByEnvironment(String environmentId) throws TechnicalException; + + /** + * Delete client registration providers by environment + * + * @param environmentId The environment ID + * @return List of deleted IDs for client registration providers + * @throws TechnicalException + */ + List deleteByEnvironmentId(String environmentId) throws TechnicalException; } diff --git a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcClientRegistrationProviderRepository.java b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcClientRegistrationProviderRepository.java index 514bf41f52a..e821adc37ef 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcClientRegistrationProviderRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcClientRegistrationProviderRepository.java @@ -181,4 +181,33 @@ public void delete(String id) throws TechnicalException { jdbcTemplate.update("delete from " + CLIENT_REGISTRATION_PROVIDER_SCOPES + " where client_registration_provider_id = ?", id); jdbcTemplate.update(getOrm().getDeleteSql(), id); } + + @Override + public List deleteByEnvironmentId(String environmentId) throws TechnicalException { + LOGGER.debug("JdbcClientRegistrationProviderRepository.deleteByEnvironmentId({})", environmentId); + try { + List rows = jdbcTemplate.queryForList( + "select id from " + tableName + " where environment_id = ?", + String.class, + environmentId + ); + + if (!rows.isEmpty()) { + jdbcTemplate.update( + "delete from " + + CLIENT_REGISTRATION_PROVIDER_SCOPES + + " where client_registration_provider_id IN (" + + getOrm().buildInClause(rows) + + ")", + rows.toArray() + ); + jdbcTemplate.update("delete from " + tableName + " where environment_id = ?", environmentId); + } + + LOGGER.debug("JdbcClientRegistrationProviderRepository.deleteByEnvironmentId({})", environmentId); + return rows; + } catch (Exception ex) { + throw new TechnicalException("Failed to delete client registration providers by environment", ex); + } + } } diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoClientRegistrationProviderRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoClientRegistrationProviderRepository.java index 25ed08fb902..d26a57c59d4 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoClientRegistrationProviderRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoClientRegistrationProviderRepository.java @@ -142,6 +142,23 @@ public Set findAllByEnvironment(String environmentId return res; } + @Override + public List deleteByEnvironmentId(String environmentId) throws TechnicalException { + LOGGER.debug("Delete client registration providers by environment [{}]", environmentId); + try { + final var subscriptionMongos = internalClientRegistrationProviderRepository + .deleteByEnvironmentId(environmentId) + .stream() + .map(ClientRegistrationProviderMongo::getId) + .toList(); + LOGGER.debug("Delete client registration providers by environment [{}] - Done", environmentId); + return subscriptionMongos; + } catch (Exception e) { + LOGGER.error("Failed to delete client registration providers by environment [{}]", environmentId, e); + throw new TechnicalException("Failed to delete client registration providers by environment"); + } + } + private ClientRegistrationProvider map(ClientRegistrationProviderMongo provider) { return (provider == null) ? null : mapper.map(provider); } diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/application/ClientRegistrationProviderMongoRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/application/ClientRegistrationProviderMongoRepository.java index be1099a34fe..2184fbb60bc 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/application/ClientRegistrationProviderMongoRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/application/ClientRegistrationProviderMongoRepository.java @@ -29,4 +29,7 @@ public interface ClientRegistrationProviderMongoRepository extends MongoRepository { @Query("{ 'environmentId': ?0 }") List findByEnvironmentId(String environmentId); + + @Query(value = "{ 'environmentId': ?0 }", delete = true) + List deleteByEnvironmentId(String environmentId); } diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ClientRegistrationProviderRepositoryTest.java b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ClientRegistrationProviderRepositoryTest.java index c90e12092a3..71a9e0cea22 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ClientRegistrationProviderRepositoryTest.java +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ClientRegistrationProviderRepositoryTest.java @@ -18,9 +18,12 @@ import static io.gravitee.repository.utils.DateUtils.compareDate; import static org.junit.Assert.*; +import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.management.model.ClientRegistrationProvider; +import io.gravitee.repository.management.model.Subscription; import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.Optional; import java.util.Set; import org.junit.Assert; @@ -226,4 +229,18 @@ public void shouldFindAllByEnvironment() throws Exception { assertNotNull(clientRegistrationProviders); assertEquals(2, clientRegistrationProviders.size()); } + + @Test + public void should_delete_by_environment_id() throws TechnicalException { + List clientRegistrationProvidersBeforeDeletion = clientRegistrationProviderRepository + .findAllByEnvironment("env-to-delete") + .stream() + .toList(); + assertEquals(2, clientRegistrationProvidersBeforeDeletion.size()); + + List clientRegistrationProvidersDeleted = clientRegistrationProviderRepository.deleteByEnvironmentId("ToBeDeleted"); + + assertEquals(2, clientRegistrationProvidersDeleted.size()); + assertEquals(0, clientRegistrationProviderRepository.findAllByEnvironment("env-to-delete").size()); + } } diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/clientregistrationprovider-tests/clientRegistrationProviders.json b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/clientregistrationprovider-tests/clientRegistrationProviders.json index 45b1aa60390..5634a4e2104 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/clientregistrationprovider-tests/clientRegistrationProviders.json +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/clientregistrationprovider-tests/clientRegistrationProviders.json @@ -39,5 +39,33 @@ "renewClientSecretMethod": "POST", "createdAt": 1000000000000, "updatedAt": 1486771200000 + }, + { + "id": "env-to-delete", + "environmentId": "ToBeDeleted", + "name": "OIDC-2", + "description": "OIDC Client registration provider", + "discoveryEndpoint": "http://localhost:8092/oidc/.well-known/openid-configuration", + "initialAccessTokenType": "CLIENT_CREDENTIALS", + "clientId": "my-client-id", + "clientSecret": "my-client-secret", + "createdAt": 1000000000000, + "updatedAt": 1486771200000 + }, + { + "id": "env-to-delete-2", + "environmentId": "ToBeDeleted", + "name": "OIDC-3", + "description": "OIDC Client registration provider", + "discoveryEndpoint": "http://localhost:8092/oidc/.well-known/openid-configuration", + "initialAccessTokenType": "CLIENT_CREDENTIALS", + "clientId": "my-client-id", + "clientSecret": "my-client-secret", + "scopes": ["scope1", "scope2", "scope3"], + "renewClientSecretSupport": true, + "renewClientSecretEndpoint": "http://localhost/endpoint", + "renewClientSecretMethod": "POST", + "createdAt": 1000000000000, + "updatedAt": 1486771200000 } ] \ No newline at end of file diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteEnvironmentCommandHandler.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteEnvironmentCommandHandler.java index 5efd1ac10d9..c358e3628e6 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteEnvironmentCommandHandler.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteEnvironmentCommandHandler.java @@ -34,6 +34,7 @@ import io.gravitee.repository.management.api.AsyncJobRepository; import io.gravitee.repository.management.api.AuditRepository; import io.gravitee.repository.management.api.CategoryRepository; +import io.gravitee.repository.management.api.ClientRegistrationProviderRepository; import io.gravitee.repository.management.api.CommandRepository; import io.gravitee.repository.management.api.CustomUserFieldsRepository; import io.gravitee.repository.management.api.DashboardRepository; @@ -120,6 +121,7 @@ public class DeleteEnvironmentCommandHandler implements CommandHandler