-
Notifications
You must be signed in to change notification settings - Fork 614
Add multi instance support, refactoring password.pp (5/x) #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chelnak
merged 1 commit into
puppetlabs:main
from
SimonHoenscheid:shoenscheid_postgresql_instances_passwd_class_to_define
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# lint:ignore:140chars | ||
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system. | ||
# @param group Overrides the default postgres user group to be used for related files in the file system. | ||
# Default value: 5432. Meaning the Postgres server listens on TCP port 5432. | ||
# @param psql_path Specifies the path to the psql command. | ||
# @param port Specifies the port for the PostgreSQL server to listen on. Note: The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change. | ||
# @param database Specifies the name of the database to connect with. On most systems this is 'postgres'. | ||
# @param module_workdir Working directory for the PostgreSQL module | ||
# @param postgres_password Sets the password for the postgres user to your specified value. By default, this setting uses the superuser account in the Postgres database, with a user called postgres and no password. | ||
# lint:endignore:140chars | ||
define postgresql::server::instance_passwd ( | ||
$user = $postgresql::server::user, | ||
$group = $postgresql::server::group, | ||
$psql_path = $postgresql::server::psql_path, | ||
$port = $postgresql::server::port, | ||
$database = $postgresql::server::default_database, | ||
$module_workdir = $postgresql::server::module_workdir, | ||
$postgres_password = $postgresql::server::postgres_password, | ||
|
||
) { | ||
$real_postgres_password = if $postgres_password =~ Sensitive { | ||
$postgres_password.unwrap | ||
} else { | ||
$postgres_password | ||
} | ||
|
||
# psql will default to connecting as $user if you don't specify name | ||
$_datbase_user_same = $database == $user | ||
$_dboption = $_datbase_user_same ? { | ||
false => " --dbname ${shell_escape($database)}", | ||
default => '' | ||
} | ||
|
||
if $real_postgres_password { | ||
# NOTE: this password-setting logic relies on the pg_hba.conf being | ||
# configured to allow the postgres system user to connect via psql | ||
# without specifying a password ('ident' or 'trust' security). This is | ||
# the default for pg_hba.conf. | ||
$escaped = postgresql::postgresql_escape($real_postgres_password) | ||
$exec_command = "${shell_escape($psql_path)}${_dboption} -c \"ALTER ROLE \\\"${shell_escape($user)}\\\" PASSWORD \${NEWPASSWD_ESCAPED}\"" # lint:ignore:140chars | ||
exec { 'set_postgres_postgrespw': | ||
# This command works w/no password because we run it as postgres system | ||
# user | ||
command => $exec_command, | ||
user => $user, | ||
group => $group, | ||
logoutput => true, | ||
cwd => $module_workdir, | ||
environment => [ | ||
"PGPASSWORD=${real_postgres_password}", | ||
"PGPORT=${port}", | ||
"NEWPASSWD_ESCAPED=${escaped}", | ||
], | ||
# With this command we're passing -h to force TCP authentication, which | ||
# does require a password. We specify the password via the PGPASSWORD | ||
# environment variable. If the password is correct (current), this | ||
# command will exit with an exit code of 0, which will prevent the main | ||
# command from running. | ||
unless => "${psql_path} -h localhost -p ${port} -c 'select 1' > /dev/null", | ||
path => '/usr/bin:/usr/local/bin:/bin', | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,12 @@ | ||
# @api private | ||
class postgresql::server::passwd { | ||
$postgres_password = if $postgresql::server::postgres_password =~ Sensitive { | ||
$postgresql::server::postgres_password.unwrap | ||
} else { | ||
$postgresql::server::postgres_password | ||
} | ||
|
||
$user = $postgresql::server::user | ||
$group = $postgresql::server::group | ||
$psql_path = $postgresql::server::psql_path | ||
$port = $postgresql::server::port | ||
$database = $postgresql::server::default_database | ||
$module_workdir = $postgresql::server::module_workdir | ||
|
||
# psql will default to connecting as $user if you don't specify name | ||
$_datbase_user_same = $database == $user | ||
$_dboption = $_datbase_user_same ? { | ||
false => " --dbname ${shell_escape($database)}", | ||
default => '' | ||
} | ||
|
||
if $postgres_password { | ||
# NOTE: this password-setting logic relies on the pg_hba.conf being | ||
# configured to allow the postgres system user to connect via psql | ||
# without specifying a password ('ident' or 'trust' security). This is | ||
# the default for pg_hba.conf. | ||
$escaped = postgresql::postgresql_escape($postgres_password) | ||
$exec_command = "${shell_escape($psql_path)}${_dboption} -c \"ALTER ROLE \\\"${shell_escape($user)}\\\" PASSWORD \${NEWPASSWD_ESCAPED}\"" | ||
exec { 'set_postgres_postgrespw': | ||
# This command works w/no password because we run it as postgres system | ||
# user | ||
command => $exec_command, | ||
user => $user, | ||
group => $group, | ||
logoutput => true, | ||
cwd => $module_workdir, | ||
environment => [ | ||
"PGPASSWORD=${postgres_password}", | ||
"PGPORT=${port}", | ||
"NEWPASSWD_ESCAPED=${escaped}", | ||
], | ||
# With this command we're passing -h to force TCP authentication, which | ||
# does require a password. We specify the password via the PGPASSWORD | ||
# environment variable. If the password is correct (current), this | ||
# command will exit with an exit code of 0, which will prevent the main | ||
# command from running. | ||
unless => "${psql_path} -h localhost -p ${port} -c 'select 1' > /dev/null", | ||
path => '/usr/bin:/usr/local/bin:/bin', | ||
} | ||
postgresql::server::instance_passwd { 'main': | ||
user => $postgresql::server::user, | ||
group => $postgresql::server::group, | ||
psql_path => $postgresql::server::psql_path, | ||
port => $postgresql::server::port, | ||
database => $postgresql::server::default_database, | ||
module_workdir => $postgresql::server::module_workdir, | ||
postgres_password => $postgresql::server::postgres_password, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'postgresql::server::instance_passwd' do | ||
let(:title) { 'main' } | ||
|
||
on_supported_os.each do |os, os_facts| | ||
context "on #{os}" do | ||
let :facts do | ||
os_facts | ||
end | ||
|
||
let :pre_condition do | ||
"class {'postgresql::server':}" | ||
end | ||
|
||
context 'with defaults from passwd class' do | ||
it { is_expected.to compile.with_all_deps } | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the indend looks a bit odd here?