Skip to content

Commit

Permalink
Fixed Issue #1: [SNS] Subscriptions all sub to the same topic arn
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-Bessette committed Mar 22, 2023
1 parent ee0babc commit e1db34e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/aws-shared-entities/attributes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class AttributesService {
return await this.repo.save(dto);
}

async deleteByArn(arn: string) {
await this.repo.delete({ arn });
}

async deleteByArnAndName(arn: string, name: string) {
await this.repo.delete({ arn, name });
}
Expand Down
4 changes: 4 additions & 0 deletions src/aws-shared-entities/tags.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class TagsService {
}
}

async deleteByArn(arn: string) {
await this.repo.delete({ arn });
}

async deleteByArnAndName(arn: string, name: string) {
await this.repo.delete({ arn, name });
}
Expand Down
7 changes: 6 additions & 1 deletion src/sns/get-subscription-attributes.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ export class GetSubscriptionAttributesHandler extends AbstractActionHandler {

protected async handle({ SubscriptionArn }: QueryParams, awsProperties: AwsProperties) {

const id = SubscriptionArn.split(':')[-1];
const id = SubscriptionArn.split(':').pop();
const subscription = await this.snsTopicSubscriptionRepo.findOne({ where: { id }});

if (!subscription) {
return;
}

const attributes = await this.attributeService.getByArn(SubscriptionArn);
const attributeMap = attributes.reduce((m, a) => {
m[a.name] = a.value;
Expand Down
9 changes: 7 additions & 2 deletions src/sns/get-topic-attributes.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Action } from '../action.enum';
import { SnsTopic } from './sns-topic.entity';
import * as Joi from 'joi';
import { AttributesService } from '../aws-shared-entities/attributes.service';
import { SnsTopicSubscription } from './sns-topic-subscription.entity';

type QueryParams = {
TopicArn: string;
Expand All @@ -17,6 +18,8 @@ export class GetTopicAttributesHandler extends AbstractActionHandler {
constructor(
@InjectRepository(SnsTopic)
private readonly snsTopicRepo: Repository<SnsTopic>,
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscriptionRepo: Repository<SnsTopicSubscription>,
private readonly attributeService: AttributesService,
) {
super();
Expand All @@ -28,18 +31,20 @@ export class GetTopicAttributesHandler extends AbstractActionHandler {

protected async handle({ TopicArn }: QueryParams, awsProperties: AwsProperties) {

const name = TopicArn.split(':')[-1];
const name = TopicArn.split(':').pop();
const topic = await this.snsTopicRepo.findOne({ where: { name }});
const attributes = await this.attributeService.getByArn(TopicArn);
const attributeMap = attributes.reduce((m, a) => {
m[a.name] = a.value;
return m;
}, {});

const subscriptionCount = await this.snsTopicSubscriptionRepo.count({ where: { topicArn: TopicArn } });

const response = {
DisplayName: topic.name,
Owner: topic.accountId,
SubscriptionsConfirmed: '0',
SubscriptionsConfirmed: `${subscriptionCount}`,
SubscriptionsDeleted: '0',
SubscriptionsPending: '0',
TopicArn: topic.topicArn,
Expand Down
2 changes: 2 additions & 0 deletions src/sns/sns.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { SnsTopicSubscription } from './sns-topic-subscription.entity';
import { SnsTopic } from './sns-topic.entity';
import { SnsHandlers } from './sns.constants';
import { SubscribeHandler } from './subscribe.handler';
import { UnsubscribeHandler } from './unsubscribe.handler';

const handlers = [
CreateTopicHandler,
Expand All @@ -30,6 +31,7 @@ const handlers = [
SetSubscriptionAttributesHandler,
SetTopicAttributesHandler,
SubscribeHandler,
UnsubscribeHandler,
];

const actions = [
Expand Down
43 changes: 43 additions & 0 deletions src/sns/unsubscribe.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import * as Joi from 'joi';
import { TagsService } from '../aws-shared-entities/tags.service';
import { AttributesService } from '../aws-shared-entities/attributes.service';
import { SnsTopicSubscription } from './sns-topic-subscription.entity';
import * as uuid from 'uuid';

type QueryParams = {
SubscriptionArn: string;
}

@Injectable()
export class UnsubscribeHandler extends AbstractActionHandler<QueryParams> {

constructor(
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscription: Repository<SnsTopicSubscription>,
private readonly tagsService: TagsService,
private readonly attributeService: AttributesService,
) {
super();
}

format = Format.Xml;
action = Action.SnsUnsubscribe;
validator = Joi.object<QueryParams, true>({
SubscriptionArn: Joi.string().required(),
});

protected async handle(params: QueryParams, awsProperties: AwsProperties) {

const id = params.SubscriptionArn.split(':').pop();
const subscription = await this.snsTopicSubscription.findOne({ where: { id } });

await this.tagsService.deleteByArn(subscription.arn);
await this.attributeService.deleteByArn(subscription.arn);
await this.snsTopicSubscription.delete({ id });
}
}
8 changes: 4 additions & 4 deletions src/sqs/receive-message.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SqsQueueEntryService } from './sqs-queue-entry.service';
import crypto from 'crypto';

type QueryParams = {
__path: string;
QueueUrl: string;
MaxNumberOfMessages?: number;
VisibilityTimeout?: number;
}
Expand All @@ -24,14 +24,14 @@ export class ReceiveMessageHandler extends AbstractActionHandler<QueryParams> {
format = Format.Xml;
action = Action.SqsReceiveMessage;
validator = Joi.object<QueryParams, true>({
__path: Joi.string().required(),
QueueUrl: Joi.string().required(),
MaxNumberOfMessages: Joi.number(),
VisibilityTimeout: Joi.number(),
});

protected async handle({ __path, MaxNumberOfMessages, VisibilityTimeout }: QueryParams, awsProperties: AwsProperties) {
protected async handle({ QueueUrl, MaxNumberOfMessages, VisibilityTimeout }: QueryParams, awsProperties: AwsProperties) {

const [accountId, name] = SqsQueue.getAccountIdAndNameFromPath(__path);
const [accountId, name] = SqsQueue.tryGetAccountIdAndNameFromPathOrArn(QueueUrl);
const records = await this.sqsQueueEntryService.recieveMessages(accountId, name, MaxNumberOfMessages, VisibilityTimeout);
return records.map(r => ({
Message: {
Expand Down
2 changes: 2 additions & 0 deletions src/sqs/sqs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DefaultActionHandlerProvider } from '../default-action-handler/default-
import { ExistingActionHandlersProvider } from '../default-action-handler/existing-action-handlers.provider';
import { CreateQueueHandler } from './create-queue.handler';
import { PurgeQueueHandler } from './purge-queue.handler';
import { ReceiveMessageHandler } from './receive-message.handler';
import { SetQueueAttributesHandler } from './set-queue-attributes.handler';
import { SqsQueueEntryService } from './sqs-queue-entry.service';
import { SqsQueue } from './sqs-queue.entity';
Expand All @@ -15,6 +16,7 @@ import { SqsHandlers } from './sqs.constants';
const handlers = [
CreateQueueHandler,
PurgeQueueHandler,
ReceiveMessageHandler,
SetQueueAttributesHandler,
]

Expand Down

0 comments on commit e1db34e

Please sign in to comment.