Use root's credentials when executing mysql.
This is necessary when running puppet as root using sudo because mysql will still look in the user's home directory in that case unless told otherwise.
This commit is contained in:
parent
c84ffdeef0
commit
bdb416053a
4 changed files with 46 additions and 37 deletions
|
@ -8,30 +8,30 @@ Puppet::Type.type(:database).provide(:mysql) do
|
|||
optional_commands :mysqladmin => 'mysqladmin'
|
||||
|
||||
def self.instances
|
||||
mysql('-NBe', "show databases").split("\n").collect do |name|
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show databases").split("\n").collect do |name|
|
||||
new(:name => name)
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
mysql('-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}")
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}")
|
||||
end
|
||||
|
||||
def destroy
|
||||
mysqladmin('-f', 'drop', @resource[:name])
|
||||
mysqladmin("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-f', 'drop', @resource[:name])
|
||||
end
|
||||
|
||||
def charset
|
||||
mysql('-NBe', "show create database `#{resource[:name]}`").match(/.*?(\S+)\s\*\//)[1]
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show create database `#{resource[:name]}`").match(/.*?(\S+)\s\*\//)[1]
|
||||
end
|
||||
|
||||
def charset=(value)
|
||||
mysql('-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
||||
end
|
||||
|
||||
def exists?
|
||||
begin
|
||||
mysql('-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
||||
rescue => e
|
||||
debug(e.message)
|
||||
return nil
|
||||
|
|
|
@ -34,19 +34,19 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
end
|
||||
|
||||
def self.query_user_privs
|
||||
results = mysql("mysql", "-Be", "describe user")
|
||||
results = mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", "describe user")
|
||||
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||
@user_privs = column_names.delete_if { |e| !( e =~/_priv$/) }
|
||||
end
|
||||
|
||||
def self.query_db_privs
|
||||
results = mysql("mysql", "-Be", "describe db")
|
||||
results = mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", "describe db")
|
||||
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||
@db_privs = column_names.delete_if { |e| !(e =~/_priv$/) }
|
||||
end
|
||||
|
||||
def mysql_flush
|
||||
mysqladmin "flush-privileges"
|
||||
mysqladmin "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "flush-privileges"
|
||||
end
|
||||
|
||||
# this parses the
|
||||
|
@ -74,11 +74,11 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
name = split_name(@resource[:name])
|
||||
case name[:type]
|
||||
when :user
|
||||
mysql "mysql", "-e", "INSERT INTO user (host, user) VALUES ('%s', '%s')" % [
|
||||
mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-e", "INSERT INTO user (host, user) VALUES ('%s', '%s')" % [
|
||||
name[:host], name[:user],
|
||||
]
|
||||
when :db
|
||||
mysql "mysql", "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [
|
||||
mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [
|
||||
name[:host], name[:user], name[:db],
|
||||
]
|
||||
end
|
||||
|
@ -87,7 +87,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
end
|
||||
|
||||
def destroy
|
||||
mysql "mysql", "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ]
|
||||
mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ]
|
||||
end
|
||||
|
||||
def row_exists?
|
||||
|
@ -96,7 +96,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
if name[:type] == :db
|
||||
fields << :db
|
||||
end
|
||||
not mysql( "mysql", "-NBe", 'SELECT "1" FROM %s WHERE %s' % [ name[:type], fields.map do |f| "%s = '%s'" % [f, name[f]] end.join(' AND ')]).empty?
|
||||
not mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM %s WHERE %s' % [ name[:type], fields.map do |f| "%s=\"%s\"" % [f, name[f]] end.join(' AND ')]).empty?
|
||||
end
|
||||
|
||||
def all_privs_set?
|
||||
|
@ -118,9 +118,9 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
|
||||
case name[:type]
|
||||
when :user
|
||||
privs = mysql "mysql", "-Be", 'select * from user where user="%s" and host="%s"' % [ name[:user], name[:host] ]
|
||||
privs = mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-Be", 'select * from mysql.user where user="%s" and host="%s"' % [ name[:user], name[:host] ]
|
||||
when :db
|
||||
privs = mysql "mysql", "-Be", 'select * from db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]
|
||||
privs = mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-Be", 'select * from mysql.db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]
|
||||
end
|
||||
|
||||
if privs.match(/^$/)
|
||||
|
@ -172,7 +172,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
stmt = stmt << set << where
|
||||
|
||||
validate_privs privs, all_privs
|
||||
mysql "mysql", "-Be", stmt
|
||||
mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", stmt
|
||||
mysql_flush
|
||||
end
|
||||
|
||||
|
|
|
@ -8,35 +8,35 @@ Puppet::Type.type(:database_user).provide(:mysql) do
|
|||
optional_commands :mysqladmin => 'mysqladmin'
|
||||
|
||||
def self.instances
|
||||
users = mysql("mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
|
||||
users = mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
|
||||
users.select{ |user| user =~ /.+@/ }.collect do |name|
|
||||
new(:name => name)
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
mysql("mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
|
||||
end
|
||||
|
||||
def destroy
|
||||
mysql("mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
||||
end
|
||||
|
||||
def password_hash
|
||||
mysql("mysql", "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
|
||||
end
|
||||
|
||||
def password_hash=(string)
|
||||
mysql("mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
|
||||
mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
|
||||
end
|
||||
|
||||
def exists?
|
||||
not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
|
||||
not mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "-NBe", "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
|
||||
end
|
||||
|
||||
def flush
|
||||
@property_hash.clear
|
||||
mysqladmin "flush-privileges"
|
||||
mysqladmin "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "flush-privileges"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,18 +1,27 @@
|
|||
require 'puppet'
|
||||
require 'mocha'
|
||||
require 'spec_helper'
|
||||
RSpec.configure do |config|
|
||||
config.mock_with :mocha
|
||||
end
|
||||
provider_class = Puppet::Type.type(:database_grant).provider(:mysql)
|
||||
describe provider_class do
|
||||
root_home = '/some/root/home'
|
||||
#root_home = ''
|
||||
|
||||
let :facts do
|
||||
{ :root_home => root_home }
|
||||
end
|
||||
|
||||
before :each do
|
||||
@resource = Puppet::Type::Database_grant.new(
|
||||
{ :privileges => 'all', :provider => 'mysql', :name => 'user@host'}
|
||||
)
|
||||
@provider = provider_class.new(@resource)
|
||||
end
|
||||
|
||||
it 'should query privilegess from the database' do
|
||||
provider_class.expects(:mysql) .with('mysql', '-Be', 'describe user').returns <<-EOT
|
||||
provider_class.expects(:mysql) .with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe user').returns <<-EOT
|
||||
Field Type Null Key Default Extra
|
||||
Host char(60) NO PRI
|
||||
User char(16) NO PRI
|
||||
|
@ -21,7 +30,7 @@ Select_priv enum('N','Y') NO N
|
|||
Insert_priv enum('N','Y') NO N
|
||||
Update_priv enum('N','Y') NO N
|
||||
EOT
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', 'describe db').returns <<-EOT
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe db').returns <<-EOT
|
||||
Field Type Null Key Default Extra
|
||||
Host char(60) NO PRI
|
||||
Db char(64) NO PRI
|
||||
|
@ -35,7 +44,7 @@ EOT
|
|||
end
|
||||
|
||||
it 'should query set priviliges' do
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', 'select * from user where user="user" and host="host"').returns <<-EOT
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
|
||||
Host User Password Select_priv Insert_priv Update_priv
|
||||
host user Y N Y
|
||||
EOT
|
||||
|
@ -43,7 +52,7 @@ EOT
|
|||
end
|
||||
|
||||
it 'should recognize when all priviliges are set' do
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', 'select * from user where user="user" and host="host"').returns <<-EOT
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
|
||||
Host User Password Select_priv Insert_priv Update_priv
|
||||
host user Y Y Y
|
||||
EOT
|
||||
|
@ -51,7 +60,7 @@ EOT
|
|||
end
|
||||
|
||||
it 'should recognize when all privileges are not set' do
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', 'select * from user where user="user" and host="host"').returns <<-EOT
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
|
||||
Host User Password Select_priv Insert_priv Update_priv
|
||||
host user Y N Y
|
||||
EOT
|
||||
|
@ -59,23 +68,23 @@ EOT
|
|||
end
|
||||
|
||||
it 'should be able to set all privileges' do
|
||||
provider_class.expects(:mysql).with('mysql', '-NBe', 'SELECT "1" FROM user WHERE user = \'user\' AND host = \'host\'').returns "1\n"
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with("flush-privileges")
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", "flush-privileges")
|
||||
@provider.privileges=(['all'])
|
||||
end
|
||||
|
||||
it 'should be able to set partial privileges' do
|
||||
provider_class.expects(:mysql).with('mysql', '-NBe', 'SELECT "1" FROM user WHERE user = \'user\' AND host = \'host\'').returns "1\n"
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'N', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with("flush-privileges")
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'N', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", "flush-privileges")
|
||||
@provider.privileges=(['Select_priv', 'Update_priv'])
|
||||
end
|
||||
|
||||
it 'should be case insensitive' do
|
||||
provider_class.expects(:mysql).with('mysql', '-NBe', 'SELECT "1" FROM user WHERE user = \'user\' AND host = \'host\'').returns "1\n"
|
||||
provider_class.expects(:mysql).with('mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with('flush-privileges')
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
|
||||
provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
|
||||
provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", 'flush-privileges')
|
||||
@provider.privileges=(['SELECT_PRIV', 'insert_priv', 'UpDaTe_pRiV'])
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue