Skip to content

Commit 15b7446

Browse files
committed
Resolve credential leak via ps while jenkins-cli is used during puppet runs
1 parent bebdaf6 commit 15b7446

File tree

7 files changed

+103
-17
lines changed

7 files changed

+103
-17
lines changed

manifests/cli.pp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
' '
6565
)
6666

67+
if !empty($jenkins::cli_password) {
68+
$cmd_environment = [
69+
"JENKINS_USER_ID=${jenkins::cli_username}",
70+
"JENKINS_API_TOKEN=${jenkins::cli_password}",
71+
]
72+
} else {
73+
$cmd_environment = undef
74+
}
75+
6776
# Do a safe restart of Jenkins (only when notified)
6877
exec { 'safe-restart-jenkins':
6978
command => "${cmd} safe-restart && /bin/sleep 10",
@@ -72,6 +81,7 @@
7281
try_sleep => $cli_try_sleep,
7382
refreshonly => true,
7483
require => File[$jar],
84+
environment => $cmd_environment,
7585
}
7686

7787
# jenkins::cli::reload should be included only after $::jenkins::cli::cmd is

manifests/cli/exec.pp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828
)
2929

3030
if $unless {
31-
$environment_run = [ "HELPER_CMD=eval ${jenkins::cli_helper::helper_cmd}" ]
31+
$environment_run = delete_undef_values(
32+
flatten([
33+
$jenkins::cli::cmd_environment,
34+
"HELPER_CMD=eval ${jenkins::cli_helper::helper_cmd}",
35+
])
36+
)
3237
} else {
33-
$environment_run = undef
38+
$environment_run = $jenkins::cli::cmd_environment
3439
}
3540

3641
exec { $title:

manifests/cli/reload.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
try_sleep => $cli_try_sleep,
1818
refreshonly => true,
1919
require => File[$jar_file],
20+
environment => $jenkins::cli::cmd_environment,
2021
}
2122
}

manifests/init.pp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@
401401
# Username / Password auth (needed for AD and other Auth Realms)
402402
if $_use_new_cli {
403403
if !empty($cli_password) {
404-
$_cli_auth_arg = "-auth '${cli_username}:${cli_password}'"
404+
# username and password passed as environment variables to prevent showing in ps output
405+
# so setting cli_auth_arg to empty string
406+
$_cli_auth_arg = ''
405407
} elsif !empty($cli_password_file) {
406408
$_cli_auth_arg = "-auth '@${cli_password_file}'"
407409
} else {

manifests/job/absent.pp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333

3434
# Delete the job
3535
exec { "jenkins delete-job ${jobname}":
36-
path => ['/usr/bin', '/usr/sbin', '/bin'],
37-
command => "${jenkins::cli::cmd} delete-job \"${jobname}\"",
38-
logoutput => false,
39-
onlyif => "test -f \"${config_path}\"",
40-
require => Exec['jenkins-cli'],
36+
path => ['/usr/bin', '/usr/sbin', '/bin'],
37+
command => "${jenkins::cli::cmd} delete-job \"${jobname}\"",
38+
logoutput => false,
39+
onlyif => "test -f \"${config_path}\"",
40+
require => Exec['jenkins-cli'],
41+
environment => $jenkins::cli::cmd_environment,
4142
}
4243

4344
}

manifests/job/present.pp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
$job_dir = "${jenkins::job_dir}/${jobname}"
6666
$config_path = "${job_dir}/config.xml"
6767

68-
# Bring variables from Class['::jenkins'] into local scope.
68+
# Bring variables from Class['jenkins'] into local scope.
6969
$cli_tries = $jenkins::cli_tries
70-
$cli_try_sleep = $jenkins::cli_try_sleep
70+
$cli_try_sleep = $jenkins::cli_try_sleep
7171

7272
Exec {
7373
logoutput => false,
@@ -80,18 +80,20 @@
8080
$cat_config = "cat \"${tmp_config_path}\""
8181
$create_job = "${jenkins_cli} create-job \"${jobname}\""
8282
exec { "jenkins create-job ${jobname}":
83-
command => "${cat_config} | ${create_job}",
84-
creates => [$config_path, "${job_dir}/builds"],
83+
command => "${cat_config} | ${create_job}",
84+
creates => [$config_path, "${job_dir}/builds"],
85+
environment => $jenkins::cli::cmd_environment,
8586
}
8687

8788
if $replace {
8889
# Use Jenkins CLI to update the job if it already exists
8990
$update_job = "${jenkins_cli} update-job ${jobname}"
9091
exec { "jenkins update-job ${jobname}":
91-
command => "${cat_config} | ${update_job}",
92-
onlyif => "test -e ${config_path}",
93-
unless => "${difftool} ${config_path} ${tmp_config_path}",
94-
notify => Exec['reload-jenkins'],
92+
command => "${cat_config} | ${update_job}",
93+
onlyif => "test -e ${config_path}",
94+
unless => "${difftool} ${config_path} ${tmp_config_path}",
95+
notify => Exec['reload-jenkins'],
96+
environment => $jenkins::cli::cmd_environment,
9597
}
9698
}
9799

spec/classes/jenkins_cli_spec.rb

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
it { is_expected.to contain_exec('reload-jenkins').with_command(%r{http://localhost:9000}) }
2626
it { is_expected.to contain_exec('reload-jenkins').with_command(%r{-i\s'/path/to/key'}) }
2727
it { is_expected.to contain_exec('reload-jenkins').that_requires('File[/path/to/libdir/jenkins-cli.jar]') }
28-
it { is_expected.to contain_exec('safe-restart-jenkins') }
28+
it { is_expected.to contain_exec('safe-restart-jenkins').with('environment' => nil) }
2929
it { is_expected.to contain_jenkins__sysconfig('HTTP_PORT').with_value('9000') }
3030

31+
<<<<<<< HEAD
3132
describe 'jenkins::cli' do
3233
describe 'relationships' do
3334
it do
@@ -39,6 +40,70 @@
3940
that_comes_before('Anchor[jenkins::end]')
4041
end
4142
end
43+
=======
44+
context '$cli => true' do
45+
let(:params) {{ :cli => true,
46+
:cli_ssh_keyfile => '/path/to/key',
47+
:config_hash => { 'HTTP_PORT' => { 'value' => '9000' } }}
48+
}
49+
it { should contain_class('jenkins::cli') }
50+
it { should contain_exec('jenkins-cli') }
51+
it { should contain_exec('reload-jenkins').with_command(/http:\/\/localhost:9000/) }
52+
it { should contain_exec('reload-jenkins').with_command(/-i\s'\/path\/to\/key'/) }
53+
it { should contain_exec('safe-restart-jenkins') }
54+
it { should contain_exec('safe-restart-jenkins').with('environment' => nil) }
55+
it { should contain_jenkins__sysconfig('HTTP_PORT').with_value('9000') }
56+
57+
describe 'jenkins::cli' do
58+
describe 'relationships' do
59+
it do
60+
should contain_class('jenkins::cli').
61+
that_requires('Class[jenkins::service]')
62+
>>>>>>> 14d095a... CIP-389 Resolve credential leak via ps with cli
63+
end
64+
65+
context '$cli_password is defined' do
66+
let(:params) do
67+
{
68+
version: '2.54',
69+
libdir: '/path/to/libdir',
70+
cli: true,
71+
cli_remoting_free: true,
72+
cli_username: 'user01',
73+
cli_password: 'password01'
74+
}
75+
end
76+
77+
it do
78+
is_expected.to contain_exec('safe-restart-jenkins').with(
79+
'environment' => [
80+
'JENKINS_USER_ID=user01',
81+
'JENKINS_API_TOKEN=password01'
82+
]
83+
)
84+
end
85+
end
86+
87+
context '$cli_password is defined' do
88+
let(:params) do
89+
{
90+
version: '2.54',
91+
libdir: '/path/to/libdir',
92+
cli: true,
93+
cli_remoting_free: true,
94+
cli_username: 'user01',
95+
cli_password: 'password01'
96+
}
97+
end
98+
99+
it do
100+
is_expected.to contain_exec('safe-restart-jenkins').with(
101+
'environment' => [
102+
'JENKINS_USER_ID=user01',
103+
'JENKINS_API_TOKEN=password01'
104+
]
105+
)
106+
end
42107
end
43108
end
44109

0 commit comments

Comments
 (0)