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 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
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\DisableClients;
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, [DisableClients::class, 'handle']);
}
}
56 changes: 56 additions & 0 deletions app/Keycloak/Listeners/DisableClients.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 DisableClients 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 disabled', [
'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 disable Keycloak client(s)', [
'integration_id' => $integrationBlocked->id->toString(),
'exception' => $throwable,
]);
}
}
93 changes: 93 additions & 0 deletions tests/Keycloak/Listeners/DisableClientTest.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\DisableClients;
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 DisableClientTest extends TestCase
{
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_disable_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 disabled', [
'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 DisableClients(
$integrationRepository,
$keycloakClientRepository,
$this->apiClient,
$this->logger
);

$createClients->handle(new IntegrationBlocked($this->integration->id));
}
}
13 changes: 13 additions & 0 deletions tests/Keycloak/Service/KeycloakApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Keycloak\Exception\KeyCloakApiFailed;
use App\Keycloak\Realm;
use App\Keycloak\RealmCollection;
use App\Keycloak\ScopeConfig;
use App\Keycloak\Service\KeycloakApiClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
Expand All @@ -33,6 +34,7 @@ final class KeycloakApiClientTest extends TestCase
private Realm $realm;
private Integration $integration;
private LoggerInterface&MockObject $logger;
private ScopeConfig $scopeConfig;

protected function setUp(): void
{
Expand All @@ -47,6 +49,12 @@ protected function setUp(): void
$this->realm = new Realm('uitidpoc', 'Acceptance');
$this->integration = $this->givenThereIsAnIntegration(Uuid::fromString(self::INTEGRATION_ID));
$this->logger = $this->createMock(LoggerInterface::class);
$this->scopeConfig = new ScopeConfig(
Uuid::fromString('824c09c0-2f3a-4fa0-bde2-8bf25c9a5b74'),
Uuid::fromString('d8a54568-26da-412b-a441-d5e2fad84478'),
Uuid::fromString('123ae05d-1c41-40c8-8716-c4654a3bfd98'),
Uuid::fromString('0743b1c7-0ea2-46af-906e-fbb6c0317514'),
);
}

public function test_can_create_client(): void
Expand All @@ -58,6 +66,7 @@ public function test_can_create_client(): void

$apiClient = new KeycloakApiClient(
$this->givenKeycloakHttpClient($this->logger, $mock),
$this->scopeConfig,
$this->logger
);

Expand All @@ -78,6 +87,7 @@ public function test_fails_to_create_client(): void

$apiClient = new KeycloakApiClient(
$this->givenKeycloakHttpClient($this->logger, $mock),
$this->scopeConfig,
$this->logger
);

Expand All @@ -102,6 +112,7 @@ public function test_fails_to_add_scope_to_client(): void

$apiClient = new KeycloakApiClient(
$this->givenKeycloakHttpClient($this->logger, $mock),
$this->scopeConfig,
$this->logger
);

Expand Down Expand Up @@ -135,6 +146,7 @@ public function test_can_fetch_client(): void

$apiClient = new KeycloakApiClient(
$this->givenKeycloakHttpClient($this->logger, $mock),
$this->scopeConfig,
$this->logger
);

Expand All @@ -158,6 +170,7 @@ public function test_client_not_found(): void

$apiClient = new KeycloakApiClient(
$this->givenKeycloakHttpClient($this->logger, $mock),
$this->scopeConfig,
$this->logger
);

Expand Down
Loading