Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 865affb

Browse files
committed
support code secret protect
1 parent 75a51cd commit 865affb

33 files changed

Lines changed: 194 additions & 81 deletions

deps/tencentcloud-sdk-nodejs/tencentcloud/scf/v20180416/models.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,8 @@ class CopyFunctionRequest extends AbstractModel {
24802480

24812481
this.Override = ''
24822482
this.CopyConfiguration = ''
2483+
2484+
this.CodeSecret = null;
24832485
}
24842486

24852487
/**
@@ -2496,7 +2498,8 @@ class CopyFunctionRequest extends AbstractModel {
24962498
this.Description = 'Description' in params ? params.Description : null;
24972499
this.TargetRegion = 'TargetRegion' in params ? params.TargetRegion : null;
24982500
this.CopyConfiguration = 'CopyConfiguration' in params ? params.CopyConfiguration : null
2499-
this.Override = 'Override' in params ? params.Override : null
2501+
this.Override = 'Override' in params ? params.Override : null;
2502+
this.CodeSecret = 'CodeSecret' in params ? params.CodeSecret : null;
25002503
}
25012504
}
25022505

lib/commands/functions/code-update.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const ora_1 = __importDefault(require("ora"));
77
const path_1 = __importDefault(require("path"));
88
const error_1 = require("../../error");
99
const function_1 = require("../../function");
10-
async function codeUpdate(ctx) {
10+
async function codeUpdate(ctx, options) {
1111
const { name, envId, config, functions } = ctx;
12+
const { codeSecret } = options;
1213
if (!name) {
1314
throw new error_1.CloudBaseError('请指定函数名称!');
1415
}
@@ -21,6 +22,7 @@ async function codeUpdate(ctx) {
2122
await function_1.updateFunctionCode({
2223
func,
2324
envId,
25+
codeSecret,
2426
functionRootPath: path_1.default.join(process.cwd(), config.functionRoot)
2527
});
2628
spinner.succeed(`[${func.name}] 函数代码更新成功!`);

lib/commands/functions/copy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const logger_1 = require("../../logger");
55
const error_1 = require("../../error");
66
async function copy(ctx, newFunctionName, targentEnvId, options) {
77
const { name, envId } = ctx;
8-
const { force } = options;
8+
const { force, codeSecret } = options;
99
if (!name || !newFunctionName) {
1010
throw new error_1.CloudBaseError('请指定函数名称!');
1111
}
1212
await function_1.copyFunction({
1313
force,
1414
envId,
15+
codeSecret,
1516
newFunctionName,
1617
functionName: name,
1718
targetEnvId: targentEnvId || envId

lib/commands/functions/deploy.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const error_1 = require("../../error");
1010
const function_1 = require("../../function");
1111
async function deploy(ctx, commandOptions) {
1212
const { name, envId, config, functions } = ctx;
13-
const { force } = commandOptions;
13+
const { force, codeSecret } = commandOptions;
1414
let isBatchCreating = false;
1515
if (!name) {
1616
const { isBatch } = await inquirer_1.default.prompt({
@@ -30,6 +30,7 @@ async function deploy(ctx, commandOptions) {
3030
force,
3131
functions,
3232
log: true,
33+
codeSecret,
3334
functionRootPath: path_1.default.join(process.cwd(), config.functionRoot)
3435
});
3536
}
@@ -43,6 +44,7 @@ async function deploy(ctx, commandOptions) {
4344
force,
4445
envId,
4546
func: newFunction,
47+
codeSecret,
4648
functionRootPath: path_1.default.join(process.cwd(), config.functionRoot)
4749
});
4850
createSpinner.succeed(`[${newFunction.name}] 函数部署成功!`);
@@ -63,6 +65,7 @@ async function deploy(ctx, commandOptions) {
6365
envId,
6466
force: true,
6567
func: newFunction,
68+
codeSecret,
6669
functionRootPath: path_1.default.join(process.cwd(), config.functionRoot)
6770
});
6871
createSpinner.succeed(`[${newFunction.name}] 函数部署成功!`);

lib/commands/functions/detail.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,23 @@ function logDetail(info, name) {
6161
.reduce((prev, next) => prev + next);
6262
console.log(chalk_1.default.green(`函数 [${name}] 信息:`) + '\n\n' + funcInfo);
6363
}
64-
async function detail(ctx) {
64+
async function detail(ctx, options) {
6565
const { envId, name, functions } = ctx;
66+
const { codeSecret } = options;
6667
if (!name) {
6768
const names = functions.map(item => item.name);
6869
const data = await function_1.batchGetFunctionsDetail({
6970
names,
70-
envId
71+
envId,
72+
codeSecret
7173
});
7274
data.forEach(info => logDetail(info, name));
7375
return;
7476
}
7577
const data = await function_1.getFunctionDetail({
7678
envId,
77-
functionName: name
79+
functionName: name,
80+
codeSecret
7881
});
7982
logDetail(data, name);
8083
}

lib/commands/functions/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const commands = [
5252
{
5353
cmd: 'functions:deploy [functionName] [envId]',
5454
options: [
55+
{
56+
flags: '--code-secret <codeSecret>',
57+
desc: '传入此参数将保护代码,格式为 36 位大小字母和数字'
58+
},
5559
{
5660
flags: '--force',
5761
desc: '如果存在同名函数,上传后覆盖同名函数'
@@ -76,12 +80,17 @@ const commands = [
7680
},
7781
{
7882
cmd: 'functions:detail [functionName] [envId]',
79-
options: [],
83+
options: [
84+
{
85+
flags: '--code-secret <codeSecret>',
86+
desc: '代码加密的函数的 CodeSecret'
87+
}
88+
],
8089
desc: '获取云函数信息',
8190
handler: async (name, envId, options) => {
8291
const { configFile } = options.parent;
8392
const ctx = await getFunctionContext(name, envId, configFile);
84-
await detail_1.detail(ctx);
93+
await detail_1.detail(ctx, options);
8594
}
8695
},
8796
{
@@ -96,12 +105,17 @@ const commands = [
96105
},
97106
{
98107
cmd: 'functions:code:update <functionName> [envId]',
99-
options: [],
108+
options: [
109+
{
110+
flags: '--code-secret <codeSecret>',
111+
desc: '传入此参数将保护代码,格式为 36 位大小字母和数字'
112+
}
113+
],
100114
desc: '更新云函数代码',
101115
handler: async (name, envId, options) => {
102116
const { configFile } = options.parent;
103117
const ctx = await getFunctionContext(name, envId, configFile);
104-
await code_update_1.codeUpdate(ctx);
118+
await code_update_1.codeUpdate(ctx, options);
105119
}
106120
},
107121
{
@@ -117,6 +131,10 @@ const commands = [
117131
{
118132
cmd: 'functions:copy <functionName> <newFunctionName> [envId] [targentEnvId]',
119133
options: [
134+
{
135+
flags: '--code-secret <codeSecret>',
136+
desc: '代码加密的函数的 CodeSecret'
137+
},
120138
{
121139
flags: '--force',
122140
desc: '如果目标环境下存在同名函数,覆盖原函数'

lib/function/create.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ const scfService = new utils_1.CloudService('scf', '2018-04-16', {
1313
Stamp: 'MINI_QCBASE'
1414
});
1515
async function createFunction(options) {
16-
const { func, functionRootPath = '', envId, force = false, base64Code = '' } = options;
16+
const { func, functionRootPath = '', envId, force = false, base64Code = '', codeSecret } = options;
1717
let base64;
1818
let packer;
1919
const funcName = func.name;
20+
if (codeSecret && !/^[A-Za-z0-9+=/]{1,160}$/.test(codeSecret)) {
21+
throw new error_1.CloudBaseError('CodeSecret 格式错误,格式为 1-160 位大小字母,数字+=/');
22+
}
2023
const validRuntime = ['Nodejs8.9', 'Php7', 'Java8'];
2124
if (func.config.runtime && !validRuntime.includes(func.config.runtime)) {
2225
throw new error_1.CloudBaseError(`${funcName} Invalid runtime value:${func.config.runtime}. Now only support: ${validRuntime.join(', ')}`);
@@ -42,6 +45,7 @@ async function createFunction(options) {
4245
Code: {
4346
ZipFile: base64
4447
},
48+
CodeSecret: codeSecret,
4549
MemorySize: 256
4650
};
4751
const { config } = func;
@@ -83,16 +87,17 @@ async function createFunction(options) {
8387
}
8488
exports.createFunction = createFunction;
8589
async function batchCreateFunctions(options) {
86-
const { functions, functionRootPath = '', envId, force, log = false } = options;
90+
const { functions, functionRootPath = '', envId, force, codeSecret, log = false } = options;
8791
const promises = functions.map(func => (async () => {
8892
const spinner = ora_1.default(`[${func.name}] 函数部署中...`);
8993
try {
9094
log && spinner.start();
9195
await createFunction({
9296
func,
93-
functionRootPath,
9497
envId,
95-
force
98+
force,
99+
codeSecret,
100+
functionRootPath
96101
});
97102
log && spinner.succeed(`[${func.name}] 函数部署成功`);
98103
}
@@ -105,10 +110,13 @@ async function batchCreateFunctions(options) {
105110
}
106111
exports.batchCreateFunctions = batchCreateFunctions;
107112
async function updateFunctionCode(options) {
108-
const { func, functionRootPath = '', envId, base64Code = '' } = options;
113+
const { func, functionRootPath = '', envId, base64Code = '', codeSecret } = options;
109114
let base64;
110115
let packer;
111116
const funcName = func.name;
117+
if (codeSecret && !/^[A-Za-z0-9+=/]{1,160}$/.test(codeSecret)) {
118+
throw new error_1.CloudBaseError('CodeSecret 格式错误,格式为 1-160 位大小字母,数字+=/');
119+
}
112120
const validRuntime = ['Nodejs8.9', 'Php7', 'Java8'];
113121
if (func.config.runtime && !validRuntime.includes(func.config.runtime)) {
114122
throw new error_1.CloudBaseError(`${funcName} 非法的运行环境:${func.config.runtime},当前支持环境:${validRuntime.join(', ')}`);
@@ -128,6 +136,7 @@ async function updateFunctionCode(options) {
128136
FunctionName: funcName,
129137
Namespace: envId,
130138
ZipFile: base64,
139+
CodeSecret: codeSecret,
131140
Handler: func.handler || 'index.main'
132141
};
133142
try {

lib/function/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ async function batchDeleteFunctions({ names, envId }) {
6262
}
6363
exports.batchDeleteFunctions = batchDeleteFunctions;
6464
async function getFunctionDetail(options) {
65-
const { functionName, envId } = options;
65+
const { functionName, envId, codeSecret } = options;
6666
const res = await scfService.request('GetFunction', {
6767
FunctionName: functionName,
6868
Namespace: envId,
69-
ShowCode: 'TRUE'
69+
ShowCode: 'TRUE',
70+
CodeSecret: codeSecret
7071
});
7172
const data = {};
7273
const validKeys = [
@@ -111,13 +112,14 @@ async function getFunctionDetail(options) {
111112
return data;
112113
}
113114
exports.getFunctionDetail = getFunctionDetail;
114-
async function batchGetFunctionsDetail({ names, envId }) {
115+
async function batchGetFunctionsDetail({ names, envId, codeSecret }) {
115116
const data = [];
116117
const promises = names.map(name => (async () => {
117118
try {
118119
const info = await getFunctionDetail({
119120
name,
120-
envId
121+
envId,
122+
codeSecret
121123
});
122124
data.push(info);
123125
}
@@ -222,7 +224,7 @@ async function batchInvokeFunctions(options) {
222224
}
223225
exports.batchInvokeFunctions = batchInvokeFunctions;
224226
async function copyFunction(options) {
225-
const { envId, functionName, newFunctionName, targetEnvId, force } = options;
227+
const { envId, functionName, newFunctionName, targetEnvId, force, codeSecret } = options;
226228
if (!envId || !functionName || !newFunctionName) {
227229
throw new error_1.CloudBaseError('参数缺失');
228230
}
@@ -231,7 +233,8 @@ async function copyFunction(options) {
231233
NewFunctionName: newFunctionName,
232234
Namespace: envId,
233235
TargetNamespace: targetEnvId || envId,
234-
Override: force ? true : false
236+
Override: force ? true : false,
237+
CodeSecret: codeSecret
235238
});
236239
}
237240
exports.copyFunction = copyFunction;

package-lock.json

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {
7-
"build": "tsc",
7+
"build": "rimraf lib types && tsc",
88
"eslint": "eslint \"./**/*.ts\"",
99
"test": "jest --runInBand --forceExit --detectOpenHandles --coverage --verbose --testTimeout=10000",
1010
"publish": "node scripts/publish.js"
@@ -56,6 +56,7 @@
5656
"eslint-config-prettier": "^5.1.0",
5757
"eslint-plugin-prettier": "^3.1.0",
5858
"jest": "^24.9.0",
59+
"rimraf": "^3.0.0",
5960
"ts-jest": "^24.0.2",
6061
"typescript": "^3.5.2"
6162
}

0 commit comments

Comments
 (0)