@@ -16,26 +16,82 @@ const { ApiError } = require('../../utils/error');
16
16
17
17
// timeout 5 minutes
18
18
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 ( ) ;
29
44
} )
30
45
. 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 ;
31
88
}
32
89
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 ) {
36
92
throw ApiError ( {
37
93
type : 'PARAMETER_CYNOSDB' ,
38
- message : `Unsupported zone, support zones: ${ supportZones . join ( ',' ) } ` ,
94
+ message : `Unsupported zone, support zones: ${ SUPPORT_ZONES . join ( ',' ) } ` ,
39
95
} ) ;
40
96
}
41
97
return true ;
@@ -138,7 +194,7 @@ async function getServerlessSpecs(capi, { minCpu, maxCpu } = {}) {
138
194
*/
139
195
async function createCluster ( capi , dbInputs ) {
140
196
const isServerless = dbInputs . DbMode === 'SERVERLESS' ;
141
- isSupportZone ( dbInputs . Zone , isServerless ) ;
197
+ isSupportZone ( dbInputs . Zone ) ;
142
198
143
199
if ( isServerless ) {
144
200
const curSpec = await getServerlessSpecs ( capi , {
@@ -292,9 +348,9 @@ async function closePublicAccess(capi, clusterId) {
292
348
293
349
module . exports = {
294
350
TIMEOUT ,
295
- PWD_CHARS ,
296
351
sleep,
297
352
generatePwd,
353
+ isValidPwd,
298
354
formatConnectOutput,
299
355
resetPwd,
300
356
createCluster,
0 commit comments