Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
fix: add s3 permissions on api user
Browse files Browse the repository at this point in the history
  • Loading branch information
Vahor committed Dec 25, 2023
1 parent 395b7a3 commit 7a58eef
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 62 deletions.
137 changes: 82 additions & 55 deletions src/aws/resources/files-bucket.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,91 @@
import * as aws from '@pulumi/aws';
import * as cloudflare from '@pulumi/cloudflare';
import { env } from '../../env';
import {env} from '../../env';

export const createFilesBucket = () => {
const bucket = new aws.s3.Bucket('files.pedaki.fr', {
bucket: 'files.pedaki.fr',
acl: 'private',
serverSideEncryptionConfiguration: {
rule: {
applyServerSideEncryptionByDefault: {
sseAlgorithm: 'aws:kms',
const bucket = new aws.s3.Bucket('files.pedaki.fr', {
bucket: 'files.pedaki.fr',
acl: 'private',
serverSideEncryptionConfiguration: {
rule: {
applyServerSideEncryptionByDefault: {
sseAlgorithm: 'aws:kms',
},
bucketKeyEnabled: true,
},
},
bucketKeyEnabled: true,
},
},
});
});

const publicAccessBlock = new aws.s3.BucketPublicAccessBlock('files.pedaki.fr-publicAccessBlock', {
bucket: bucket.id,
blockPublicAcls: false,
blockPublicPolicy: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
});
const publicAccessBlock = new aws.s3.BucketPublicAccessBlock(
'files.pedaki.fr-publicAccessBlock',
{
bucket: bucket.id,
blockPublicAcls: false,
blockPublicPolicy: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
},
);

const policy = new aws.s3.BucketPolicy(
'files-bucket-policy',
{
bucket: bucket.id,
policy: bucket.arn.apply(arn =>
JSON.stringify({
// all files should be encrypted
Version: '2012-10-17',
Statement: [
{
Sid: 'DenyUnEncryptedObjectUploads',
Effect: 'Deny',
Principal: '*',
Action: 's3:PutObject',
Resource: `${arn}/*`,
Condition: {
StringNotEquals: {
's3:x-amz-server-side-encryption': 'aws:kms',
},
},
},
],
}),
),
},
{ dependsOn: [publicAccessBlock] },
);
const policy = new aws.s3.BucketPolicy(
'files-bucket-policy',
{
bucket: bucket.id,
policy: bucket.arn.apply(arn =>
JSON.stringify({
// all files should be encrypted
Version: '2012-10-17',
Statement: [
{
Sid: 'DenyUnEncryptedObjectUploads',
Effect: 'Deny',
Principal: '*',
Action: 's3:PutObject',
Resource: `${arn}/*`,
Condition: {
StringNotEquals: {
's3:x-amz-server-side-encryption': 'aws:kms',
},
},
},
{
Sid: 'AllowPublicReadAccess',
Effect: 'Allow',
Principal: '*',
Action: 's3:GetObject',
Resource: `${arn}/*`,
Condition: {
StringEquals: {
's3:ExistingObjectTag/public': 'true',
},
},
},
{
Sid: 'DenyPublicReadAccess',
Effect: 'Deny',
Principal: '*',
Action: 's3:GetObject',
Resource: `${arn}/*`,
Condition: {
StringNotEquals: {
's3:ExistingObjectTag/public': 'true',
},
},
}
],
}),
),
},
{dependsOn: [publicAccessBlock]},
);

const record = new cloudflare.Record('files.pedaki.fr', {
name: 'files',
type: 'CNAME',
value: bucket.bucketDomainName,
zoneId: env.CLOUDFLARE_ZONE_ID,
proxied: true,
ttl: 1, // TTL must be set to 1 when proxied is true
comment: `pulumi (infrastructure repo)`,
});
const record = new cloudflare.Record('files.pedaki.fr', {
name: 'files',
type: 'CNAME',
value: bucket.bucketDomainName,
zoneId: env.CLOUDFLARE_ZONE_ID,
proxied: true,
ttl: 1, // TTL must be set to 1 when proxied is true
comment: `pulumi (infrastructure repo)`,
});
};
17 changes: 10 additions & 7 deletions src/aws/resources/static-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ export const createStaticBucket = () => {
bucket: 'static.pedaki.fr',
});

const publicAccessBlock = new aws.s3.BucketPublicAccessBlock('static.pedaki.fr-publicAccessBlock', {
bucket: bucket.id,
blockPublicAcls: false,
blockPublicPolicy: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
});
const publicAccessBlock = new aws.s3.BucketPublicAccessBlock(
'static.pedaki.fr-publicAccessBlock',
{
bucket: bucket.id,
blockPublicAcls: true,
ignorePublicAcls: true,
blockPublicPolicy: false,
restrictPublicBuckets: false,
},
);

const _ = new aws.s3.BucketPolicy(
'static-bucket-policy',
Expand Down
11 changes: 11 additions & 0 deletions src/aws/users/api-pulumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ export const createApiPulumiUser = () => {
Effect: 'Allow',
Resource: '*',
},
{
Sid: 'AllowS3',
Action: ['s3:*'],
Effect: 'Allow',
Resource: [
'arn:aws:s3:::files.pedaki.fr',
'arn:aws:s3:::files.pedaki.fr/*',
'arn:aws:s3:::static.pedaki.fr',
'arn:aws:s3:::static.pedaki.fr/*',
],
},
],
},
});
Expand Down

0 comments on commit 7a58eef

Please sign in to comment.