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

Commit

Permalink
feat: add encrypted bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
Vahor committed Dec 26, 2023
1 parent 3a2f163 commit a53fc67
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 22 deletions.
10 changes: 8 additions & 2 deletions aws/policies/pulumi.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"s3:PutBucketPublicAccessBlock",
"s3:PutBucketPolicy",
"s3:PutEncryptionConfiguration",
"s3:DeleteBucketPolicy"
"s3:DeleteBucketPolicy",
"s3:PutBucketTagging"
],
"Resource": [
"arn:aws:s3:::files.pedaki.fr",
"arn:aws:s3:::encrypted.pedaki.fr",
"arn:aws:s3:::static.pedaki.fr"
]
},
Expand All @@ -28,7 +30,9 @@
"iam:GetUserPolicy",
"iam:PutUserPolicy",
"iam:ListAccessKeys",
"iam:ListAttachedUserPolicies"
"iam:ListAttachedUserPolicies",
"iam:TagUser",
"iam:UntagUser"
],
"Resource": [
"*"
Expand Down Expand Up @@ -56,6 +60,8 @@
"rds:DescribeDBParameters",
"rds:CreateDBParameterGroup",
"rds:ListTagsForResource",
"rds:AddTagsToResource",
"rds:RemoveTagsFromResource",
"rds:DescribeDBParameterGroups"
],
"Resource": "*"
Expand Down
4 changes: 4 additions & 0 deletions src/aws/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const TAGS = {
team: 'pedaki',
repository: 'infrastructure',
};
2 changes: 2 additions & 0 deletions src/aws/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createEncryptedBucket } from './resources/encrypted-bucket';
import { createFilesBucket } from './resources/files-bucket';
import { createRdsParameterGroup } from './resources/rds-group';
import { createStaticBucket } from './resources/static-bucket';
import { createSharedParameters } from './secrets/parameters';
import { createApiPulumiUser } from './users/api-pulumi';

createFilesBucket();
createEncryptedBucket();
createStaticBucket();
createApiPulumiUser();
createSharedParameters();
Expand Down
81 changes: 81 additions & 0 deletions src/aws/resources/encrypted-bucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as aws from '@pulumi/aws';
import * as cloudflare from '@pulumi/cloudflare';
import { env } from '../../env';
import { TAGS } from '../constants';

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

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

const policy = new aws.s3.BucketPolicy(
'encrypted-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: 'DenyUnEncryptedObjectDownloads',
Effect: 'Deny',
Principal: '*',
Action: 's3:GetObject',
Resource: `${arn}/*`,
Condition: {
Bool: {
'aws:SecureTransport': 'false',
},
},
},
],
}),
),
},
{ dependsOn: [publicAccessBlock] },
);

const record = new cloudflare.Record('encrypted.pedaki.fr', {
name: 'encrypted',
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)`,
});
};
22 changes: 2 additions & 20 deletions src/aws/resources/files-bucket.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import * as aws from '@pulumi/aws';
import * as cloudflare from '@pulumi/cloudflare';
import { env } from '../../env';
import { TAGS } from '../constants';

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

const publicAccessBlock = new aws.s3.BucketPublicAccessBlock(
Expand All @@ -36,18 +30,6 @@ export const createFilesBucket = () => {
// 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',
Expand Down
2 changes: 2 additions & 0 deletions src/aws/resources/rds-group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as aws from '@pulumi/aws';
import { TAGS } from '../constants';

export const createRdsParameterGroup = () => {
const group = new aws.rds.ParameterGroup('rds-pedaki', {
family: 'mysql8.0',
description: 'Shared parameter group',
name: 'rds-pedaki',
parameters: [{ name: 'require_secure_transport', value: '1' }],
tags: TAGS,
});
};
3 changes: 3 additions & 0 deletions src/aws/resources/static-bucket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as aws from '@pulumi/aws';
import * as cloudflare from '@pulumi/cloudflare';
import { env } from '../../env';
import { TAGS } from '../constants';

export const createStaticBucket = () => {
const bucket = new aws.s3.Bucket('static.pedaki.fr', {
bucket: 'static.pedaki.fr',
acl: 'public-read',
tags: TAGS,
});

const publicAccessBlock = new aws.s3.BucketPublicAccessBlock(
Expand Down
2 changes: 2 additions & 0 deletions src/aws/secrets/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as aws from '@pulumi/aws';
import { env } from '../../env';
import { TAGS } from '../constants';

export const createSharedParameters = () => {
createSecret(
Expand Down Expand Up @@ -67,6 +68,7 @@ function createSecret(name: string, description: string, value: string) {
description,
type: 'SecureString',
value: value,
tags: TAGS,
});

return secret.name;
Expand Down
4 changes: 4 additions & 0 deletions src/aws/users/api-pulumi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as aws from '@pulumi/aws';
import { TAGS } from '../constants';

export const createApiPulumiUser = () => {
// IAM user that will be responsible to create the ec2, rds, etc. for the whole stack
const user = new aws.iam.User('api-pulumi', {
name: 'api-pulumi',
tags: TAGS,
});

const _ = new aws.iam.UserPolicy('api-pulumi-policy', {
Expand Down Expand Up @@ -69,6 +71,8 @@ export const createApiPulumiUser = () => {
'arn:aws:s3:::files.pedaki.fr/*',
'arn:aws:s3:::static.pedaki.fr',
'arn:aws:s3:::static.pedaki.fr/*',
'arn:aws:s3:::encrypted.pedaki.fr',
'arn:aws:s3:::encrypted.pedaki.fr/*',
],
},
],
Expand Down

0 comments on commit a53fc67

Please sign in to comment.