apf-node-common 1.0.109 → 1.0.110
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.
- package/ApplicationContextService.js +103 -103
- package/CoreUtils.js +59 -59
- package/Logger/index.js +93 -93
- package/Logger/loggerTransports.js +137 -137
- package/SSMConfig.js +44 -44
- package/ScheduleCalculationService.js +0 -0
- package/auditlog/AuditLogger.js +58 -58
- package/auditlog/AuditLoggerRepository.js +38 -38
- package/auditlog/AuditLoggerService.js +37 -37
- package/config/SSMParameters.js +16 -16
- package/constants/CommonMessages.js +14 -14
- package/constants/Frequency.js +8 -8
- package/constants/TimeZone.js +11 -11
- package/constants/UserType.js +9 -9
- package/exception/CustomException.js +36 -36
- package/exception/SendResponse.js +139 -139
- package/index.js +187 -187
- package/package.json +29 -29
- package/test/AWSUtilityIntegrationTest.js +94 -94
- package/test/FrequencyValidatorTest.js +0 -0
- package/test/LambdaCommunicationServiceTest.js +83 -83
- package/test/ScheduleCalculationServiceTest.js +0 -0
- package/utils/HashIds.js +139 -139
- package/utils/NumberFormatter.js +253 -253
- package/utils/aws/AESEncryptionUsingKMS.js +106 -106
- package/utils/aws/AWSAPIKeyGenerator.js +307 -307
- package/utils/aws/AWSS3Utils.js +128 -128
- package/utils/aws/AWSSMSUtils.js +63 -63
- package/utils/aws/AWSSNSBasedEmailDispatcher.js +37 -37
- package/utils/aws/AWSSNSBasedSMSDispatcher.js +37 -37
- package/utils/aws/AWSSNSUtils.js +38 -38
- package/utils/aws/LambdaCommunicationService.js +232 -232
- package/utils/enumHelper.js +7 -7
- package/utils/thirdparty/URLShorteningService.js +25 -25
- package/validation/CoreValidations.js +45 -45
- package/validation/FrequencyValidator.js +0 -0
- package/validation/SchemaValidation.js +106 -106
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
const AWS = require('aws-sdk');
|
|
2
|
-
const crypto = require('crypto');
|
|
3
|
-
|
|
4
|
-
const environment = (process.env.NODE_ENVIRONMENT || 'Test');
|
|
5
|
-
const awsRegion = (process.env.AWS_DEFAULT_REGION || 'us-east-2');
|
|
6
|
-
AWS.config.update({ region: awsRegion });
|
|
7
|
-
|
|
8
|
-
const kms = new AWS.KMS();
|
|
9
|
-
const algorithm = 'AES-256-CBC';
|
|
10
|
-
|
|
11
|
-
module.exports = {
|
|
12
|
-
|
|
13
|
-
async encrypt(initVector,plainText,keyInfo){
|
|
14
|
-
try{
|
|
15
|
-
const masterKey = extractMasterKey(keyInfo,'keyid');
|
|
16
|
-
const params = {
|
|
17
|
-
KeyId: masterKey,
|
|
18
|
-
KeySpec: 'AES_256'// Specifies the type of data key to return.
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const dataKey = await kms.generateDataKey(params).promise();
|
|
22
|
-
|
|
23
|
-
// Copy the length of the CiphertextBlob as first byte and concatenate the CiphertextBlob to it
|
|
24
|
-
var length = Buffer.from(['0x' + dataKey.CiphertextBlob.length.toString(16)]);
|
|
25
|
-
var str = Buffer.concat([length, dataKey.CiphertextBlob]);
|
|
26
|
-
|
|
27
|
-
// Get a random IV
|
|
28
|
-
const iv = await crypto.randomBytes(16);
|
|
29
|
-
|
|
30
|
-
// Concatenate the iv to the buffer
|
|
31
|
-
str = Buffer.concat([str, iv]);
|
|
32
|
-
|
|
33
|
-
// Create aes encryptor with KMS plain key
|
|
34
|
-
var encryptor = await crypto.createCipheriv(algorithm, dataKey.Plaintext, iv);
|
|
35
|
-
|
|
36
|
-
// Encrypt the data
|
|
37
|
-
encryptor.write(plainText);
|
|
38
|
-
encryptor.end();
|
|
39
|
-
|
|
40
|
-
// Concatenate the encrypted to the buffer and return the base64 string
|
|
41
|
-
str = Buffer.concat([str, encryptor.read()]);
|
|
42
|
-
|
|
43
|
-
return str.toString('base64');
|
|
44
|
-
}catch(error){
|
|
45
|
-
console.log(`error occured while encrypting ${plainText}`);
|
|
46
|
-
console.log(error);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
async decrypt(text) {
|
|
52
|
-
try {
|
|
53
|
-
// Convert the base64 string to buffer
|
|
54
|
-
var buf = Buffer.from(text, 'base64');
|
|
55
|
-
|
|
56
|
-
// Get the CiphertextBlob length stored in the firsty byte
|
|
57
|
-
var length = parseInt(buf[0].toString());
|
|
58
|
-
|
|
59
|
-
// Extract the CiphertextBlob, IV and
|
|
60
|
-
var cipherBlob = buf.slice(1, length+1);
|
|
61
|
-
var iv = buf.slice(length + 1, length + 1 + 16);
|
|
62
|
-
var encText = buf.slice(length + 1 + 16, buf.length);
|
|
63
|
-
|
|
64
|
-
// Decrypt the CiphertextBlob and get the plaintext key
|
|
65
|
-
const dataKey = await kms.decrypt({ 'CiphertextBlob':cipherBlob }).promise();
|
|
66
|
-
|
|
67
|
-
// Create aes decryptor and decrypt the text
|
|
68
|
-
var decryptor = await crypto.createDecipheriv(algorithm, dataKey.Plaintext, iv)
|
|
69
|
-
decryptor.write(encText);
|
|
70
|
-
decryptor.end();
|
|
71
|
-
return decryptor.read().toString();
|
|
72
|
-
}catch (error) {
|
|
73
|
-
console.log(`error occured while decrypting ${text}`);
|
|
74
|
-
console.log(error); }
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function extractMasterKey(keyInfo, fieldName){
|
|
80
|
-
let keyArn;
|
|
81
|
-
var keyWithValues = keyInfo.split(';');
|
|
82
|
-
for(var keyWithValue of keyWithValues){
|
|
83
|
-
var parts = keyWithValue.split('=');
|
|
84
|
-
if(parts[0].toLowerCase() === fieldName.toLowerCase()){
|
|
85
|
-
keyArn = parts[1];
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return keyArn;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/*const ssm = arn value;
|
|
93
|
-
|
|
94
|
-
encrypt('','4124932222222223',ssm).then(async value=>{
|
|
95
|
-
console.log(value);
|
|
96
|
-
data = await decrypt(value)
|
|
97
|
-
console.log(data);
|
|
98
|
-
}).catch(error=>{
|
|
99
|
-
console.log(error)
|
|
100
|
-
})*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
1
|
+
const AWS = require('aws-sdk');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
const environment = (process.env.NODE_ENVIRONMENT || 'Test');
|
|
5
|
+
const awsRegion = (process.env.AWS_DEFAULT_REGION || 'us-east-2');
|
|
6
|
+
AWS.config.update({ region: awsRegion });
|
|
7
|
+
|
|
8
|
+
const kms = new AWS.KMS();
|
|
9
|
+
const algorithm = 'AES-256-CBC';
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
|
|
13
|
+
async encrypt(initVector,plainText,keyInfo){
|
|
14
|
+
try{
|
|
15
|
+
const masterKey = extractMasterKey(keyInfo,'keyid');
|
|
16
|
+
const params = {
|
|
17
|
+
KeyId: masterKey,
|
|
18
|
+
KeySpec: 'AES_256'// Specifies the type of data key to return.
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const dataKey = await kms.generateDataKey(params).promise();
|
|
22
|
+
|
|
23
|
+
// Copy the length of the CiphertextBlob as first byte and concatenate the CiphertextBlob to it
|
|
24
|
+
var length = Buffer.from(['0x' + dataKey.CiphertextBlob.length.toString(16)]);
|
|
25
|
+
var str = Buffer.concat([length, dataKey.CiphertextBlob]);
|
|
26
|
+
|
|
27
|
+
// Get a random IV
|
|
28
|
+
const iv = await crypto.randomBytes(16);
|
|
29
|
+
|
|
30
|
+
// Concatenate the iv to the buffer
|
|
31
|
+
str = Buffer.concat([str, iv]);
|
|
32
|
+
|
|
33
|
+
// Create aes encryptor with KMS plain key
|
|
34
|
+
var encryptor = await crypto.createCipheriv(algorithm, dataKey.Plaintext, iv);
|
|
35
|
+
|
|
36
|
+
// Encrypt the data
|
|
37
|
+
encryptor.write(plainText);
|
|
38
|
+
encryptor.end();
|
|
39
|
+
|
|
40
|
+
// Concatenate the encrypted to the buffer and return the base64 string
|
|
41
|
+
str = Buffer.concat([str, encryptor.read()]);
|
|
42
|
+
|
|
43
|
+
return str.toString('base64');
|
|
44
|
+
}catch(error){
|
|
45
|
+
console.log(`error occured while encrypting ${plainText}`);
|
|
46
|
+
console.log(error);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
async decrypt(text) {
|
|
52
|
+
try {
|
|
53
|
+
// Convert the base64 string to buffer
|
|
54
|
+
var buf = Buffer.from(text, 'base64');
|
|
55
|
+
|
|
56
|
+
// Get the CiphertextBlob length stored in the firsty byte
|
|
57
|
+
var length = parseInt(buf[0].toString());
|
|
58
|
+
|
|
59
|
+
// Extract the CiphertextBlob, IV and
|
|
60
|
+
var cipherBlob = buf.slice(1, length+1);
|
|
61
|
+
var iv = buf.slice(length + 1, length + 1 + 16);
|
|
62
|
+
var encText = buf.slice(length + 1 + 16, buf.length);
|
|
63
|
+
|
|
64
|
+
// Decrypt the CiphertextBlob and get the plaintext key
|
|
65
|
+
const dataKey = await kms.decrypt({ 'CiphertextBlob':cipherBlob }).promise();
|
|
66
|
+
|
|
67
|
+
// Create aes decryptor and decrypt the text
|
|
68
|
+
var decryptor = await crypto.createDecipheriv(algorithm, dataKey.Plaintext, iv)
|
|
69
|
+
decryptor.write(encText);
|
|
70
|
+
decryptor.end();
|
|
71
|
+
return decryptor.read().toString();
|
|
72
|
+
}catch (error) {
|
|
73
|
+
console.log(`error occured while decrypting ${text}`);
|
|
74
|
+
console.log(error); }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function extractMasterKey(keyInfo, fieldName){
|
|
80
|
+
let keyArn;
|
|
81
|
+
var keyWithValues = keyInfo.split(';');
|
|
82
|
+
for(var keyWithValue of keyWithValues){
|
|
83
|
+
var parts = keyWithValue.split('=');
|
|
84
|
+
if(parts[0].toLowerCase() === fieldName.toLowerCase()){
|
|
85
|
+
keyArn = parts[1];
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return keyArn;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/*const ssm = arn value;
|
|
93
|
+
|
|
94
|
+
encrypt('','4124932222222223',ssm).then(async value=>{
|
|
95
|
+
console.log(value);
|
|
96
|
+
data = await decrypt(value)
|
|
97
|
+
console.log(data);
|
|
98
|
+
}).catch(error=>{
|
|
99
|
+
console.log(error)
|
|
100
|
+
})*/
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|