Skip to content

Instantly share code, notes, and snippets.

@dmattia
Created October 4, 2021 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmattia/7ac415f36ead6599aa7b39096f4037f8 to your computer and use it in GitHub Desktop.
Save dmattia/7ac415f36ead6599aa7b39096f4037f8 to your computer and use it in GitHub Desktop.
// external
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
const awsAccountId = aws.getCallerIdentity().then(({ accountId }) => accountId);
const accountArn = pulumi.interpolate`arn:aws:iam::${awsAccountId}:root`;
const kmsPolicy = accountArn.apply(
(arn) =>
({
Version: '2012-10-17',
Id: 'airgap_telemetry_policy',
Statement: [
// This is the default KMS policy
{
Sid: 'Enable IAM User Permissions',
Effect: 'Allow',
Principal: {
AWS: arn,
},
Action: 'kms:*',
Resource: '*',
},
// Give SNS permissions to use this key
{
Sid: 'Enable SNS Permissions',
Effect: 'Allow',
Principal: { Service: ['sns.amazonaws.com'] },
Action: ['kms:Decrypt', 'kms:GenerateDataKey*'],
Resource: '*',
},
],
} as aws.iam.PolicyDocument),
);
const kmsKey = new aws.kms.Key(`key`, {
policy: kmsPolicy.apply((policy) => JSON.stringify(policy)),
});
const snsTopic = new aws.sns.Topic('receiver', {
name: 'race-condition',
kmsMasterKeyId: kmsKey.id,
});
const bucket = new aws.s3.Bucket('bucket', {
bucketPrefix: 'race-condition',
});
const policy = new aws.iam.Policy(`policy`, {
name: 'raceConditionPolicy',
path: '/',
policy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
's3:AbortMultipartUpload',
's3:GetBucketLocation',
's3:GetObject',
's3:ListBucket',
's3:ListBucketMultipartUploads',
's3:PutObject',
],
Resource: [bucket.arn, pulumi.interpolate`${bucket.arn}/*`],
},
],
},
});
const role = new aws.iam.Role(`role`, {
name: 'raceConditionRole',
managedPolicyArns: [policy.arn],
assumeRolePolicy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: 'sts:AssumeRole',
Principal: {
Service: 'firehose.amazonaws.com',
},
},
],
},
});
const firehoseStream = new aws.kinesis.FirehoseDeliveryStream(
'raw_data_stream',
{
name: 'raceConditionStream',
destination: 's3',
s3Configuration: {
roleArn: role.arn,
bucketArn: bucket.arn,
},
},
);
const snsPolicy = new aws.iam.Policy(`sns_policy`, {
name: 'raceConditionPolicySns',
path: '/',
policy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
'firehose:DescribeDeliveryStream',
'firehose:ListDeliveryStreams',
'firehose:ListTagsForDeliveryStream',
'firehose:PutRecord',
'firehose:PutRecordBatch',
],
Resource: [firehoseStream.arn],
},
],
},
});
const snsRole = new aws.iam.Role(`sns_role`, {
name: 'raceConditionRoleSns',
managedPolicyArns: [snsPolicy.arn],
assumeRolePolicy: {
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: 'sts:AssumeRole',
Principal: {
Service: 'sns.amazonaws.com',
},
},
],
},
});
new aws.sns.TopicSubscription('firehose_sub', {
topic: snsTopic.arn,
protocol: 'firehose',
endpoint: firehoseStream.arn,
subscriptionRoleArn: snsRole.arn,
});
@dmattia
Copy link
Author

dmattia commented Oct 4, 2021

Race condition in pulumi/AWS (probably AWS):

Fails with:
Screen Shot 2021-10-04 at 9 11 57 AM

Immediately after that failure, it succeeds with:
Screen Shot 2021-10-04 at 9 14 53 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment