|
9 | 9 | # grant => ['SELECT', 'UPDATE'], |
10 | 10 | # } |
11 | 11 | # |
| 12 | +# @param name |
| 13 | +# The name of the database to create. Database names must: |
| 14 | +# * be longer than 64 characters. |
| 15 | +# * not contain / \ or . characters. |
| 16 | +# * not contain characters that are not permitted in file names. |
| 17 | +# * not end with space characters. |
12 | 18 | # @param user |
13 | 19 | # The user for the database you're creating. |
14 | 20 | # @param password |
|
28 | 34 | # @param grant_options |
29 | 35 | # The grant_options for the grant for user@host on the database. |
30 | 36 | # @param sql |
31 | | -# The path to the sqlfile you want to execute. This can be single file specified as string, or it can be an array of strings. |
| 37 | +# The path to the sqlfile you want to execute. This can be a an array containing one or more file paths. |
32 | 38 | # @param enforce_sql |
33 | 39 | # Specifies whether executing the sqlfiles should happen on every run. If set to false, sqlfiles only run once. |
34 | 40 | # @param ensure |
|
41 | 47 | define mysql::db ( |
42 | 48 | $user, |
43 | 49 | Variant[String, Sensitive[String]] $password, |
44 | | - $tls_options = undef, |
45 | | - $dbname = $name, |
46 | | - $charset = 'utf8', |
47 | | - $collate = 'utf8_general_ci', |
48 | | - $host = 'localhost', |
49 | | - $grant = 'ALL', |
50 | | - $grant_options = undef, |
51 | | - Optional[Variant[Array, Hash, String]] $sql = undef, |
52 | | - $enforce_sql = false, |
53 | | - Enum['absent', 'present'] $ensure = 'present', |
54 | | - $import_timeout = 300, |
55 | | - $import_cat_cmd = 'cat', |
56 | | - $mysql_exec_path = undef, |
| 50 | + $tls_options = undef, |
| 51 | + String $dbname = $name, |
| 52 | + $charset = 'utf8', |
| 53 | + $collate = 'utf8_general_ci', |
| 54 | + $host = 'localhost', |
| 55 | + $grant = 'ALL', |
| 56 | + $grant_options = undef, |
| 57 | + Optional[Array] $sql = undef, |
| 58 | + $enforce_sql = false, |
| 59 | + Enum['absent', 'present'] $ensure = 'present', |
| 60 | + $import_timeout = 300, |
| 61 | + Enum['cat', 'zcat', 'bzcat'] $import_cat_cmd = 'cat', |
| 62 | + $mysql_exec_path = undef, |
57 | 63 | ) { |
58 | | - $table = "${dbname}.*" |
| 64 | + include 'mysql::client' |
59 | 65 |
|
60 | | - $sql_inputs = join([$sql], ' ') |
| 66 | + # Ensure that the database name is valid. |
| 67 | + if $dbname !~ /^[^\/?%*:|\""<>.\s;]{1,64}$/ { |
| 68 | + $message = "The database name '${dbname}' is invalid. Values must: |
| 69 | + * be longer than 64 characters. |
| 70 | + * not contain // \\ or . characters. |
| 71 | + * not contain characters that are not permitted in file names. |
| 72 | + * not end with space characters." |
| 73 | + fail($message) |
| 74 | + } |
61 | 75 |
|
62 | | - include 'mysql::client' |
| 76 | + # Ensure that the sql files passed are valid file paths. |
| 77 | + if $sql { |
| 78 | + $sql.each | $sqlfile | { |
| 79 | + if $sqlfile !~ /^\/(?:[A-Za-z0-9_-]+\/?+)+(?:.[A-Za-z0-9]+)$/ { |
| 80 | + $message = "The file '${sqlfile}' is invalid. A a valid file path is expected." |
| 81 | + fail($message) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
63 | 85 |
|
64 | 86 | if ($mysql_exec_path) { |
65 | 87 | $_mysql_exec_path = $mysql_exec_path |
|
84 | 106 | ensure_resource('mysql_user', "${user}@${host}", $user_resource) |
85 | 107 |
|
86 | 108 | if $ensure == 'present' { |
| 109 | + $table = "${dbname}.*" |
| 110 | + |
87 | 111 | mysql_grant { "${user}@${host}/${table}": |
88 | 112 | privileges => $grant, |
89 | 113 | provider => 'mysql', |
|
96 | 120 | ], |
97 | 121 | } |
98 | 122 |
|
99 | | - $refresh = ! $enforce_sql |
100 | | - |
101 | 123 | if $sql { |
102 | 124 | exec { "${dbname}-import": |
103 | | - command => "${import_cat_cmd} ${sql_inputs} | mysql ${dbname}", |
| 125 | + command => "${import_cat_cmd} ${shell_join($sql)} | mysql ${dbname}", |
104 | 126 | logoutput => true, |
105 | 127 | environment => "HOME=${::root_home}", |
106 | | - refreshonly => $refresh, |
| 128 | + refreshonly => ! $enforce_sql, |
107 | 129 | path => "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:${_mysql_exec_path}", |
108 | 130 | require => Mysql_grant["${user}@${host}/${table}"], |
109 | 131 | subscribe => Mysql_database[$dbname], |
|
0 commit comments