Skip to content

Commit 0477c97

Browse files
committed
fix(cos): remove default acl config and support policy config
1 parent 3659639 commit 0477c97

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

__tests__/cos.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ describe('Cos', () => {
1010
};
1111
const bucket = `serverless-cos-test-${process.env.TENCENT_APP_ID}`;
1212
const staticPath = path.join(__dirname, 'static');
13+
const policy = {
14+
Statement: [
15+
{
16+
Principal: { qcs: ['qcs::cam::anyone:anyone'] },
17+
Effect: 'Allow',
18+
Action: [
19+
'name/cos:HeadBucket',
20+
'name/cos:ListMultipartUploads',
21+
'name/cos:ListParts',
22+
'name/cos:GetObject',
23+
'name/cos:HeadObject',
24+
'name/cos:OptionsObject',
25+
],
26+
Resource: [`qcs::cos:${process.env.REGION}:uid/${process.env.TENCENT_APP_ID}:${bucket}/*`],
27+
},
28+
],
29+
version: '2.0',
30+
};
1331
const inputs = {
1432
bucket: bucket,
1533
src: staticPath,
@@ -43,6 +61,9 @@ describe('Cos', () => {
4361
force: true,
4462
protocol: 'https',
4563
replace: true,
64+
acl: {
65+
permissions: 'public-read',
66+
},
4667
};
4768
const cos = new Cos(credentials, process.env.REGION);
4869

@@ -65,6 +86,29 @@ describe('Cos', () => {
6586
expect(content).toMatch(/Serverless\sFramework/gi);
6687
});
6788

89+
test('should deploy Cos success with policy', async () => {
90+
inputs.acl.permissions = 'private';
91+
inputs.policy = policy;
92+
const res = await cos.deploy(inputs);
93+
await sleep(1000);
94+
const reqUrl = `https://${bucket}.cos.${process.env.REGION}.myqcloud.com/index.html`;
95+
const content = await request.get(reqUrl);
96+
expect(res).toEqual(inputs);
97+
expect(content).toMatch(/Serverless\sFramework/gi);
98+
});
99+
100+
test('should deploy website success with policy', async () => {
101+
websiteInputs.acl.permissions = 'private';
102+
websiteInputs.policy = policy;
103+
const res = await cos.website(websiteInputs);
104+
await sleep(1000);
105+
const websiteUrl = `${inputs.bucket}.cos-website.${process.env.REGION}.myqcloud.com`;
106+
const reqUrl = `${websiteInputs.protocol}://${websiteUrl}`;
107+
const content = await request.get(reqUrl);
108+
expect(res).toBe(websiteUrl);
109+
expect(content).toMatch(/Serverless\sFramework/gi);
110+
});
111+
68112
test('should remove Cos success', async () => {
69113
await cos.remove(inputs);
70114
try {

src/modules/cos/index.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ class Cos {
183183
}
184184
}
185185

186+
async setPolicy(inputs = {}) {
187+
console.log(`Setting policy for bucket ${inputs.bucket}`);
188+
const setPolicyParams = {
189+
Bucket: inputs.bucket,
190+
Region: this.region,
191+
};
192+
if (inputs.policy) {
193+
setPolicyParams.Policy = inputs.policy;
194+
}
195+
const setPolicyHandler = this.promisify(this.cosClient.putBucketPolicy.bind(this.cosClient));
196+
try {
197+
await setPolicyHandler(setPolicyParams);
198+
} catch (e) {
199+
throw new ApiError({
200+
type: `API_COS_putBucketPolicy`,
201+
message: e.message,
202+
stack: e.stack,
203+
reqId: e.reqId,
204+
code: e.code,
205+
});
206+
}
207+
}
208+
186209
async setTags(inputs = {}) {
187210
console.log(`Setting tags for ${this.region}'s bucket: ${inputs.bucket}`);
188211
const tags = [];
@@ -422,10 +445,6 @@ class Cos {
422445
},
423446
};
424447

425-
if (inputs.cors && inputs.cors.length > 0) {
426-
await this.setAcl(inputs);
427-
}
428-
429448
const setWebsiteHandler = this.promisify(this.cosClient.putBucketWebsite.bind(this.cosClient));
430449
try {
431450
await setWebsiteHandler(staticHostParams);
@@ -615,17 +634,16 @@ class Cos {
615634
force: true,
616635
});
617636

618-
inputs.acl = {
619-
permissions: 'public-read',
620-
grantRead: '',
621-
grantWrite: '',
622-
grantFullControl: '',
623-
};
624-
await this.setAcl(inputs);
637+
if (inputs.acl) {
638+
await this.setAcl(inputs);
639+
}
640+
641+
if (inputs.policy) {
642+
await this.setPolicy(inputs);
643+
}
625644

626645
await this.setWebsite(inputs);
627646

628-
// 对cors进行额外处理
629647
if (inputs.cors) {
630648
await this.setCors(inputs);
631649
}
@@ -681,6 +699,9 @@ class Cos {
681699
if (inputs.acl) {
682700
await this.setAcl(inputs);
683701
}
702+
if (inputs.policy) {
703+
await this.setPolicy(inputs);
704+
}
684705
if (inputs.cors) {
685706
await this.setCors(inputs);
686707
} else {

0 commit comments

Comments
 (0)