Skip to content

Commit c27ef55

Browse files
Filipovici-Andreiaustb
authored andcommitted
(maint) Make sure the ssl rules for the read user are created only when it's needed.
1 parent e15fe50 commit c27ef55

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

manifests/database/postgresql.pp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
$postgresql_ssl_cert_path = $puppetdb::params::postgresql_ssl_cert_path,
1717
$postgresql_ssl_ca_cert_path = $puppetdb::params::postgresql_ssl_ca_cert_path,
1818
$read_database_username = $puppetdb::params::read_database_username,
19-
$read_database_password = $puppetdb::params::read_database_password
19+
$read_database_password = $puppetdb::params::read_database_password,
20+
$read_database_host = $puppetdb::params::read_database_host
2021
) inherits puppetdb::params {
2122

2223
if $manage_server {
@@ -31,6 +32,15 @@
3132
port => scanf($database_port, '%i')[0],
3233
}
3334

35+
# We need to create the ssl connection for the read user, when
36+
# manage_database is set to true, or when read_database_host is defined.
37+
# Otherwise we don't create it.
38+
if $manage_database or $read_database_host != undef{
39+
$create_read_user_rule = true
40+
} else {
41+
$create_read_user_rule = false
42+
}
43+
3444
# configure PostgreSQL communication with Puppet Agent SSL certificates if
3545
# postgresql_ssl_on is set to true
3646
if $postgresql_ssl_on {
@@ -41,7 +51,8 @@
4151
puppetdb_server => $puppetdb_server,
4252
postgresql_ssl_key_path => $postgresql_ssl_key_path,
4353
postgresql_ssl_cert_path => $postgresql_ssl_cert_path,
44-
postgresql_ssl_ca_cert_path => $postgresql_ssl_ca_cert_path
54+
postgresql_ssl_ca_cert_path => $postgresql_ssl_ca_cert_path,
55+
create_read_user_rule => $create_read_user_rule
4556
}
4657
}
4758

manifests/database/ssl_configuration.pp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
$database_name = $puppetdb::params::database_name,
55
$database_username = $puppetdb::params::database_username,
66
$read_database_username = $puppetdb::params::read_database_username,
7+
$read_database_host = $puppetdb::params::read_database_host,
78
$puppetdb_server = $puppetdb::params::puppetdb_server,
89
$postgresql_ssl_key_path = $puppetdb::params::postgresql_ssl_key_path,
910
$postgresql_ssl_cert_path = $puppetdb::params::postgresql_ssl_cert_path,
10-
$postgresql_ssl_ca_cert_path = $puppetdb::params::postgresql_ssl_ca_cert_path
11+
$postgresql_ssl_ca_cert_path = $puppetdb::params::postgresql_ssl_ca_cert_path,
12+
$create_read_user_rule = false,
1113
) inherits puppetdb::params {
1214
File {
1315
ensure => present,
@@ -56,9 +58,11 @@
5658
puppetdb_server => $puppetdb_server,
5759
}
5860

59-
puppetdb::database::postgresql_ssl_rules { "Configure postgresql ssl rules for ${read_database_username}":
60-
database_name => $database_name,
61-
database_username => $read_database_username,
62-
puppetdb_server => $puppetdb_server,
61+
if $create_read_user_rule {
62+
puppetdb::database::postgresql_ssl_rules { "Configure postgresql ssl rules for ${read_database_username}":
63+
database_name => $database_name,
64+
database_username => $read_database_username,
65+
puppetdb_server => $puppetdb_server,
66+
}
6367
}
6468
}

manifests/init.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
postgresql_ssl_ca_cert_path => $postgresql_ssl_ca_cert_path,
201201
read_database_username => $read_database_username,
202202
read_database_password => $read_database_password,
203+
read_database_host => $read_database_host,
203204
before => $database_before
204205
}
205206
}

spec/unit/classes/database/ssl_configuration_spec.rb

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,8 @@
103103
.with_auth_option("map=#{identity_map} clientcert=1")
104104
end
105105

106-
it 'has hba rule for puppetdb-read user ipv4' do
107-
is_expected.to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv4)")
108-
.with_type('hostssl')
109-
.with_database(params[:database_name])
110-
.with_user(params[:read_database_username])
111-
.with_address('0.0.0.0/0')
112-
.with_auth_method('cert')
113-
.with_order(0)
114-
.with_auth_option("map=#{read_identity_map} clientcert=1")
106+
it 'does not create hba rule for puppetdb-read user ipv4' do
107+
is_expected.not_to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv4)")
115108
end
116109

117110
it 'has hba rule for puppetdb user ipv6' do
@@ -125,15 +118,8 @@
125118
.with_auth_option("map=#{identity_map} clientcert=1")
126119
end
127120

128-
it 'has hba rule for puppetdb-read user ipv6' do
129-
is_expected.to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv6)")
130-
.with_type('hostssl')
131-
.with_database(params[:database_name])
132-
.with_user(params[:read_database_username])
133-
.with_address('::0/0')
134-
.with_auth_method('cert')
135-
.with_order(0)
136-
.with_auth_option("map=#{read_identity_map} clientcert=1")
121+
it 'does not create hba rule for puppetdb-read user ipv6' do
122+
is_expected.not_to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv6)")
137123
end
138124

139125
it 'has ident rule' do
@@ -143,11 +129,8 @@
143129
.with_database_username(params[:database_name])
144130
end
145131

146-
it 'has read ident rule' do
147-
is_expected.to contain_postgresql__server__pg_ident_rule("Map the SSL certificate of the server as a #{params[:read_database_username]} user")
148-
.with_map_name(read_identity_map)
149-
.with_system_username(facts[:fqdn])
150-
.with_database_username(params[:read_database_username])
132+
it 'does not create read ident rule' do
133+
is_expected.not_to contain_postgresql__server__pg_ident_rule("Map the SSL certificate of the server as a #{params[:read_database_username]} user")
151134
end
152135

153136
context 'when the puppetdb_server is set' do
@@ -166,5 +149,44 @@
166149
.with_database_username(params[:database_name])
167150
end
168151
end
152+
153+
context 'when the create_read_user_rule is set to true' do
154+
let(:params) do
155+
{
156+
database_name: 'puppetdb',
157+
read_database_username: 'puppetdb-read',
158+
create_read_user_rule: true,
159+
}
160+
end
161+
162+
it 'has hba rule for puppetdb-read user ipv4' do
163+
is_expected.to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv4)")
164+
.with_type('hostssl')
165+
.with_database(params[:database_name])
166+
.with_user(params[:read_database_username])
167+
.with_address('0.0.0.0/0')
168+
.with_auth_method('cert')
169+
.with_order(0)
170+
.with_auth_option("map=#{read_identity_map} clientcert=1")
171+
end
172+
173+
it 'has hba rule for puppetdb-read user ipv6' do
174+
is_expected.to contain_postgresql__server__pg_hba_rule("Allow certificate mapped connections to #{params[:database_name]} as #{params[:read_database_username]} (ipv6)")
175+
.with_type('hostssl')
176+
.with_database(params[:database_name])
177+
.with_user(params[:read_database_username])
178+
.with_address('::0/0')
179+
.with_auth_method('cert')
180+
.with_order(0)
181+
.with_auth_option("map=#{read_identity_map} clientcert=1")
182+
end
183+
184+
it 'has read ident rule' do
185+
is_expected.to contain_postgresql__server__pg_ident_rule("Map the SSL certificate of the server as a #{params[:read_database_username]} user")
186+
.with_map_name(read_identity_map)
187+
.with_system_username(facts[:fqdn])
188+
.with_database_username(params[:read_database_username])
189+
end
190+
end
169191
end
170192
end

0 commit comments

Comments
 (0)