Skip to content

Commit

Permalink
Update infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Apr 5, 2024
1 parent 871a9c7 commit 81296af
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 55 deletions.
31 changes: 25 additions & 6 deletions infrastructure/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ export class CognitoAuthConstruct extends Construct {
super(scope, id);
const userPool = new CfnUserPool(this, "UserPool", {
userPoolName: "S3MediaPlayerUserPool-" + id,
deletionProtection: "INACTIVE",
userPoolAddOns: {
advancedSecurityMode: "ENFORCED"
},
adminCreateUserConfig: {
allowAdminCreateUserOnly: true,
unusedAccountValidityDays: 30,
unusedAccountValidityDays: 7,
inviteMessageTemplate: {
emailSubject: `S3 Media Player ${props.domain} - Invitation`,
emailMessage: `Hi {username}!
Expand All @@ -42,9 +46,10 @@ If you have any questions, please contact ${props.contactEmailAddress}`
},
emailVerificationSubject: `S3 Media Player ${props.domain} - Email verification`,
emailVerificationMessage: `Hi!
To verify your email address at ${props.domain} please enter this code: {####}.
If you have any questions, please contact ${props.contactEmailAddress}`,
To verify your email address at ${props.domain} please enter this code: {####}.
If you have any questions, please contact ${props.contactEmailAddress}`,
mfaConfiguration: "OFF",
enabledMfas: [],
policies: {
passwordPolicy: {
minimumLength: 6,
Expand All @@ -58,8 +63,21 @@ If you have any questions, please contact ${props.contactEmailAddress}`,

const webClient = new CfnUserPoolClient(this, "Client", {
generateSecret: false,
userPoolId: userPool.ref,
allowedOAuthFlowsUserPoolClient: false,
refreshTokenValidity: 30, // days
userPoolId: userPool.ref
accessTokenValidity: 2, // hours
idTokenValidity: 2, // hours
tokenValidityUnits: {
refreshToken: "days",
accessToken: "hours",
idToken: "hours"
},
preventUserExistenceErrors: "ENABLED",
supportedIdentityProviders: ["COGNITO"],
authSessionValidity: 15, // minutes
readAttributes: ["preferred_username"],
writeAttributes: []
});

const identityPool = new CfnIdentityPool(this, "IdentityPool", {
Expand All @@ -72,7 +90,7 @@ If you have any questions, please contact ${props.contactEmailAddress}`,
});

this.userRole = new Role(this, "UserRole", {
maxSessionDuration: Duration.hours(1),
maxSessionDuration: Duration.hours(3),
assumedBy: new FederatedPrincipal("cognito-identity.amazonaws.com", {
"StringEquals": { "cognito-identity.amazonaws.com:aud": identityPool.ref },
"ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "authenticated" }
Expand All @@ -83,7 +101,8 @@ If you have any questions, please contact ${props.contactEmailAddress}`,
groupName: "Users",
description: "Group for users",
roleArn: this.userRole.roleArn,
userPoolId: userPool.ref
userPoolId: userPool.ref,
precedence: 10
});

new CfnIdentityPoolRoleAttachment(this, "RoleAttachment", {
Expand Down
11 changes: 7 additions & 4 deletions infrastructure/lib/static-content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CfnOutput, RemovalPolicy } from 'aws-cdk-lib';
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager';
import { CloudFrontWebDistribution, OriginAccessIdentity, PriceClass, SecurityPolicyProtocol, SSLMethod, ViewerCertificate, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
import { CloudFrontWebDistribution, HttpVersion, OriginAccessIdentity, PriceClass, SecurityPolicyProtocol, SSLMethod, ViewerCertificate, ViewerProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { CfnRecordSetGroup } from 'aws-cdk-lib/aws-route53';
import { BlockPublicAccess, Bucket, BucketPolicy } from 'aws-cdk-lib/aws-s3';
Expand All @@ -20,7 +20,8 @@ export class StaticContentConstruct extends Construct {

const staticContentBucket = new Bucket(this, "Bucket", {
removalPolicy: RemovalPolicy.DESTROY,
blockPublicAccess: BlockPublicAccess.BLOCK_ALL
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
websiteIndexDocument: "index.html"
});

const certificate = Certificate.fromCertificateArn(this, "Certificate", props.sslCertificateArn);
Expand All @@ -30,6 +31,7 @@ export class StaticContentConstruct extends Construct {
});

const cloudfrontDistribution = new CloudFrontWebDistribution(this, "CloudFrontDistribution", {
enabled: true,
comment: `${props.domain}`,
originConfigs: [{
behaviors: [{ isDefaultBehavior: true }],
Expand All @@ -41,12 +43,13 @@ export class StaticContentConstruct extends Construct {
defaultRootObject: "index.html",
enableIpV6: true,
viewerCertificate: ViewerCertificate.fromAcmCertificate(certificate, {
securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2018,
securityPolicy: SecurityPolicyProtocol.TLS_V1_2_2021,
sslMethod: SSLMethod.SNI,
aliases: [props.domain]
}),
priceClass: PriceClass.PRICE_CLASS_100,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
httpVersion: HttpVersion.HTTP2_AND_3,
});

const bucketPolicy = new BucketPolicy(this, "AllowReadAccessToCloudFront", { bucket: staticContentBucket });
Expand Down
64 changes: 23 additions & 41 deletions infrastructure/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"devDependencies": {
"@aws-cdk/assert": "^2.68.0",
"@types/jest": "^29.5.12",
"@types/node": "20.11.25",
"@types/node": "20.12.4",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "~5.4.2"
"typescript": "~5.4.4"
},
"dependencies": {
"aws-cdk": "^2.132.0",
"aws-cdk-lib": "^2.132.0",
"aws-cdk": "^2.135.0",
"aws-cdk-lib": "^2.135.0",
"constructs": "^10.3.0",
"source-map-support": "^0.5.21"
}
Expand Down

0 comments on commit 81296af

Please sign in to comment.