Skip to content

Commit 8ce0d48

Browse files
committed
fix(cynosdb): generate password rule
1 parent 471ac76 commit 8ce0d48

File tree

5 files changed

+83
-23
lines changed

5 files changed

+83
-23
lines changed

__tests__/cynosdb.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ const {
33
getClusterDetail,
44
sleep,
55
generatePwd,
6+
isValidPwd,
67
offlineCluster,
7-
PWD_CHARS,
88
} = require('../src/modules/cynosdb/utils');
99

10-
const pwdReg = new RegExp(`[${PWD_CHARS}]{8,64}`);
11-
1210
describe('Cynosdb', () => {
1311
jest.setTimeout(600000);
1412
const credentials = {
@@ -33,6 +31,7 @@ describe('Cynosdb', () => {
3331
const res = generatePwd();
3432
expect(typeof res).toBe('string');
3533
expect(res.length).toBe(8);
34+
expect(isValidPwd(res)).toBe(true);
3635
});
3736

3837
test('[generatePwd] should get random password with customize length 6', () => {
@@ -91,7 +90,7 @@ describe('Cynosdb', () => {
9190
zone: inputs.zone,
9291
vpcConfig: inputs.vpcConfig,
9392
instanceCount: 1,
94-
adminPassword: expect.stringMatching(pwdReg),
93+
adminPassword: expect.any(String),
9594
clusterId: expect.stringContaining('cynosdbmysql-'),
9695
minCpu: 0.5,
9796
maxCpu: 2,
@@ -110,6 +109,7 @@ describe('Cynosdb', () => {
110109
],
111110
});
112111

112+
expect(isValidPwd(res.adminPassword)).toBe(true);
113113
({ clusterId } = res);
114114
});
115115

__tests__/triggers/cls.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe('Cls', () => {
2121

2222
const clsInputs = {
2323
region: 'ap-guangzhou',
24-
name: 'cls-test',
25-
topic: 'cls-topic-test',
24+
name: 'cls-trigger-test',
25+
topic: 'cls-topic-trigger-test',
2626
period: 7,
2727
rule: {
2828
full_text: {

src/modules/cfs/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class CFS {
5757
console.log(`Updating CFS id: ${inputs.fileSystemId}, name: ${inputs.fsName}`);
5858
await apis.updateCfs(this.capi, inputs.fileSystemId, updateParams);
5959
console.log(`Update CFS id: ${inputs.fileSystemId}, name: ${inputs.fsName} success.`);
60+
} else {
61+
console.log(
62+
`CFS ${inputs.fileSystemId}, name: ${inputs.fsName} already exist, nothing to update`,
63+
);
6064
}
6165

6266
outputs.fileSystemId = inputs.fileSystemId;

src/modules/cynosdb/utils.js

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,82 @@ const { ApiError } = require('../../utils/error');
1616

1717
// timeout 5 minutes
1818
const TIMEOUT = 5 * 60 * 1000;
19-
const SUPPORT_ZONES = ['ap-beijing-3', 'ap-guangzhou-4', 'ap-nanjing-1', 'ap-shanghai-2'];
20-
const SERVERLESS_SUPPORT_ZONES = ['ap-shanghai-2', 'ap-nanjing-1'];
21-
const PWD_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*_-';
22-
23-
function generatePwd(length) {
24-
length = length || 8;
25-
return Array(length)
26-
.fill(PWD_CHARS)
27-
.map((item) => {
28-
return item[Math.floor(Math.random() * item.length)];
19+
const SUPPORT_ZONES = ['ap-beijing-3', 'ap-guangzhou-4', 'ap-shanghai-2', 'ap-nanjing-1'];
20+
21+
function generatePwd(length = 8) {
22+
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz';
23+
const NUMBER = '0123456789';
24+
const SPECIAL = '~!@#$%^&*_-';
25+
26+
let password = '';
27+
let character = '';
28+
while (password.length < length) {
29+
const entity1 = Math.ceil(ALPHABET.length * Math.random() * Math.random());
30+
const entity2 = Math.ceil(SPECIAL.length * Math.random() * Math.random());
31+
const entity3 = Math.ceil(NUMBER.length * Math.random() * Math.random());
32+
33+
let hold = ALPHABET.charAt(entity1);
34+
hold = password.length % 2 === 0 ? hold.toUpperCase() : hold;
35+
character += hold;
36+
character += SPECIAL.charAt(entity2);
37+
character += NUMBER.charAt(entity3);
38+
password = character;
39+
}
40+
password = password
41+
.split('')
42+
.sort(function() {
43+
return 0.5 - Math.random();
2944
})
3045
.join('');
46+
47+
return password.substr(0, length);
48+
}
49+
50+
function isValidPwd(password) {
51+
const minLen = 8;
52+
const maxLen = 64;
53+
const pwdLen = password.length;
54+
if (pwdLen < minLen || pwdLen > maxLen) {
55+
return false;
56+
}
57+
58+
const numStr = '0123456789';
59+
const lowerCaseLetter = 'abcdefghijklmnopqrstuvwxyz';
60+
const upperCaseLetter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
61+
const specStr = "~!@#$%^&*_-+=`|\\(){}[]:;'<>,.?/";
62+
63+
let numFlag = 0;
64+
let lowerCaseFlag = 0;
65+
let upperCaseFlag = 0;
66+
let specFlag = 0;
67+
68+
for (let i = 0; i < pwdLen; i++) {
69+
const curChar = password[i];
70+
if (numStr.indexOf(curChar) !== -1) {
71+
numFlag = 1;
72+
} else if (lowerCaseLetter.indexOf(curChar) !== -1) {
73+
lowerCaseFlag = 1;
74+
} else if (upperCaseLetter.indexOf(curChar) !== -1) {
75+
upperCaseFlag = 1;
76+
} else if (specStr.indexOf(curChar) !== -1) {
77+
specFlag = 1;
78+
} else {
79+
return false;
80+
}
81+
}
82+
83+
if (numFlag + lowerCaseFlag + upperCaseFlag + specFlag < 3) {
84+
return false;
85+
}
86+
87+
return true;
3188
}
3289

33-
function isSupportZone(zone, isServerless = false) {
34-
const supportZones = isServerless ? SERVERLESS_SUPPORT_ZONES : SUPPORT_ZONES;
35-
if (supportZones.indexOf(zone) === -1) {
90+
function isSupportZone(zone) {
91+
if (SUPPORT_ZONES.indexOf(zone) === -1) {
3692
throw ApiError({
3793
type: 'PARAMETER_CYNOSDB',
38-
message: `Unsupported zone, support zones: ${supportZones.join(',')}`,
94+
message: `Unsupported zone, support zones: ${SUPPORT_ZONES.join(',')}`,
3995
});
4096
}
4197
return true;
@@ -138,7 +194,7 @@ async function getServerlessSpecs(capi, { minCpu, maxCpu } = {}) {
138194
*/
139195
async function createCluster(capi, dbInputs) {
140196
const isServerless = dbInputs.DbMode === 'SERVERLESS';
141-
isSupportZone(dbInputs.Zone, isServerless);
197+
isSupportZone(dbInputs.Zone);
142198

143199
if (isServerless) {
144200
const curSpec = await getServerlessSpecs(capi, {
@@ -292,9 +348,9 @@ async function closePublicAccess(capi, clusterId) {
292348

293349
module.exports = {
294350
TIMEOUT,
295-
PWD_CHARS,
296351
sleep,
297352
generatePwd,
353+
isValidPwd,
298354
formatConnectOutput,
299355
resetPwd,
300356
createCluster,

src/modules/triggers/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class BaseTrigger {
3333
}
3434
const { Triggers = [], TotalCount } = await Apis.SCF.ListTriggers(this.capi, listOptions);
3535
if (TotalCount > 100) {
36-
const res = await this.getTriggerList(functionName, namespace, qualifier);
36+
const res = await this.getTriggerList({ functionName, namespace, qualifier });
3737
return Triggers.concat(res);
3838
}
3939

0 commit comments

Comments
 (0)