Skip to content

Instantly share code, notes, and snippets.

@larryebaum
Forked from afiune/README.md
Last active September 26, 2017 20:55
Show Gist options
  • Save larryebaum/bc21337784b952e0eb908fe51603c8db to your computer and use it in GitHub Desktop.
Save larryebaum/bc21337784b952e0eb908fe51603c8db to your computer and use it in GitHub Desktop.
Automate LDAP Bulking User Creation

LDAP Bulking User Creation Process

This is temporal automation to create/update multiple LDAP users in Chef Automate. If a user appears in the input file and already exists as a user, the user roles will be updated as specified within the input file. If the user does not exist, it will be created with the specified roles.

Prerequisites

The minimum prerequisites to be able to use this automation are:

Create list of users file

The automation will ask you to provide a file with the list of users to create with the following format:

[USERNAME]|[ROLES]
[USERNAME]|[ROLES]

An example of this file at:

https://gist.github.com/afiune/ac5b4b7074ee9191a876d29ae73fe536#file-users-example-list

Run the automation

Once you have all the prerequisites in place, and the list of users to create, open a terminal and go to the directory that you have previously configured the delivery-cli to point to your Chef Automate Server.

Then run:

➜ ./bulk-user-creation.rb
Automate LDAP Bulking User Creation
Insert the user list file: user.list
Creating Users from 'user.list'
  user1: done.
  user2: done.
#!/opt/chefdk/embedded/bin/ruby
#
# LDAP Bulking User Creation Process
#
# The file format for the list of users must looks like:
# [USERNAME]|[ROLES]
# [USERNAME]|[ROLES]
#
# Where [ROLES] is a comma separated list of roles to
# grant to the user.
#
# Example of this file at:
# => https://gist.github.com/afiune/ac5b4b7074ee9191a876d29ae73fe536#file-users-example-list
require 'JSON'
puts "Automate LDAP Bulking User Creation\n"
# Verify we have the delivery-cli installed
raise "\nERROR: Unable to find the delivery-cli.\n" \
"Install the latest version of ChefDK from: "\
"https://downloads.chef.io/chef-dk/" unless system("which delivery > /dev/null")
# Verify we can communicate to the Automate Server
# and that there is already a `.delivery/cli.toml`
user_out = %x( delivery api get users )
unless user_out =~ /"_links": {/
raise "\nERROR: Unable to interact with the Automate Server.\n" \
'Please make sure you are located in a directory where ' \
'you have already ran the `delivery setup` command.' \
end
# Input the list of users to create
print 'Insert the user list file:'
list_file = gets.strip
raise "File '#{list_file}' not found" unless File.exist?(list_file)
print 'Prune users not in user list file? (yes/no):'
prune = gets.strip
case prune
when 'no'
print 'Retaining all users'
when 'yes'
print "Evaluating list of Automate users to delete...\n"
out0 = %x( delivery api get users --no-color )
userlistjson = JSON.parse(out0)
filecontent = IO.read(list_file)
userlistjson['users'].each do |user|
if filecontent.include? user
print "Existing user #{user} in input file... retaining...\n"
else
print "Existing user #{user } not listed in input file... deleting...\n"
# %x( delivery api delete users/#{user} --no-color )
end
end
end
# Query Listfile for new/duplicates and write each to new file or array.
puts "Creating Users from '#{list_file}'"
File.open(list_file).each do |line|
user_info = line.split('|')
username = user_info[0].strip
roles = user_info[1].strip.split(',').map { |r| '"' + r + '"' }.join(',')
print " #{username}: "
out1 = %x( delivery api post external-users -d '{"name": "#{username}"}' --no-color )
if out1 =~ /conflict|Conflict/
puts 'user exists... updating roles...'
out2 = %x( delivery api post authz/users/#{username} -d '{"set": [#{roles}]}' --no-color )
if out2 =~ /error|Error/
puts 'unable to update roles.'
else
puts 'roles updated.'
end
else
puts 'created new user... applying roles'
out3 = %x( delivery api post authz/users/#{username} -d '{"grant": [#{roles}]}' --no-color )
if out3 =~ /error|Error/
puts 'unable to update roles.'
else
puts 'roles updated.'
end
end
end
user1|admin,committer,reviewer,shipper,observer
user2|committer,reviewer,shipper,observer
user3|admin
user4|admin
user5|observer,shipper
user6|reviewer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment