-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
74 lines (64 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
const https = require('https');
const AWS = require('aws-sdk');
AWS.config.region = process.env['S3_REGION'];
const s3 = new AWS.S3();
const s3Bucket = process.env['S3_BUCKET'];
const firebaseDatabaseUrl = process.env['FIREBASE_URL']
const encryptedFirebaseToken = process.env['FIREBASE_TOKEN'];
let decryptedFirebaseToken;
function fetchFromFirebase() {
const options = {
host: firebaseDatabaseUrl,
path: '/.json?auth=' + decryptedFirebaseToken,
};
return new Promise(function(resolve, reject) {
var req = https.get(options, function(res) {
var bodyChunks = '';
res.on('data', function(chunk) {
bodyChunks += chunk;
}).on('end', function() {
return resolve(bodyChunks);
});
});
req.on('error', function(e) {
return reject(e);
});
});
}
function putObjectToS3(data){
const s3 = new AWS.S3();
const params = {
Bucket : s3Bucket,
Key : '' + Date.now() + '.json',
Body : data,
ContentType: 'application/json',
};
s3.putObject(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
console.log('Backup Done');
});
}
exports.handler = function(event, context, callback) {
console.log('Backup Starting');
if (decryptedFirebaseToken) {
fetchFromFirebase()
.then(function(result) {
putObjectToS3(result);
});
} else {
const kms = new AWS.KMS();
kms.decrypt({ CiphertextBlob: new Buffer(encryptedFirebaseToken, 'base64') }, (err, data) => {
if (err) {
console.log('Decrypt error:', err);
return callback(err);
}
decryptedFirebaseToken = data.Plaintext.toString('ascii');
fetchFromFirebase()
.then(function(result) {
putObjectToS3(result);
});
});
}
};