-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#186518458] Added script update-s3-broker-users-with-permissions-bou…
…ndary-policy
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
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
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
74 changes: 74 additions & 0 deletions
74
scripts/add_permissions_boundary_to_existing_s3_broker_users.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "aws-sdk-iam" | ||
require "optparse" | ||
|
||
def main(env, policy_name, dry_run) | ||
iam_client = Aws::IAM::Client.new | ||
|
||
existing_policies = iam_client.list_policies(scope: "Local").policies | ||
matching_policy = existing_policies.find { |policy| policy.policy_name == policy_name } | ||
|
||
unless matching_policy | ||
puts "Error: The provided policy does correspond to an existing IAM policy." | ||
exit 1 | ||
end | ||
|
||
boundary_policy_arn = matching_policy.arn | ||
|
||
response = iam_client.list_entities_for_policy(policy_arn: boundary_policy_arn, entity_filter: "User") | ||
|
||
# Iterate through the users and add the policy if it doesn't exist | ||
response.policy_users.each do |user| | ||
user_name = user.user_name | ||
|
||
next unless user_name.include? env | ||
|
||
# Check if the policy is already attached to the user | ||
attached_boundary_policies = iam_client.list_entities_for_policy(policy_arn: boundary_policy_arn, entity_filter: "User").policy_users | ||
|
||
if attached_boundary_policies.any? { |p| p.user_name == user_name } | ||
puts "Policy already attached to user: #{user_name}" | ||
else | ||
unless dry_run | ||
iam_client.put_user_permissions_boundary( | ||
user_name:, | ||
permissions_boundary: boundary_policy_arn, | ||
) | ||
end | ||
puts "Policy attached to user: #{user_name}" | ||
end | ||
end | ||
end | ||
|
||
ARGV << "-h" if ARGV.empty? | ||
|
||
options = {} | ||
parser = OptionParser.new { |opts| | ||
opts.banner = "Usage: ./add_permissions_boundary_to_existing_s3_broker_users.rb [options]" | ||
|
||
opts.on("--env DEPLOY_ENV", String, "Specify the env this script should operate on") do |env| | ||
options[:env] = env | ||
end | ||
|
||
opts.on("--policy_name POLICY_NAME", String, "Specify the policy that will be added to the s3 broker users") do |policy_name| | ||
options[:policy_name] = policy_name | ||
end | ||
|
||
opts.on("--dry-run", TrueClass, "Specify --dry-run if you want to run the script without changing anything") do |dry_run| | ||
puts "Dry run? #{dry_run}" | ||
options[:dry_run] = true | ||
end | ||
|
||
opts.on_tail("-h", "--help", "Show help") do | ||
puts opts | ||
exit | ||
end | ||
}.parse! | ||
parser.parse! | ||
|
||
if options[:env].nil? || options[:policy_name].nil? | ||
raise "--env and --policy_name are mandatory" | ||
end | ||
|
||
main(options[:env], options[:policy_name], options[:dry_run]) if $PROGRAM_NAME == __FILE__ |