Skip to content

Commit

Permalink
Adding stsUtils + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosGamero committed Oct 23, 2024
1 parent 2557b24 commit 51f5dff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
16 changes: 0 additions & 16 deletions packages/sns/lib/utils/snsAttributeUtils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:<region>:<account-id>:<topic-name>
* 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}`
}
20 changes: 20 additions & 0 deletions packages/sns/lib/utils/stsUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -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"`)
})
})
})
14 changes: 14 additions & 0 deletions packages/sns/lib/utils/stsUtils.ts
Original file line number Diff line number Diff line change
@@ -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:<region>:<account-id>:<topic-name>
* 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}`
}

0 comments on commit 51f5dff

Please sign in to comment.