diff --git a/manifests/grant.pp b/manifests/grant.pp index 6be6ee1..82323a8 100644 --- a/manifests/grant.pp +++ b/manifests/grant.pp @@ -50,9 +50,11 @@ default => "`${dbname}`", } + $nice_mysql_host = regsubst($mysql_host, '/', '_') + $mysql_grant_file = $dbname ? { - /^(\*|%)$/ => "mysqlgrant-${mysql_user}-${mysql_host}-all.sql", - default => "mysqlgrant-${mysql_user}-${mysql_host}-${dbname}.sql", + /^(\*|%)$/ => "mysqlgrant-${mysql_user}-${nice_mysql_host}-all.sql", + default => "mysqlgrant-${mysql_user}-${nice_mysql_host}-${dbname}.sql", } # If dbname has a wildcard, we don't want to create anything @@ -91,7 +93,7 @@ } - exec { "mysqlgrant-${mysql_user}-${mysql_host}-${dbname}": + exec { "mysqlgrant-${mysql_user}-${nice_mysql_host}-${dbname}": command => $exec_command, require => $exec_require, subscribe => File[$mysql_grant_file], @@ -100,13 +102,13 @@ } if $mysql_db_init_query_file != '' and $mysql_create_db == true { - mysql::queryfile { "mysql_db_init_query_file-${mysql_host}-${dbname}": + mysql::queryfile { "mysql_db_init_query_file-${nice_mysql_host}-${dbname}": mysql_file => $mysql_db_init_query_file, mysql_user => $mysql_user, mysql_password => $mysql_password, mysql_db => $mysql_db, mysql_host => $mysql_host, - subscribe => Exec["mysqlgrant-${mysql_user}-${mysql_host}-${dbname}"], + subscribe => Exec["mysqlgrant-${mysql_user}-${nice_mysql_host}-${dbname}"], } } } diff --git a/manifests/user.pp b/manifests/user.pp index c5a110a..52af1ad 100644 --- a/manifests/user.pp +++ b/manifests/user.pp @@ -18,7 +18,8 @@ } } - $mysql_grant_file = "mysqluser-${mysql_user}-${mysql_host}.sql" + $nice_mysql_host = regsubst($mysql_host, '/', '_') + $mysql_grant_file = "mysqluser-${mysql_user}-${nice_mysql_host}.sql" file { $mysql_grant_file: ensure => present, @@ -29,7 +30,7 @@ content => template('mysql/user.erb'), } - exec { "mysqluser-${mysql_user}-${mysql_host}": + exec { "mysqluser-${mysql_user}-${nice_mysql_host}": command => "mysql --defaults-file=/root/.my.cnf -uroot < ${mysql_grant_filepath}/${mysql_grant_file}", require => [ Service['mysql'], File['/root/.my.cnf'] ], subscribe => File[$mysql_grant_file], diff --git a/spec/defines/grant_spec.rb b/spec/defines/grant_spec.rb index b8dd741..6bcd34c 100644 --- a/spec/defines/grant_spec.rb +++ b/spec/defines/grant_spec.rb @@ -96,4 +96,18 @@ FLUSH PRIVILEGES ; ") } end + + describe 'Test grant all privileges on all databases (*) in an IP subnet. Should not create the databases' do + let(:facts) { { :mysql_root_password => 'rootpassword' } } + let(:params) { { :name => 'sample1', + :mysql_db => '*', + :mysql_host => '10.42.42.0/255.255.255.0', + :mysql_user => 'someuser', + :mysql_password => 'somepassword', } } + it { should contain_file('mysqlgrant-someuser-10.42.42.0_255.255.255.0-all.sql').with_content("# This file is managed by Puppet. DO NOT EDIT. +GRANT ALL ON *.* TO 'someuser'@'10.42.42.0/255.255.255.0' IDENTIFIED BY 'somepassword'; +FLUSH PRIVILEGES ; +") } + it { should contain_exec('mysqlgrant-someuser-10.42.42.0_255.255.255.0-*').with_command('mysql --defaults-file=/root/.my.cnf -uroot < /root/puppet-mysql/mysqlgrant-someuser-10.42.42.0_255.255.255.0-all.sql') } + end end