-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e5b283
commit cf89e89
Showing
2 changed files
with
56 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,43 @@ | ||
import { GetCallerIdentityCommand, type STSClient } from '@aws-sdk/client-sts' | ||
import { | ||
GetCallerIdentityCommand, | ||
type GetCallerIdentityCommandOutput, | ||
type STSClient, | ||
} from '@aws-sdk/client-sts' | ||
|
||
let callerIdentityPromise: Promise<void> | undefined | ||
let callerIdentityCached: GetCallerIdentityCommandOutput | undefined | ||
|
||
/** | ||
* 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 identityResponse = await getAndCacheCallerIdentity(client) | ||
const region = | ||
typeof client.config.region === 'string' ? client.config.region : await client.config.region() | ||
|
||
return `arn:aws:sns:${region}:${identityResponse.Account}:${topicName}` | ||
} | ||
|
||
export const clearCachedCallerIdentity = () => { | ||
callerIdentityPromise = undefined | ||
callerIdentityCached = undefined | ||
} | ||
|
||
const getAndCacheCallerIdentity = async ( | ||
client: STSClient, | ||
): Promise<GetCallerIdentityCommandOutput> => { | ||
if (!callerIdentityCached) { | ||
if (!callerIdentityPromise) { | ||
callerIdentityPromise = client.send(new GetCallerIdentityCommand({})).then((response) => { | ||
callerIdentityCached = response | ||
}) | ||
} | ||
|
||
await callerIdentityPromise | ||
callerIdentityPromise = undefined | ||
} | ||
|
||
return callerIdentityCached! | ||
} |