Skip to content

Finish conversion of postgresql_password function #1161

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class { 'postgresql::server':

postgresql::server::db { 'mydatabasename':
user => 'mydatabaseuser',
password => postgresql_password('mydatabaseuser', 'mypassword'),
password => postgresql::postgresql_password('mydatabaseuser', 'mypassword'),
}
```

Expand All @@ -94,7 +94,7 @@ class { 'postgresql::server':
}

postgresql::server::role { 'marmot':
password_hash => postgresql_password('marmot', 'mypasswd'),
password_hash => postgresql::postgresql_password('marmot', 'mypasswd'),
}

postgresql::server::database_grant { 'test1':
Expand Down
97 changes: 79 additions & 18 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ _Private Classes_
* [`postgresql::postgresql_password`](#postgresqlpostgresql_password): This function returns the postgresql password hash from the clear text username / password
* [`postgresql_acls_to_resources_hash`](#postgresql_acls_to_resources_hash): This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources. It is not intended to be used outsid
* [`postgresql_escape`](#postgresql_escape): This function safely escapes a string using a consistent random tag
* [`postgresql_password`](#postgresql_password): This function returns the postgresql password hash from the clear text username / password
* [`postgresql_password`](#postgresql_password): DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.

**Tasks**

Expand Down Expand Up @@ -1097,6 +1097,14 @@ Sets PostgreSQL version

Default value: `undef`

##### `extra_systemd_config`

Data type: `Any`

Adds extra config to systemd config file, can for instance be used to add extra openfiles. This can be a multi line string

Default value: $postgresql::params::extra_systemd_config

##### `manage_selinux`

Data type: `Boolean`
Expand Down Expand Up @@ -1667,6 +1675,22 @@ Specifies whether to grant or revoke the privilege. Default is to grant the priv

Default value: 'present'

##### `group`

Data type: `String`

Sets the OS group to run psql

Default value: $postgresql::server::group

##### `psql_path`

Data type: `String`

Sets the path to psql command

Default value: $postgresql::server::psql_path

##### `object_arguments`

Data type: `Array[String[1],0]`
Expand Down Expand Up @@ -2181,6 +2205,38 @@ Specify whether to create or drop the role. Specifying 'present' creates the rol

Default value: 'present'

##### `psql_user`

Data type: `Any`

Sets the OS user to run psql

Default value: $postgresql::server::user

##### `psql_group`

Data type: `Any`

Sets the OS group to run psql

Default value: $postgresql::server::group

##### `psql_path`

Data type: `Any`

Sets path to psql command

Default value: $postgresql::server::psql_path

##### `module_workdir`

Data type: `Any`

Specifies working directory under which the psql command should be executed. May need to specify if '/tmp' is on volume mounted with noexec option.

Default value: $postgresql::server::module_workdir

### postgresql::server::schema

Create a new schema.
Expand Down Expand Up @@ -2815,26 +2871,25 @@ to get the full benefit of the modern function API.

Type: Ruby 4.x API

postgresql_password.rb
---- original file header ----
This function returns the postgresql password hash from the clear text username / password

@return Returns the postgresql password hash from the clear text username / password.
#### `postgresql::postgresql_password(Variant[String[1],Integer] $username, Variant[String[1],Integer] $password)`

#### `postgresql::postgresql_password(Any *$args)`
The postgresql::postgresql_password function.

postgresql_password.rb
---- original file header ----
Returns: `String` The postgresql password hash from the clear text username / password.

@return Returns the postgresql password hash from the clear text username / password.
##### `username`

Returns: `Data type` Describe what the function returns here
Data type: `Variant[String[1],Integer]`

##### `*args`
The clear text `username`

Data type: `Any`
##### `password`

The original array of arguments. Port this to individually managed params
to get the full benefit of the modern function API.
Data type: `Variant[String[1],Integer]`

The clear text `password`

### postgresql_acls_to_resources_hash

Expand Down Expand Up @@ -2884,15 +2939,21 @@ Returns: `Any` Safely escapes a string using $$ using a random tag which should

### postgresql_password

Type: Ruby 3.x API
Type: Ruby 4.x API

This function returns the postgresql password hash from the clear text username / password
DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.

#### `postgresql_password()`
#### `postgresql_password(Any *$args)`

The postgresql_password function.

Returns: `Any`

##### `*args`

Data type: `Any`

This function returns the postgresql password hash from the clear text username / password

Returns: `Any` Returns the postgresql password hash from the clear text username / password.

## Tasks

Expand Down
47 changes: 10 additions & 37 deletions lib/puppet/functions/postgresql/postgresql_password.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
# hash a string as mysql's "PASSWORD()" function would do it
require 'digest/md5'

# postgresql_password.rb
# ---- original file header ----
#
# @summary
# This function returns the postgresql password hash from the clear text username / password
# @return Returns the postgresql password hash from the clear text username / password.
#
#
# @summary This function returns the postgresql password hash from the clear text username / password
Puppet::Functions.create_function(:'postgresql::postgresql_password') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
# @param username
# The clear text `username`
# @param password
# The clear text `password`
#
# @return [String]
# The postgresql password hash from the clear text username / password.
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
param 'Variant[String[1],Integer]', :username
param 'Variant[String[1],Integer]', :password
end

def default_impl(*args)
if args.size != 2
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
"given (#{args.size} for 2)")
end

username = args[0]
password = args[1]

def default_impl(username, password)
'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)
end
end
10 changes: 10 additions & 0 deletions lib/puppet/functions/postgresql_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# @summary DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.
Puppet::Functions.create_function(:postgresql_password) do
dispatch :deprecation_gen do
repeated_param 'Any', :args
end
def deprecation_gen(*args)
call_function('deprecation', 'postgresql_password', 'This method is deprecated, please use postgresql::postgresql_password instead.')
call_function('postgresql::postgresql_password', *args)
end
end
22 changes: 0 additions & 22 deletions lib/puppet/parser/functions/postgresql_password.rb

This file was deleted.

5 changes: 5 additions & 0 deletions spec/functions/postgresql_password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe 'postgresql_password' do
it_behaves_like 'postgresql_password function'
end
37 changes: 1 addition & 36 deletions spec/functions/postgresql_postgresql_password_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
require 'spec_helper'

describe 'postgresql::postgresql_password' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################
it_behaves_like 'postgresql_password function'
end
14 changes: 14 additions & 0 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@
def param(type, title, param)
param_value(catalogue, type, title, param)
end

shared_examples 'postgresql_password function' do
it { is_expected.not_to eq(nil) }

it {
is_expected.to run.with_params('foo', 'bar').and_return('md596948aad3fcae80c08a35c9b5958cd89')
}
it {
is_expected.to run.with_params('foo', 1234).and_return('md539a0e1b308278a8de5e007cd1f795920')
}
it 'raises an error if there is only 1 argument' do
is_expected.to run.with_params('foo').and_raise_error(StandardError)
end
end
16 changes: 0 additions & 16 deletions spec/unit/functions/postgresql_password_spec.rb

This file was deleted.