generated from szikszail/npm-package
/
s3.ts
140 lines (130 loc) · 3.74 KB
/
s3.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Match } from "aws-cdk-lib/assertions";
import { CfnBucket, CfnBucketPolicy } from "aws-cdk-lib/aws-s3";
import { AdvancedMatcher } from "./advanced-matcher";
import { AdvancedTemplate } from "./advanced-template";
import { RemovableResource } from "./resource";
/**
* A test construct for an S3 BucketPolicy
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.CfnBucketPolicy.html}
*/
export class S3BucketPolicy extends RemovableResource {
private s3Bucket: S3Bucket;
constructor(template: AdvancedTemplate, props?: any) {
super(CfnBucketPolicy.CFN_RESOURCE_TYPE_NAME, template, props);
}
/**
* Sets a matching S3 Bucket the BucketPolicy is connected
* @param s3Bucket The S3 Bucket test construct
* @returns
*/
public forBucket(s3Bucket: S3Bucket) {
this.s3Bucket = s3Bucket;
this.withProperty('Bucket', {
Ref: this.s3Bucket.id,
});
return this;
}
/**
* Sets to match an S3 BucketPolicy with Public Access.
* @returns
*/
public withPublicAccess() {
this.withProperty('PolicyDocument', {
Statement: Match.arrayWith([
Match.objectLike({
Action: 's3:GetObject',
Effect: 'Allow',
Principal: { AWS: '*' },
Resource: AdvancedMatcher.fnJoin(
Match.arrayWith([
this.s3Bucket.arn,
])
)
})
])
});
return this;
}
}
/**
* A test construct for an S3 Bucket
* @see {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.CfnBucket.html}
*/
export class S3Bucket extends RemovableResource {
constructor(template: AdvancedTemplate, props?: any) {
super(CfnBucket.CFN_RESOURCE_TYPE_NAME, template, props);
}
/**
* Sets a matching bucket name
* @param name Either the whole of a partial bucket name
* @returns
*/
public withBucketName(name: string) {
this.withProperty('BucketName', Match.stringLikeRegexp(name));
return this;
}
/**
* Sets a matching website hosting configuration
* @param options The website hosting options
* @returns
*/
public withWebsiteHosting(options: {
redirectTo?: string,
redirectProtocol?: string,
indexDocument?: string,
errorDocument?: string,
} = {}) {
this.withProperty(
'WebsiteConfiguration',
options.redirectTo
? {
RedirectAllRequestsTo: {
HostName: Match.stringLikeRegexp(options.redirectTo),
Protocol: Match.stringLikeRegexp(options.redirectProtocol || 'https'),
}
}
: {
IndexDocument: Match.stringLikeRegexp(options.indexDocument || 'index.html'),
ErrorDocument: Match.stringLikeRegexp(options.errorDocument || 'index.html'),
}
);
return this;
}
/**
* Sets a matching CORS configuration
* @param options The CORS options
* @returns
*/
public withCorsEnabled(options: {
methods?: string | string[],
origins?: string | string[],
} = {}) {
this.withProperty(
'CorsConfiguration',
{
CorsRules: Match.arrayWith([
Match.objectEquals({
AllowedMethods: Array.isArray(options.methods)
? Match.arrayEquals(options.methods)
: Match.arrayWith([options.methods || "GET"]),
AllowedOrigins: Array.isArray(options.origins)
? Match.arrayEquals(options.origins)
: Match.arrayWith([options.origins || "*"]),
})
])
}
);
return this;
}
/**
* Creates a matcher for the website URL of the bucket
*/
get websiteUrl() {
return AdvancedMatcher.fnSelect(
AdvancedMatcher.fnSplit(
AdvancedMatcher.fnGetAtt(this.id, "WebsiteURL")
)
);
}
// TODO: withAutoDeleteObjects
}