Last active
March 10, 2019 15:44
-
-
Save felipeandres254/2428c67fa4f441593e830dfb49cab66f to your computer and use it in GitHub Desktop.
Google Apps Script web endpoint to auto-validate AWS Certificates
This file contains 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
function doPost(event) { | |
// Check script token | |
var token = PropertiesService.getScriptProperties().getProperties().token || ''; | |
if (!event.parameter.token || event.parameter.token != token) { | |
return ContentService.createTextOutput(JSON.stringify({ success: false, message: 'Invalid token' })) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
// Check domain parameter | |
if (!event.parameter.domain) { | |
return ContentService.createTextOutput(JSON.stringify({ success: false, message: 'Domain is required' })) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
// Check threads | |
var threads = GmailApp.search('subject:certificate approval'); | |
if (threads.length == 0) { | |
return ContentService.createTextOutput(JSON.stringify({ success: false, message: 'No threads to process' })) | |
.setMimeType(ContentService.MimeType.JSON); | |
} | |
var links = []; threads.forEach(function(thread) { | |
// Check messages | |
var messages = thread.getMessages(); | |
if (messages.length == 0) { | |
Logger.log('No messages to process'); return; | |
} | |
messages.forEach(function(message) { | |
// Check domain match | |
if (message.getSubject() != 'Certificate approval for '+event.parameter.domain) { | |
Logger.log('Domain does not match'); return; | |
} | |
// Get approval link | |
var link = ''; message.getBody().match(/<a ([^>]+)>/g).some(function(a) { | |
if (/id="approval_url"/.test(a)) { | |
link = a.match(/href="([^"]+)"/)[1].replace(/&/g, '&'); | |
return true; | |
} | |
}); | |
if (link == '') { | |
Logger.log('No approval link to process'); | |
message.moveToTrash(); return; | |
} | |
var context = link.match(/context=(.+)/)[1] | |
var exists = links.some(function(eLink) { | |
return context == eLink.match(/context=(.+)/)[1]; | |
}); | |
exists ? message.moveToTrash() : links.push(link); | |
}); | |
}); | |
return ContentService.createTextOutput(JSON.stringify({ success: links.length>0, domain: event.parameter.domain, results: links })) | |
.setMimeType(ContentService.MimeType.JSON); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment