Skip to content

Commit

Permalink
Support Postgres DSN that contains no password
Browse files Browse the repository at this point in the history
Allow the Postgres connection DNS to contain no password which can be needed in situations like with Cloud SQL.
  • Loading branch information
treydock committed Mar 16, 2023
1 parent ec03956 commit e98fbc6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ class { 'sensu::backend':
}
```

**NOTE** Set `postgresql_password` to `false` if you want the DSN to only contain a username.

### Installing Plugins

Plugin management is handled by the `sensu::plugins` class.
Expand Down
2 changes: 1 addition & 1 deletion manifests/backend.pp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
Boolean $manage_postgresql_db = true,
String $postgresql_name = 'postgresql',
String $postgresql_user = 'sensu',
String $postgresql_password = 'changeme',
Variant[String, Boolean] $postgresql_password = 'changeme',
Stdlib::Host $postgresql_host = 'localhost',
Stdlib::Port $postgresql_port = 5432,
String $postgresql_dbname = 'sensu',
Expand Down
14 changes: 12 additions & 2 deletions manifests/backend/datastore/postgresql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

$user = $sensu::backend::postgresql_user
$password = $sensu::backend::postgresql_password
if ! $password {
$password_dsn = '' # lint:ignore:empty_string_assignment
} else {
$password_dsn = ":${password}"
}
$host = $sensu::backend::postgresql_host
$port = $sensu::backend::postgresql_port
$dbname = $sensu::backend::postgresql_dbname
$sslmode = $sensu::backend::postgresql_sslmode
$dsn = "postgresql://${user}:${password}@${host}:${port}/${dbname}?sslmode=${sslmode}"
$dsn = "postgresql://${user}${password_dsn}@${host}:${port}/${dbname}?sslmode=${sslmode}"

sensu_postgres_config { $sensu::backend::postgresql_name:
ensure => $sensu::backend::datastore_ensure,
Expand All @@ -25,9 +30,14 @@
}

if $sensu::backend::manage_postgresql_db and $sensu::backend::datastore_ensure == 'present' {
if ! $password {
$db_password = undef
} else {
$db_password = postgresql::postgresql_password($user, $password)
}
postgresql::server::db { $dbname:
user => $user,
password => postgresql::postgresql_password($user, $password),
password => $db_password,
before => Sensu_postgres_config[$sensu::backend::postgresql_name],
}
}
Expand Down
20 changes: 19 additions & 1 deletion spec/classes/backend_datastore_postgresql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,25 @@ class { 'sensu::backend': }
it { should_not contain_file('sensu-backend postgresql_crl') }
it { should_not contain_file('sensu-backend postgresql_cert') }
it { should_not contain_file('sensu-backend postgresql_key') }


context 'with an empty password' do
let(:pre_condition) do
<<-EOS
class { '::postgresql::globals': version => '11' }
class { '::postgresql::server': }
class { 'sensu::backend': postgresql_password => false }
EOS
end

it do
should contain_sensu_postgres_config('postgresql').with({
:ensure => 'present',
:dsn => sensitive('postgresql://sensu@localhost:5432/sensu?sslmode=require'),
:pool_size => '20',
})
end
end

context 'sslmode defined' do
let(:pre_condition) do
<<-EOS
Expand Down

0 comments on commit e98fbc6

Please sign in to comment.