2011-05-18 17:45:08 +04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'net/ldap'
|
|
|
|
require 'optparse'
|
|
|
|
require 'yaml'
|
2011-05-23 17:39:42 +04:00
|
|
|
require 'kwalify'
|
2018-02-07 00:29:48 +03:00
|
|
|
require 'pg'
|
2018-03-13 18:21:20 +03:00
|
|
|
require "pg_ldap_sync/logger"
|
2011-05-18 17:45:08 +04:00
|
|
|
|
|
|
|
module PgLdapSync
|
|
|
|
class Application
|
|
|
|
attr_accessor :config_fname
|
|
|
|
attr_accessor :log
|
|
|
|
attr_accessor :test
|
|
|
|
|
|
|
|
def string_to_symbol(hash)
|
|
|
|
if hash.kind_of?(Hash)
|
|
|
|
return hash.inject({}){|h, v|
|
|
|
|
raise "expected String instead of #{h.inspect}" unless v[0].kind_of?(String)
|
|
|
|
h[v[0].intern] = string_to_symbol(v[1])
|
|
|
|
h
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-23 17:39:42 +04:00
|
|
|
|
|
|
|
def validate_config(config, schema, fname)
|
|
|
|
schema = YAML.load_file(schema)
|
|
|
|
validator = Kwalify::Validator.new(schema)
|
|
|
|
errors = validator.validate(config)
|
|
|
|
if errors && !errors.empty?
|
|
|
|
errors.each do |err|
|
|
|
|
log.fatal "error in #{fname}: [#{err.path}] #{err.message}"
|
|
|
|
end
|
2018-03-13 18:21:20 +03:00
|
|
|
raise InvalidConfig, 78 # EX_CONFIG
|
2011-05-23 17:39:42 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_config_file(fname)
|
|
|
|
raise "Config file #{fname.inspect} does not exist" unless File.exist?(fname)
|
|
|
|
config = YAML.load(File.read(fname))
|
|
|
|
|
|
|
|
schema_fname = File.join(File.dirname(__FILE__), '../../config/schema.yaml')
|
|
|
|
validate_config(config, schema_fname, fname)
|
|
|
|
|
|
|
|
@config = string_to_symbol(config)
|
|
|
|
end
|
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
LdapRole = Struct.new :name, :dn, :member_dns
|
|
|
|
|
|
|
|
def search_ldap_users
|
|
|
|
ldap_user_conf = @config[:ldap_users]
|
|
|
|
|
|
|
|
users = []
|
2011-07-07 17:07:44 +04:00
|
|
|
res = @ldap.search(:base => ldap_user_conf[:base], :filter => ldap_user_conf[:filter]) do |entry|
|
2011-05-18 17:45:08 +04:00
|
|
|
name = entry[ldap_user_conf[:name_attribute]].first
|
|
|
|
|
|
|
|
unless name
|
2011-05-24 10:06:08 +04:00
|
|
|
log.warn "user attribute #{ldap_user_conf[:name_attribute].inspect} not defined for #{entry.dn}"
|
2011-05-18 17:45:08 +04:00
|
|
|
next
|
|
|
|
end
|
2012-11-14 14:03:50 +04:00
|
|
|
name.downcase! if ldap_user_conf[:lowercase_name]
|
2011-05-18 17:45:08 +04:00
|
|
|
|
|
|
|
log.info "found user-dn: #{entry.dn}"
|
|
|
|
user = LdapRole.new name, entry.dn
|
|
|
|
users << user
|
|
|
|
entry.each do |attribute, values|
|
|
|
|
log.debug " #{attribute}:"
|
|
|
|
values.each do |value|
|
2011-05-23 12:49:25 +04:00
|
|
|
log.debug " --->#{value.inspect}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-07 17:07:44 +04:00
|
|
|
raise LdapError, "LDAP: #{@ldap.get_operation_result.message}" unless res
|
2011-05-18 17:45:08 +04:00
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_ldap_groups
|
|
|
|
ldap_group_conf = @config[:ldap_groups]
|
|
|
|
|
|
|
|
groups = []
|
2011-07-07 17:07:44 +04:00
|
|
|
res = @ldap.search(:base => ldap_group_conf[:base], :filter => ldap_group_conf[:filter]) do |entry|
|
2011-05-18 17:45:08 +04:00
|
|
|
name = entry[ldap_group_conf[:name_attribute]].first
|
|
|
|
|
|
|
|
unless name
|
2011-05-24 10:06:08 +04:00
|
|
|
log.warn "user attribute #{ldap_group_conf[:name_attribute].inspect} not defined for #{entry.dn}"
|
2011-05-18 17:45:08 +04:00
|
|
|
next
|
|
|
|
end
|
2012-11-14 14:03:50 +04:00
|
|
|
name.downcase! if ldap_group_conf[:lowercase_name]
|
2011-05-18 17:45:08 +04:00
|
|
|
|
|
|
|
log.info "found group-dn: #{entry.dn}"
|
|
|
|
group = LdapRole.new name, entry.dn, entry[ldap_group_conf[:member_attribute]]
|
|
|
|
groups << group
|
|
|
|
entry.each do |attribute, values|
|
|
|
|
log.debug " #{attribute}:"
|
|
|
|
values.each do |value|
|
2011-05-23 12:49:25 +04:00
|
|
|
log.debug " --->#{value.inspect}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-07 17:07:44 +04:00
|
|
|
raise LdapError, "LDAP: #{@ldap.get_operation_result.message}" unless res
|
2011-05-18 17:45:08 +04:00
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
|
|
|
PgRole = Struct.new :name, :member_names
|
|
|
|
|
2018-02-07 00:31:06 +03:00
|
|
|
# List of default roles taken from https://www.postgresql.org/docs/current/static/default-roles.html
|
|
|
|
PG_BUILTIN_ROLES = %w[ pg_signal_backend pg_monitor pg_read_all_settings pg_read_all_stats pg_stat_scan_tables]
|
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
def search_pg_users
|
|
|
|
pg_users_conf = @config[:pg_users]
|
|
|
|
|
|
|
|
users = []
|
2011-07-07 18:17:01 +04:00
|
|
|
res = pg_exec "SELECT rolname FROM pg_roles WHERE #{pg_users_conf[:filter]}"
|
2011-05-18 17:45:08 +04:00
|
|
|
res.each do |tuple|
|
2011-05-24 15:52:21 +04:00
|
|
|
user = PgRole.new tuple[0]
|
2018-02-07 00:31:06 +03:00
|
|
|
next if PG_BUILTIN_ROLES.include?(user.name)
|
2011-05-24 15:52:21 +04:00
|
|
|
log.info{ "found pg-user: #{user.name.inspect}"}
|
2011-05-18 17:45:08 +04:00
|
|
|
users << user
|
|
|
|
end
|
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_pg_groups
|
|
|
|
pg_groups_conf = @config[:pg_groups]
|
|
|
|
|
|
|
|
groups = []
|
2011-07-07 18:17:01 +04:00
|
|
|
res = pg_exec "SELECT rolname, oid FROM pg_roles WHERE #{pg_groups_conf[:filter]}"
|
2011-05-18 17:45:08 +04:00
|
|
|
res.each do |tuple|
|
2018-02-06 16:55:20 +03:00
|
|
|
res2 = pg_exec "SELECT pr.rolname FROM pg_auth_members pam JOIN pg_roles pr ON pr.oid=pam.member WHERE pam.roleid=#{@pgconn.escape_string(tuple[1])}"
|
2011-05-24 15:52:21 +04:00
|
|
|
member_names = res2.map{|row| row[0] }
|
|
|
|
group = PgRole.new tuple[0], member_names
|
2018-02-07 00:31:06 +03:00
|
|
|
next if PG_BUILTIN_ROLES.include?(group.name)
|
2011-05-24 15:52:21 +04:00
|
|
|
log.info{ "found pg-group: #{group.name.inspect} with members: #{member_names.inspect}"}
|
2011-05-18 17:45:08 +04:00
|
|
|
groups << group
|
|
|
|
end
|
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
|
|
|
def uniq_names(list)
|
|
|
|
names = {}
|
|
|
|
new_list = list.select do |entry|
|
|
|
|
name = entry.name
|
|
|
|
if names[name]
|
|
|
|
log.warn{ "duplicated group/user #{name.inspect} (#{entry.inspect})" }
|
|
|
|
next false
|
|
|
|
else
|
|
|
|
names[name] = true
|
|
|
|
next true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return new_list
|
|
|
|
end
|
|
|
|
|
|
|
|
MatchedRole = Struct.new :ldap, :pg, :name, :state, :type
|
|
|
|
|
|
|
|
def match_roles(ldaps, pgs, type)
|
|
|
|
ldap_by_name = ldaps.inject({}){|h,u| h[u.name] = u; h }
|
|
|
|
pg_by_name = pgs.inject({}){|h,u| h[u.name] = u; h }
|
|
|
|
|
2011-05-23 12:34:53 +04:00
|
|
|
roles = []
|
2011-05-18 17:45:08 +04:00
|
|
|
ldaps.each do |ld|
|
|
|
|
pg = pg_by_name[ld.name]
|
2011-05-23 12:34:53 +04:00
|
|
|
role = MatchedRole.new ld, pg, ld.name
|
|
|
|
roles << role
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
pgs.each do |pg|
|
|
|
|
ld = ldap_by_name[pg.name]
|
|
|
|
next if ld
|
2011-05-23 12:34:53 +04:00
|
|
|
role = MatchedRole.new ld, pg, pg.name
|
|
|
|
roles << role
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
2011-05-23 12:34:53 +04:00
|
|
|
roles.each do |r|
|
|
|
|
r.state = case
|
|
|
|
when r.ldap && !r.pg then :create
|
|
|
|
when !r.ldap && r.pg then :drop
|
|
|
|
when r.pg && r.ldap then :keep
|
|
|
|
else raise "invalid user #{r.inspect}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
2011-05-23 12:34:53 +04:00
|
|
|
r.type = type
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
2011-05-23 12:34:53 +04:00
|
|
|
log.info{
|
|
|
|
roles.each do |role|
|
|
|
|
log.debug{ "#{role.state} #{role.type}: #{role.name}" }
|
|
|
|
end
|
|
|
|
"#{type} stat: create: #{roles.count{|r| r.state==:create }} drop: #{roles.count{|r| r.state==:drop }} keep: #{roles.count{|r| r.state==:keep }}"
|
|
|
|
}
|
|
|
|
return roles
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
2018-03-13 18:21:20 +03:00
|
|
|
def try_sql(text)
|
|
|
|
begin
|
|
|
|
@pgconn.exec "SAVEPOINT try_sql;"
|
|
|
|
@pgconn.exec text
|
|
|
|
rescue PG::Error => err
|
|
|
|
@pgconn.exec "ROLLBACK TO try_sql;"
|
|
|
|
|
|
|
|
log.error{ "#{err} (#{err.class})" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-07 18:17:01 +04:00
|
|
|
def pg_exec_modify(sql)
|
2011-05-24 15:52:21 +04:00
|
|
|
log.info{ "SQL: #{sql}" }
|
2011-05-18 17:45:08 +04:00
|
|
|
unless self.test
|
2018-03-13 18:21:20 +03:00
|
|
|
try_sql sql
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-07 18:17:01 +04:00
|
|
|
def pg_exec(sql)
|
|
|
|
res = @pgconn.exec sql
|
|
|
|
(0...res.num_tuples).map{|t| (0...res.num_fields).map{|i| res.getvalue(t, i) } }
|
|
|
|
end
|
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
def create_pg_role(role)
|
|
|
|
pg_conf = @config[role.type==:user ? :pg_users : :pg_groups]
|
2011-07-07 18:17:01 +04:00
|
|
|
pg_exec_modify "CREATE ROLE \"#{role.name}\" #{pg_conf[:create_options]}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def drop_pg_role(role)
|
2011-07-07 18:17:01 +04:00
|
|
|
pg_exec_modify "DROP ROLE \"#{role.name}\""
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
2011-07-08 17:50:33 +04:00
|
|
|
def sync_roles_to_pg(roles, for_state)
|
|
|
|
roles.sort{|a,b| a.name<=>b.name }.each do |role|
|
|
|
|
create_pg_role(role) if role.state==:create && for_state==:create
|
|
|
|
drop_pg_role(role) if role.state==:drop && for_state==:drop
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-23 12:44:07 +04:00
|
|
|
MatchedMembership = Struct.new :role_name, :has_member, :state
|
2011-05-20 17:33:46 +04:00
|
|
|
|
|
|
|
def match_memberships(ldap_roles, pg_roles)
|
|
|
|
ldap_by_dn = ldap_roles.inject({}){|h,r| h[r.dn] = r; h }
|
|
|
|
ldap_by_m2m = ldap_roles.inject([]){|a,r|
|
|
|
|
next a unless r.member_dns
|
|
|
|
a + r.member_dns.map{|dn|
|
2011-05-23 12:44:07 +04:00
|
|
|
if has_member=ldap_by_dn[dn]
|
|
|
|
[r.name, has_member.name]
|
2011-05-20 17:33:46 +04:00
|
|
|
else
|
|
|
|
log.warn{"ldap member with dn #{dn} is unknown"}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
}.compact
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_by_name = pg_roles.inject({}){|h,r| h[r.name] = r; h }
|
|
|
|
pg_by_m2m = pg_roles.inject([]){|a,r|
|
|
|
|
next a unless r.member_names
|
|
|
|
a + r.member_names.map{|name|
|
2011-05-23 13:00:56 +04:00
|
|
|
if has_member=pg_by_name[name]
|
|
|
|
[r.name, has_member.name]
|
|
|
|
else
|
2011-05-20 17:33:46 +04:00
|
|
|
log.warn{"pg member with name #{name} is unknown"}
|
2011-05-23 13:00:56 +04:00
|
|
|
nil
|
2011-05-20 17:33:46 +04:00
|
|
|
end
|
|
|
|
}.compact
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships = (ldap_by_m2m & pg_by_m2m).map{|r,mo| MatchedMembership.new r, mo, :keep }
|
|
|
|
memberships += (ldap_by_m2m - pg_by_m2m).map{|r,mo| MatchedMembership.new r, mo, :grant }
|
|
|
|
memberships += (pg_by_m2m - ldap_by_m2m).map{|r,mo| MatchedMembership.new r, mo, :revoke }
|
2011-05-23 12:34:53 +04:00
|
|
|
|
|
|
|
log.info{
|
|
|
|
memberships.each do |membership|
|
2011-05-23 12:44:07 +04:00
|
|
|
log.debug{ "#{membership.state} #{membership.role_name} to #{membership.has_member}" }
|
2011-05-23 12:34:53 +04:00
|
|
|
end
|
|
|
|
"membership stat: grant: #{memberships.count{|u| u.state==:grant }} revoke: #{memberships.count{|u| u.state==:revoke }} keep: #{memberships.count{|u| u.state==:keep }}"
|
|
|
|
}
|
2011-05-20 17:33:46 +04:00
|
|
|
return memberships
|
|
|
|
end
|
|
|
|
|
2011-05-18 17:45:08 +04:00
|
|
|
def grant_membership(role_name, add_members)
|
2011-05-20 17:51:50 +04:00
|
|
|
pg_conf = @config[:pg_groups]
|
2011-05-18 17:45:08 +04:00
|
|
|
add_members_escaped = add_members.map{|m| "\"#{m}\"" }.join(",")
|
2011-07-07 18:17:01 +04:00
|
|
|
pg_exec_modify "GRANT \"#{role_name}\" TO #{add_members_escaped} #{pg_conf[:grant_options]}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_membership(role_name, rm_members)
|
|
|
|
rm_members_escaped = rm_members.map{|m| "\"#{m}\"" }.join(",")
|
2011-07-07 18:17:01 +04:00
|
|
|
pg_exec_modify "REVOKE \"#{role_name}\" FROM #{rm_members_escaped}"
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
2011-07-08 17:50:33 +04:00
|
|
|
def sync_membership_to_pg(memberships, for_state)
|
2011-05-20 17:33:46 +04:00
|
|
|
grants = {}
|
2011-07-08 17:50:33 +04:00
|
|
|
memberships.select{|ms| ms.state==for_state }.each do |ms|
|
2011-05-20 17:33:46 +04:00
|
|
|
grants[ms.role_name] ||= []
|
2011-05-23 12:44:07 +04:00
|
|
|
grants[ms.role_name] << ms.has_member
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
2011-05-20 17:33:46 +04:00
|
|
|
|
2011-07-08 17:50:33 +04:00
|
|
|
grants.each do |role_name, members|
|
|
|
|
grant_membership(role_name, members) if for_state==:grant
|
|
|
|
revoke_membership(role_name, members) if for_state==:revoke
|
|
|
|
end
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start!
|
2011-05-23 17:39:42 +04:00
|
|
|
read_config_file(@config_fname)
|
2011-05-18 17:45:08 +04:00
|
|
|
|
2011-05-23 17:39:42 +04:00
|
|
|
# gather LDAP users and groups
|
2011-05-18 17:45:08 +04:00
|
|
|
@ldap = Net::LDAP.new @config[:ldap_connection]
|
|
|
|
ldap_users = uniq_names search_ldap_users
|
|
|
|
ldap_groups = uniq_names search_ldap_groups
|
|
|
|
|
2011-05-23 17:39:42 +04:00
|
|
|
# gather PGs users and groups
|
2018-01-31 13:47:28 +03:00
|
|
|
@pgconn = PG.connect @config[:pg_connection]
|
2018-03-13 18:21:20 +03:00
|
|
|
begin
|
|
|
|
@pgconn.transaction do
|
|
|
|
pg_users = uniq_names search_pg_users
|
|
|
|
pg_groups = uniq_names search_pg_groups
|
|
|
|
|
|
|
|
# compare LDAP to PG users and groups
|
|
|
|
mroles = match_roles(ldap_users, pg_users, :user)
|
|
|
|
mroles += match_roles(ldap_groups, pg_groups, :group)
|
|
|
|
|
|
|
|
# compare LDAP to PG memberships
|
|
|
|
mmemberships = match_memberships(ldap_users+ldap_groups, pg_users+pg_groups)
|
|
|
|
|
|
|
|
# drop/revoke roles/memberships first
|
|
|
|
sync_membership_to_pg(mmemberships, :revoke)
|
|
|
|
sync_roles_to_pg(mroles, :drop)
|
|
|
|
# create/grant roles/memberships
|
|
|
|
sync_roles_to_pg(mroles, :create)
|
|
|
|
sync_membership_to_pg(mmemberships, :grant)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@pgconn.close
|
|
|
|
end
|
2011-05-24 13:24:32 +04:00
|
|
|
|
2018-03-13 18:21:20 +03:00
|
|
|
# Determine exitcode
|
|
|
|
if log.had_errors?
|
|
|
|
raise ErrorExit, 1
|
|
|
|
end
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(argv)
|
|
|
|
s = self.new
|
|
|
|
s.config_fname = '/etc/pg_ldap_sync.yaml'
|
2018-03-13 18:21:20 +03:00
|
|
|
s.log = Logger.new($stdout, @error_counters)
|
2011-05-18 17:45:08 +04:00
|
|
|
s.log.level = Logger::ERROR
|
|
|
|
|
2011-05-24 13:24:32 +04:00
|
|
|
OptionParser.new do |opts|
|
2018-02-06 23:58:53 +03:00
|
|
|
opts.version = VERSION
|
2011-05-18 17:45:08 +04:00
|
|
|
opts.banner = "Usage: #{$0} [options]"
|
2018-03-13 18:21:20 +03:00
|
|
|
opts.on("-v", "--[no-]verbose", "Increase verbose level"){|v| s.log.level += v ? -1 : 1 }
|
2011-05-18 17:45:08 +04:00
|
|
|
opts.on("-c", "--config FILE", "Config file [#{s.config_fname}]", &s.method(:config_fname=))
|
|
|
|
opts.on("-t", "--[no-]test", "Don't do any change in the database", &s.method(:test=))
|
|
|
|
|
2011-05-24 13:24:32 +04:00
|
|
|
opts.parse!(argv)
|
2011-05-18 17:45:08 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
s.start!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|