Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPF-523 Deleting integration should block keycloak clients #1211

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Keycloak/KeycloakServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Events\IntegrationCreated;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Domain\Integrations\Events\IntegrationUnblocked;
use App\Domain\Integrations\Events\IntegrationUpdated;
use App\Domain\Integrations\Events\IntegrationUrlCreated;
Expand Down Expand Up @@ -74,6 +75,7 @@ private function bootstrapEventHandling(): void
Event::listen(IntegrationUpdated::class, [UpdateClients::class, 'handle']);
Event::listen(IntegrationBlocked::class, [BlockClients::class, 'handle']);
Event::listen(IntegrationUnblocked::class, [UnblockClients::class, 'handle']);
Event::listen(IntegrationDeleted::class, [BlockClients::class, 'handle']);

Event::listen(MissingClientsDetected::class, [CreateClients::class, 'handleCreatingMissingClients']);

Expand Down
10 changes: 4 additions & 6 deletions app/Keycloak/Listeners/BlockClients.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Keycloak\Client\ApiClient;
use App\Keycloak\Exception\KeyCloakApiFailed;
use App\Keycloak\Repositories\KeycloakClientRepository;
Expand All @@ -19,24 +19,22 @@ final class BlockClients implements ShouldQueue
use Queueable;

public function __construct(
private readonly IntegrationRepository $integrationRepository,
private readonly KeycloakClientRepository $keycloakClientRepository,
private readonly ApiClient $client,
private readonly LoggerInterface $logger
) {
}

public function handle(IntegrationBlocked $integrationBlocked): void
public function handle(IntegrationBlocked|IntegrationDeleted $integrationBlocked): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a more generic name for the parameter?

{
$integration = $this->integrationRepository->getById($integrationBlocked->id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: it's fine like it is now, but there is a getByIdWithTrashed to get deleted integration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, thank you for that info, but we also don't use anything from the integration except the id so fetching it doesn't have any added value.

$keycloakClients = $this->keycloakClientRepository->getByIntegrationId($integrationBlocked->id);

foreach ($keycloakClients as $keycloakClient) {
try {
$this->client->blockClient($keycloakClient);

$this->logger->info('Keycloak client blocked', [
'integration_id' => $integration->id->toString(),
'integration_id' => $integrationBlocked->id->toString(),
'client_id' => $keycloakClient->id->toString(),
'environment' => $keycloakClient->environment->value,
]);
Expand All @@ -46,7 +44,7 @@ public function handle(IntegrationBlocked $integrationBlocked): void
}
}

public function failed(IntegrationBlocked $integrationBlocked, Throwable $throwable): void
public function failed(IntegrationBlocked|IntegrationDeleted $integrationBlocked, Throwable $throwable): void
{
$this->logger->error('Failed to block Keycloak client(s)', [
'integration_id' => $integrationBlocked->id->toString(),
Expand Down
41 changes: 20 additions & 21 deletions tests/Keycloak/Listeners/BlockClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Tests\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Integration;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Keycloak\Client;
use App\Keycloak\Client\ApiClient;
use App\Keycloak\Listeners\BlockClients;
Expand All @@ -23,39 +22,32 @@ final class BlockClientsTest extends TestCase
{
use CreatesIntegration;
use KeycloakHttpClientFactory;


use RealmFactory;

private const SECRET = 'my-secret';
private const INTEGRATION_ID = '3f2c8aa3-6d5d-4a72-ba41-ab26bc8e591d';

private Integration $integration;
private ApiClient&MockObject $apiClient;
private LoggerInterface&MockObject $logger;

protected function setUp(): void
{
parent::setUp();

// This is a search API integration
$this->integration = $this->givenThereIsAnIntegration(Uuid::uuid4());

$this->apiClient = $this->createMock(ApiClient::class);
$this->logger = $this->createMock(LoggerInterface::class);
}

public function test_block_clients_when_integration_is_blocked(): void
/**
* @dataProvider differentWaysToBlockClients
*/
public function test_block_clients_when_integration_is_blocked_or_deleted(IntegrationBlocked|IntegrationDeleted $event): void
{
$integrationRepository = $this->createMock(IntegrationRepository::class);
$integrationRepository->expects($this->once())
->method('getById')
->with($this->integration->id)
->willReturn($this->integration);
$integrationId = Uuid::fromString(self::INTEGRATION_ID);

$clients = [];
foreach ($this->givenAllRealms()
as $realm) {
$client = new Client(Uuid::uuid4(), $this->integration->id, Uuid::uuid4()->toString(), self::SECRET, $realm->environment);
foreach ($this->givenAllRealms() as $realm) {
$client = new Client(Uuid::uuid4(), $integrationId, Uuid::uuid4()->toString(), self::SECRET, $realm->environment);

$clients[$client->id->toString()] = $client;
}
Expand All @@ -73,22 +65,29 @@ public function test_block_clients_when_integration_is_blocked(): void
$this->assertArrayHasKey('integration_id', $options);
$this->assertArrayHasKey('environment', $options);

$this->assertEquals($this->integration->id->toString(), $options['integration_id']);
$this->assertEquals(self::INTEGRATION_ID, $options['integration_id']);
});

$keycloakClientRepository = $this->createMock(KeycloakClientRepository::class);
$keycloakClientRepository->expects($this->once())
->method('getByIntegrationId')
->with($this->integration->id)
->with($integrationId)
->willReturn($clients);

$createClients = new BlockClients(
$integrationRepository,
$keycloakClientRepository,
$this->apiClient,
$this->logger
);

$createClients->handle(new IntegrationBlocked($this->integration->id));
$createClients->handle($event);
}

public static function differentWaysToBlockClients(): array
{
return [
[new IntegrationBlocked(Uuid::fromString(self::INTEGRATION_ID))],
[new IntegrationDeleted(Uuid::fromString(self::INTEGRATION_ID))],
];
}
}
Loading