Last active
November 24, 2022 01:16
-
-
Save ranfysvalle02/ca0ab20a02d618a1e95ee4fba96cd5e5 to your computer and use it in GitHub Desktop.
Atlas Logs + IPWHOIS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ADMIN_API_BASE_URL = "https://realm.mongodb.com/api/admin/v3.0"; | |
exports = async function() { | |
const axios = require('axios'); | |
let getBearerToken = async function(){ | |
var data = JSON.stringify({ | |
"username": "__USERNAME_GOES_HERE__", | |
"apiKey": "__APIKEY_GOES_HERE__" | |
}); | |
var config = { | |
method: 'post', | |
url: 'https://realm.mongodb.com/api/admin/v3.0/auth/providers/mongodb-cloud/login', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
data : data | |
}; | |
let r = await axios(config); | |
return r.data.access_token; | |
}; | |
let bToken = await getBearerToken(); | |
let getLogs = async function(){ | |
var config = { | |
method: 'get', | |
//url: 'https://realm.mongodb.com/api/admin/v3.0/groups/<group-id>/apps/<app-id>/logs?start_date=2022-11-22T17:36:36.489Z&end_date=2022-11-23T17:36:36.489Z', | |
url: 'https://realm.mongodb.com/api/admin/v3.0/groups/<group-id>/apps/<app-id>/logs', | |
headers: { | |
'Authorization': 'Bearer '+bToken, | |
'Content-Type': 'application/json' | |
}, | |
}; | |
let r = await axios(config); | |
return r.data; | |
} | |
let r = await getLogs(); | |
let logs = r.logs; | |
let logsEnhanced = []; | |
for(let i = 0; i < logs.length; i++){ | |
if(logs[i]['remote_ip_address']){ | |
let ipLookup = await axios({method:"get",url:'https://ipwho.is/'+encodeURIComponent(logs[i]['remote_ip_address'])+"?fields=country,city&output=json"}); | |
logs[i]['IPWHOIS'] = ipLookup.data; | |
} | |
logsEnhanced.push(logs[i]); | |
} | |
return logs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment