add support for postgres-pr

This commit is contained in:
Lars Kanis 2011-05-24 13:52:21 +02:00
parent 4777f62ccd
commit e43a96b19b
3 changed files with 35 additions and 11 deletions

View File

@ -5,9 +5,29 @@ require 'net/ldap'
require 'optparse' require 'optparse'
require 'yaml' require 'yaml'
require 'logger' require 'logger'
require 'pg'
require 'kwalify' require 'kwalify'
begin
require 'pg'
rescue LoadError => e
begin
require 'postgres'
class PGconn
alias initialize_before_hash_change initialize
def initialize(*args)
arg = args.first
if args.length==1 && arg.kind_of?(Hash)
initialize_before_hash_change(arg[:host], arg[:port], nil, nil, arg[:dbname], arg[:user], arg[:password])
else
initialize_before_hash_change(*args)
end
end
end
rescue LoadError
raise e
end
end
require 'pg_ldap_sync' require 'pg_ldap_sync'
module PgLdapSync module PgLdapSync
@ -111,8 +131,8 @@ class Application
users = [] users = []
res = @pgconn.exec "SELECT rolname FROM pg_roles WHERE #{pg_users_conf[:filter]}" res = @pgconn.exec "SELECT rolname FROM pg_roles WHERE #{pg_users_conf[:filter]}"
res.each do |tuple| res.each do |tuple|
user = PgRole.new tuple['rolname'] user = PgRole.new tuple[0]
log.info{ "found pg-user: #{user.name}"} log.info{ "found pg-user: #{user.name.inspect}"}
users << user users << user
end end
return users return users
@ -124,10 +144,10 @@ class Application
groups = [] groups = []
res = @pgconn.exec "SELECT rolname, oid FROM pg_roles WHERE #{pg_groups_conf[:filter]}" res = @pgconn.exec "SELECT rolname, oid FROM pg_roles WHERE #{pg_groups_conf[:filter]}"
res.each do |tuple| res.each do |tuple|
res2 = @pgconn.exec "SELECT pr.rolname FROM pg_auth_members pam JOIN pg_roles pr ON pr.oid=pam.member WHERE pam.roleid=$1", [{:value=>tuple['oid']}] res2 = @pgconn.exec "SELECT pr.rolname FROM pg_auth_members pam JOIN pg_roles pr ON pr.oid=pam.member WHERE pam.roleid=#{PGconn.escape(tuple[1])}"
member_names = res2.field_values 'rolname' member_names = res2.map{|row| row[0] }
group = PgRole.new tuple['rolname'], member_names group = PgRole.new tuple[0], member_names
log.info{ "found pg-group: #{group.name}"} log.info{ "found pg-group: #{group.name.inspect} with members: #{member_names.inspect}"}
groups << group groups << group
end end
return groups return groups
@ -186,10 +206,10 @@ class Application
return roles return roles
end end
def pg_exec(sql, params=nil) def pg_exec(sql)
log.info{ "SQL: #{sql}" + (params ? " params: #{params}" : '') } log.info{ "SQL: #{sql}" }
unless self.test unless self.test
@pgconn.exec sql, params @pgconn.exec sql
end end
end end

View File

@ -15,8 +15,12 @@ ldap_groups:
member_attribute: member member_attribute: member
pg_connection: pg_connection:
host: localhost
dbname: postgres dbname: postgres
# needed for postgres-pr:
# host: localhost
# port: 54321
# user: username
# password: secret
pg_users: pg_users:
filter: rolcanlogin AND NOT rolsuper filter: rolcanlogin AND NOT rolsuper