Skip to content

Instantly share code, notes, and snippets.

@jerry42
Created March 17, 2021 11:19
Show Gist options
  • Save jerry42/d96a38d17f53442f4bb35d561481da09 to your computer and use it in GitHub Desktop.
Save jerry42/d96a38d17f53442f4bb35d561481da09 to your computer and use it in GitHub Desktop.
"use strict"
const shell = require("shelljs")
const args = process.argv
if (args[2] == undefined) {
console.log(`You must enter your policy name`)
return false
}
const policyName = args[2]
let listRole = shell.exec(`aws iam list-roles | grep RoleName`, { silent: true }).stdout.split("\n")
console.log(`Searching for policy in ${listRole.length - 1} role(s)`)
for (let i = 0; i < listRole.length - 1; i++) {
let t = listRole[i].split(":")
let current_role = t[1].replace('"', "").replace('"', "").replace(",", "").trim()
let policiesList = JSON.parse(shell.exec(`aws iam list-attached-role-policies --role-name ${current_role}`, { silent: true }))
Object.entries(policiesList.AttachedPolicies).forEach(([key, value]) => {
if (value.PolicyName == policyName) {
console.log(`Policy (${value.PolicyName}) found for role : "${current_role}"`)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment