-
Notifications
You must be signed in to change notification settings - Fork 0
PAT-1895 Build Storage client from resolved StorageApiToken #531
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30e56ad
feat(api-bundle): add StorageClientApiFactory building client from re…
pepamartinec 1cff8b7
feat(api-bundle): register StorageClientApiFactory as a service with …
pepamartinec aa5af0a
feat(api-bundle): add request-bound RequestStorageClientFactory with …
pepamartinec d9be4fc
refactor(api-bundle): merge StorageClientApiFactory into RequestStora…
pepamartinec c3aa246
refactor(api-bundle): rename RequestStorageClientFactory to StorageCl…
pepamartinec df6d1e7
refactor(api-bundle): rename resolver to StorageClientApiFactoryResolver
pepamartinec 394d4e0
feat(api-bundle): resolve nullable StorageClientApiFactory argument t…
pepamartinec 7c5f9a0
refactor(api-bundle): simplify StorageClientApiFactoryResolver contro…
pepamartinec ea8b61c
chore(api-bundle): Simplify comments
pepamartinec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
libs/api-bundle/src/StorageApiClient/StorageClientApiFactory.php
This file contains hidden or 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,54 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Keboola\ApiBundle\StorageApiClient; | ||
|
|
||
| use Keboola\StorageApiBranch\ClientWrapper; | ||
| use Keboola\StorageApiBranch\Factory\AuthType; | ||
| use Keboola\StorageApiBranch\Factory\ClientOptions; | ||
| use Keboola\StorageApiBranch\StorageApiToken; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| class StorageClientApiFactory | ||
| { | ||
| public const RUN_ID_HEADER = 'X-KBC-RunId'; | ||
|
|
||
| public function __construct( | ||
| private readonly ClientOptions $baseClientOptions, | ||
| private readonly Request $request, | ||
| private readonly StorageApiToken $token, | ||
| ) { | ||
| } | ||
|
|
||
| public function createClientWrapper(?ClientOptions $clientOptions = null): ClientWrapper | ||
| { | ||
| $options = clone $this->baseClientOptions; | ||
| if ($clientOptions !== null) { | ||
| $options->addValuesFrom($clientOptions); | ||
| } | ||
|
|
||
| $options->setToken($this->token->getTokenValue()); | ||
| $options->setAuthType(AuthType::STORAGE_TOKEN); | ||
| $options->setRunId($this->getRunId($options)); | ||
|
|
||
| return new ClientWrapper($options); | ||
| } | ||
|
|
||
| private function getRunId(ClientOptions $options): string | ||
| { | ||
| $runId = (string) $this->request->headers->get(self::RUN_ID_HEADER); | ||
|
|
||
| if ($runId === '') { | ||
| $runIdGenerator = $options->getRunIdGenerator(); | ||
| if ($runIdGenerator !== null) { | ||
| $runId = $runIdGenerator($options); | ||
| assert(is_string($runId)); | ||
| } else { | ||
| $runId = uniqid('run-'); | ||
| } | ||
| } | ||
|
|
||
| return $runId; | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
libs/api-bundle/src/StorageApiClient/StorageClientApiFactoryResolver.php
This file contains hidden or 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,44 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Keboola\ApiBundle\StorageApiClient; | ||
|
|
||
| use Keboola\StorageApiBranch\Factory\ClientOptions; | ||
| use Keboola\StorageApiBranch\StorageApiToken; | ||
| use RuntimeException; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
| use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
|
||
| class StorageClientApiFactoryResolver implements ValueResolverInterface | ||
| { | ||
| public function __construct( | ||
| private readonly ClientOptions $baseClientOptions, | ||
| private readonly TokenStorageInterface $tokenStorage, | ||
| ) { | ||
| } | ||
|
|
||
| public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
| { | ||
| if ($argument->getType() !== StorageClientApiFactory::class) { | ||
| return []; | ||
| } | ||
|
|
||
| $user = $this->tokenStorage->getToken()?->getUser(); | ||
| if ($user instanceof StorageApiToken) { | ||
| return [new StorageClientApiFactory($this->baseClientOptions, $request, $user)]; | ||
| } | ||
|
|
||
| if ($argument->isNullable()) { | ||
| return [null]; | ||
| } | ||
|
|
||
| throw new RuntimeException(sprintf( | ||
| 'Cannot resolve argument "$%s": no authenticated Storage API token. Guard the ' | ||
| . 'controller with #[StorageApiTokenAuth], or make the argument nullable to allow null.', | ||
| $argument->getName(), | ||
| )); | ||
| } | ||
| } |
This file contains hidden or 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
137 changes: 137 additions & 0 deletions
137
libs/api-bundle/tests/StorageApiClient/StorageClientApiFactoryResolverTest.php
This file contains hidden or 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,137 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Keboola\ApiBundle\Tests\StorageApiClient; | ||
|
|
||
| use Keboola\ApiBundle\Security\StorageApiToken\StorageApiToken as SecurityStorageApiToken; | ||
| use Keboola\ApiBundle\StorageApiClient\StorageClientApiFactory; | ||
| use Keboola\ApiBundle\StorageApiClient\StorageClientApiFactoryResolver; | ||
| use Keboola\StorageApiBranch\Factory\ClientOptions; | ||
| use PHPUnit\Framework\TestCase; | ||
| use RuntimeException; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
| use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory; | ||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
|
||
| class StorageClientApiFactoryResolverTest extends TestCase | ||
| { | ||
| private function resolver(TokenStorageInterface $tokenStorage): StorageClientApiFactoryResolver | ||
| { | ||
| return new StorageClientApiFactoryResolver(new ClientOptions('https://connection.test'), $tokenStorage); | ||
| } | ||
|
|
||
| private function metadataFor(object $controller, string $arg): ArgumentMetadata | ||
| { | ||
| foreach ((new ArgumentMetadataFactory())->createArgumentMetadata($controller) as $metadata) { | ||
| if ($metadata->getName() === $arg) { | ||
| return $metadata; | ||
| } | ||
| } | ||
|
|
||
| self::fail(sprintf('Controller has no argument "%s"', $arg)); | ||
| } | ||
|
|
||
| public function testResolvesNothingForOtherArgumentTypes(): void | ||
| { | ||
| $controller = new class { | ||
| public function __invoke(string $foo): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
| $tokenStorage->expects(self::never())->method('getToken'); | ||
|
|
||
| $result = $this->resolver($tokenStorage)->resolve(new Request(), $this->metadataFor($controller, 'foo')); | ||
|
|
||
| self::assertSame([], [...$result]); | ||
| } | ||
|
|
||
| public function testResolvesBoundFactoryBuildingClientFromSecurityToken(): void | ||
| { | ||
| $controller = new class { | ||
| public function __invoke(StorageClientApiFactory $storage): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $storageToken = new SecurityStorageApiToken([], 'resolved-token'); | ||
| $securityToken = $this->createMock(TokenInterface::class); | ||
| $securityToken->expects(self::once())->method('getUser')->willReturn($storageToken); | ||
| $tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
| $tokenStorage->expects(self::once())->method('getToken')->willReturn($securityToken); | ||
|
|
||
| $result = [...$this->resolver($tokenStorage)->resolve( | ||
| new Request(), | ||
| $this->metadataFor($controller, 'storage'), | ||
| )]; | ||
|
|
||
| self::assertCount(1, $result); | ||
| self::assertInstanceOf(StorageClientApiFactory::class, $result[0]); | ||
| self::assertSame('resolved-token', $result[0]->createClientWrapper()->getClientOptionsReadOnly()->getToken()); | ||
| } | ||
|
|
||
| public function testThrowsForRequiredArgumentWhenNoStorageApiToken(): void | ||
| { | ||
| $controller = new class { | ||
| public function __invoke(StorageClientApiFactory $storage): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
| $tokenStorage->expects(self::once())->method('getToken')->willReturn(null); | ||
|
|
||
| $this->expectException(RuntimeException::class); | ||
| $this->expectExceptionMessage('#[StorageApiTokenAuth]'); | ||
|
|
||
| [...$this->resolver($tokenStorage)->resolve(new Request(), $this->metadataFor($controller, 'storage'))]; | ||
| } | ||
|
|
||
| public function testResolvesNullForNullableArgumentWhenNoStorageApiToken(): void | ||
| { | ||
| // Dual-guarded controller (e.g. also #[ApplicationTokenAuth]) authenticated through the | ||
| // other path: the security user is not a StorageApiToken, so a nullable argument gets null. | ||
| $controller = new class { | ||
| public function __invoke(?StorageClientApiFactory $storage): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
| $tokenStorage->expects(self::once())->method('getToken')->willReturn(null); | ||
|
|
||
| $result = [...$this->resolver($tokenStorage)->resolve( | ||
| new Request(), | ||
| $this->metadataFor($controller, 'storage'), | ||
| )]; | ||
|
|
||
| self::assertSame([null], $result); | ||
| } | ||
|
|
||
| public function testResolvesFactoryForNullableArgumentWhenStorageApiTokenPresent(): void | ||
| { | ||
| $controller = new class { | ||
| public function __invoke(?StorageClientApiFactory $storage): void | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| $storageToken = new SecurityStorageApiToken([], 'resolved-token'); | ||
| $securityToken = $this->createMock(TokenInterface::class); | ||
| $securityToken->expects(self::once())->method('getUser')->willReturn($storageToken); | ||
| $tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
| $tokenStorage->expects(self::once())->method('getToken')->willReturn($securityToken); | ||
|
|
||
| $result = [...$this->resolver($tokenStorage)->resolve( | ||
| new Request(), | ||
| $this->metadataFor($controller, 'storage'), | ||
| )]; | ||
|
|
||
| self::assertCount(1, $result); | ||
| self::assertInstanceOf(StorageClientApiFactory::class, $result[0]); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming is hard. Nasleduju pattern existujicich factory (
StorageClientRequestFactory,StorageClientPlainFactory)