@geek-fun/serverlessinsight 0.7.7 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/package.json +15 -14
  2. package/dist/src/common/aliyunClient/ossOperations.js +41 -0
  3. package/dist/src/common/aliyunClient/ramOperations.js +161 -7
  4. package/dist/src/common/iamStatements.js +48 -0
  5. package/dist/src/common/imsClient.js +1 -0
  6. package/dist/src/common/tencentClient/camOperations.js +317 -0
  7. package/dist/src/common/tencentClient/cosOperations.js +67 -0
  8. package/dist/src/common/tencentClient/index.js +18 -0
  9. package/dist/src/common/tencentClient/scfOperations.js +2 -0
  10. package/dist/src/common/volcengineClient/iamOperations.js +133 -47
  11. package/dist/src/common/volcengineClient/tosOperations.js +79 -0
  12. package/dist/src/lang/en.js +24 -0
  13. package/dist/src/lang/zh-CN.js +24 -0
  14. package/dist/src/parser/bucketParser.js +31 -0
  15. package/dist/src/parser/functionParser.js +34 -0
  16. package/dist/src/stack/aliyunStack/fc3Planner.js +2 -1
  17. package/dist/src/stack/aliyunStack/fc3Resource.js +72 -14
  18. package/dist/src/stack/aliyunStack/ossResource.js +63 -0
  19. package/dist/src/stack/aliyunStack/ossTypes.js +9 -0
  20. package/dist/src/stack/scfStack/cosResource.js +58 -0
  21. package/dist/src/stack/scfStack/cosTypes.js +9 -0
  22. package/dist/src/stack/scfStack/scfPlanner.js +1 -1
  23. package/dist/src/stack/scfStack/scfResource.js +195 -22
  24. package/dist/src/stack/scfStack/scfTypes.js +6 -4
  25. package/dist/src/stack/volcengineStack/tosResource.js +56 -0
  26. package/dist/src/stack/volcengineStack/tosTypes.js +9 -0
  27. package/dist/src/stack/volcengineStack/vefaasPlanner.js +2 -1
  28. package/dist/src/stack/volcengineStack/vefaasResource.js +46 -5
  29. package/dist/src/types/domains/state.js +2 -0
  30. package/dist/src/validator/bucketSchema.js +45 -0
  31. package/dist/src/validator/functionSchema.js +43 -0
  32. package/dist/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +15 -14
  34. package/samples/aliyun-poc-api-cdn.yml +75 -0
@@ -0,0 +1,317 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCamOperations = void 0;
4
+ const iamStatements_1 = require("../iamStatements");
5
+ const logger_1 = require("../logger");
6
+ const lang_1 = require("../../lang");
7
+ // SCF built-in execution policy (same idea as Aliyun's FC_EXECUTION_POLICY)
8
+ const SCF_EXECUTION_POLICY = JSON.stringify({
9
+ Version: '2.0',
10
+ Statement: [
11
+ {
12
+ Effect: 'Allow',
13
+ Action: ['scf:InvokeFunction'],
14
+ Resource: '*',
15
+ },
16
+ {
17
+ Effect: 'Allow',
18
+ Action: ['cls:logset:putlog', 'cls:logset:create*', 'cls:logset:get*'],
19
+ Resource: '*',
20
+ },
21
+ {
22
+ Effect: 'Allow',
23
+ Action: ['cos:GetObject', 'cos:PutObject', 'cos:DeleteObject', 'cos:ListBucket'],
24
+ Resource: '*',
25
+ },
26
+ ],
27
+ });
28
+ const SCF_EXECUTION_STATEMENTS = (0, iamStatements_1.parseBuiltInStatements)(SCF_EXECUTION_POLICY);
29
+ const mapToCamStatement = (stmt) => {
30
+ const result = {
31
+ Effect: stmt.effect,
32
+ Action: stmt.action,
33
+ Resource: stmt.resource,
34
+ };
35
+ if (stmt.sid) {
36
+ result.Sid = stmt.sid;
37
+ }
38
+ return result;
39
+ };
40
+ const buildExecutionPolicy = (customStatements) => {
41
+ const merged = (0, iamStatements_1.mergePolicyStatements)(SCF_EXECUTION_STATEMENTS, customStatements, mapToCamStatement);
42
+ return (0, iamStatements_1.buildPolicyDocument)(merged, { version: '2.0' });
43
+ };
44
+ const buildAssumeRolePolicy = (trustedServices) => JSON.stringify({
45
+ Version: '2.0',
46
+ Statement: [
47
+ {
48
+ Effect: 'Allow',
49
+ Action: ['sts:AssumeRole'],
50
+ Principal: {
51
+ Service: trustedServices,
52
+ },
53
+ },
54
+ ],
55
+ });
56
+ // Try to delete a policy by name. Tencent CAM DeletePolicy requires a numeric
57
+ // PolicyId, so we first list policies to find the ID by name.
58
+ const deletePolicyByName = async (camClient, policyName) => {
59
+ try {
60
+ const listResp = await camClient.ListPolicies({ Keyword: policyName, Scope: 'Local' });
61
+ const found = listResp.List?.find((p) => p.PolicyName === policyName);
62
+ if (found?.PolicyId) {
63
+ await camClient.DeletePolicy({ PolicyId: [found.PolicyId] });
64
+ }
65
+ }
66
+ catch {
67
+ // Idempotent cleanup - best effort
68
+ }
69
+ };
70
+ const createCamOperations = (camClient) => {
71
+ const attachPolicy = async (roleName, policyName) => {
72
+ try {
73
+ await camClient.AttachRolePolicy({
74
+ PolicyName: policyName,
75
+ AttachRoleName: roleName,
76
+ });
77
+ }
78
+ catch (error) {
79
+ if (error && typeof error === 'object' && 'code' in error) {
80
+ const code = String(error.code);
81
+ if (code === 'InvalidParameter.AttachmentRoleAlreadyExist') {
82
+ return; // Already attached - idempotent
83
+ }
84
+ }
85
+ throw error;
86
+ }
87
+ };
88
+ const detachPolicy = async (roleName, policyName) => {
89
+ try {
90
+ await camClient.DetachRolePolicy({
91
+ PolicyName: policyName,
92
+ DetachRoleName: roleName,
93
+ });
94
+ }
95
+ catch (error) {
96
+ if (error && typeof error === 'object' && 'code' in error) {
97
+ const code = String(error.code);
98
+ if (code === 'ResourceNotFound.PolicyNotAttached' ||
99
+ code === 'ResourceNotFound.RoleNotExist') {
100
+ return; // Not found - idempotent
101
+ }
102
+ }
103
+ throw error;
104
+ }
105
+ };
106
+ return {
107
+ createRole: async (roleName, trustedServices, description, customStatements, managedPolicies) => {
108
+ const assumeRolePolicy = buildAssumeRolePolicy(trustedServices);
109
+ try {
110
+ const response = await camClient.CreateRole({
111
+ RoleName: roleName,
112
+ PolicyDocument: assumeRolePolicy,
113
+ Description: description ?? `ServerlessInsight SCF execution role for ${roleName}`,
114
+ });
115
+ // Build base role info; CreateRole only returns RoleId,
116
+ // so get full info (including RoleArn) via GetRole
117
+ const roleId = response.RoleId;
118
+ let roleArn;
119
+ try {
120
+ const getResp = await camClient.GetRole({ RoleName: roleName });
121
+ roleArn = getResp.RoleInfo?.RoleArn;
122
+ }
123
+ catch {
124
+ // optional enrichment
125
+ }
126
+ const roleInfo = {
127
+ roleName,
128
+ roleId,
129
+ roleArn,
130
+ };
131
+ // Attach inline policy with built-in + custom statements
132
+ const policyName = `${roleName}-policy`;
133
+ try {
134
+ await camClient.CreatePolicy({
135
+ PolicyName: policyName,
136
+ PolicyDocument: buildExecutionPolicy(customStatements),
137
+ Description: `SCF execution policy for ${roleName}`,
138
+ });
139
+ await attachPolicy(roleName, policyName);
140
+ }
141
+ catch (error) {
142
+ if (error && typeof error === 'object' && 'code' in error) {
143
+ const code = String(error.code);
144
+ if (code === 'InvalidParameter.PolicyAlreadyExist') {
145
+ // Policy already exists - reuse it
146
+ await attachPolicy(roleName, policyName);
147
+ }
148
+ else {
149
+ throw error;
150
+ }
151
+ }
152
+ else {
153
+ throw error;
154
+ }
155
+ }
156
+ // Attach managed policies
157
+ if (managedPolicies && managedPolicies.length > 0) {
158
+ for (const policyArn of managedPolicies) {
159
+ await attachPolicy(roleName, policyArn);
160
+ }
161
+ }
162
+ return { ...roleInfo, policyName };
163
+ }
164
+ catch (error) {
165
+ if (error && typeof error === 'object' && 'code' in error) {
166
+ const code = String(error.code);
167
+ if (code === 'InvalidParameter.RoleAlreadyExist') {
168
+ logger_1.logger.warn(lang_1.lang.__('TENCENT_CAM_ROLE_ALREADY_EXISTS', { roleName }));
169
+ // Try to get the role info
170
+ try {
171
+ const getResponse = await camClient.GetRole({ RoleName: roleName });
172
+ const roleData = getResponse.RoleInfo;
173
+ const policyName = `${roleName}-policy`;
174
+ // Re-attach built-in policy (idempotent)
175
+ try {
176
+ await camClient.CreatePolicy({
177
+ PolicyName: policyName,
178
+ PolicyDocument: buildExecutionPolicy(customStatements),
179
+ Description: `SCF execution policy for ${roleName}`,
180
+ });
181
+ await attachPolicy(roleName, policyName);
182
+ }
183
+ catch {
184
+ /* already exists */
185
+ }
186
+ // Re-attach managed policies
187
+ if (managedPolicies && managedPolicies.length > 0) {
188
+ for (const policyArn of managedPolicies) {
189
+ await attachPolicy(roleName, policyArn);
190
+ }
191
+ }
192
+ return {
193
+ roleName,
194
+ roleId: roleData?.RoleId,
195
+ roleArn: roleData?.RoleArn,
196
+ policyName,
197
+ };
198
+ }
199
+ catch (recoveryError) {
200
+ // eslint-disable-next-line preserve-caught-error
201
+ throw new Error(lang_1.lang.__('TENCENT_CAM_ROLE_DRIFT_RECOVERY_FAILED', {
202
+ roleName,
203
+ error: recoveryError instanceof Error ? recoveryError.message : String(recoveryError),
204
+ }));
205
+ }
206
+ }
207
+ }
208
+ throw error;
209
+ }
210
+ },
211
+ getRole: async (roleName) => {
212
+ try {
213
+ const response = await camClient.GetRole({ RoleName: roleName });
214
+ const roleData = response.RoleInfo;
215
+ if (!roleData)
216
+ return null;
217
+ return {
218
+ roleName: roleData.RoleName,
219
+ roleId: roleData.RoleId,
220
+ roleArn: roleData.RoleArn,
221
+ description: roleData.Description,
222
+ policyName: `${roleName}-policy`,
223
+ };
224
+ }
225
+ catch (error) {
226
+ if (error && typeof error === 'object' && 'code' in error) {
227
+ const code = String(error.code);
228
+ if (code === 'ResourceNotFound.RoleNotExist')
229
+ return null;
230
+ }
231
+ throw error;
232
+ }
233
+ },
234
+ deleteRole: async (roleName, managedPolicies) => {
235
+ const policyName = `${roleName}-policy`;
236
+ // Detach managed policies first (don't delete them)
237
+ if (managedPolicies && managedPolicies.length > 0) {
238
+ for (const policyArn of managedPolicies) {
239
+ await detachPolicy(roleName, policyArn);
240
+ }
241
+ }
242
+ // Detach and delete inline policy
243
+ await detachPolicy(roleName, policyName);
244
+ await deletePolicyByName(camClient, policyName);
245
+ // Delete role
246
+ try {
247
+ await camClient.DeleteRole({ RoleName: roleName });
248
+ logger_1.logger.info(lang_1.lang.__('TENCENT_CAM_ROLE_DELETED', { roleName }));
249
+ }
250
+ catch (error) {
251
+ if (error && typeof error === 'object' && 'code' in error) {
252
+ const code = String(error.code);
253
+ if (code === 'ResourceNotFound.RoleNotExist')
254
+ return;
255
+ }
256
+ throw error;
257
+ }
258
+ },
259
+ updateRolePolicy: async (roleName, customStatements) => {
260
+ const policyName = `${roleName}-policy`;
261
+ // Detach + delete old
262
+ await detachPolicy(roleName, policyName);
263
+ await deletePolicyByName(camClient, policyName);
264
+ // Create + attach new
265
+ try {
266
+ await camClient.CreatePolicy({
267
+ PolicyName: policyName,
268
+ PolicyDocument: buildExecutionPolicy(customStatements),
269
+ Description: `SCF execution policy for ${roleName}`,
270
+ });
271
+ }
272
+ catch (error) {
273
+ if (error && typeof error === 'object' && 'code' in error) {
274
+ const code = String(error.code);
275
+ if (code !== 'InvalidParameter.PolicyAlreadyExist')
276
+ throw error;
277
+ }
278
+ else {
279
+ throw error;
280
+ }
281
+ }
282
+ await attachPolicy(roleName, policyName);
283
+ },
284
+ updateManagedPolicies: async (roleName, desiredPolicies) => {
285
+ try {
286
+ const response = await camClient.ListAttachedRolePolicies({
287
+ RoleName: roleName,
288
+ Page: 1,
289
+ Rp: 200,
290
+ });
291
+ const currentPolicies = response.List ?? [];
292
+ const currentNames = currentPolicies.map((p) => p.PolicyName ?? '');
293
+ // Detach removed policies
294
+ for (const current of currentNames) {
295
+ if (current && !desiredPolicies.includes(current)) {
296
+ await detachPolicy(roleName, current);
297
+ }
298
+ }
299
+ // Attach new policies
300
+ for (const desired of desiredPolicies) {
301
+ if (!currentNames.includes(desired)) {
302
+ await attachPolicy(roleName, desired);
303
+ }
304
+ }
305
+ }
306
+ catch (error) {
307
+ if (error && typeof error === 'object' && 'code' in error) {
308
+ const code = String(error.code);
309
+ if (code === 'ResourceNotFound.RoleNotExist')
310
+ return;
311
+ }
312
+ throw error;
313
+ }
314
+ },
315
+ };
316
+ };
317
+ exports.createCamOperations = createCamOperations;
@@ -555,6 +555,73 @@ const createCosOperations = (cosClient, region, dnsOps) => {
555
555
  bindCustomDomain,
556
556
  unbindCustomDomain,
557
557
  getCosEndpoint,
558
+ putBucketPolicy: async (bucketName, region, policy) => {
559
+ await new Promise((resolve, reject) => {
560
+ cosClient.putBucketPolicy({
561
+ Bucket: bucketName,
562
+ Region: region,
563
+ Policy: policy,
564
+ }, (err) => {
565
+ if (err)
566
+ reject(err);
567
+ else
568
+ resolve();
569
+ });
570
+ });
571
+ logger_1.logger.info(lang_1.lang.__('COS_BUCKET_POLICY_SET', { bucketName }));
572
+ },
573
+ getBucketPolicy: async (bucketName, region) => {
574
+ try {
575
+ const result = await new Promise((resolve, reject) => {
576
+ cosClient.getBucketPolicy({
577
+ Bucket: bucketName,
578
+ Region: region,
579
+ }, (err, data) => {
580
+ if (err)
581
+ reject(err);
582
+ else
583
+ resolve(data);
584
+ });
585
+ });
586
+ if (result.Policy) {
587
+ return typeof result.Policy === 'string' ? JSON.parse(result.Policy) : result.Policy;
588
+ }
589
+ return null;
590
+ }
591
+ catch (err) {
592
+ const e = err;
593
+ if (e.statusCode === 404 || e.statusCode === 403 || e.code === 'NoSuchBucketPolicy') {
594
+ return null;
595
+ }
596
+ throw err;
597
+ }
598
+ },
599
+ deleteBucketPolicy: async (bucketName, region) => {
600
+ try {
601
+ // COS SDK doesn't have a direct deleteBucketPolicy method.
602
+ // We delete by setting an empty policy.
603
+ const emptyPolicy = {
604
+ version: '2.0',
605
+ statement: [],
606
+ };
607
+ await new Promise((resolve, reject) => {
608
+ cosClient.putBucketPolicy({
609
+ Bucket: bucketName,
610
+ Region: region,
611
+ Policy: emptyPolicy,
612
+ }, (err) => {
613
+ if (err)
614
+ reject(err);
615
+ else
616
+ resolve();
617
+ });
618
+ });
619
+ logger_1.logger.info(lang_1.lang.__('COS_BUCKET_POLICY_DELETED', { bucketName }));
620
+ }
621
+ catch {
622
+ // Best effort cleanup
623
+ }
624
+ },
558
625
  };
559
626
  };
560
627
  exports.createCosOperations = createCosOperations;
@@ -42,11 +42,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.createTencentClient = void 0;
43
43
  const tencentcloud = __importStar(require("tencentcloud-sdk-nodejs-scf"));
44
44
  const cynosdb = __importStar(require("tencentcloud-sdk-nodejs-cynosdb"));
45
+ const tencentCam = __importStar(require("tencentcloud-sdk-nodejs-cam"));
45
46
  const tencentEs = __importStar(require("tencentcloud-sdk-nodejs-es"));
46
47
  const tencentSsl = __importStar(require("tencentcloud-sdk-nodejs-ssl"));
47
48
  const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
48
49
  const scfOperations_1 = require("./scfOperations");
49
50
  const cosOperations_1 = require("./cosOperations");
51
+ const camOperations_1 = require("./camOperations");
50
52
  const tdsqlcOperations_1 = require("./tdsqlcOperations");
51
53
  const esOperations_1 = require("./esOperations");
52
54
  const dnspodOperations_1 = require("./dnspodOperations");
@@ -54,6 +56,7 @@ const sslOperations_1 = require("./sslOperations");
54
56
  __exportStar(require("./types"), exports);
55
57
  const ScfClient = tencentcloud.scf.v20180416.Client;
56
58
  const CynosdbClient = cynosdb.cynosdb.v20190107.Client;
59
+ const CamClient = tencentCam.cam.v20190116.Client;
57
60
  const EsClient = tencentEs.es.v20180416.Client;
58
61
  const SslClient = tencentSsl.ssl.v20191205.Client;
59
62
  // Initialize SDK clients (internal)
@@ -114,6 +117,19 @@ const initializeSdkClients = (context) => {
114
117
  },
115
118
  };
116
119
  const sslClient = new SslClient(sslClientConfig);
120
+ const camClientConfig = {
121
+ credential: {
122
+ secretId: context.accessKeyId,
123
+ secretKey: context.accessKeySecret,
124
+ },
125
+ region: context.region,
126
+ profile: {
127
+ httpProfile: {
128
+ endpoint: 'cam.tencentcloudapi.com',
129
+ },
130
+ },
131
+ };
132
+ const camClient = new CamClient(camClientConfig);
117
133
  return {
118
134
  scf: scfClient,
119
135
  cos: cosClient,
@@ -121,6 +137,7 @@ const initializeSdkClients = (context) => {
121
137
  es: esClient,
122
138
  ssl: sslClient,
123
139
  dns: (0, dnspodOperations_1.createDnsClient)(context),
140
+ cam: camClient,
124
141
  };
125
142
  };
126
143
  const createTencentClient = (context) => {
@@ -129,6 +146,7 @@ const createTencentClient = (context) => {
129
146
  return {
130
147
  scf: (0, scfOperations_1.createScfOperations)(sdkClients.scf),
131
148
  cos: (0, cosOperations_1.createCosOperations)(sdkClients.cos, context.region, dnsOps),
149
+ cam: (0, camOperations_1.createCamOperations)(sdkClients.cam),
132
150
  tdsqlc: (0, tdsqlcOperations_1.createTdsqlcOperations)(sdkClients.cynosdb, context),
133
151
  es: (0, esOperations_1.createTencentEsOperations)(sdkClients.es, context),
134
152
  ssl: (0, sslOperations_1.createSslOperations)(sdkClients.ssl),
@@ -12,6 +12,7 @@ const createScfOperations = (scfClient) => ({
12
12
  Runtime: config.Runtime,
13
13
  MemorySize: config.MemorySize,
14
14
  Timeout: config.Timeout,
15
+ ...(config.Role && { Role: config.Role }),
15
16
  ...(config.Environment && { Environment: config.Environment }),
16
17
  };
17
18
  await scfClient.CreateFunction(params);
@@ -120,6 +121,7 @@ const createScfOperations = (scfClient) => ({
120
121
  Runtime: config.Runtime,
121
122
  MemorySize: config.MemorySize,
122
123
  Timeout: config.Timeout,
124
+ ...(config.Role && { Role: config.Role }),
123
125
  ...(config.Environment && { Environment: config.Environment }),
124
126
  };
125
127
  await scfClient.UpdateFunctionConfiguration(params);