Skip to content

Commit

Permalink
feat: delete client registration provider linked to env
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Dec 20, 2024
1 parent 36dd104 commit 7b388c6
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -38,4 +39,13 @@ public interface ClientRegistrationProviderRepository extends CrudRepository<Cli
* @throws TechnicalException if something goes wrong
*/
Set<ClientRegistrationProvider> 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<String> deleteByEnvironmentId(String environmentId) throws TechnicalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> deleteByEnvironmentId(String environmentId) throws TechnicalException {
LOGGER.debug("JdbcClientRegistrationProviderRepository.deleteByEnvironmentId({})", environmentId);
try {
List<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ public Set<ClientRegistrationProvider> findAllByEnvironment(String environmentId
return res;
}

@Override
public List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@
public interface ClientRegistrationProviderMongoRepository extends MongoRepository<ClientRegistrationProviderMongo, String> {
@Query("{ 'environmentId': ?0 }")
List<ClientRegistrationProviderMongo> findByEnvironmentId(String environmentId);

@Query(value = "{ 'environmentId': ?0 }", delete = true)
List<ClientRegistrationProviderMongo> deleteByEnvironmentId(String environmentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ClientRegistrationProvider> clientRegistrationProvidersBeforeDeletion = clientRegistrationProviderRepository
.findAllByEnvironment("env-to-delete")
.stream()
.toList();
assertEquals(2, clientRegistrationProvidersBeforeDeletion.size());

List<String> clientRegistrationProvidersDeleted = clientRegistrationProviderRepository.deleteByEnvironmentId("ToBeDeleted");

assertEquals(2, clientRegistrationProvidersDeleted.size());
assertEquals(0, clientRegistrationProviderRepository.findAllByEnvironment("env-to-delete").size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,6 +121,7 @@ public class DeleteEnvironmentCommandHandler implements CommandHandler<DeleteEnv
private final AuditRepository auditRepository;
private final CategoryRepository categoryRepository;
private final CommandRepository commandRepository;
private final ClientRegistrationProviderRepository clientRegistrationProviderRepository;
private final CustomUserFieldsRepository customUserFieldsRepository;
private final DashboardRepository dashboardRepository;
private final DictionaryRepository dictionaryRepository;
Expand Down Expand Up @@ -167,6 +169,7 @@ public DeleteEnvironmentCommandHandler(
@Lazy AsyncJobRepository asyncJobRepository,
@Lazy AuditRepository auditRepository,
@Lazy CategoryRepository categoryRepository,
@Lazy ClientRegistrationProviderRepository clientRegistrationProviderRepository,
@Lazy CommandRepository commandRepository,
@Lazy CustomUserFieldsRepository customUserFieldsRepository,
@Lazy DashboardRepository dashboardRepository,
Expand Down Expand Up @@ -220,6 +223,7 @@ public DeleteEnvironmentCommandHandler(
this.applicationRepository = applicationRepository;
this.auditRepository = auditRepository;
this.categoryRepository = categoryRepository;
this.clientRegistrationProviderRepository = clientRegistrationProviderRepository;
this.commandRepository = commandRepository;
this.customUserFieldsRepository = customUserFieldsRepository;
this.dashboardRepository = dashboardRepository;
Expand Down Expand Up @@ -372,6 +376,7 @@ private void deleteEnvironment(ExecutionContext executionContext, EnvironmentEnt
metadataRepository.deleteByReferenceIdAndReferenceType(environment.getId(), MetadataReferenceType.ENVIRONMENT);
environmentService.delete(environment.getId());
auditRepository.deleteByReferenceIdAndReferenceType(environment.getId(), Audit.AuditReferenceType.ENVIRONMENT);
clientRegistrationProviderRepository.deleteByEnvironmentId(environment.getId());
}

private void deleteApis(ExecutionContext executionContext) throws TechnicalException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public class DeleteCommandHandlerTest {
// Forgot repositories
TicketRepository.class,
QualityRuleRepository.class,
EntrypointRepository.class,
ClientRegistrationProviderRepository.class
EntrypointRepository.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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;
Expand Down Expand Up @@ -283,6 +284,9 @@ public class DeleteEnvironmentCommandHandlerTest {
@Mock
private SearchEngineService searchEngineService;

@Mock
private ClientRegistrationProviderRepository clientRegistrationProviderRepository;

private DeleteEnvironmentCommandHandler cut;

@Before
Expand Down Expand Up @@ -348,6 +352,7 @@ public void setUp() throws Exception {
asyncJobRepository,
auditRepository,
categoryRepository,
clientRegistrationProviderRepository,
commandRepository,
customUserFieldsRepository,
dashboardRepository,
Expand Down Expand Up @@ -470,6 +475,7 @@ public void should_delete_environment() throws TechnicalException {
verify(metadataRepository).deleteByReferenceIdAndReferenceType(ENV_ID, MetadataReferenceType.ENVIRONMENT);
verify(scoringRulesetRepository).deleteByReferenceId(ENV_ID, "ENVIRONMENT");
verify(environmentService).delete(ENV_ID);
verify(clientRegistrationProviderRepository).deleteByEnvironmentId(ENV_ID);
}

private void verifyDeleteApplications(ExecutionContext executionContext) throws TechnicalException {
Expand Down

0 comments on commit 7b388c6

Please sign in to comment.