Skip to content

Commit c9d12cd

Browse files
author
Julian Todt
committed
Use puppet4 functions-api
1 parent 3c29f05 commit c9d12cd

18 files changed

+292
-16
lines changed

examples/mysql_user.pp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
mysql_user{ 'redmine@localhost':
88
ensure => present,
9-
password_hash => mysql_password('redmine'),
9+
password_hash => mysql::password('redmine'),
1010
require => Class['mysql::server'],
1111
}
1212

1313
mysql_user{ 'dan@localhost':
1414
ensure => present,
15-
password_hash => mysql_password('blah')
15+
password_hash => mysql::password('blah')
1616
}
1717

1818
mysql_user{ 'dan@%':
1919
ensure => present,
20-
password_hash => mysql_password('blah'),
20+
password_hash => mysql::password('blah'),
2121
}
2222

2323
mysql_user{ 'socketplugin@%':
@@ -27,6 +27,6 @@
2727

2828
mysql_user{ 'socketplugin@%':
2929
ensure => present,
30-
password_hash => mysql_password('blah'),
30+
password_hash => mysql::password('blah'),
3131
plugin => 'mysql_native_password',
3232
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Recursively merges two or more hashes together and returns the resulting hash.
2+
# For example:
3+
#
4+
# $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }
5+
# $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } }
6+
# $merged_hash = mysql::deepmerge($hash1, $hash2)
7+
# # The resulting hash is equivalent to:
8+
# # $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } }
9+
#
10+
# When there is a duplicate key that is a hash, they are recursively merged.
11+
# When there is a duplicate key that is not a hash, the key in the rightmost hash will "win."
12+
# When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate),
13+
# the rightmost style will win.
14+
Puppet::Functions.create_function(:'mysql::deepmerge') do
15+
16+
def deepmerge(*args)
17+
if args.length < 2
18+
raise Puppet::ParseError, _('mysql_deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length }
19+
end
20+
21+
result = {}
22+
args.each do |arg|
23+
next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
24+
# If the argument was not a hash, skip it.
25+
unless arg.is_a?(Hash)
26+
raise Puppet::ParseError, _('mysql_deepmerge: unexpected argument type %{arg_class}, only expects hash arguments.') % { args_class: args.class }
27+
end
28+
29+
# We need to make a copy of the hash since it is frozen by puppet
30+
current = deep_copy(arg)
31+
32+
# Now we have to traverse our hash assigning our non-hash values
33+
# to the matching keys in our result while following our hash values
34+
# and repeating the process.
35+
overlay(result, current)
36+
end
37+
return(result)
38+
end
39+
40+
def normalized?(hash, key)
41+
return true if hash.key?(key)
42+
return false unless key =~ %r{-|_}
43+
other_key = key.include?('-') ? key.tr('-', '_') : key.tr('_', '-')
44+
return false unless hash.key?(other_key)
45+
hash[key] = hash.delete(other_key)
46+
true
47+
end
48+
49+
def overlay(hash1, hash2)
50+
hash2.each do |key, value|
51+
if normalized?(hash1, key) && value.is_a?(Hash) && hash1[key].is_a?(Hash)
52+
overlay(hash1[key], value)
53+
else
54+
hash1[key] = value
55+
end
56+
end
57+
end
58+
59+
def deep_copy(inputhash)
60+
if !inputhash.is_a? Hash
61+
return inputhash
62+
else
63+
hash = Hash.new
64+
inputhash.each do |k,v|
65+
hash.store(k, deep_copy(v))
66+
end
67+
return hash
68+
end
69+
end
70+
end

lib/puppet/functions/mysql/dirname.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Returns the dirname of a path.
2+
Puppet::Functions.create_function(:'mysql::dirname') do
3+
dispatch :dirname do
4+
required_param 'Variant[String, Undef]', :path
5+
return_type 'String'
6+
end
7+
8+
def dirname(path)
9+
return '' if path == nil
10+
return File.dirname(path)
11+
end
12+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'digest/sha1'
2+
# Returns the mysql password hash from the clear text password.
3+
# Hash a string as mysql's "PASSWORD()" function would do it
4+
Puppet::Functions.create_function(:'mysql::password') do
5+
dispatch :password do
6+
required_param 'String', :password
7+
return_type 'String'
8+
end
9+
10+
def password(password)
11+
return '' if password.empty?
12+
return password if password =~ %r{\*[A-F0-9]{40}$}
13+
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(password)).upcase
14+
end
15+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# When given a hash this function strips out all blank entries.
2+
Puppet::Functions.create_function(:'mysql::strip_hash') do
3+
dispatch :strip_hash do
4+
required_param 'Hash', :hash
5+
return_type 'Hash'
6+
end
7+
8+
def strip_hash(hash)
9+
# Filter out all the top level blanks.
10+
hash.reject { |_k, v| v == '' }.each do |_k, v|
11+
v.reject! { |_ki, vi| vi == '' } if v.is_a?(Hash)
12+
end
13+
end
14+
end

manifests/backup/mysqlbackup.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
mysql_user { "${backupuser}@localhost":
2727
ensure => $ensure,
28-
password_hash => mysql_password($backuppassword),
28+
password_hash => mysql::password($backuppassword),
2929
require => Class['mysql::server::root_password'],
3030
}
3131

@@ -88,7 +88,7 @@
8888
'password' => $backuppassword,
8989
}
9090
}
91-
$options = mysql_deepmerge($default_options, $mysql::server::override_options)
91+
$options = mysql::deepmerge($default_options, $mysql::server::override_options)
9292

9393
file { 'mysqlbackup-config-file':
9494
path => '/etc/mysql/conf.d/meb.cnf',

manifests/backup/mysqldump.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
mysql_user { "${backupuser}@localhost":
3232
ensure => $ensure,
33-
password_hash => mysql_password($backuppassword),
33+
password_hash => mysql::password($backuppassword),
3434
require => Class['mysql::server::root_password'],
3535
}
3636

manifests/backup/xtrabackup.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
if $backupuser and $backuppassword {
3333
mysql_user { "${backupuser}@localhost":
3434
ensure => $ensure,
35-
password_hash => mysql_password($backuppassword),
35+
password_hash => mysql::password($backuppassword),
3636
require => Class['mysql::server::root_password'],
3737
}
3838

manifests/db.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
$user_resource = {
3333
ensure => $ensure,
34-
password_hash => mysql_password($password),
34+
password_hash => mysql::password($password),
3535
}
3636
ensure_resource('mysql_user', "${user}@${host}", $user_resource)
3737

manifests/server.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
}
5050

5151
# Create a merged together set of options. Rightmost hashes win over left.
52-
$options = mysql_deepmerge($mysql::params::default_options, $override_options)
52+
$options = mysql::deepmerge($mysql::params::default_options, $override_options)
5353

5454
Class['mysql::server::root_password'] -> Mysql::Db <| |>
5555

manifests/server/binarylog.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$logbin = pick($options['mysqld']['log-bin'], $options['mysqld']['log_bin'], false)
88

99
if $logbin {
10-
$logbindir = mysql_dirname($logbin)
10+
$logbindir = mysql::dirname($logbin)
1111

1212
#Stop puppet from managing directory if just a filename/prefix is specified
1313
if $logbindir != '.' {

manifests/server/config.pp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# on some systems this is /etc/my.cnf.d, while Debian has /etc/mysql/conf.d and FreeBSD something in /usr/local. For the latter systems,
2222
# managing this basedir is also required, to have it available before the package is installed.
23-
$includeparentdir = mysql_dirname($includedir)
23+
$includeparentdir = mysql::dirname($includedir)
2424
if $includeparentdir != '/' and $includeparentdir != '/etc' {
2525
file { $includeparentdir:
2626
ensure => directory,
@@ -39,8 +39,8 @@
3939

4040
# on mariadb systems, $includedir is not defined, but /etc/my.cnf.d has
4141
# to be managed to place the server.cnf there
42-
$configparentdir = mysql_dirname($mysql::server::config_file)
43-
if $configparentdir != '/' and $configparentdir != '/etc' and $configparentdir != $includedir and $configparentdir != mysql_dirname($includedir) {
42+
$configparentdir = mysql::dirname($mysql::server::config_file)
43+
if $configparentdir != '/' and $configparentdir != '/etc' and $configparentdir != $includedir and $configparentdir != mysql::dirname($includedir) {
4444
file { $configparentdir:
4545
ensure => directory,
4646
mode => '0755',

manifests/server/monitor.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
mysql_user { "${mysql_monitor_username}@${mysql_monitor_hostname}":
1111
ensure => present,
12-
password_hash => mysql_password($mysql_monitor_password),
12+
password_hash => mysql::password($mysql_monitor_password),
1313
require => Class['mysql::server::service'],
1414
}
1515

manifests/server/root_password.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if $mysql::server::create_root_user == true and $mysql::server::root_password != 'UNSET' {
2323
mysql_user { 'root@localhost':
2424
ensure => present,
25-
password_hash => mysql_password($mysql::server::root_password),
25+
password_hash => mysql::password($mysql::server::root_password),
2626
require => Exec['remove install pass']
2727
}
2828
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#! /usr/bin/env ruby -S rspec # rubocop:disable Lint/ScriptPermission
2+
3+
require 'spec_helper'
4+
5+
describe 'mysql::deepmerge' do
6+
it 'exists' do
7+
is_expected.not_to eq(nil)
8+
end
9+
10+
it 'throws error with no arguments' do
11+
is_expected.to run.with_params.and_raise_error(Puppet::ParseError)
12+
end
13+
14+
it 'throws error with only one argument' do
15+
is_expected.to run.with_params({ 'one' => 1 }).and_raise_error(Puppet::ParseError)
16+
end
17+
18+
it 'accepts empty strings as puppet undef' do
19+
is_expected.to run.with_params({}, '')
20+
end
21+
22+
index_values = %w[one two three]
23+
expected_values_one = %w[1 2 2]
24+
it 'is able to mysql_deepmerge two hashes' do
25+
new_hash = subject.execute({ 'one' => '1', 'two' => '1' }, { 'two' => '2', 'three' => '2' })
26+
index_values.each_with_index do |index, expected|
27+
expect(new_hash[index]).to eq(expected_values_one[expected])
28+
end
29+
end
30+
31+
it 'mysql_deepmerges multiple hashes' do
32+
hash = subject.execute({ 'one' => 1 }, { 'one' => '2' }, { 'one' => '3' })
33+
expect(hash['one']).to eq('3')
34+
end
35+
36+
it 'accepts empty hashes' do
37+
is_expected.to run.with_params({}, {}, {}).and_return({})
38+
end
39+
40+
expected_values_two = [1, 2, 'four' => 4]
41+
it 'mysql_deepmerges subhashes' do
42+
hash = subject.execute({ 'one' => 1 }, { 'two' => 2, 'three' => { 'four' => 4 } })
43+
index_values.each_with_index do |index, expected|
44+
expect(hash[index]).to eq(expected_values_two[expected])
45+
end
46+
end
47+
48+
it 'appends to subhashes' do
49+
hash = subject.execute({ 'one' => { 'two' => 2 } }, { 'one' => { 'three' => 3 } })
50+
expect(hash['one']).to eq('two' => 2, 'three' => 3)
51+
end
52+
53+
expected_values_three = [1, 'dos', { 'four' => 4, 'five' => 5 }]
54+
it 'appends to subhashes 2' do
55+
hash = subject.execute({ 'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, { 'two' => 'dos', 'three' => { 'five' => 5 } })
56+
index_values.each_with_index do |index, expected|
57+
expect(hash[index]).to eq(expected_values_three[expected])
58+
end
59+
end
60+
61+
index_values_two = %w[key1 key2]
62+
expected_values_four = [{ 'a' => 1, 'b' => 99 }, 'c' => 3]
63+
it 'appends to subhashes 3' do
64+
hash = subject.execute({ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, { 'key1' => { 'b' => 99 } })
65+
index_values_two.each_with_index do |index, expected|
66+
expect(hash[index]).to eq(expected_values_four[expected])
67+
end
68+
end
69+
70+
it 'equates keys mod dash and underscore #value' do
71+
hash = subject.execute({ 'a-b-c' => 1 }, { 'a_b_c' => 10 })
72+
expect(hash['a_b_c']).to eq(10)
73+
end
74+
it 'equates keys mod dash and underscore #not' do
75+
hash = subject.execute({ 'a-b-c' => 1 }, { 'a_b_c' => 10 })
76+
expect(hash).not_to have_key('a-b-c')
77+
end
78+
79+
index_values_three = ['a_b_c', 'b-c-d']
80+
expected_values_five = [10, { 'e-f-g' => 3, 'c_d_e' => 12 }]
81+
index_values_error = ['a-b-c', 'b_c_d']
82+
index_values_three.each_with_index do |index, expected|
83+
it 'keeps style of the last when keys are equal mod dash and underscore #value' do
84+
hash = subject.execute({ 'a-b-c' => 1, 'b_c_d' => { 'c-d-e' => 2, 'e-f-g' => 3 } }, { 'a_b_c' => 10, 'b-c-d' => { 'c_d_e' => 12 } })
85+
expect(hash[index]).to eq(expected_values_five[expected])
86+
end
87+
it 'keeps style of the last when keys are equal mod dash and underscore #not' do
88+
hash = subject.execute({ 'a-b-c' => 1, 'b_c_d' => { 'c-d-e' => 2, 'e-f-g' => 3 } }, { 'a_b_c' => 10, 'b-c-d' => { 'c_d_e' => 12 } })
89+
expect(hash).not_to have_key(index_values_error[expected])
90+
end
91+
end
92+
end

spec/functions/mysql_dirname_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe 'mysql::dirname' do
4+
it 'exists' do
5+
is_expected.not_to eq(nil)
6+
end
7+
8+
it 'raises a ArgumentError if there is less than 1 arguments' do
9+
is_expected.to run.with_params.and_raise_error(ArgumentError)
10+
end
11+
12+
it 'raises a ArgumentError if there is more than 1 arguments' do
13+
is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError)
14+
end
15+
16+
it 'returns path of file' do
17+
is_expected.to run.with_params('spec/functions/mysql_dirname_spec.rb').and_return('spec/functions')
18+
end
19+
end

spec/functions/mysql_password_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
describe 'mysql::password' do
4+
it 'exists' do
5+
is_expected.not_to eq(nil)
6+
end
7+
8+
it 'raises a ArgumentError if there is less than 1 arguments' do
9+
is_expected.to run.with_params.and_raise_error(ArgumentError)
10+
end
11+
12+
it 'raises a ArgumentError if there is more than 1 arguments' do
13+
is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError)
14+
end
15+
16+
it 'converts password into a hash' do
17+
is_expected.to run.with_params('password').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19')
18+
end
19+
20+
it 'converts an empty password into a empty string' do
21+
is_expected.to run.with_params('').and_return('')
22+
end
23+
24+
it 'does not convert a password that is already a hash' do
25+
is_expected.to run.with_params('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19')
26+
end
27+
end

0 commit comments

Comments
 (0)