|
| 1 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// CloudTrail S3 bucket log to SumoLogic // |
| 3 | +// https://github.com/SumoLogic/sumologic-aws-lambda // |
| 4 | +// // |
| 5 | +// YOU MUST CREATE A SUMO LOGIC ENDPOINT CALLED SUMO_ENDPOINT AND PASTE IN ENVIRONMENTAL VARIABLES BELOW // |
| 6 | +// https://help.sumologic.com/Send_Data/Sources/02Sources_for_Hosted_Collectors/HTTP_Source // |
| 7 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 8 | +// SumoLogic Endpoint to post logs |
| 9 | +var SumoURL = process.env.SUMO_ENDPOINT; |
| 10 | + |
| 11 | +var AWS = require('aws-sdk'); |
| 12 | +var s3 = new AWS.S3(); |
| 13 | +var https = require('https'); |
| 14 | +var zlib = require('zlib'); |
| 15 | +var url = require('url'); |
| 16 | + |
| 17 | +function s3LogsToSumo(bucket, objKey,context) { |
| 18 | + var urlObject = url.parse(SumoURL); |
| 19 | + var options = { |
| 20 | + 'hostname': urlObject.hostname, |
| 21 | + 'path': urlObject.pathname, |
| 22 | + 'method': 'POST' |
| 23 | + }; |
| 24 | + options.headers = { |
| 25 | + 'X-Sumo-Name': objKey, |
| 26 | + }; |
| 27 | + var req = https.request(options, function(res) { |
| 28 | + var body = ''; |
| 29 | + console.log('Status:', res.statusCode); |
| 30 | + res.setEncoding('utf8'); |
| 31 | + res.on('data', function(chunk) { body += chunk; }); |
| 32 | + res.on('end', function() { |
| 33 | + console.log('Successfully processed HTTPS response'); |
| 34 | + context.succeed(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + var finalData = ''; |
| 38 | + |
| 39 | + if (objKey.match(/CloudTrail-Digest/)) { |
| 40 | + console.log("digest file are ignored"); |
| 41 | + context.succeed(); |
| 42 | + } |
| 43 | + |
| 44 | + var s3Stream = s3.getObject({Bucket: bucket, Key: objKey}).createReadStream(); |
| 45 | + s3Stream.on('error', function() { |
| 46 | + console.log( |
| 47 | + 'Error getting object "' + objKey + '" from bucket "' + bucket + '". ' + |
| 48 | + 'Make sure they exist and your bucket is in the same region as this function.'); |
| 49 | + context.fail(); |
| 50 | + }); |
| 51 | + var gunzip = zlib.createGunzip(); |
| 52 | + s3Stream.pipe(gunzip); |
| 53 | + gunzip.on('data',function(data) { |
| 54 | + finalData += data.toString(); |
| 55 | + }).on('end',function(end){ |
| 56 | + // READ THE UNZIPPED CloudTrail logs |
| 57 | + var records = JSON.parse(finalData); |
| 58 | + console.log(records.Records.length + " cloudtrail records in this file"); |
| 59 | + for (var i = 0, len = records.Records.length; i < len; i++) { |
| 60 | + req.write(JSON.stringify(records.Records[i]) + '\n'); |
| 61 | + } |
| 62 | + req.end(); |
| 63 | + }).on('error',function(error) { |
| 64 | + context.fail(error); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +exports.handler = function(event, context) { |
| 69 | + //options.agent = new https.Agent(options); |
| 70 | + // Validate URL has been set |
| 71 | + var urlObject = url.parse(SumoURL); |
| 72 | + if (urlObject.protocol != 'https:' || urlObject.host === null || urlObject.path === null) { |
| 73 | + context.fail('Invalid SUMO_ENDPOINT environment variable: ' + SumoURL); |
| 74 | + } |
| 75 | + var bucket = event.Records[0].s3.bucket.name; |
| 76 | + var objKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); |
| 77 | + console.log('Bucket: '+bucket + ' ObjectKey: ' + objKey); |
| 78 | + s3LogsToSumo(bucket, objKey, context); |
| 79 | +} |
0 commit comments