Skip to content

Commit 84b9108

Browse files
committed
Encryption and decryption now works
1 parent ad83ec8 commit 84b9108

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

lib/actions/publish.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function processAction(msg, cfg) {
3333
body: msg.body.payload || msg.body,
3434
attachments: msg.attachments
3535
});
36+
console.log(encryptedData, encryptor.decryptMessageContent(encryptedData));
3637
channel.publish(amqpExchange, 'foo', encryptedData, {
3738
contentType: "application/octet-stream",
3839
messageId: msg.id
@@ -42,6 +43,10 @@ function processAction(msg, cfg) {
4243
console.log('Message publishing confirmed id=%s', msg.id);
4344
this.emit('data', msg);
4445
this.emit('end');
46+
console.log('Closing the channel');
47+
yield channel.close();
48+
console.log('Closing the connnection');
49+
yield conn.close();
4550
}.bind(this)).catch(err => {
4651
console.log('Error occurred', err.stack || err);
4752
this.emit('error', err);

lib/cipher.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ function encryptIV(rawData) {
3434
function decryptIV(encData) {
3535
debug('About to decrypt:', encData);
3636

37-
if (!_.isString(encData)) {
38-
throw new Error('RabbitMQ message cipher.decryptIV() accepts only string as parameter.');
39-
}
40-
4137
if (!PASSWORD) {
42-
return encData;
38+
console.log('Decryption will be skipped as ELASTICIO_MESSAGE_CRYPTO_PASSWORD env is empty');
39+
return encData;
4340
}
4441

4542
if (!VECTOR) {

lib/encryptor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function encryptMessageContent(messagePayload) {
77
return cipher.encrypt(JSON.stringify(messagePayload));
88
}
99

10-
function decryptMessageContent(messagePayload, messageHeaders) {
11-
if (!messagePayload || messagePayload.toString().length === 0) {
12-
return null;
10+
function decryptMessageContent(messagePayload) {
11+
if (!messagePayload || ! Buffer.isBuffer(messagePayload)) {
12+
throw new Error("Message payload supplied for decryption is either empty or not a Buffer");
1313
}
1414
try {
15-
return JSON.parse(cipher.decrypt(messagePayload.toString()));
15+
return JSON.parse(cipher.decrypt(messagePayload));
1616
} catch (err) {
1717
console.error(err.stack);
1818
throw Error('Failed to decrypt message: ' + err.message);

0 commit comments

Comments
 (0)