Skip to content

Commit 447bf08

Browse files
committed
Pass potentially Sensitive params as Sensitive
In 699f944 the parameters started to accept Sensitive but it didn't default to Sensitive. They also weren't converting data coming from Hiera. This adds a data-in-modules setup and sets lookup_options for those. This means Kafo (which heavily relies on Hiera) will pass sensitive values. The commit also missed settings.yml.erb and database.yml.erb which now also unwraps if needed. Ideally this would be converted to EPP instead but this fixes it in the short term. However, hammer_root.yml.epp somehow doesn't properly render the sensitive data. That's why it's unwrapper for now, just to make it work. A test case is added to prevent future regressions. It also changes the data type to accept Sensitive[Undef] which is needed if Hiera unconditionally converts the value to Sensitive. Fixes: 699f944
1 parent 699f944 commit 447bf08

File tree

9 files changed

+42
-12
lines changed

9 files changed

+42
-12
lines changed

data/common.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
lookup_options:
2+
'^foreman::(db|email_smtp|initial_admin)_password$':
3+
convert_to: "Sensitive"
4+
'^foreman::oauth_consumer_(key|secret)$':
5+
convert_to: "Sensitive"
6+
foreman::cli::password:
7+
convert_to: "Sensitive"
8+
foreman::plugin::supervisory_authority::secret_token:
9+
convert_to: "Sensitive"

hiera.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
version: 5
3+
4+
defaults: # Used for any hierarchy level that omits these keys.
5+
datadir: data # This path is relative to hiera.yaml's directory.
6+
data_hash: yaml_data # Use the built-in YAML backend.
7+
8+
hierarchy:
9+
- name: "common"
10+
path: "common.yaml"

manifests/cli.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
String $version = $foreman::cli::params::version,
3131
Boolean $manage_root_config = $foreman::cli::params::manage_root_config,
3232
Optional[String] $username = $foreman::cli::params::username,
33-
Optional[Variant[String, Sensitive[String]]] $password = $foreman::cli::params::password,
33+
Variant[Optional[String], Sensitive[Optional[String]]] $password = $foreman::cli::params::password,
3434
Boolean $use_sessions = $foreman::cli::params::use_sessions,
3535
Boolean $refresh_cache = $foreman::cli::params::refresh_cache,
3636
Integer[-1] $request_timeout = $foreman::cli::params::request_timeout,
@@ -93,7 +93,7 @@
9393
'foreman/hammer_root.yml.epp',
9494
{
9595
username => $username_real,
96-
password => $password_real,
96+
password => if $password_real =~ Sensitive { $password_real.unwrap } else { $password },
9797
}
9898
),
9999
}

manifests/init.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
Variant[Undef, Enum['UNSET'], Stdlib::Port] $db_port = 'UNSET',
216216
Optional[String] $db_database = 'UNSET',
217217
Optional[String] $db_username = $foreman::params::db_username,
218-
Optional[Variant[String, Sensitive[String]]] $db_password = $foreman::params::db_password,
218+
Variant[Optional[String], Sensitive[Optional[String]]] $db_password = $foreman::params::db_password,
219219
Optional[String] $db_sslmode = 'UNSET',
220220
Optional[String] $db_root_cert = undef,
221221
Integer[0] $db_pool = $foreman::params::db_pool,
@@ -265,7 +265,7 @@
265265
Optional[Stdlib::Fqdn] $email_smtp_domain = $foreman::params::email_smtp_domain,
266266
Enum['none', 'plain', 'login', 'cram-md5'] $email_smtp_authentication = $foreman::params::email_smtp_authentication,
267267
Optional[String] $email_smtp_user_name = $foreman::params::email_smtp_user_name,
268-
Optional[Variant[String, Sensitive[String]]] $email_smtp_password = $foreman::params::email_smtp_password,
268+
Variant[Optional[String], Sensitive[Optional[String]]] $email_smtp_password = $foreman::params::email_smtp_password,
269269
Optional[String] $email_reply_address = $foreman::params::email_reply_address,
270270
Optional[String] $email_subject_prefix = $foreman::params::email_subject_prefix,
271271
String $telemetry_prefix = $foreman::params::telemetry_prefix,

manifests/params.pp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
$db_username = 'foreman'
3333
# Generate and cache the password on the master once
3434
# In multi-puppetmaster setups, the user should specify their own
35-
$db_password = extlib::cache_data('foreman_cache_data', 'db_password', extlib::random_password(32))
35+
$db_password = Sensitive(extlib::cache_data('foreman_cache_data', 'db_password', extlib::random_password(32)))
3636
# Default database connection pool
3737
$db_pool = 5
3838
# if enabled, will run rake jobs, which depend on the database
@@ -147,13 +147,13 @@
147147
# We need the REST API interface with OAuth for some REST Puppet providers
148148
$oauth_active = true
149149
$oauth_map_users = false
150-
$oauth_consumer_key = extlib::cache_data('foreman_cache_data', 'oauth_consumer_key', extlib::random_password(32))
151-
$oauth_consumer_secret = extlib::cache_data('foreman_cache_data', 'oauth_consumer_secret', extlib::random_password(32))
150+
$oauth_consumer_key = Sensitive(extlib::cache_data('foreman_cache_data', 'oauth_consumer_key', extlib::random_password(32)))
151+
$oauth_consumer_secret = Sensitive(extlib::cache_data('foreman_cache_data', 'oauth_consumer_secret', extlib::random_password(32)))
152152
$oauth_effective_user = 'admin'
153153

154154
# Initial admin account details
155155
$initial_admin_username = 'admin'
156-
$initial_admin_password = extlib::cache_data('foreman_cache_data', 'admin_password', extlib::random_password(16))
156+
$initial_admin_password = Sensitive(extlib::cache_data('foreman_cache_data', 'admin_password', extlib::random_password(16)))
157157
$initial_admin_first_name = undef
158158
$initial_admin_last_name = undef
159159
$initial_admin_email = undef

spec/classes/foreman_cli_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
CONFIG
5959
)
6060
end
61+
62+
describe 'using Sensitive' do
63+
let(:params) { super().merge(password: sensitive('secret')) }
64+
65+
it 'should contain settings' do
66+
is_expected.to contain_file('/root/.hammer/cli.modules.d/foreman.yml')
67+
.with_content(/:password: 'secret'/)
68+
end
69+
end
6170
end
6271

6372
describe 'with manage_root_config=false' do

templates/database.yml.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
username: <%= username %>
2929
<% end -%>
3030
<% unless (password = scope.lookupvar("::foreman::db_password")) == 'UNSET' -%>
31-
password: "<%= password %>"
31+
password: "<%= password.respond_to?(:unwrap) ? password.unwrap : password %>"
3232
<% end -%>
3333
pool: <%= @db_pool %>

templates/hammer_root.yml.epp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%- |
22
Optional[String] $username,
3-
Optional[Variant[String, Sensitive[String]]] $password,
3+
Variant[Optional[String], Sensitive[Optional[String]]] $password,
44
| -%>
55
:foreman:
66
# Credentials. You'll be asked for the interactively if you leave them blank here

templates/settings.yaml.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# The following values are used for providing default settings during db migrate
1313
:oauth_active: <%= scope.lookupvar("foreman::oauth_active") %>
1414
:oauth_map_users: <%= scope.lookupvar("foreman::oauth_map_users") %>
15-
:oauth_consumer_key: <%= scope.lookupvar("foreman::oauth_consumer_key") %>
16-
:oauth_consumer_secret: <%= scope.lookupvar("foreman::oauth_consumer_secret") %>
15+
<% oauth_key = scope.lookupvar("foreman::oauth_consumer_key") -%>
16+
:oauth_consumer_key: <%= oauth_key.respond_to?(:unwrap) ? oauth_key.unwrap : oauth_key %>
17+
<% oauth_secret = scope.lookupvar("foreman::oauth_consumer_secret") -%>
18+
:oauth_consumer_secret: <%= oauth_secret.respond_to?(:unwrap) ? oauth_secret.unwrap : oauth_secret %>
1719

1820
# Websockets
1921
:websockets_encrypt: <%= scope.lookupvar("foreman::websockets_encrypt") %>

0 commit comments

Comments
 (0)