Skip to content

Commit c73a37b

Browse files
committed
Implement a sensitive param for postgresql_psql
1 parent 62e322e commit c73a37b

File tree

4 files changed

+26
-34
lines changed

4 files changed

+26
-34
lines changed

lib/puppet/provider/postgresql_psql/ruby.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def run_command(command, user, group, environment)
6262
failonfail: false,
6363
combine: true,
6464
override_locale: true,
65-
custom_environment: environment)
65+
custom_environment: environment,
66+
sensitive: resource[:sensitive] == :true)
6667
[output, $CHILD_STATUS.dup]
6768
end
6869
end

lib/puppet/type/postgresql_psql.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def matches(value)
124124
newvalues(:true, :false)
125125
end
126126

127+
newparam(:sensitive, boolean: true) do
128+
desc "If 'true', then the executed command will not be echoed into the log. Use this to protect sensitive information passing through."
129+
130+
defaultto(:false)
131+
newvalues(:true, :false)
132+
end
133+
127134
autorequire(:class) { ['Postgresql::Server::Service'] }
128135

129136
def should_run_sql(refreshing = false)

manifests/server/role.pp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# @param update_password If set to true, updates the password on changes. Set this to false to not modify the role's password after creation.
44
# @param password_hash Sets the hash to use during password creation.
55
# @param createdb Specifies whether to grant the ability to create new databases with this role.
6-
# @param createrole Specifies whether to grant the ability to create new roles with this role.
7-
# @param db Database used to connect to.
6+
# @param createrole Specifies whether to grant the ability to create new roles with this role.
7+
# @param db Database used to connect to.
88
# @param port Port to use when connecting.
99
# @param login Specifies whether to grant login capability for the new role.
1010
# @param inherit Specifies whether to grant inherit capability for the new role.
@@ -76,18 +76,16 @@
7676
$superuser_sql = $superuser ? { true => 'SUPERUSER', default => 'NOSUPERUSER' }
7777
$replication_sql = $replication ? { true => 'REPLICATION', default => '' }
7878
if ($password_hash != false) {
79-
$environment = "NEWPGPASSWD=${password_hash}"
80-
$password_sql = "ENCRYPTED PASSWORD '\$NEWPGPASSWD'"
79+
$password_sql = "ENCRYPTED PASSWORD '${password_hash}'"
8180
} else {
8281
$password_sql = ''
83-
$environment = []
8482
}
8583

8684
postgresql_psql { "CREATE ROLE ${username} ENCRYPTED PASSWORD ****":
8785
command => "CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}",
8886
unless => "SELECT 1 FROM pg_roles WHERE rolname = '${username}'",
89-
environment => $environment,
9087
require => undef,
88+
sensitive => true,
9189
}
9290

9391
postgresql_psql { "ALTER ROLE \"${username}\" ${superuser_sql}":
@@ -136,7 +134,7 @@
136134
postgresql_psql { "ALTER ROLE ${username} ENCRYPTED PASSWORD ****":
137135
command => "ALTER ROLE \"${username}\" ${password_sql}",
138136
unless => "SELECT 1 FROM pg_shadow WHERE usename = '${username}' AND passwd = '${pwd_hash_sql}'",
139-
environment => $environment,
137+
sensitive => true,
140138
}
141139
}
142140
} else {

spec/acceptance/postgresql_psql_spec.rb

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,39 +134,25 @@ class { 'postgresql::server': } ->
134134
apply_manifest(pp_nine, expect_changes: true)
135135
end
136136

137-
context 'with secure password passing by environment' do
138-
it 'runs SQL that contanins password passed by environment' do
139-
select = "select \\'$PASS_TO_EMBED\\'"
140-
pp = <<-MANIFEST.unindent
137+
context 'when setting sensitive => true' do
138+
it 'runs queries without leaking to the log' do
139+
select = "select \\'pa$swD\\'"
140+
pp = <<~MANIFEST
141141
class { 'postgresql::server': } ->
142-
postgresql_psql { 'password embedded by environment: #{select}':
142+
postgresql_psql { 'password protected by sensitive: #{select}':
143143
db => 'postgres',
144144
psql_user => 'postgres',
145+
sensitive => true,
145146
command => '#{select}',
146-
environment => [
147-
'PASS_TO_EMBED=pa$swD',
148-
],
149-
}
150-
MANIFEST
151-
apply_manifest(pp, catch_failures: true)
152-
apply_manifest(pp, expect_changes: false)
153-
end
154-
it 'runs SQL that contanins password passed by environment in check' do
155-
select = "select 1 where \\'$PASS_TO_EMBED\\'=\\'passwD\\'"
156-
pp = <<-MANIFEST.unindent
157-
class { 'postgresql::server': } ->
158-
postgresql_psql { 'password embedded by environment in check: #{select}':
159-
db => 'postgres',
160-
psql_user => 'postgres',
161-
command => 'invalid sql query',
162-
unless => '#{select}',
163-
environment => [
164-
'PASS_TO_EMBED=passwD',
165-
],
166147
}
167148
MANIFEST
149+
result = apply_manifest(pp, catch_failures: true, debug: true)
150+
expect(result.stdout).not_to contain('pa$swD')
151+
expect(result.stderr).not_to contain('pa$swD')
168152

169-
idempotent_apply(pp)
153+
result = apply_manifest(pp, expect_changes: false, debug: true)
154+
expect(result.stdout).not_to contain('pa$swD')
155+
expect(result.stderr).not_to contain('pa$swD')
170156
end
171157
end
172158
end

0 commit comments

Comments
 (0)