init prisma and migrate sns to prisma

This commit is contained in:
Matthew Bessette 2024-12-18 19:00:06 -05:00
parent a5c90f7a26
commit a7fdedd310
35 changed files with 2188 additions and 9927 deletions

View File

@ -1,4 +0,0 @@
version: 3.7
services:
s3_provider:
image: minio

5237
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,49 +10,28 @@
"test": "jest"
},
"dependencies": {
"@nestjs/common": "^9.3.10",
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.3.10",
"@nestjs/platform-express": "^9.3.10",
"@nestjs/typeorm": "^9.0.1",
"@nestjs/common": "^10.4.15",
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.4.15",
"@nestjs/platform-express": "^10.4.15",
"@prisma/client": "^6.1.0",
"class-transformer": "^0.5.1",
"execa": "^9.5.2",
"joi": "^17.9.0",
"js2xmlparser": "^5.0.0",
"morgan": "^1.10.0",
"rxjs": "^7.8.0",
"sqlite3": "^5.1.6",
"typeorm": "^0.3.12",
"uuidv4": "^6.2.13"
},
"devDependencies": {
"@aws-sdk/client-sns": "^3.321.1",
"@nestjs/cli": "^9.3.0",
"@nestjs/testing": "^9.4.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.1",
"@types/supertest": "^2.0.12",
"@types/joi": "^17.2.2",
"@types/node": "^22.10.2",
"eslint": "^8.36.0",
"jest": "^29.5.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0"
"prisma": "^6.1.0"
},
"jest": {
"globalSetup": "./_jest_/setup.ts",
"globalTeardown": "./_jest_/teardown.ts",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.*spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"engines": {
"node": ">=22.11.0",
"npm": ">=10.9.0"
}
}

49
prisma/schema.prisma Normal file
View File

@ -0,0 +1,49 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = ":memory:"
}
model Attribute {
id Int @id @default(autoincrement())
arn String
name String
value String
@@index([arn])
}
model Audit {
id String @id
createdAt DateTime @default(now())
action String?
request String?
response String?
}
model SnsTopic {
name String @id
accountId String
region String
}
model SnsTopicSubscription {
id String @id
topicArn String
endpoint String?
protocol String
accountId String
region String
}
model Tag {
id Int @id @default(autoincrement())
arn String
name String
value String
@@index([arn])
}

View File

@ -1,19 +0,0 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from '../app.module';
const globalSetup = async (_globalConfig, _projectConfig) => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const app: INestApplication = module.createNestApplication();
await app.listen(4566);
globalThis.__TESTMODULE__ = module;
globalThis.__NESTAPP__ = app;
globalThis.__ENDPOINT__ = 'http://127.0.0.1:4566';
}
export default globalSetup;

View File

@ -1,8 +0,0 @@
import { INestApplication } from '@nestjs/common';
const globalTeardown = async (_globalConfig, _projectConfig) => {
await (globalThis.__NESTAPP__ as INestApplication).close();
}
export default globalTeardown;

View File

@ -0,0 +1,9 @@
import { Module } from "@nestjs/common";
import { PrismaService } from "./prisma.service";
@Module({
imports: [],
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}

View File

@ -0,0 +1,10 @@
import { OnModuleInit } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
const tables = await this.$queryRawUnsafe('.tables');
console.log({ tables })
}
}

View File

@ -1,24 +1,21 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { ActionHandlers } from './app.constants';
import { CommonConfig } from './config/common-config.interface';
import { AppController } from './app.controller';
import { AuditInterceptor } from './audit/audit.interceptor';
import { AwsSharedEntitiesModule } from './aws-shared-entities/aws-shared-entities.module';
import localConfig from './config/local.config';
import { IAMHandlers } from './iam/iam.constants';
import { IamModule } from './iam/iam.module';
import { KMSHandlers } from './kms/kms.constants';
import { KmsModule } from './kms/kms.module';
import { SecretsManagerHandlers } from './secrets-manager/secrets-manager.constants';
import { SecretsManagerModule } from './secrets-manager/secrets-manager.module';
import { SnsHandlers } from './sns/sns.constants';
import { SnsModule } from './sns/sns.module';
import { AppController } from './app.controller';
import { AwsSharedEntitiesModule } from './aws-shared-entities/aws-shared-entities.module';
import { SecretsManagerModule } from './secrets-manager/secrets-manager.module';
import { SecretsManagerHandlers } from './secrets-manager/secrets-manager.constants';
import { SqsModule } from './sqs/sqs.module';
import { SqsHandlers } from './sqs/sqs.constants';
import { Audit } from './audit/audit.entity';
import { AuditInterceptor } from './audit/audit.interceptor';
import { KmsModule } from './kms/kms.module';
import { KMSHandlers } from './kms/kms.constants';
import { configValidator } from './config/config.validator';
import { IamModule } from './iam/iam.module';
import { IAMHandlers } from './iam/iam.constants';
import { SqsModule } from './sqs/sqs.module';
@Module({
imports: [
@ -26,17 +23,6 @@ import { IAMHandlers } from './iam/iam.constants';
load: [localConfig],
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService<CommonConfig>) => ({
type: 'sqlite',
database: configService.get('DB_DATABASE') === ':memory:' ? configService.get('DB_DATABASE') : `${__dirname}/../data/${configService.get('DB_DATABASE')}`,
logging: configService.get('DB_LOGGING'),
synchronize: configService.get('DB_SYNCHRONIZE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
}),
}),
TypeOrmModule.forFeature([Audit]),
IamModule,
KmsModule,
SecretsManagerModule,

View File

@ -1,20 +0,0 @@
import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryColumn } from 'typeorm';
@Entity('audit')
export class Audit extends BaseEntity {
@PrimaryColumn()
id: string;
@CreateDateColumn()
createdAt: string;
@Column({ nullable: true })
action: string;
@Column({ nullable: true })
request: string;
@Column({ nullable: true })
response: string;
}

View File

@ -1,55 +1,68 @@
import { CallHandler, ExecutionContext, Inject, Injectable, NestInterceptor } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { randomUUID } from 'crypto';
import { Observable, tap } from 'rxjs';
import { Repository } from 'typeorm';
import { Audit } from './audit.entity';
import * as uuid from 'uuid';
import { Request as ExpressRequest } from 'express';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
import { ActionHandlers } from '../app.constants';
import { Action } from '../action.enum';
interface Request extends ExpressRequest {
headers: {
'x-amz-target'?: string;
},
body: {
'Action'?: string;
}
}
@Injectable()
export class AuditInterceptor<T> implements NestInterceptor<T, Response> {
constructor(
@InjectRepository(Audit)
private readonly auditRepo: Repository<Audit>,
@Inject(ActionHandlers)
private readonly handlers: ActionHandlers,
private readonly prismaService: PrismaService,
) {}
intercept(context: ExecutionContext, next: CallHandler<T>): Observable<any> {
const requestId = uuid.v4();
const requestId = randomUUID();
const httpContext = context.switchToHttp();
const request = httpContext.getRequest<Request>();
const request = httpContext.getRequest();
const targetHeaderKey = Object.keys(request.headers).find( k => k.toLocaleLowerCase() === 'x-amz-target');
const action = request.headers[targetHeaderKey] ? request.headers[targetHeaderKey] : request.body.Action;
const hasTargetHeader = Object.keys(request.headers).some( k => k.toLocaleLowerCase() === 'x-amz-target');
const action = hasTargetHeader ? request.headers['x-amz-target'] : request.body.Action;
const { value: resolvedAction } = Joi.string().required().valid(...Object.values(Action)).validate(action) as { value: Action | undefined };
const response = context.switchToHttp().getResponse();
response.header('x-amzn-RequestId', requestId);
if (!this.handlers[action]?.audit) {
if (!resolvedAction || !this.handlers[resolvedAction]?.audit) {
return next.handle();
}
return next.handle().pipe(
tap({
next: async (data) => await this.auditRepo.create({
id: requestId,
action,
request: JSON.stringify({ __path: request.path, ...request.headers, ...request.body }),
response: JSON.stringify(data),
}).save(),
next: async (data) => await this.prismaService.audit.create({
data: {
id: requestId,
action,
request: JSON.stringify({ __path: request.path, ...request.headers, ...request.body }),
response: JSON.stringify(data),
}
}),
error: async (error) => await this.auditRepo.create({
id: requestId,
action,
request: JSON.stringify({ __path: request.path, ...request.headers, ...request.body }),
response: JSON.stringify(error),
}).save(),
error: async (error) => await this.prismaService.audit.create({
data: {
id: requestId,
action,
request: JSON.stringify({ __path: request.path, ...request.headers, ...request.body }),
response: JSON.stringify(error),
}
}),
})
);
}

View File

@ -1,18 +0,0 @@
import { BaseEntity, Column, Entity, Index, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';
@Entity('attributes')
export class Attribute extends BaseEntity {
@PrimaryGeneratedColumn({ name: 'id' })
id: string;
@Column({ name: 'arn', nullable: false })
@Index()
arn: string;
@Column({ name: 'name', nullable: false })
name: string;
@Column({ name: 'value', nullable: false })
value: string;
}

View File

@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Attribute } from './attributes.entity';
import { CreateAttributeDto } from './create-attribute.dto';
import { Attribute, Prisma } from '@prisma/client';
import { PrismaService } from '../_prisma/prisma.service';
import { breakdownAwsQueryParam } from '../util/breakdown-aws-query-param';
const ResourcePolicyName = 'ResourcePolicy';
@ -10,62 +10,75 @@ const ResourcePolicyName = 'ResourcePolicy';
export class AttributesService {
constructor(
@InjectRepository(Attribute)
private readonly repo: Repository<Attribute>,
private readonly prismaService: PrismaService,
) {}
async getByArn(arn: string): Promise<Attribute[]> {
return await this.repo.find({ where: { arn }});
return await this.prismaService.attribute.findMany({ where: { arn }});
}
async getResourcePolicyByArn(arn: string): Promise<Attribute> {
return await this.repo.findOne({ where: { arn, name: ResourcePolicyName }});
async getResourcePolicyByArn(arn: string): Promise<Attribute | null> {
return await this.prismaService.attribute.findFirst({ where: { arn, name: ResourcePolicyName }});
}
async getByArnAndName(arn: string, name: string): Promise<Attribute> {
return await this.repo.findOne({ where: { arn, name }});
async getByArnAndName(arn: string, name: string): Promise<Attribute | null> {
return await this.prismaService.attribute.findFirst({ where: { arn, name }});
}
async getByArnAndNames(arn: string, names: string[]): Promise<Attribute[]> {
return await this.repo.find({ where: { arn, name: In(names) }});
return await this.prismaService.attribute.findMany({ where: {
arn,
name: {
in: names
}
}});
}
async createResourcePolicy(arn: string, value: string): Promise<Attribute> {
return await this.create({arn, value, name: ResourcePolicyName });
}
async create(dto: CreateAttributeDto): Promise<Attribute> {
return await this.repo.save(dto);
async create(data: Prisma.AttributeCreateArgs['data']): Promise<Attribute> {
return await this.prismaService.attribute.create({ data });
}
async deleteByArn(arn: string) {
await this.repo.delete({ arn });
async deleteByArn(arn: string): Promise<void> {
await this.prismaService.attribute.deleteMany({ where: { arn } });
}
async deleteByArnAndName(arn: string, name: string) {
await this.repo.delete({ arn, name });
async deleteByArnAndName(arn: string, name: string): Promise<void> {
await this.prismaService.attribute.deleteMany({ where: { arn, name } });
}
async createMany(arn: string, records: { key: string, value: string }[]): Promise<void> {
for (const record of records) {
await this.create({ arn, name: record.key, value: record.value });
}
await this.prismaService.attribute.createMany({
data: records.map(r => ({
name: r.key,
value: r.value,
arn,
}))
});
}
static attributePairs(queryParams: Record<string, string>): { key: string, value: string }[] {
const pairs = [null];
const pairs: { key: string, value: string }[] = [];
for (const param of Object.keys(queryParams)) {
const [type, _, idx, slot] = param.split('.');
const components = breakdownAwsQueryParam(param);
if (!components) {
return [];
}
const [type, _, idx, slot] = components;
if (type === 'Attributes') {
if (!pairs[+idx]) {
pairs[+idx] = { key: '', value: ''};
if (!pairs[idx]) {
pairs[idx] = { key: '', value: ''};
}
pairs[+idx][slot] = queryParams[param];
pairs[idx][slot] = queryParams[param];
}
}
pairs.shift();
return pairs;
}

View File

@ -1,5 +0,0 @@
export interface CreateAttributeDto {
arn: string;
name: string;
value: string;
}

View File

@ -1,18 +0,0 @@
import { BaseEntity, Column, Entity, Index, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';
@Entity('tags')
export class Tag extends BaseEntity {
@PrimaryGeneratedColumn({ name: 'id' })
id: string;
@Column({ name: 'arn', nullable: false})
@Index()
arn: string;
@Column({ name: 'name', nullable: false })
name: string;
@Column({ name: 'value', nullable: false })
value: string;
}

View File

@ -1,53 +1,61 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Tag } from './tags.entity';
import { CreateTagDto } from './create-tag.dto';
import { Prisma, Tag } from '@prisma/client';
import { PrismaService } from '../_prisma/prisma.service';
import { breakdownAwsQueryParam } from '../util/breakdown-aws-query-param';
@Injectable()
export class TagsService {
constructor(
@InjectRepository(Tag)
private readonly repo: Repository<Tag>,
private readonly prismaService: PrismaService,
) {}
async getByArn(arn: string): Promise<Tag[]> {
return await this.repo.find({ where: { arn }});
return await this.prismaService.tag.findMany({ where: { arn }});
}
async create(dto: CreateTagDto): Promise<Tag> {
return await this.repo.save(dto);
async create(data: Prisma.TagCreateArgs['data']): Promise<Tag> {
return await this.prismaService.tag.create({ data })
}
async createMany(arn: string, records: { Key: string, Value: string }[]): Promise<void> {
for (const record of records) {
await this.create({ arn, name: record.Key, value: record.Value });
}
async createMany(arn: string, records: { key: string, value: string }[]): Promise<void> {
await this.prismaService.attribute.createMany({
data: records.map(r => ({
name: r.key,
value: r.value,
arn,
}))
});
}
async deleteByArn(arn: string) {
await this.repo.delete({ arn });
async deleteByArn(arn: string): Promise<void> {
await this.prismaService.tag.deleteMany({ where: { arn } });
}
async deleteByArnAndName(arn: string, name: string) {
await this.repo.delete({ arn, name });
async deleteByArnAndName(arn: string, name: string): Promise<void> {
await this.prismaService.tag.deleteMany({ where: { arn, name } });
}
static tagPairs(queryParams: Record<string, string>): { Key: string, Value: string }[] {
const pairs = [null];
static tagPairs(queryParams: Record<string, string>): { key: string, value: string }[] {
const pairs: { key: string, value: string }[] = [];
for (const param of Object.keys(queryParams)) {
const [type, _, idx, slot] = param.split('.');
const components = breakdownAwsQueryParam(param);
if (!components) {
return [];
}
const [type, _, idx, slot] = components;
if (type === 'Tags') {
if (!pairs[+idx]) {
pairs[+idx] = { Key: '', Value: ''};
pairs[+idx] = { key: '', value: ''};
}
pairs[+idx][slot] = queryParams[param];
}
}
pairs.shift();
return pairs;
}

View File

@ -1,19 +1,19 @@
import { ClassSerializerInterceptor } from '@nestjs/common';
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import * as morgan from 'morgan';
import { CommonConfig } from './config/common-config.interface';
import { ConfigService } from '@nestjs/config';
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { CommonConfig } from './config/common-config.interface';
const bodyParser = require('body-parser');
(async () => {
const app = await NestFactory.create(AppModule);
app.use(morgan('dev'));
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
app.use(bodyParser.json({ type: 'application/x-amz-json-1.1'}));
const configService: ConfigService<CommonConfig> = app.get(ConfigService)
const configService: ConfigService<CommonConfig, true> = app.get(ConfigService);
await app.listen(configService.get('PORT'), () => console.log(`Listening on port ${configService.get('PORT')}`));
})();

View File

@ -1,165 +0,0 @@
import { CreateTopicCommand, CreateTopicCommandOutput, GetSubscriptionAttributesCommand, GetTopicAttributesCommand, ListTagsForResourceCommand, ListTopicsCommand, PublishCommand, SNSClient, SubscribeCommand, SubscribeCommandOutput } from '@aws-sdk/client-sns';
import { TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Tag } from '../../aws-shared-entities/tags.entity';
import { SnsTopicSubscription } from '../sns-topic-subscription.entity';
import { SnsTopic } from '../sns-topic.entity';
describe('SNS Module', () => {
let snsClient: SNSClient;
beforeAll(async () => {
snsClient = new SNSClient({
endpoint: globalThis.__ENDPOINT__,
});
});
const describeCleanup = async () => {
const testModule: TestingModule = globalThis.__TESTMODULE__;
const snsTopicRepo = testModule.get<Repository<SnsTopic>>(getRepositoryToken(SnsTopic));
await snsTopicRepo.delete({});
const subscriptionRepo = testModule.get<Repository<SnsTopicSubscription>>(getRepositoryToken(SnsTopicSubscription));
await subscriptionRepo.delete({});
const tagsRepo = testModule.get<Repository<Tag>>(getRepositoryToken(Tag));
await tagsRepo.delete({});
};
describe('creation', () => {
afterAll(describeCleanup);
it('can create a topic', async () => {
const response = await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-1',
}));
expect(response.TopicArn).toBe('arn:aws:sns:us-east-1:000000000000:test-topic-1');
});
it('can subscribe', async () => {
const topicResponse = await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-2',
}));
const response = await snsClient.send(new SubscribeCommand({
TopicArn: topicResponse.TopicArn,
Protocol: 'https',
Endpoint: 'https://google.com',
}));
expect(response.SubscriptionArn).toBeDefined();
});
it('can publish', async () => {
const topicResponse = await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-3',
}));
const response = await snsClient.send(new PublishCommand({
Message: "hello world",
TopicArn: topicResponse.TopicArn,
}));
expect(response.MessageId).toBeDefined();
});
});
describe('reading', () => {
afterAll(describeCleanup);
let subscribedTopic: CreateTopicCommandOutput;
let subscription: SubscribeCommandOutput;
beforeAll(async () => {
subscribedTopic = await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-4',
Tags: [{ Key: 'V_a', Value: 'a' }, { Key: 'V_b', Value: 'b', }]
}));
await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-5',
}));
await snsClient.send(new CreateTopicCommand({
Name: 'test-topic-6',
}));
subscription = await snsClient.send(new SubscribeCommand({
TopicArn: subscribedTopic.TopicArn,
Protocol: 'https',
Endpoint: 'https://google.com',
}));
});
it('can get subscription attributes', async () => {
const response = await snsClient.send(new GetSubscriptionAttributesCommand({
SubscriptionArn: subscription.SubscriptionArn,
}));
expect(response.Attributes).toEqual(expect.objectContaining({
"ConfirmationWasAuthenticated": "true",
"PendingConfirmation": "false",
"Owner": "000000000000",
"SubscriptionArn": subscription.SubscriptionArn,
"TopicArn": subscribedTopic.TopicArn,
"TracingConfig": "PassThrough"
}));
});
it('can get topic attributes', async () => {
const response = await snsClient.send(new GetTopicAttributesCommand({
TopicArn: subscribedTopic.TopicArn,
}));
expect(response.Attributes).toEqual(expect.objectContaining({
"DisplayName": 'test-topic-4',
"Owner": "000000000000",
"SubscriptionsConfirmed": "1",
"SubscriptionsDeleted": "0",
"SubscriptionsPending": "0",
"TopicArn": subscribedTopic.TopicArn,
"TracingConfig": "PassThrough"
}));
});
it('can list tags for resource', async () => {
const response = await snsClient.send(new ListTagsForResourceCommand({
ResourceArn: subscribedTopic.TopicArn,
}));
expect(response.Tags).toHaveLength(2);
const map = new Map(response.Tags.map(({ Key, Value }) => [ Key, Value ]));
expect(map.get('V_a')).toBe('a');
expect(map.get('V_b')).toBe('b');
});
it('can list all topics', async () => {
const response = await snsClient.send(new ListTopicsCommand({}));
expect(response.Topics).toHaveLength(3);
});
});
// describe('updating', () => {
// afterAll(describeCleanup);
// });
// describe('deleting', () => {
// afterAll(describeCleanup);
// });
});

View File

@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import { SnsTopic } from './sns-topic.entity';
import * as Joi from 'joi';
import { TagsService } from '../aws-shared-entities/tags.service';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
Name: string;
@ -15,8 +15,7 @@ type QueryParams = {
export class CreateTopicHandler extends AbstractActionHandler<QueryParams> {
constructor(
@InjectRepository(SnsTopic)
private readonly snsTopicRepo: Repository<SnsTopic>,
private readonly prismaService: PrismaService,
private readonly tagsService: TagsService,
) {
super();
@ -30,15 +29,18 @@ export class CreateTopicHandler extends AbstractActionHandler<QueryParams> {
const { Name: name } = params;
const topic = await this.snsTopicRepo.create({
name,
accountId: awsProperties.accountId,
region: awsProperties.region,
}).save();
const topic = await this.prismaService.snsTopic.create({
data: {
name,
accountId: awsProperties.accountId,
region: awsProperties.region,
},
});
const tags = TagsService.tagPairs(params);
await this.tagsService.createMany(topic.topicArn, tags);
const arn = ArnUtil.fromTopic(topic);
await this.tagsService.createMany(arn, tags);
return { TopicArn: topic.topicArn };
return { TopicArn: arn };
}
}

View File

@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import * as Joi from 'joi';
import { SnsTopicSubscription } from './sns-topic-subscription.entity';
import { AttributesService } from '../aws-shared-entities/attributes.service';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
SubscriptionArn: string;
@ -15,8 +15,7 @@ type QueryParams = {
export class GetSubscriptionAttributesHandler extends AbstractActionHandler {
constructor(
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscriptionRepo: Repository<SnsTopicSubscription>,
private readonly prismaService: PrismaService,
private readonly attributeService: AttributesService,
) {
super();
@ -29,7 +28,7 @@ export class GetSubscriptionAttributesHandler extends AbstractActionHandler {
protected async handle({ SubscriptionArn }: QueryParams, awsProperties: AwsProperties) {
const id = SubscriptionArn.split(':').pop();
const subscription = await this.snsTopicSubscriptionRepo.findOne({ where: { id }});
const subscription = await this.prismaService.snsTopicSubscription.findFirst({ where: { id }});
if (!subscription) {
return;
@ -39,13 +38,13 @@ export class GetSubscriptionAttributesHandler extends AbstractActionHandler {
const attributeMap = attributes.reduce((m, a) => {
m[a.name] = a.value;
return m;
}, {});
}, {} as Record<string, string>);
const response = {
ConfirmationWasAuthenticated: 'true',
PendingConfirmation: 'false',
Owner: subscription.accountId,
SubscriptionArn: subscription.arn,
SubscriptionArn: ArnUtil.fromTopicSub(subscription),
TopicArn: subscription.topicArn,
...attributeMap,
TracingConfig: attributeMap['TracingConfig'] ?? 'PassThrough',

View File

@ -1,12 +1,11 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
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';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
TopicArn: string;
@ -16,10 +15,7 @@ type QueryParams = {
export class GetTopicAttributesHandler extends AbstractActionHandler {
constructor(
@InjectRepository(SnsTopic)
private readonly snsTopicRepo: Repository<SnsTopic>,
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscriptionRepo: Repository<SnsTopicSubscription>,
private readonly prismaService: PrismaService,
private readonly attributeService: AttributesService,
) {
super();
@ -32,7 +28,7 @@ export class GetTopicAttributesHandler extends AbstractActionHandler {
protected async handle({ TopicArn }: QueryParams, awsProperties: AwsProperties) {
const name = TopicArn.split(':').pop();
const topic = await this.snsTopicRepo.findOne({ where: { name }});
const topic = await this.prismaService.snsTopic.findFirst({ where: { name }});
if (!topic) {
throw new BadRequestException();
@ -42,9 +38,9 @@ export class GetTopicAttributesHandler extends AbstractActionHandler {
const attributeMap = attributes.reduce((m, a) => {
m[a.name] = a.value;
return m;
}, {});
}, {} as Record<string, string>);
const subscriptionCount = await this.snsTopicSubscriptionRepo.count({ where: { topicArn: TopicArn } });
const subscriptionCount = await this.prismaService.snsTopicSubscription.count({ where: { topicArn: TopicArn } });
const response = {
DisplayName: topic.name,
@ -52,7 +48,7 @@ export class GetTopicAttributesHandler extends AbstractActionHandler {
SubscriptionsConfirmed: `${subscriptionCount}`,
SubscriptionsDeleted: '0',
SubscriptionsPending: '0',
TopicArn: topic.topicArn,
TopicArn: ArnUtil.fromTopic(topic),
...attributeMap,
TracingConfig: attributeMap['TracingConfig'] ?? 'PassThrough',
}

View File

@ -1,10 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import { SnsTopic } from './sns-topic.entity';
import * as Joi from 'joi';
import { TagsService } from '../aws-shared-entities/tags.service';
type QueryParams = {

View File

@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import { SnsTopic } from './sns-topic.entity';
import * as Joi from 'joi';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
NextToken: number;
@ -14,8 +14,7 @@ type QueryParams = {
export class ListTopicsHandler extends AbstractActionHandler {
constructor(
@InjectRepository(SnsTopic)
private readonly snsTopicRepo: Repository<SnsTopic>,
private readonly prismaService: PrismaService,
) {
super();
}
@ -26,8 +25,11 @@ export class ListTopicsHandler extends AbstractActionHandler {
protected async handle({ NextToken: skip }: QueryParams, awsProperties: AwsProperties) {
const [ topics, total ] = await this.snsTopicRepo.findAndCount({ order: { name: 'DESC' }, take: 100, skip });
const response = { Topics: { member: topics.map(t => ({ TopicArn: t.topicArn } ))} };
const [ topics, total ] = await Promise.all([
this.prismaService.snsTopic.findMany({ orderBy: { name: 'desc' }, take: 100, skip }),
this.prismaService.snsTopic.count(),
]);
const response = { Topics: { member: topics.map(t => ({ TopicArn: ArnUtil.fromTopic(t) } ))} };
if (total >= 100) {
return {

View File

@ -1,14 +1,14 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as Joi from 'joi';
import * as uuid from 'uuid';
import { PrismaService } from '../_prisma/prisma.service';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import * as Joi from 'joi';
import { SqsQueueEntryService } from '../sqs/sqs-queue-entry.service';
import { SnsTopicSubscription } from './sns-topic-subscription.entity';
import * as uuid from 'uuid';
import { AttributesService } from '../aws-shared-entities/attributes.service';
import { SqsQueueEntryService } from '../sqs/sqs-queue-entry.service';
import { SqsQueue } from '../sqs/sqs-queue.entity';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
TopicArn: string;
@ -21,8 +21,7 @@ type QueryParams = {
export class PublishHandler extends AbstractActionHandler<QueryParams> {
constructor(
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscriptionRepo: Repository<SnsTopicSubscription>,
private readonly prismaService: PrismaService,
private readonly sqsQueueEntryService: SqsQueueEntryService,
private readonly attributeService: AttributesService,
) {
@ -46,13 +45,14 @@ export class PublishHandler extends AbstractActionHandler<QueryParams> {
}
const MessageId = uuid.v4();
const subscriptions = await this.snsTopicSubscriptionRepo.find({ where: { topicArn: arn } });
const subscriptions = await this.prismaService.snsTopicSubscription.findMany({ where: { topicArn: arn } });
const topicAttributes = await this.attributeService.getByArn(arn);
for (const sub of subscriptions) {
const attributes = await this.attributeService.getByArn(sub.arn);
if (sub.protocol === 'sqs') {
const { value: isRaw } = attributes.find(a => a.name === 'RawMessageDelivery');
const subArn = ArnUtil.fromTopicSub(sub);
const attributes = await this.attributeService.getByArn(subArn);
if (sub.protocol === 'sqs' && sub.endpoint) {
const { value: isRaw } = attributes.find(a => a.name === 'RawMessageDelivery') ?? {};
const [queueAccountId, queueName] = SqsQueue.tryGetAccountIdAndNameFromPathOrArn(sub.endpoint);
const message = isRaw === 'true' ? Message : JSON.stringify({
@ -65,7 +65,7 @@ export class PublishHandler extends AbstractActionHandler<QueryParams> {
SignatureVersion: topicAttributes.find(a => a.name === 'SignatureVersion')?.value ?? '1',
Signature: '',
SigningCertURL: '',
UnsubscribeURL: `${awsProperties.host}/?Action=Unsubscribe&SubscriptionArn=${sub.arn}`,
UnsubscribeURL: `${awsProperties.host}/?Action=Unsubscribe&SubscriptionArn=${subArn}`,
});
await this.sqsQueueEntryService.publish(queueAccountId, queueName, message);

View File

@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import * as Joi from 'joi';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import * as Joi from 'joi';
import { AttributesService } from '../aws-shared-entities/attributes.service';
type QueryParams = {

View File

@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import * as Joi from 'joi';
import { AbstractActionHandler, AwsProperties, Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import * as Joi from 'joi';
import { AttributesService } from '../aws-shared-entities/attributes.service';
type QueryParams = {

View File

@ -1,27 +0,0 @@
import { BaseEntity, Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';
@Entity('sns_topic_subscription')
export class SnsTopicSubscription extends BaseEntity {
@PrimaryColumn({ name: 'id' })
id: string;
@Column({ name: 'topic_arn' })
topicArn: string;
@Column({ name: 'endpoint', nullable: true })
endpoint: string;
@Column({ name: 'protocol' })
protocol: string;
@Column({ name: 'account_id', nullable: false })
accountId: string;
@Column({ name: 'region', nullable: false })
region: string;
get arn() {
return `${this.topicArn}:${this.id}`;
}
}

View File

@ -1,18 +0,0 @@
import { BaseEntity, Column, Entity, PrimaryColumn } from 'typeorm';
@Entity('sns_topic')
export class SnsTopic extends BaseEntity {
@PrimaryColumn({ name: 'name' })
name: string;
@Column({ name: 'account_id', nullable: false })
accountId: string;
@Column({ name: 'region', nullable: false })
region: string;
get topicArn(): string {
return `arn:aws:sns:${this.region}:${this.accountId}:${this.name}`;
}
}

View File

@ -1,9 +1,7 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AbstractActionHandler, Format } from '../abstract-action.handler';
import { Format } from '../abstract-action.handler';
import { Action } from '../action.enum';
import { AwsSharedEntitiesModule } from '../aws-shared-entities/aws-shared-entities.module';
import { ExistingActionHandlers } from '../default-action-handler/default-action-handler.constants';
import { DefaultActionHandlerProvider } from '../default-action-handler/default-action-handler.provider';
import { ExistingActionHandlersProvider } from '../default-action-handler/existing-action-handlers.provider';
import { SqsModule } from '../sqs/sqs.module';
@ -15,11 +13,10 @@ import { ListTopicsHandler } from './list-topics.handler';
import { PublishHandler } from './publish.handler';
import { SetSubscriptionAttributesHandler } from './set-subscription-attributes.handler';
import { SetTopicAttributesHandler } from './set-topic-attributes.handler';
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';
import { PrismaModule } from '../_prisma/prisma.module';
const handlers = [
CreateTopicHandler,
@ -81,8 +78,8 @@ const actions = [
@Module({
imports: [
TypeOrmModule.forFeature([SnsTopic, SnsTopicSubscription]),
AwsSharedEntitiesModule,
PrismaModule,
SqsModule,
],
providers: [

View File

@ -1,15 +1,13 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { randomUUID } from 'crypto';
import * as Joi from 'joi';
import { PrismaService } from '../_prisma/prisma.service';
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';
import { SqsQueueEntryService } from '../sqs/sqs-queue-entry.service';
import { SqsQueue } from '../sqs/sqs-queue.entity';
import { TagsService } from '../aws-shared-entities/tags.service';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
TopicArn: string;
@ -21,11 +19,9 @@ type QueryParams = {
export class SubscribeHandler extends AbstractActionHandler<QueryParams> {
constructor(
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscription: Repository<SnsTopicSubscription>,
private readonly prismaService: PrismaService,
private readonly tagsService: TagsService,
private readonly attributeService: AttributesService,
private readonly sqsQueueEntryService: SqsQueueEntryService,
) {
super();
}
@ -40,21 +36,25 @@ export class SubscribeHandler extends AbstractActionHandler<QueryParams> {
protected async handle(params: QueryParams, awsProperties: AwsProperties) {
const subscription = await this.snsTopicSubscription.create({
id: uuid.v4(),
topicArn: params.TopicArn,
protocol: params.Protocol,
endpoint: params.Endpoint,
accountId: awsProperties.accountId,
region: awsProperties.region,
}).save();
const subscription = await this.prismaService.snsTopicSubscription.create({
data: {
id: randomUUID(),
topicArn: params.TopicArn,
protocol: params.Protocol,
endpoint: params.Endpoint,
accountId: awsProperties.accountId,
region: awsProperties.region,
}
});
const arn = ArnUtil.fromTopicSub(subscription);
const tags = TagsService.tagPairs(params);
await this.tagsService.createMany(subscription.arn, tags);
await this.tagsService.createMany(arn, tags);
const attributes = AttributesService.attributePairs(params);
await this.attributeService.createMany(subscription.arn, attributes);
await this.attributeService.createMany(arn, attributes);
return { SubscriptionArn: subscription.arn };
return { SubscriptionArn: arn };
}
}

View File

@ -1,13 +1,11 @@
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';
import { PrismaService } from '../_prisma/prisma.service';
import { ArnUtil } from '../util/arn-util.static';
type QueryParams = {
SubscriptionArn: string;
@ -17,8 +15,7 @@ type QueryParams = {
export class UnsubscribeHandler extends AbstractActionHandler<QueryParams> {
constructor(
@InjectRepository(SnsTopicSubscription)
private readonly snsTopicSubscription: Repository<SnsTopicSubscription>,
private readonly prismaService: PrismaService,
private readonly tagsService: TagsService,
private readonly attributeService: AttributesService,
) {
@ -34,10 +31,16 @@ export class UnsubscribeHandler extends AbstractActionHandler<QueryParams> {
protected async handle(params: QueryParams, awsProperties: AwsProperties) {
const id = params.SubscriptionArn.split(':').pop();
const subscription = await this.snsTopicSubscription.findOne({ where: { id } });
const subscription = await this.prismaService.snsTopicSubscription.findFirst({ where: { id } });
await this.tagsService.deleteByArn(subscription.arn);
await this.attributeService.deleteByArn(subscription.arn);
await this.snsTopicSubscription.delete({ id });
if (!subscription) {
return;
}
const arn = ArnUtil.fromTopicSub(subscription);
await this.tagsService.deleteByArn(arn);
await this.attributeService.deleteByArn(arn);
await this.prismaService.snsTopicSubscription.delete({ where: { id } });
}
}

View File

@ -0,0 +1,12 @@
import { SnsTopic, SnsTopicSubscription } from "@prisma/client";
export class ArnUtil {
static fromTopic(topic: SnsTopic): string {
return `arn:aws:sns:${topic.region}:${topic.accountId}:${topic.name}`;
}
static fromTopicSub(topicSub: SnsTopicSubscription): string {
return `${topicSub.topicArn}:${topicSub.id}`;
}
}

View File

@ -0,0 +1,11 @@
export const breakdownAwsQueryParam = (paramKey: string): [string, unknown, number, 'key' | 'value'] | null => {
const parts = paramKey.split('.');
if (parts.length !== 4) {
return null;
}
const [type, _, idx, slot] = parts;
return [type, _, +idx, slot as 'key' | 'value'];
}

View File

@ -6,12 +6,13 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"target": "es2023",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"resolveJsonModule": true,
"strict": true
},
"exclude": ["node_modules"],
"include": ["src/**/*.ts"]

6004
yarn.lock

File diff suppressed because it is too large Load Diff