Skip to content

[Messenger] Allow auto creation of sqs queue #58575

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
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Allow auto creation of sqs queue
If the accountId is the same as the current caller identity
  • Loading branch information
kevinrudde committed Oct 29, 2024
commit 488fe38b76f1eac1c45d3af07d061b1880aa780c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Transport;

use AsyncAws\Core\Sts\StsClient;
use AsyncAws\Sqs\Enum\MessageSystemAttributeName;
use AsyncAws\Sqs\Enum\QueueAttributeName;
use AsyncAws\Sqs\Result\ReceiveMessageResult;
Expand Down Expand Up @@ -53,6 +54,7 @@ class Connection

private array $configuration;
private SqsClient $client;
private StsClient $stsClient;
private ?ReceiveMessageResult $currentResponse = null;
/** @var array[] */
private array $buffer = [];
Expand All @@ -61,9 +63,11 @@ public function __construct(
array $configuration,
?SqsClient $client = null,
private ?string $queueUrl = null,
?StsClient $stsClient = null,
) {
$this->configuration = array_replace_recursive(self::DEFAULT_OPTIONS, $configuration);
$this->client = $client ?? new SqsClient([]);
$this->stsClient = $stsClient ?? new StsClient([]);
}

public function __sleep(): array
Expand Down Expand Up @@ -274,8 +278,11 @@ public function setup(): void
return;
}

if (null !== $this->configuration['account']) {
throw new InvalidArgumentException(\sprintf('The Amazon SQS queue "%s" does not exist (or you don\'t have permissions on it), and can\'t be created when an account is provided.', $this->configuration['queue_name']));
$accountId = $this->stsClient->getCallerIdentity()->getAccount();

// If the queue does not exist, we can create it only if the account is the same
if (null !== $this->configuration['account'] && $accountId !== $this->configuration['account']) {
throw new InvalidArgumentException(\sprintf('The Amazon SQS queue "%s" does not exist (or you don\'t have permissions on it), and can\'t be created when an other account is provided.', $this->configuration['queue_name']));
}

$parameters = ['QueueName' => $this->configuration['queue_name']];
Expand Down