-
Notifications
You must be signed in to change notification settings - Fork 26
fix: rate limit migration assistant routes #198
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
Open
Jozsef Damokos (jozsefdamokos)
wants to merge
2
commits into
trunk
Choose a base branch
from
fix/migration-assistant-rate-limits
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
62 changes: 62 additions & 0 deletions
62
src/DependencyInjection/CompilerPass/MigrationRateLimiterCompilerPass.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,62 @@ | ||
| <?php declare(strict_types=1); | ||
| /* | ||
| * (c) shopware AG <info@shopware.com> | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace SwagMigrationAssistant\DependencyInjection\CompilerPass; | ||
|
|
||
| use Psr\Clock\ClockInterface; | ||
| use Shopware\Core\Framework\Log\Package; | ||
| use Shopware\Core\Framework\RateLimiter\RateLimiter; | ||
| use Shopware\Core\Framework\RateLimiter\RateLimiterFactory; | ||
| use Shopware\Core\System\SystemConfig\SystemConfigService; | ||
| use SwagMigrationAssistant\RateLimiter\MigrationApiRateLimiter; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| use Symfony\Component\RateLimiter\Storage\CacheStorage; | ||
|
|
||
| #[Package('fundamentals@after-sales')] | ||
| class MigrationRateLimiterCompilerPass implements CompilerPassInterface | ||
| { | ||
| /** | ||
| * @var array<string, array{enabled: bool, policy: string, limit: int, interval: string}> | ||
| */ | ||
| private const RATE_LIMITERS = [ | ||
| MigrationApiRateLimiter::LOG_ACCESS => [ | ||
| 'enabled' => true, | ||
| 'policy' => 'sliding_window', | ||
| 'limit' => 30, | ||
| 'interval' => '60 seconds', | ||
| ], | ||
| MigrationApiRateLimiter::DOWNLOAD => [ | ||
| 'enabled' => true, | ||
| 'policy' => 'sliding_window', | ||
| 'limit' => 10, | ||
| 'interval' => '60 seconds', | ||
| ], | ||
| ]; | ||
|
|
||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| $rateLimiter = $container->getDefinition(RateLimiter::class); | ||
|
|
||
| foreach (self::RATE_LIMITERS as $name => $config) { | ||
| $limiterFactory = new Definition(RateLimiterFactory::class); | ||
| $limiterFactory->addArgument($config + ['id' => $name]); | ||
|
|
||
| $cacheStorage = new Definition(CacheStorage::class); | ||
| $cacheStorage->addArgument(new Reference('cache.rate_limiter')); | ||
|
|
||
| $limiterFactory->addArgument($cacheStorage); | ||
| $limiterFactory->addArgument(new Reference(SystemConfigService::class)); | ||
| $limiterFactory->addArgument(new Reference(ClockInterface::class)); | ||
| $limiterFactory->addArgument(new Reference('lock.factory')); | ||
|
|
||
| $rateLimiter->addMethodCall('registerLimiterFactory', [$name, $limiterFactory]); | ||
| } | ||
| } | ||
| } |
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
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,43 @@ | ||
| <?php declare(strict_types=1); | ||
| /* | ||
| * (c) shopware AG <info@shopware.com> | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace SwagMigrationAssistant\RateLimiter; | ||
|
|
||
| use Shopware\Core\Framework\Log\Package; | ||
| use Shopware\Core\Framework\RateLimiter\RateLimiter; | ||
| use Shopware\Core\PlatformRequest; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| #[Package('fundamentals@after-sales')] | ||
| class MigrationApiRateLimiter | ||
| { | ||
| final public const LOG_ACCESS = 'swag_migration_log_access'; | ||
|
|
||
| final public const DOWNLOAD = 'swag_migration_download'; | ||
|
|
||
| public function __construct( | ||
| private readonly RateLimiter $rateLimiter, | ||
| ) { | ||
| } | ||
|
|
||
| public function ensureLogAccessAccepted(Request $request): void | ||
| { | ||
| $this->rateLimiter->ensureAccepted(self::LOG_ACCESS, $this->resolveKey($request)); | ||
| } | ||
|
|
||
| public function ensureDownloadAccepted(Request $request): void | ||
| { | ||
| $this->rateLimiter->ensureAccepted(self::DOWNLOAD, $this->resolveKey($request)); | ||
| } | ||
|
|
||
| private function resolveKey(Request $request): string | ||
| { | ||
| return $request->attributes->getString(PlatformRequest::ATTRIBUTE_OAUTH_ACCESS_TOKEN_ID) | ||
| ?: $request->getClientIp() | ||
| ?: 'unknown'; | ||
| } | ||
| } | ||
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
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.
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.
is this per limit, one method a platform thing? I would rather go with a single method that takes request and limiter key