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-438 Block integration #1123

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions app/Keycloak/KeycloakServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace App\Keycloak;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Events\IntegrationCreated;
use App\Domain\Integrations\Events\IntegrationUpdated;
use App\Keycloak\Client\KeycloakHttpClient;
use App\Keycloak\Listeners\BlockClients;
use App\Keycloak\Listeners\CreateClients;
use App\Keycloak\Listeners\UpdateClients;
use App\Keycloak\Repositories\EloquentKeycloakClientRepository;
Expand Down Expand Up @@ -82,5 +84,6 @@ private function bootstrapEventHandling(): void

Event::listen(IntegrationCreated::class, [CreateClients::class, 'handle']);
Event::listen(IntegrationUpdated::class, [UpdateClients::class, 'handle']);
Event::listen(IntegrationBlocked::class, [BlockClients::class, 'handle']);
grubolsch marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 56 additions & 0 deletions app/Keycloak/Listeners/BlockClients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace App\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Keycloak\Exception\KeyCloakApiFailed;
use App\Keycloak\Repositories\KeycloakClientRepository;
use App\Keycloak\Service\ApiClient;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Psr\Log\LoggerInterface;
use Throwable;

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
{
$integration = $this->integrationRepository->getById($integrationBlocked->id);
$keycloakClients = $this->keycloakClientRepository->getByIntegrationId($integrationBlocked->id);

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

$this->logger->info('Keycloak client blocked', [
'integration_id' => $integration->id->toString(),
'client_id' => $keycloakClient->id->toString(),
'realm' => $keycloakClient->realm->internalName,
]);
} catch (KeyCloakApiFailed $e) {
$this->failed($integrationBlocked, $e);
}
}
}

public function failed(IntegrationBlocked $integrationBlocked, Throwable $throwable): void
{
$this->logger->error('Failed to block Keycloak client(s)', [
'integration_id' => $integrationBlocked->id->toString(),
'exception' => $throwable,
]);
}
}
93 changes: 93 additions & 0 deletions tests/Keycloak/Listeners/BlockClientsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Tests\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Integration;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Keycloak\Client;
use App\Keycloak\Config;
use App\Keycloak\Listeners\BlockClients;
use App\Keycloak\RealmCollection;
use App\Keycloak\Repositories\KeycloakClientRepository;
use App\Keycloak\Service\ApiClient;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Tests\IntegrationHelper;
use Tests\Keycloak\KeycloakHelper;

final class BlockClientsTest extends TestCase
grubolsch marked this conversation as resolved.
Show resolved Hide resolved
{
use IntegrationHelper;
use KeycloakHelper;

private const SECRET = 'my-secret';

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

protected function setUp(): void
{
$this->config = new Config(
true,
'https://example.com/',
'client_name',
self::SECRET,
RealmCollection::getRealms(),
);

// 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
{
$integrationRepository = $this->createMock(IntegrationRepository::class);
$integrationRepository->expects($this->once())
->method('getById')
->with($this->integration->id)
->willReturn($this->integration);

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

$this->apiClient->expects($this->once())
->method('disableClient')
->with($client);

$this->logger->expects($this->once())
->method('info')
->with('Keycloak client blocked', [
'integration_id' => $this->integration->id->toString(),
'client_id' => $client->id->toString(),
'realm' => $client->realm->internalName,
]);

$clients[] = $client;
}

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

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

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