Skip to content

Combine multiple grants into one while checking state #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions lib/puppet/provider/mysql_grant/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
commands mysql_raw: 'mysql'

def self.instances
instances = []
instance_configs = {}
users.map do |user|
user_string = cmd_user(user)
query = "SHOW GRANTS FOR #{user_string};"
Expand Down Expand Up @@ -45,13 +45,6 @@ def self.instances
(priv == 'ALL PRIVILEGES') ? 'ALL' : priv.strip
end
end
sorted_privileges = stripped_privileges.sort
if newer_than('mysql' => '8.0.0') && sorted_privileges == ['ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROLE', 'CREATE ROUTINE', 'CREATE TABLESPACE', 'CREATE TEMPORARY TABLES', 'CREATE USER',
'CREATE VIEW', 'DELETE', 'DROP', 'DROP ROLE', 'EVENT', 'EXECUTE', 'FILE', 'INDEX', 'INSERT', 'LOCK TABLES', 'PROCESS', 'REFERENCES',
'RELOAD', 'REPLICATION CLIENT', 'REPLICATION SLAVE', 'SELECT', 'SHOW DATABASES', 'SHOW VIEW', 'SHUTDOWN', 'SUPER', 'TRIGGER',
'UPDATE']
sorted_privileges = ['ALL']
end
# Same here, but to remove OPTION leaving just GRANT.
options = if %r{WITH\sGRANT\sOPTION}.match?(rest)
['GRANT']
Expand All @@ -61,16 +54,40 @@ def self.instances
# fix double backslash that MySQL prints, so resources match
table.gsub!('\\\\', '\\')
# We need to return an array of instances so capture these
instances << new(
name: "#{user}@#{host}/#{table}",
ensure: :present,
name = "#{user}@#{host}/#{table}"
if instance_configs.key?(name)
instance_config = instance_configs[name]
stripped_privileges.concat instance_config[:privileges]
options.concat instance_config[:options]
end

sorted_privileges = stripped_privileges.uniq.sort
if newer_than('mysql' => '8.0.0') && sorted_privileges == ['ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROLE', 'CREATE ROUTINE', 'CREATE TABLESPACE', 'CREATE TEMPORARY TABLES', 'CREATE USER',
'CREATE VIEW', 'DELETE', 'DROP', 'DROP ROLE', 'EVENT', 'EXECUTE', 'FILE', 'INDEX', 'INSERT', 'LOCK TABLES', 'PROCESS', 'REFERENCES',
'RELOAD', 'REPLICATION CLIENT', 'REPLICATION SLAVE', 'SELECT', 'SHOW DATABASES', 'SHOW VIEW', 'SHUTDOWN', 'SUPER', 'TRIGGER',
'UPDATE']
sorted_privileges = ['ALL']
end

instance_configs[name] = {
privileges: sorted_privileges,
table: table,
user: "#{user}@#{host}",
options: options,
)
options: options.uniq,
}
end
end
instances = []
instance_configs.map do |name, config|
instances << new(
name: name,
ensure: :present,
privileges: config[:privileges],
table: config[:table],
user: config[:user],
options: config[:options],
)
end
instances
end

Expand Down
17 changes: 17 additions & 0 deletions spec/acceptance/types/mysql_grant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,21 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
expect(result.exit_code).to be_zero
end
end

describe 'multiple lines privileges', if: Gem::Version.new(mysql_version) > Gem::Version.new('8.0.0') && Gem::Version.new(mysql_version) < Gem::Version.new('10.0.0') do
pp = <<-MANIFEST
mysql_user { 'multi@localhost':
ensure => present,
}
mysql_grant { 'multi@localhost/*.*':
user => 'multi@localhost',
privileges => ['SELECT', 'INSERT', 'UPDATE', 'BACKUP_ADMIN', 'FLUSH_TABLES'],
table => '*.*',
require => Mysql_user['multi@localhost'],
}
MANIFEST
it 'check idempotency in MySQL 8' do
idempotent_apply(pp)
end
end
end