From 56a718efb2d8dd64b78a2ccd821223c5f4210e14 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 12 Jan 2024 11:29:27 +0200 Subject: [PATCH] UHF-9239: Remove VaultAuthorizer --- helfi_api_base.services.yml | 7 -- src/ApiClient/ApiAuthorizerInterface.php | 20 ------ src/ApiClient/ApiClientBase.php | 17 ----- src/ApiClient/VaultAuthorizer.php | 72 ------------------- .../src/Unit/ApiClient/ApiClientBaseTest.php | 46 ------------ .../Unit/ApiClient/VaultAuthorizerTest.php | 70 ------------------ 6 files changed, 232 deletions(-) delete mode 100644 src/ApiClient/ApiAuthorizerInterface.php delete mode 100644 src/ApiClient/VaultAuthorizer.php delete mode 100644 tests/src/Unit/ApiClient/VaultAuthorizerTest.php diff --git a/helfi_api_base.services.yml b/helfi_api_base.services.yml index 3b3b3a9c..8232b1ee 100644 --- a/helfi_api_base.services.yml +++ b/helfi_api_base.services.yml @@ -151,10 +151,3 @@ services: - '@cache.default' - '@datetime.time' - '@helfi_api_base.environment_resolver' - - helfi_api_base.vault_authorizer: - class: Drupal\helfi_api_base\ApiClient\VaultAuthorizer - abstract: true - arguments: - - '@config.factory' - - '@helfi_api_base.vault_manager' diff --git a/src/ApiClient/ApiAuthorizerInterface.php b/src/ApiClient/ApiAuthorizerInterface.php deleted file mode 100644 index b74e168a..00000000 --- a/src/ApiClient/ApiAuthorizerInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -authorizer?->getAuthorization(); - } - /** * Gets the default request options. * @@ -109,10 +96,6 @@ protected function getRequestOptions(string $environmentName, array $options = [ 'curl' => [CURLOPT_TCP_KEEPALIVE => TRUE], ]; - if ($this->hasAuthorization()) { - $default['headers']['Authorization'] = $this->authorizer?->getAuthorization(); - } - if ($environmentName === 'local') { // Disable SSL verification in local environment. $default['verify'] = FALSE; diff --git a/src/ApiClient/VaultAuthorizer.php b/src/ApiClient/VaultAuthorizer.php deleted file mode 100644 index bd8a7eb6..00000000 --- a/src/ApiClient/VaultAuthorizer.php +++ /dev/null @@ -1,72 +0,0 @@ -getToken()) { - return sprintf('Basic %s', $token); - } - - return NULL; - } - - /** - * Gets the authorization token. - * - * @return string|null - * The authorization token. - */ - private function getToken() : ?string { - if ($authorization = $this->vaultManager->get($this->vaultKey)) { - return $authorization->data(); - } - - // Provide a BC layer to fetch API keys from previously used - // configuration. - // @todo remove this once all projects have migrated to Vault. - if ($this->fallback) { - [$config, $key] = explode(':', $this->fallback); - - return $this->configFactory->get($config)?->get($key); - } - - return NULL; - } - -} diff --git a/tests/src/Unit/ApiClient/ApiClientBaseTest.php b/tests/src/Unit/ApiClient/ApiClientBaseTest.php index 78c46509..c9e1bd38 100644 --- a/tests/src/Unit/ApiClient/ApiClientBaseTest.php +++ b/tests/src/Unit/ApiClient/ApiClientBaseTest.php @@ -93,8 +93,6 @@ private function getTimeMock(int $expectedTime) : ObjectProphecy { * The time prophecy. * @param \Psr\Log\LoggerInterface|null $logger * The logger. - * @param \Drupal\helfi_api_base\ApiClient\ApiAuthorizerInterface|null $authorizer - * The api authorizer. * @param \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface|null $environmentResolver * The environment resolver. * @param array $requestOptions @@ -107,7 +105,6 @@ private function getSut( ClientInterface $client = NULL, TimeInterface $time = NULL, LoggerInterface $logger = NULL, - ApiAuthorizerInterface $authorizer = NULL, EnvironmentResolverInterface $environmentResolver = NULL, array $requestOptions = [], ) : ApiClient { @@ -134,53 +131,10 @@ private function getSut( $time, $environmentResolver, $logger, - $authorizer, $requestOptions, ); } - /** - * Tests authorization. - * - * @covers ::__construct - * @covers ::makeRequest - * @covers ::getRequestOptions - * @covers ::hasAuthorization - * @covers \Drupal\helfi_api_base\ApiClient\ApiResponse::__construct - * @covers \Drupal\helfi_api_base\ApiClient\VaultAuthorizer::__construct - * @covers \Drupal\helfi_api_base\ApiClient\VaultAuthorizer::getAuthorization - * @covers \Drupal\helfi_api_base\ApiClient\VaultAuthorizer::getToken - */ - public function testAuthorization() : void { - $requests = []; - $client = $this->createMockHistoryMiddlewareHttpClient($requests, [ - new Response(200, body: json_encode(['key' => 'value'])), - ]); - - $sut = $this->getSut( - $client, - authorizer: new VaultAuthorizer( - $this->getConfigFactoryStub([]), - new VaultManager([ - new AuthorizationToken('vault_key', '123'), - ]), - 'vault_key', - ), - requestOptions: [ - 'headers' => ['X-Custom-Header' => '1'], - ], - ); - - $sut->exposedMakeRequest('GET', '/foo'); - - $this->assertCount(1, $requests); - // Make sure SSL verification is disabled on local. - $this->assertFalse($requests[0]['options']['verify']); - // Make sure headers are set. - $this->assertEquals('Basic 123', $requests[0]['request']->getHeader('Authorization')[0]); - $this->assertEquals('1', $requests[0]['request']->getHeader('X-Custom-Header')[0]); - } - /** * Test makeRequest(). * diff --git a/tests/src/Unit/ApiClient/VaultAuthorizerTest.php b/tests/src/Unit/ApiClient/VaultAuthorizerTest.php deleted file mode 100644 index f70a8592..00000000 --- a/tests/src/Unit/ApiClient/VaultAuthorizerTest.php +++ /dev/null @@ -1,70 +0,0 @@ -getConfigFactoryStub([]), - $vaultManager, - self::VAULT_MANAGER_KEY, - ); - $this->assertEquals('Basic 123', $sut->getAuthorization()); - } - - /** - * @covers ::__construct - * @covers ::getAuthorization - * @covers ::getToken - */ - public function testEmptyAuthorization() : void { - $sut = new VaultAuthorizer( - $this->getConfigFactoryStub([]), - new VaultManager([]), - self::VAULT_MANAGER_KEY, - ); - $this->assertNull($sut->getAuthorization()); - } - - /** - * @covers ::__construct - * @covers ::getAuthorization - * @covers ::getToken - */ - public function testFallbackConfigAuthorization() : void { - $sut = new VaultAuthorizer( - $this->getConfigFactoryStub(['test_config' => ['key' => '123']]), - new VaultManager([]), - self::VAULT_MANAGER_KEY, - 'test_config:key' - - ); - $this->assertEquals('Basic 123', $sut->getAuthorization()); - } - -}