-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from keboola/roman-pst-2407-extra-akv-client
PST-2407: Add extra AKV client
- Loading branch information
Showing
14 changed files
with
1,071 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\ObjectEncryptor\Temporary; | ||
|
||
use Closure; | ||
use Retry\Policy\SimpleRetryPolicy; | ||
use Retry\RetryContextInterface; | ||
|
||
class CallbackRetryPolicy extends SimpleRetryPolicy | ||
{ | ||
private Closure $shouldRetryCallback; | ||
|
||
public function __construct( | ||
callable $shouldRetryCallback, | ||
int $maxAttempts = 3, | ||
) { | ||
parent::__construct($maxAttempts); | ||
$this->shouldRetryCallback = $shouldRetryCallback(...); | ||
} | ||
|
||
public function canRetry(RetryContextInterface $context): bool | ||
{ | ||
$e = $context->getLastException(); | ||
|
||
if (($this->shouldRetryCallback)($e, $context) !== true) { | ||
return false; | ||
} | ||
|
||
return parent::canRetry($context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\ObjectEncryptor\Temporary; | ||
|
||
use Keboola\AzureKeyVaultClient\Authentication\AuthenticatorFactory; | ||
use Keboola\AzureKeyVaultClient\Authentication\AuthenticatorInterface; | ||
use Keboola\AzureKeyVaultClient\Exception\ClientException; | ||
use Keboola\AzureKeyVaultClient\GuzzleClientFactory; | ||
|
||
class TransAuthenticatorFactory extends AuthenticatorFactory | ||
{ | ||
public function getAuthenticator(GuzzleClientFactory $clientFactory, string $resource): AuthenticatorInterface | ||
{ | ||
$authenticator = new TransClientCredentialsEnvironmentAuthenticator($clientFactory, $resource); | ||
try { | ||
$authenticator->checkUsability(); | ||
return $authenticator; | ||
} catch (ClientException) { | ||
throw new TransClientNotAvailableException; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\ObjectEncryptor\Temporary; | ||
|
||
use Keboola\AzureKeyVaultClient\Client; | ||
use Keboola\AzureKeyVaultClient\GuzzleClientFactory; | ||
|
||
class TransClient extends Client | ||
{ | ||
public function __construct(GuzzleClientFactory $clientFactory, ?string $encryptorId) | ||
{ | ||
$vaultBaseUrl = (string) getenv(self::determinateVaultUrlEnvName($encryptorId)); | ||
|
||
if ($vaultBaseUrl === '') { | ||
throw new TransClientNotAvailableException; | ||
} | ||
|
||
parent::__construct( | ||
$clientFactory, | ||
new TransAuthenticatorFactory(), | ||
$vaultBaseUrl, | ||
); | ||
} | ||
|
||
public static function determinateVaultUrlEnvName(?string $encryptorId): string | ||
{ | ||
$transEnvName = 'TRANS_AZURE_KEY_VAULT_URL'; | ||
|
||
if (!empty($encryptorId)) { // not null or empty string | ||
$suffix = (string) preg_replace('/[\s\-_]+/', '_', $encryptorId); | ||
$suffix = trim($suffix, '_'); | ||
$transEnvName .= sprintf('_%s', strtoupper($suffix)); | ||
} | ||
|
||
return $transEnvName; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Temporary/TransClientCredentialsEnvironmentAuthenticator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\ObjectEncryptor\Temporary; | ||
|
||
use Keboola\AzureKeyVaultClient\Authentication\ClientCredentialsEnvironmentAuthenticator; | ||
|
||
class TransClientCredentialsEnvironmentAuthenticator extends ClientCredentialsEnvironmentAuthenticator | ||
{ | ||
protected const ENV_AZURE_TENANT_ID = 'TRANS_AZURE_TENANT_ID'; | ||
protected const ENV_AZURE_CLIENT_ID = 'TRANS_AZURE_CLIENT_ID'; | ||
protected const ENV_AZURE_CLIENT_SECRET = 'TRANS_AZURE_CLIENT_SECRET'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\ObjectEncryptor\Temporary; | ||
|
||
use Exception; | ||
|
||
class TransClientNotAvailableException extends Exception | ||
{ | ||
} |
Oops, something went wrong.