-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathAwsProvider.php
More file actions
executable file
·713 lines (601 loc) · 21 KB
/
AwsProvider.php
File metadata and controls
executable file
·713 lines (601 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/**
* Copyright 2014 Underground Elephant
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package qpush-bundle
* @copyright Underground Elephant 2014
* @license Apache License, Version 2.0
*/
namespace Uecode\Bundle\QPushBundle\Provider;
use Aws\Sns\Exception\SnsException;
use Aws\Sns\SnsClient;
use Aws\Sqs\Exception\SqsException;
use Aws\Sqs\SqsClient;
use Doctrine\Common\Cache\Cache;
use Monolog\Logger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Uecode\Bundle\QPushBundle\Event\Events;
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
use Uecode\Bundle\QPushBundle\Event\NotificationEvent;
use Uecode\Bundle\QPushBundle\Message\Message;
/**
* @author Keith Kirk <kkirk@undergroundelephant.com>
*/
class AwsProvider extends AbstractProvider
{
/**
* Aws SQS Client
*
* @var SqsClient
*/
private $sqs;
/**
* Aws SNS Client
*
* @var SnsClient
*/
private $sns;
/**
* SQS Queue URL
*
* @var string
*/
private $queueUrl;
/**
* SNS Topic ARN
*
* @var string
*/
private $topicArn;
/**
* @param string $name
* @param array $options
* @param mixed $client
* @param Cache $cache
* @param Logger $logger
*/
public function __construct($name, array $options, $client, Cache $cache, Logger $logger)
{
$this->name = $name;
$this->options = $options;
$this->cache = $cache;
$this->logger = $logger;
// get() method used for sdk v2, create methods for v3
$useGet = method_exists($client, 'get');
$this->sqs = $useGet ? $client->get('Sqs') : $client->createSqs();
$this->sns = $useGet ? $client->get('Sns') : $client->createSns();
}
/**
* @return string
*/
public function getProvider()
{
return 'AWS';
}
/**
* Builds the configured queues
*
* If a Queue name is passed and configured, this method will build only that
* Queue.
*
* All Create methods are idempotent, if the resource exists, the current ARN
* will be returned
*
* @return bool
*/
public function create()
{
$this->createQueue();
if ($this->options['push_notifications']) {
// Create the SNS Topic
$this->createTopic();
if(!$this->options['push_notifications_only']) {
// Add the SQS Queue as a Subscriber to the SNS Topic
$this->subscribeToTopic(
$this->topicArn,
'sqs',
$this->sqs->getQueueArn($this->queueUrl)
);
}
// Add configured Subscribers to the SNS Topic
foreach ($this->options['subscribers'] as $subscriber) {
$this->subscribeToTopic(
$this->topicArn,
$subscriber['protocol'],
$subscriber['endpoint']
);
}
}
return true;
}
/**
* @return Boolean
*/
public function destroy()
{
$key = $this->getNameWithPrefix() . '_url';
$this->cache->delete($key);
if ($this->queueExists()) {
// Delete the SQS Queue
$this->sqs->deleteQueue([
'QueueUrl' => $this->queueUrl
]);
$this->log(200,"SQS Queue removed", ['QueueUrl' => $this->queueUrl]);
}
$topicExists = $this->topicExists();
$key = $this->getNameWithPrefix() . '_arn';
$this->cache->delete($key);
if ($this->options['push_notifications_only'] || ($topicExists || !empty($this->queueUrl))) {
// Delete the SNS Topic
$topicArn = !empty($this->topicArn)
? $this->topicArn
: str_replace('sqs', 'sns', $this->queueUrl)
;
$this->sns->deleteTopic([
'TopicArn' => $topicArn
]);
$this->log(200,"SNS Topic removed", ['TopicArn' => $topicArn]);
}
return true;
}
/**
* {@inheritDoc}
*
* This method will either use a SNS Topic to publish a queued message or
* straight to SQS depending on the application configuration.
*
* @return string
*/
public function publish(array $message, array $options = [])
{
$mergedOptions = $this->mergeOptions($options);
if (isset($options['message_deduplication_id'])) {
$mergedOptions['message_deduplication_id'] = $options['message_deduplication_id'];
}
if (isset($options['message_group_id'])) {
$mergedOptions['message_group_id'] = $options['message_group_id'];
}
$options = $mergedOptions;
$publishStart = microtime(true);
// ensures that the SQS Queue and SNS Topic exist
if(!$this->options['push_notifications_only']) {
if (!$this->queueExists()) {
$this->create();
}
}
if ($options['push_notifications']) {
if (!$this->topicExists()) {
$this->create();
}
if($this->options['push_notifications_only']) {
$jsonMessage = json_encode($message);
$message = [
'default' => $jsonMessage,
'http' => $jsonMessage,
'https' => $jsonMessage,
];
} else {
$message = [
'default' => $this->getNameWithPrefix(),
'sqs' => json_encode($message),
'http' => $this->getNameWithPrefix(),
'https' => $this->getNameWithPrefix(),
];
}
$result = $this->sns->publish([
'TopicArn' => $this->topicArn,
'Subject' => $this->getName(),
'Message' => json_encode($message),
'MessageStructure' => 'json'
]);
$context = [
'TopicArn' => $this->topicArn,
'MessageId' => $result->get('MessageId'),
'push_notifications' => $options['push_notifications'],
'publish_time' => microtime(true) - $publishStart
];
$this->log(200,"Message published to SNS", $context);
return $result->get('MessageId');
}
$arguments = [
'QueueUrl' => $this->queueUrl,
'MessageBody' => json_encode($message),
'DelaySeconds' => $options['message_delay']
];
if ($this->isQueueFIFO()) {
if (isset($options['message_deduplication_id'])) {
// Always use user supplied dedup id
$arguments['MessageDeduplicationId'] = $options['message_deduplication_id'];
} elseif ($options['content_based_deduplication'] !== true) {
// If none is supplied and option "content_based_deduplication" is not set, generate default
$arguments['MessageDeduplicationId'] = hash('sha256', json_encode($message));
}
$arguments['MessageGroupId'] = $this->getNameWithPrefix();
if (isset($options['message_group_id'])) {
$arguments['MessageGroupId'] = $options['message_group_id'];
}
}
$result = $this->sqs->sendMessage($arguments);
$context = [
'QueueUrl' => $this->queueUrl,
'MessageId' => $result->get('MessageId'),
'push_notifications' => $options['push_notifications'],
'fifo' => $options['fifo']
];
if ($this->isQueueFIFO()) {
if (isset($arguments['MessageDeduplicationId'])) {
$context['message_deduplication_id'] = $arguments['MessageDeduplicationId'];
}
$context['message_group_id'] = $arguments['MessageGroupId'];
}
$this->log(200,"Message published to SQS", $context);
return $result->get('MessageId');
}
/**
* {@inheritDoc}
*/
public function receive(array $options = [])
{
$options = $this->mergeOptions($options);
if (!$this->queueExists()) {
$this->create();
}
$result = $this->sqs->receiveMessage([
'QueueUrl' => $this->queueUrl,
'MaxNumberOfMessages' => $options['messages_to_receive'],
'WaitTimeSeconds' => $options['receive_wait_time']
]);
$messages = $result->get('Messages') ?: [];
// Convert to Message Class
foreach ($messages as &$message) {
$id = $message['MessageId'];
$metadata = [
'ReceiptHandle' => $message['ReceiptHandle'],
'MD5OfBody' => $message['MD5OfBody']
];
// When using SNS, the SQS Body is the entire SNS Message
if(is_array($body = json_decode($message['Body'], true))
&& isset($body['Message'])
) {
$body = json_decode($body['Message'], true);
}
$message = new Message($id, $body, $metadata);
$context = ['MessageId' => $id];
$this->log(200,"Message fetched from SQS Queue", $context);
}
return $messages;
}
/**
* {@inheritDoc}
*
* @return bool
*/
public function delete($id)
{
if($this->options['push_notifications_only']) {
return true;
} else {
if (!$this->queueExists()) {
return false;
}
$this->sqs->deleteMessage([
'QueueUrl' => $this->queueUrl,
'ReceiptHandle' => $id
]);
$context = [
'QueueUrl' => $this->queueUrl,
'ReceiptHandle' => $id
];
$this->log(200,"Message deleted from SQS Queue", $context);
return true;
}
}
/**
* Return the Queue Url
*
* This method relies on in-memory cache and the Cache provider
* to reduce the need to needlessly call the create method on an existing
* Queue.
*
* @return boolean
*/
public function queueExists()
{
if (isset($this->queueUrl)) {
return true;
}
$key = $this->getNameWithPrefix() . '_url';
if ($this->cache->contains($key)) {
$this->queueUrl = $this->cache->fetch($key);
return true;
}
try {
$result = $this->sqs->getQueueUrl([
'QueueName' => $this->getNameWithPrefix()
]);
$this->queueUrl = $result->get('QueueUrl');
if ($this->queueUrl !== null) {
$this->cache->save($key, $this->queueUrl);
return true;
}
} catch (SqsException $e) {}
return false;
}
/**
* Creates an SQS Queue and returns the Queue Url
*
* The create method for SQS Queues is idempotent - if the queue already
* exists, this method will return the Queue Url of the existing Queue.
*/
public function createQueue()
{
if(!$this->options['push_notifications_only']) {
$attributes = [
'VisibilityTimeout' => $this->options['message_timeout'],
'MessageRetentionPeriod' => $this->options['message_expiration'],
'ReceiveMessageWaitTimeSeconds' => $this->options['receive_wait_time']
];
if ($this->isQueueFIFO()) {
$attributes['FifoQueue'] = 'true';
$attributes['ContentBasedDeduplication'] = $this->options['content_based_deduplication'] === true
? 'true'
: 'false';
}
$result = $this->sqs->createQueue(['QueueName' => $this->getNameWithPrefix(), 'Attributes' => $attributes]);
$this->queueUrl = $result->get('QueueUrl');
$key = $this->getNameWithPrefix() . '_url';
$this->cache->save($key, $this->queueUrl);
$this->log(200, "Created SQS Queue", ['QueueUrl' => $this->queueUrl]);
if ($this->options['push_notifications']) {
$policy = $this->createSqsPolicy();
$this->sqs->setQueueAttributes([
'QueueUrl' => $this->queueUrl,
'Attributes' => [
'Policy' => $policy,
]
]);
$this->log(200, "Created Updated SQS Policy");
}
}
}
/**
* Creates a Policy for SQS that's required to allow SNS SendMessage access
*
* @return string
*/
public function createSqsPolicy()
{
$arn = $this->sqs->getQueueArn($this->queueUrl);
return json_encode([
'Version' => '2008-10-17',
'Id' => sprintf('%s/SQSDefaultPolicy', $arn),
'Statement' => [
[
'Sid' => 'SNSPermissions',
'Effect' => 'Allow',
'Principal' => ['AWS' => '*'],
'Action' => 'SQS:SendMessage',
'Resource' => $arn
]
]
]);
}
/**
* Checks to see if a Topic exists
*
* This method relies on in-memory cache and the Cache provider
* to reduce the need to needlessly call the create method on an existing
* Topic.
*
* @return boolean
*/
public function topicExists()
{
if (isset($this->topicArn)) {
return true;
}
$key = $this->getNameWithPrefix() . '_arn';
if ($this->cache->contains($key)) {
$this->topicArn = $this->cache->fetch($key);
return true;
}
if (!empty($this->queueUrl)) {
$queueArn = $this->sqs->getQueueArn($this->queueUrl);
$topicArn = str_replace('sqs', 'sns', $queueArn);
try {
$this->sns->getTopicAttributes([
'TopicArn' => $topicArn
]);
} catch (SnsException $e) {
return false;
}
$this->topicArn = $topicArn;
$this->cache->save($key, $this->topicArn);
return true;
}
return false;
}
/**
* Creates a SNS Topic and returns the ARN
*
* The create method for the SNS Topics is idempotent - if the topic already
* exists, this method will return the Topic ARN of the existing Topic.
*
*
* @return bool
*/
public function createTopic()
{
if (!$this->options['push_notifications']) {
return false;
}
$name = str_replace('.', '-', $this->getNameWithPrefix());
$result = $this->sns->createTopic([
'Name' => $name
]);
$this->topicArn = $result->get('TopicArn');
$key = $name . '_arn';
$this->cache->save($key, $this->topicArn);
$this->log(200, "Created SNS Topic", ['TopicARN' => $this->topicArn]);
return true;
}
/**
* Get a list of Subscriptions for the specified SNS Topic
*
* @param string $topicArn The SNS Topic Arn
*
* @return array
*/
public function getTopicSubscriptions($topicArn)
{
$result = $this->sns->listSubscriptionsByTopic([
'TopicArn' => $topicArn
]);
return $result->get('Subscriptions');
}
/**
* Subscribes an endpoint to a SNS Topic
*
* @param string $topicArn The ARN of the Topic
* @param string $protocol The protocol of the Endpoint
* @param string $endpoint The Endpoint of the Subscriber
*
* @return string
*/
public function subscribeToTopic($topicArn, $protocol, $endpoint)
{
// Check against the current Topic Subscriptions
$subscriptions = $this->getTopicSubscriptions($topicArn);
foreach ($subscriptions as $subscription) {
if ($endpoint === $subscription['Endpoint']) {
return $subscription['SubscriptionArn'];
}
}
$result = $this->sns->subscribe([
'TopicArn' => $topicArn,
'Protocol' => $protocol,
'Endpoint' => $endpoint
]);
$arn = $result->get('SubscriptionArn');
$context = [
'Endpoint' => $endpoint,
'Protocol' => $protocol,
'SubscriptionArn' => $arn
];
$this->log(200, "Endpoint Subscribed to SNS Topic", $context);
return $arn;
}
/**
* Unsubscribes an endpoint from a SNS Topic
*
* The method will return TRUE on success, or FALSE if the Endpoint did not
* have a Subscription on the SNS Topic
*
* @param string $topicArn The ARN of the Topic
* @param string $protocol The protocol of the Endpoint
* @param string $endpoint The Endpoint of the Subscriber
*
* @return Boolean
*/
public function unsubscribeFromTopic($topicArn, $protocol, $endpoint)
{
// Check against the current Topic Subscriptions
$subscriptions = $this->getTopicSubscriptions($topicArn);
foreach ($subscriptions as $subscription) {
if ($endpoint === $subscription['Endpoint']) {
$this->sns->unsubscribe([
'SubscriptionArn' => $subscription['SubscriptionArn']
]);
$context = [
'Endpoint' => $endpoint,
'Protocol' => $protocol,
'SubscriptionArn' => $subscription['SubscriptionArn']
];
$this->log(200,"Endpoint unsubscribed from SNS Topic", $context);
return true;
}
}
return false;
}
/**
* Handles SNS Notifications
*
* For Subscription notifications, this method will automatically confirm
* the Subscription request
*
* For Message notifications, this method polls the queue and dispatches
* the `{queue}.message_received` event for each message retrieved
*
* @param NotificationEvent $event The Notification Event
* @param string $eventName Name of the event
* @param EventDispatcherInterface $dispatcher
*
* @return void
*/
public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
if (NotificationEvent::TYPE_SUBSCRIPTION == $event->getType()) {
$topicArn = $event->getNotification()->getMetadata()->get('TopicArn');
$token = $event->getNotification()->getMetadata()->get('Token');
$this->sns->confirmSubscription([
'TopicArn' => $topicArn,
'Token' => $token
]);
$context = ['TopicArn' => $topicArn];
$this->log(200,"Subscription to SNS Confirmed", $context);
return;
}
if($this->options['push_notifications_only']) {
$notification = $event->getNotification();
$message = new Message($notification->getId(), $notification->getBody(), (array)$notification->getMetadata());
$messageEvent = new MessageEvent($this->name, $message);
$dispatcher->dispatch(Events::Message($this->name), $messageEvent);
} else {
$messages = $this->receive();
foreach ($messages as $message) {
$messageEvent = new MessageEvent($this->name, $message);
$dispatcher->dispatch(Events::Message($this->name), $messageEvent);
}
}
}
/**
* Removes the message from queue after all other listeners have fired
*
* If an earlier listener has erred or stopped propagation, this method
* will not fire and the Queued Message should become visible in queue again.
*
* Stops Event Propagation after removing the Message
*
* @param MessageEvent $event The SQS Message Event
*
* @return void
*/
public function onMessageReceived(MessageEvent $event)
{
$receiptHandle = $event
->getMessage()
->getMetadata()
->get('ReceiptHandle');
$this->delete($receiptHandle);
$event->stopPropagation();
}
/**
* @return bool
*/
private function isQueueFIFO()
{
return $this->options['fifo'] === true;
}
}