diff --git a/packages/sns/lib/utils/snsAttributeUtils.ts b/packages/sns/lib/utils/snsAttributeUtils.ts index 78acb84e..98b8369d 100644 --- a/packages/sns/lib/utils/snsAttributeUtils.ts +++ b/packages/sns/lib/utils/snsAttributeUtils.ts @@ -1,4 +1,3 @@ -import { GetCallerIdentityCommand, type STSClient } from '@aws-sdk/client-sts' import type { ZodSchema } from 'zod' // See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_version.html @@ -62,18 +61,3 @@ export function generateFilterAttributes( FilterPolicyScope: 'MessageBody', } } - -/** - * Manually builds the ARN of a topic based on the current AWS account and the topic name. - * It follows the following pattern: arn:aws:sns::: - * Doc -> https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html - * - * // TODO: add tests - */ -export const buildTopicArn = async (client: STSClient, topicName: string) => { - const identityResponse = await client.send(new GetCallerIdentityCommand({})) - const region = - typeof client.config.region === 'string' ? client.config.region : await client.config.region() - - return `arn:aws:sns:${region}:${identityResponse.Account}:${topicName}` -} diff --git a/packages/sns/lib/utils/stsUtils.spec.ts b/packages/sns/lib/utils/stsUtils.spec.ts new file mode 100644 index 00000000..8aa0901f --- /dev/null +++ b/packages/sns/lib/utils/stsUtils.spec.ts @@ -0,0 +1,20 @@ +import type { STSClient } from '@aws-sdk/client-sts' +import { beforeAll, describe, expect, it } from 'vitest' +import { registerDependencies } from '../../test/utils/testContext' +import { buildTopicArn } from './stsUtils' + +describe('stsUtils', () => { + let stsClient: STSClient + + beforeAll(async () => { + const diContainer = await registerDependencies({}, false) + stsClient = diContainer.cradle.stsClient + }) + + describe('buildTopicArn', () => { + it('build ARN for topic', async () => { + const topicName = 'my-test-topic' + await expect(buildTopicArn(stsClient, topicName)).resolves.toMatchInlineSnapshot(`"arn:aws:sns:eu-west-1:000000000000:my-test-topic"`) + }) + }) +}) diff --git a/packages/sns/lib/utils/stsUtils.ts b/packages/sns/lib/utils/stsUtils.ts new file mode 100644 index 00000000..908bda8e --- /dev/null +++ b/packages/sns/lib/utils/stsUtils.ts @@ -0,0 +1,14 @@ +import { GetCallerIdentityCommand, type STSClient } from '@aws-sdk/client-sts' + +/** + * Manually builds the ARN of a topic based on the current AWS account and the topic name. + * It follows the following pattern: arn:aws:sns::: + * Doc -> https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html + */ +export const buildTopicArn = async (client: STSClient, topicName: string) => { + const identityResponse = await client.send(new GetCallerIdentityCommand({})) + const region = + typeof client.config.region === 'string' ? client.config.region : await client.config.region() + + return `arn:aws:sns:${region}:${identityResponse.Account}:${topicName}` +}