-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiam-admin-import
executable file
·136 lines (117 loc) · 3.82 KB
/
iam-admin-import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "fog"
require "fileutils"
require 'trollop'
require 'yaml'
require 'awesome_print'
require 'csv'
require_relative 'environment.rb'
require_relative 'menu.rb'
require_relative 'account.rb'
include Environment
include Menu
include Account
def send_email(connection, variables)
message= {'Subject' =>{}, 'Body' =>{'Text'=>{},'Html'=>{}}}
filename = File.dirname(__FILE__) + '/templates/welcome_email.txt.erb' # @arg1 and @arg2 are used in example.rhtml
email_body = ERB.new(File.read(filename)).result(binding)
message['Subject']['Data'] = variables[:subject]
message['Body']['Text']['Data'] = email_body
puts variables[:email]
connection.send_email(variables[:from],{'ToAddresses' => [variables[:email]]}, message)
end
def password()
array = (0...5).map{ ('a'..'z').to_a[rand(26)] }
array = array + (0...5).map{ ('A'..'Z').to_a[rand(26)] }
array = array + (0...2).map{ (0..19).to_a[rand(20)] }
array.sort_by{rand}.join
end
def get_account_id(settings)
@connection = Fog::Compute::AWS.new(settings)
@connection.security_groups.all.first.owner_id
end
def create_group(name, permissions)
groups = @connection.list_groups.body["Groups"]
filtered_groups = groups.find {|x| x["GroupName"] == name }
if !filtered_groups.nil?
# puts filtered_groups
puts "#{name} group exists"
return
end
@connection.create_group(name)
@connection.put_group_policy(name, name, permissions)
puts "#{name} group created"
end
def create_user(name, password)
user = @connection.users.get(name)
unless user.nil?
puts "#{name} user exists"
return
end
user = @connection.users.new({:id => name})
user.save
@connection.create_login_profile(name, password)
puts "#{name} created"
access_key = user.access_keys.new
access_key.save
access_key
end
def assign_user(group, name)
@connection.add_user_to_group(group, name)
puts "#{name} assigned to #{group}"
end
def email_connection
settings = set_environment(email_data['account'])
settings.delete("region") unless settings["region"] == nil
Fog::AWS::SES.new(settings)
end
def iam_data
YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'iam.yml'))
end
def email_data
YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'email.yml'))
end
opts = Trollop::options do
opt :environment, "", :type => :string
end
account_name = nil
settings = {}
if opts[:environment] == "" or opts[:environment] == nil then
settings[:aws_access_key_id] = input("please enter access_key")
settings[:aws_secret_access_key] = input("please enter secret_access_key")
account_name = input("please name the account")
else
settings = set_environment(opts[:environment])
settings.delete('region') unless settings["region"] == nil
account_name = opts[:environment]
end
puts settings
account_id = get_account_id(settings)
account_name = set_account_alias(settings, account_name)
iam_data.keys.each do |group_name|
policy = {
'Statement' => [
'Effect' => 'Allow',
'Action' => '*',
'Resource' => '*'
]
}
@connection = Fog::AWS::IAM.new(settings)
create_group(group_name, policy )
@connection.update_account_password_policy(8, 1000, 1,false,true, true, true, true, false, false)
url = "https://#{account_name}.signin.aws.amazon.com/console"
subject = "Welcome to AWS Account #{account_name}"
iam_data[group_name].each do |user|
user_name = user['username']
email = user['email']
user_password = password()
access_key = create_user(user_name, user_password)
unless access_key.nil?
assign_user(group_name, user_name)
variables = {from: email_data['from'], email: email, subject: subject, user_name: user_name, password: user_password, url: url, access_key: access_key.id, secret_access_key: access_key.secret_access_key, account_name: account_name }
send_email(email_connection, variables)
end
end
end