Created
April 12, 2018 22:00
-
-
Save tegomass/1cd87146108b926a054ee4ec86500420 to your computer and use it in GitHub Desktop.
basical structure for aws-sdk's calls
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
const AWS = require('aws-sdk'); | |
const fs = require('fs'); | |
AWS.config.update({ | |
region: 'eu-west-1' | |
}); | |
var cloudwatchlogs = new AWS.CloudWatchLogs({apiVersion: '2014-03-28', region: 'eu-west-1'}); | |
var iot = new AWS.Iot({apiVersion: '2015-05-28', region: 'eu-west-1'}); | |
function listCertificates() { | |
const params = { | |
ascendingOrder: false, | |
pageSize: 250 | |
}; | |
return new Promise((resolve, reject) => { | |
iot.listCertificates(params, function(err, data) { | |
if (err) { | |
reject(err); | |
} | |
else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
function getPolicies(principal) { | |
const params = { | |
principal: principal, | |
ascendingOrder: true, | |
pageSize: 50 | |
}; | |
return new Promise((resolve, reject) => { | |
iot.listPrincipalPolicies(params, function(err, data) { | |
if (err) { | |
reject(err); | |
} | |
else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
function getPolicy(policyName) { | |
const params = { | |
policyName: policyName /* required */ | |
}; | |
return new Promise((resolve, reject) => { | |
iot.getPolicy(params, function(err, data) { | |
if (err) { | |
reject(err); | |
} | |
else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
function listPolicies() { | |
const params = { | |
ascendingOrder: true, | |
pageSize: 250 | |
}; | |
return new Promise((resolve, reject) => { | |
iot.listPolicies(params, function(err, data) { | |
if (err) { | |
reject(err); | |
} | |
else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
async function getPoliciesWithBase() { | |
const listPoliciesResponse = await listPolicies(); | |
const policies = listPoliciesResponse.policies; | |
for (let policy of policies) { | |
const detailedPolicy = await getPolicy(policy.policyName); | |
const policyDocument = detailedPolicy.policyDocument; | |
const regex = policyDocument.match(/topicfilter\/(.+?)\/RCS/); | |
const hasBase = regex !== null; | |
policy.base = hasBase ? regex[1] : 'No base'; | |
} | |
fs.writeFileSync('__eee.json', JSON.stringify(policies, null, 2)); | |
// const addPolicies = await addPoliciesToCertificates(); | |
// console.log(certificates); | |
console.log('aaa'); | |
return policies; | |
} | |
async function run() { | |
const policies = await getPoliciesWithBase(); //contains all policies with base attached | |
const listCertificatesResponse = await listCertificates(); | |
const certificates = listCertificatesResponse.certificates; | |
for (let certificate of certificates) { | |
const getPoliciesResponse = await getPolicies(certificate.certificateArn); | |
certificate.policies = getPoliciesResponse.policies; | |
for (let policy of certificate.policies) { | |
policy.base = policies.find(a => a.policyName === policy.policyName).base; | |
} | |
} | |
fs.writeFileSync('__fff.json', JSON.stringify(certificates, null, 2)); | |
console.log('bbb'); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment