Skip to content

Instantly share code, notes, and snippets.

@felipeandres254
Last active March 10, 2019 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipeandres254/2428c67fa4f441593e830dfb49cab66f to your computer and use it in GitHub Desktop.
Save felipeandres254/2428c67fa4f441593e830dfb49cab66f to your computer and use it in GitHub Desktop.
Google Apps Script web endpoint to auto-validate AWS Certificates
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(/&amp;/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