Skip to content
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

Resolve credential leak via ps while jenkins-cli is used during puppe… #946

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions manifests/cli.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
}

$jar = "${jenkins::libdir}/jenkins-cli.jar"
$extract_jar = "jar -xf ${jenkins::libdir}/jenkins.war WEB-INF/jenkins-cli.jar"
$move_jar = "mv WEB-INF/jenkins-cli.jar ${jar}"
$download_jar = "wget http://localhost:${jenkins::port}/jnlpJars/jenkins-cli.jar -O ${jar}.downloading"
$move_jar = "mv ${jar}.downloading ${jar}"
$remove_dir = 'rm -rf WEB-INF'
$cli_tries = $jenkins::cli_tries
$cli_try_sleep = $jenkins::cli_try_sleep
Expand All @@ -36,7 +36,7 @@
creates => $jar,
}
~> exec { 'jenkins-cli' :
command => "${extract_jar} && ${move_jar} && ${remove_dir}",
command => "${download_jar} && ${move_jar}",
path => ['/bin', '/usr/bin'],
cwd => '/tmp',
refreshonly => true,
Expand Down Expand Up @@ -64,6 +64,15 @@
' '
)

if !empty($jenkins::cli_password) {
$cmd_environment = [
"JENKINS_USER_ID=${jenkins::cli_username}",
"JENKINS_API_TOKEN=${jenkins::cli_password}",
]
} else {
$cmd_environment = undef
}

# Do a safe restart of Jenkins (only when notified)
exec { 'safe-restart-jenkins':
command => "${cmd} safe-restart && /bin/sleep 10",
Expand All @@ -72,6 +81,7 @@
try_sleep => $cli_try_sleep,
refreshonly => true,
require => File[$jar],
environment => $cmd_environment,
}

# jenkins::cli::reload should be included only after $::jenkins::cli::cmd is
Expand Down
9 changes: 7 additions & 2 deletions manifests/cli/exec.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
)

if $unless {
$environment_run = [ "HELPER_CMD=eval ${jenkins::cli_helper::helper_cmd}" ]
$environment_run = delete_undef_values(
flatten([
$jenkins::cli::cmd_environment,
"HELPER_CMD=eval ${jenkins::cli_helper::helper_cmd}",
])
)
} else {
$environment_run = undef
$environment_run = $jenkins::cli::cmd_environment
}

exec { $title:
Expand Down
1 change: 1 addition & 0 deletions manifests/cli/reload.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
try_sleep => $cli_try_sleep,
refreshonly => true,
require => File[$jar_file],
environment => $jenkins::cli::cmd_environment,
}
}
4 changes: 3 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@
# Username / Password auth (needed for AD and other Auth Realms)
if $_use_new_cli {
if !empty($cli_password) {
$_cli_auth_arg = "-auth '${cli_username}:${cli_password}'"
# username and password passed as environment variables to prevent showing in ps output
# so setting cli_auth_arg to empty string
$_cli_auth_arg = ''
} elsif !empty($cli_password_file) {
$_cli_auth_arg = "-auth '@${cli_password_file}'"
} else {
Expand Down
11 changes: 6 additions & 5 deletions manifests/job/absent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

# Delete the job
exec { "jenkins delete-job ${jobname}":
path => ['/usr/bin', '/usr/sbin', '/bin'],
command => "${jenkins::cli::cmd} delete-job \"${jobname}\"",
logoutput => false,
onlyif => "test -f \"${config_path}\"",
require => Exec['jenkins-cli'],
path => ['/usr/bin', '/usr/sbin', '/bin'],
command => "${jenkins::cli::cmd} delete-job \"${jobname}\"",
logoutput => false,
onlyif => "test -f \"${config_path}\"",
require => Exec['jenkins-cli'],
environment => $jenkins::cli::cmd_environment,
}

}
18 changes: 10 additions & 8 deletions manifests/job/present.pp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
$job_dir = "${jenkins::job_dir}/${jobname}"
$config_path = "${job_dir}/config.xml"

# Bring variables from Class['::jenkins'] into local scope.
# Bring variables from Class['jenkins'] into local scope.
$cli_tries = $jenkins::cli_tries
$cli_try_sleep = $jenkins::cli_try_sleep
$cli_try_sleep = $jenkins::cli_try_sleep

Exec {
logoutput => false,
Expand All @@ -80,18 +80,20 @@
$cat_config = "cat \"${tmp_config_path}\""
$create_job = "${jenkins_cli} create-job \"${jobname}\""
exec { "jenkins create-job ${jobname}":
command => "${cat_config} | ${create_job}",
creates => [$config_path, "${job_dir}/builds"],
command => "${cat_config} | ${create_job}",
creates => [$config_path, "${job_dir}/builds"],
environment => $jenkins::cli::cmd_environment,
}

if $replace {
# Use Jenkins CLI to update the job if it already exists
$update_job = "${jenkins_cli} update-job ${jobname}"
exec { "jenkins update-job ${jobname}":
command => "${cat_config} | ${update_job}",
onlyif => "test -e ${config_path}",
unless => "${difftool} ${config_path} ${tmp_config_path}",
notify => Exec['reload-jenkins'],
command => "${cat_config} | ${update_job}",
onlyif => "test -e ${config_path}",
unless => "${difftool} ${config_path} ${tmp_config_path}",
notify => Exec['reload-jenkins'],
environment => $jenkins::cli::cmd_environment,
}
}

Expand Down
46 changes: 45 additions & 1 deletion spec/classes/jenkins_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
it { is_expected.to contain_exec('reload-jenkins').with_command(%r{http://localhost:9000}) }
it { is_expected.to contain_exec('reload-jenkins').with_command(%r{-i\s'/path/to/key'}) }
it { is_expected.to contain_exec('reload-jenkins').that_requires('File[/path/to/libdir/jenkins-cli.jar]') }
it { is_expected.to contain_exec('safe-restart-jenkins') }
it { is_expected.to contain_exec('safe-restart-jenkins').with('environment' => nil) }
it { is_expected.to contain_jenkins__sysconfig('HTTP_PORT').with_value('9000') }

describe 'jenkins::cli' do
Expand All @@ -40,6 +40,50 @@
end
end
end

context '$cli_password is defined' do
let(:params) do
{
version: '2.54',
libdir: '/path/to/libdir',
cli: true,
cli_remoting_free: true,
cli_username: 'user01',
cli_password: 'password01'
}
end

it do
is_expected.to contain_exec('safe-restart-jenkins').with(
'environment' => [
'JENKINS_USER_ID=user01',
'JENKINS_API_TOKEN=password01'
]
)
end
end

context '$cli_password is defined' do
let(:params) do
{
version: '2.54',
libdir: '/path/to/libdir',
cli: true,
cli_remoting_free: true,
cli_username: 'user01',
cli_password: 'password01'
}
end

it do
is_expected.to contain_exec('safe-restart-jenkins').with(
'environment' => [
'JENKINS_USER_ID=user01',
'JENKINS_API_TOKEN=password01'
]
)
end
end
end

context '$cli => false' do
Expand Down