Skip to content

Commit 01e0ad8

Browse files
authored
fix: fix the frontend deployment issue (#39)
fix: fix the frontend deployment issue - create si defined roles for ossdeployment to fix policy name conflicts issue - update schema validation to move required property of delete_force as optional - enable support to specify the access control of bucket Refs: #36 --------- Signed-off-by: seven <[email protected]>
1 parent 93974f3 commit 01e0ad8

File tree

6 files changed

+112
-95
lines changed

6 files changed

+112
-95
lines changed

samples/aliyun-poc-bucket.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: 0.0.1
2+
3+
provider:
4+
name: aliyun
5+
region: cn-chengdu
6+
7+
8+
service: insight-bucket-poc
9+
10+
tags:
11+
owner: geek-fun
12+
13+
buckets:
14+
insight_poc_bucket:
15+
name: insight-poc-bucket
16+
website:
17+
code: dist
18+
index: index.html
19+
error_page: 404.html
20+
error_code: 404

src/parser/bucketParser.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BucketDomain, BucketRaw } from '../types';
1+
import { BucketAccessEnum, BucketDomain, BucketRaw } from '../types';
22

33
export const parseBucket = (buckets: {
44
[key: string]: BucketRaw;
@@ -13,6 +13,9 @@ export const parseBucket = (buckets: {
1313
versioning: bucket.versioning,
1414
security: bucket.security
1515
? {
16+
acl: bucket.security.acl
17+
? (bucket.security.acl as BucketAccessEnum)
18+
: BucketAccessEnum.PRIVATE,
1619
force_delete: bucket.security.force_delete ?? false,
1720
sse_algorithm: bucket.security.sse_algorithm,
1821
sse_kms_master_key_id: bucket.security.sse_kms_master_key_id,

src/stack/rosStack/bucket.ts

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { ActionContext, BucketDomain } from '../../types';
1+
import { ActionContext, BucketAccessEnum, BucketDomain } from '../../types';
22
import * as oss from '@alicloud/ros-cdk-oss';
33
import * as ros from '@alicloud/ros-cdk-core';
44
import { getAssets, replaceReference } from '../../common';
55
import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
66
import path from 'node:path';
7+
import { RosRole } from '@alicloud/ros-cdk-ram';
8+
9+
const aclMap = new Map([
10+
[BucketAccessEnum.PRIVATE, 'private'],
11+
[BucketAccessEnum.PUBLIC_READ, 'public-read'],
12+
[BucketAccessEnum.PUBLIC_READ_WRITE, 'public-read-write'],
13+
]);
714

815
export const resolveBuckets = (
916
scope: ros.Construct,
@@ -13,9 +20,42 @@ export const resolveBuckets = (
1320
if (!buckets) {
1421
return undefined;
1522
}
23+
const bucketSources = buckets.some((bucket) => bucket?.website?.code);
24+
let siAutoOssDeploymentBucketRole: RosRole | undefined;
25+
if (bucketSources) {
26+
siAutoOssDeploymentBucketRole = new RosRole(
27+
scope,
28+
'si_auto_od_bucket_role',
29+
{
30+
roleName: ros.Fn.sub('si-auto-od-bucket-role-${ALIYUN::StackId}'),
31+
description:
32+
'roles created by ServerlessInsight for oss deployment to put files to oss bucket during deployment',
33+
deletionForce: false,
34+
ignoreExisting: false,
35+
assumeRolePolicyDocument: {
36+
version: '1',
37+
statement: [
38+
{
39+
action: 'sts:AssumeRole',
40+
effect: 'Allow',
41+
principal: { service: ['fc.aliyuncs.com'] },
42+
},
43+
],
44+
},
45+
policyAttachments: {
46+
system: ['AliyunOSSFullAccess', 'AliyunLogFullAccess'],
47+
},
48+
},
49+
true,
50+
);
51+
}
52+
1653
buckets.forEach((bucket) => {
1754
const ossBucket = new oss.Bucket(scope, replaceReference(bucket.key, context), {
1855
bucketName: replaceReference(bucket.name, context),
56+
accessControl: aclMap.get(
57+
replaceReference(bucket.security?.acl, context) ?? ('' as BucketAccessEnum),
58+
),
1959
websiteConfigurationV2: bucket.website
2060
? {
2161
indexDocument: {
@@ -34,10 +74,11 @@ export const resolveBuckets = (
3474
const filePath = path.resolve(process.cwd(), replaceReference(bucket.website.code, context));
3575
new ossDeployment.BucketDeployment(
3676
scope,
37-
`${replaceReference(bucket.key, context)}_bucket_code_deployment`,
77+
`si_auto_${bucket.key}_bucket_code_deployment`,
3878
{
3979
sources: getAssets(filePath),
4080
destinationBucket: ossBucket.attrName,
81+
roleArn: siAutoOssDeploymentBucketRole!.attrArn,
4182
timeout: 3000,
4283
logMonitoring: false,
4384
retainOnCreate: false,

src/types/domains/bucket.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type BucketRaw = {
88
};
99

1010
security?: {
11+
acl?: string;
1112
force_delete?: boolean;
1213
sse_algorithm?: string;
1314
sse_kms_master_key_id?: string;
@@ -20,6 +21,12 @@ export type BucketRaw = {
2021
};
2122
};
2223

24+
export enum BucketAccessEnum {
25+
PRIVATE = 'PRIVATE',
26+
PUBLIC_READ = 'PUBLIC_READ',
27+
PUBLIC_READ_WRITE = 'PUBLIC_READ_WRITE',
28+
}
29+
2330
export type BucketDomain = {
2431
key: string;
2532
name: string;
@@ -31,6 +38,7 @@ export type BucketDomain = {
3138
};
3239

3340
security?: {
41+
acl: BucketAccessEnum;
3442
force_delete: boolean;
3543
sse_algorithm?: string;
3644
sse_kms_master_key_id?: string;

src/validator/bucketSchema.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const bucketSchema = {
2929
security: {
3030
type: 'object',
3131
properties: {
32+
access: {
33+
type: 'string',
34+
enum: ['PRIVATE', 'PUBLIC_READ', 'PUBLIC_READ_WRITE'],
35+
},
3236
force_delete: {
3337
type: 'boolean',
3438
},
@@ -39,7 +43,6 @@ export const bucketSchema = {
3943
type: 'string',
4044
},
4145
},
42-
required: ['force_delete'],
4346
},
4447
website: {
4548
type: 'object',

tests/fixtures/deployFixture.ts

+33-91
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ export const bucketWithWebsiteRos = {
13061306
},
13071307
ROSTemplateFormatVersion: '2015-09-01',
13081308
Resources: {
1309-
FCFunctionFormy_bucket_bucket_code_deployment: {
1309+
FCFunctionForsi_auto_my_bucket_bucket_code_deployment: {
13101310
Properties: {
13111311
CAPort: 9000,
13121312
Code: {
@@ -1340,103 +1340,18 @@ export const bucketWithWebsiteRos = {
13401340
MemorySize: 128,
13411341
Runtime: 'python3.10',
13421342
ServiceName: {
1343-
'Fn::GetAtt': ['FCServiceFormy_bucket_bucket_code_deployment', 'ServiceName'],
1343+
'Fn::GetAtt': ['FCServiceForsi_auto_my_bucket_bucket_code_deployment', 'ServiceName'],
13441344
},
13451345
Timeout: 3000,
13461346
},
13471347
Type: 'ALIYUN::FC::Function',
13481348
},
1349-
FCRoleFormy_bucket_bucket_code_deployment: {
1350-
Properties: {
1351-
AssumeRolePolicyDocument: {
1352-
Statement: [
1353-
{
1354-
Action: 'sts:AssumeRole',
1355-
Effect: 'Allow',
1356-
Principal: {
1357-
Service: ['fc.aliyuncs.com'],
1358-
},
1359-
},
1360-
],
1361-
Version: '1',
1362-
},
1363-
DeletionForce: false,
1364-
IgnoreExisting: false,
1365-
Policies: [
1366-
{
1367-
PolicyDocument: {
1368-
Statement: [
1369-
{
1370-
Action: ['oss:*'],
1371-
Effect: 'Allow',
1372-
Resource: ['*'],
1373-
},
1374-
],
1375-
Version: '1',
1376-
},
1377-
PolicyName: 'AliyunOSSFullAccess',
1378-
},
1379-
{
1380-
PolicyDocument: {
1381-
Statement: [
1382-
{
1383-
Action: ['log:*'],
1384-
Effect: 'Allow',
1385-
Resource: ['*'],
1386-
},
1387-
{
1388-
Action: ['ram:CreateServiceLinkedRole'],
1389-
Condition: {
1390-
StringEquals: {
1391-
'ram:ServiceName': [
1392-
'audit.log.aliyuncs.com',
1393-
'alert.log.aliyuncs.com',
1394-
'middlewarelens.log.aliyuncs.com',
1395-
'storagelens.log.aliyuncs.com',
1396-
'ai-lens.log.aliyuncs.com',
1397-
'securitylens.log.aliyuncs.com',
1398-
],
1399-
},
1400-
},
1401-
Effect: 'Allow',
1402-
Resource: ['*'],
1403-
},
1404-
],
1405-
Version: '1',
1406-
},
1407-
PolicyName: 'AliyunLogFullAccess',
1408-
},
1409-
],
1410-
RoleName: {
1411-
'Fn::Join': [
1412-
'-',
1413-
[
1414-
'ros-cdk',
1415-
{
1416-
'Fn::Select': [
1417-
0,
1418-
{
1419-
'Fn::Split': [
1420-
'-',
1421-
{
1422-
Ref: 'ALIYUN::StackId',
1423-
},
1424-
],
1425-
},
1426-
],
1427-
},
1428-
],
1429-
],
1430-
},
1431-
},
1432-
Type: 'ALIYUN::RAM::Role',
1433-
},
1434-
FCServiceFormy_bucket_bucket_code_deployment: {
1349+
FCServiceForsi_auto_my_bucket_bucket_code_deployment: {
14351350
Properties: {
14361351
DeletionForce: false,
14371352
Description: 'FC service for oss deployment by CDK',
14381353
Role: {
1439-
'Fn::GetAtt': ['FCRoleFormy_bucket_bucket_code_deployment', 'Arn'],
1354+
'Fn::GetAtt': ['si_auto_od_bucket_role', 'Arn'],
14401355
},
14411356
ServiceName: {
14421357
'Fn::Join': [
@@ -1483,7 +1398,7 @@ export const bucketWithWebsiteRos = {
14831398
},
14841399
Type: 'ALIYUN::OSS::Bucket',
14851400
},
1486-
my_bucket_bucket_code_deployment: {
1401+
si_auto_my_bucket_bucket_code_deployment: {
14871402
Properties: {
14881403
Parameters: {
14891404
destinationBucket: {
@@ -1501,11 +1416,38 @@ export const bucketWithWebsiteRos = {
15011416
],
15021417
},
15031418
ServiceToken: {
1504-
'Fn::GetAtt': ['FCFunctionFormy_bucket_bucket_code_deployment', 'ARN'],
1419+
'Fn::GetAtt': ['FCFunctionForsi_auto_my_bucket_bucket_code_deployment', 'ARN'],
15051420
},
15061421
Timeout: 3000,
15071422
},
15081423
Type: 'ALIYUN::ROS::CustomResource',
15091424
},
1425+
si_auto_od_bucket_role: {
1426+
Properties: {
1427+
AssumeRolePolicyDocument: {
1428+
Statement: [
1429+
{
1430+
Action: 'sts:AssumeRole',
1431+
Effect: 'Allow',
1432+
Principal: {
1433+
Service: ['fc.aliyuncs.com'],
1434+
},
1435+
},
1436+
],
1437+
Version: '1',
1438+
},
1439+
DeletionForce: false,
1440+
Description:
1441+
'roles created by ServerlessInsight for oss deployment to put files to oss bucket during deployment',
1442+
IgnoreExisting: false,
1443+
PolicyAttachments: {
1444+
System: ['AliyunOSSFullAccess', 'AliyunLogFullAccess'],
1445+
},
1446+
RoleName: {
1447+
'Fn::Sub': 'si-auto-od-bucket-role-${ALIYUN::StackId}',
1448+
},
1449+
},
1450+
Type: 'ALIYUN::RAM::Role',
1451+
},
15101452
},
15111453
};

0 commit comments

Comments
 (0)