Skip to content

Commit e19bc76

Browse files
authored
Merge pull request #473 from bigpresh/bigpresh/git_as_specified_uid_even_with_identity
Always run as given user, even if identity set
2 parents bc01b89 + e4454f9 commit e19bc76

File tree

3 files changed

+153
-153
lines changed

3 files changed

+153
-153
lines changed

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
Puppet::Type.type(:vcsrepo).provide(:git, parent: Puppet::Provider::Vcsrepo) do
66
desc 'Supports Git repositories'
77

8-
has_command(:git, 'git') do
9-
environment('HOME' => ENV['HOME'])
10-
end
11-
128
has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes,
139
:user, :depth, :branch, :submodules
1410

@@ -128,13 +124,13 @@ def working_copy_exists?
128124
at_path do
129125
if @resource.value(:source)
130126
begin
131-
return git('config', '--get', "remote.#{@resource.value(:remote)}.url").chomp == default_url
127+
return git_with_identity('config', '--get', "remote.#{@resource.value(:remote)}.url").chomp == default_url
132128
rescue Puppet::ExecutionFailure
133129
return false
134130
end
135131
else
136132
begin
137-
git('status')
133+
git_with_identity('status')
138134
return true
139135
rescue Puppet::ExecutionFailure
140136
return false
@@ -174,11 +170,11 @@ def update_remote_url(remote_name, remote_url)
174170

175171
def source
176172
at_path do
177-
remotes = git('remote').split("\n")
173+
remotes = git_with_identity('remote').split("\n")
178174

179-
return git('config', '--get', "remote.#{remotes[0]}.url").chomp if remotes.size == 1
175+
return git_with_identity('config', '--get', "remote.#{remotes[0]}.url").chomp if remotes.size == 1
180176
Hash[remotes.map do |remote|
181-
[remote, git('config', '--get', "remote.#{remote}.url").chomp]
177+
[remote, git_with_identity('config', '--get', "remote.#{remote}.url").chomp]
182178
end]
183179
end
184180
end
@@ -247,7 +243,7 @@ def convert_working_copy_to_bare
247243
FileUtils.rm_rf(@resource.value(:path))
248244
FileUtils.mv(tempdir, @resource.value(:path))
249245
at_path do
250-
git('config', '--local', '--bool', 'core.bare', 'true')
246+
exec_git('config', '--local', '--bool', 'core.bare', 'true')
251247
return unless @resource.value(:ensure) == :mirror
252248
raise('Cannot have empty repository that is also a mirror.') unless @resource.value(:source)
253249
set_mirror
@@ -268,7 +264,7 @@ def convert_bare_to_working_copy
268264
FileUtils.mv(tempdir, File.join(@resource.value(:path), '.git'))
269265
if commits?
270266
at_path do
271-
git('config', '--local', '--bool', 'core.bare', 'false')
267+
exec_git('config', '--local', '--bool', 'core.bare', 'false')
272268
reset('HEAD')
273269
git_with_identity('checkout', '--force')
274270
update_owner_and_excludes
@@ -280,7 +276,7 @@ def convert_bare_to_working_copy
280276
def mirror?
281277
at_path do
282278
begin
283-
git('config', '--get-regexp', 'remote\..*\.mirror')
279+
git_with_identity('config', '--get-regexp', 'remote\..*\.mirror')
284280
return true
285281
rescue Puppet::ExecutionFailure
286282
return false
@@ -291,10 +287,10 @@ def mirror?
291287
def set_mirror
292288
at_path do
293289
if @resource.value(:source).is_a?(String)
294-
git('config', "remote.#{@resource.value(:remote)}.mirror", 'true')
290+
git_with_identity('config', "remote.#{@resource.value(:remote)}.mirror", 'true')
295291
else
296292
@resource.value(:source).each_key do |remote|
297-
git('config', "remote.#{remote}.mirror", 'true')
293+
git_with_identity('config', "remote.#{remote}.mirror", 'true')
298294
end
299295
end
300296
end
@@ -304,14 +300,14 @@ def set_no_mirror
304300
at_path do
305301
if @resource.value(:source).is_a?(String)
306302
begin
307-
git('config', '--unset', "remote.#{@resource.value(:remote)}.mirror")
303+
exec_git('config', '--unset', "remote.#{@resource.value(:remote)}.mirror")
308304
rescue Puppet::ExecutionFailure
309305
next
310306
end
311307
else
312308
@resource.value(:source).each_key do |remote|
313309
begin
314-
git('config', '--unset', "remote.#{remote}.mirror")
310+
exec_git('config', '--unset', "remote.#{remote}.mirror")
315311
rescue Puppet::ExecutionFailure
316312
next
317313
end
@@ -326,7 +322,7 @@ def set_no_mirror
326322
def bare_git_config_exists?
327323
return false unless File.exist?(File.join(@resource.value(:path), 'config'))
328324
begin
329-
at_path { git('config', '--list', '--file', 'config') }
325+
at_path { git_with_identity('config', '--list', '--file', 'config') }
330326
true
331327
rescue Puppet::ExecutionFailure
332328
false
@@ -564,7 +560,7 @@ def update_owner_and_excludes
564560
end
565561

566562
def git_version
567-
git('--version').match(%r{[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?})[0]
563+
exec_git('--version').match(%r{[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?})[0]
568564
end
569565

570566
# @!visibility private
@@ -577,34 +573,37 @@ def git_with_identity(*args)
577573
end
578574

579575
if @resource.value(:identity)
580-
Tempfile.open('git-helper', Puppet[:statedir]) do |f|
581-
f.puts '#!/bin/sh'
582-
f.puts 'SSH_AUTH_SOCKET='
583-
f.puts 'export SSH_AUTH_SOCKET'
584-
f.puts 'exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no ' \
585-
"-oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
586-
f.close
576+
ssh_opts = {
577+
IgnoreUnknown: 'IdentityAgent',
578+
IdentitiesOnly: 'yes',
579+
IdentityAgent: 'none',
580+
PasswordAuthentication: 'no',
581+
KbdInteractiveAuthentication: 'no',
582+
}
583+
ssh_command = "ssh -i #{@resource.value(:identity)} "
584+
ssh_command += ssh_opts.map { |option, value| "-o \"#{option} #{value}\"" }.join ' '
587585

588-
FileUtils.chmod(0o755, f.path)
586+
env_git_ssh_command_save = ENV['GIT_SSH_COMMAND']
587+
ENV['GIT_SSH_COMMAND'] = ssh_command
589588

590-
env_git_ssh_save = ENV['GIT_SSH']
591-
env_git_ssh_command_save = ENV['GIT_SSH_COMMAND']
589+
ret = exec_git(*args)
592590

593-
ENV['GIT_SSH'] = f.path
594-
ENV['GIT_SSH_COMMAND'] = nil # Unset GIT_SSH_COMMAND environment variable
591+
ENV['GIT_SSH_COMMAND'] = env_git_ssh_command_save
595592

596-
ret = git(*args)
597-
598-
ENV['GIT_SSH'] = env_git_ssh_save
599-
ENV['GIT_SSH_COMMAND'] = env_git_ssh_command_save
593+
ret
594+
else
595+
exec_git(*args)
596+
end
597+
end
600598

601-
return ret
602-
end
603-
elsif @resource.value(:user) && @resource.value(:user) != Facter['id'].value
599+
# Execute git with the given args, running it as the user specified.
600+
def exec_git(*args)
601+
exec_args = { failonfail: true, combine: true }
602+
if @resource.value(:user) && @resource.value(:user) != Facter['id'].value
604603
env = Etc.getpwnam(@resource.value(:user))
605-
Puppet::Util::Execution.execute("git #{args.join(' ')}", uid: @resource.value(:user), failonfail: true, custom_environment: { 'HOME' => env['dir'] }, combine: true)
606-
else
607-
git(*args)
604+
exec_args[:custom_environment] = { 'HOME' => env['dir'] }
605+
exec_args[:uid] = @resource.value(:user)
608606
end
607+
Puppet::Util::Execution.execute([:git, args], **exec_args)
609608
end
610609
end

spec/acceptance/clone_repo_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@
437437
run_shell('mkdir -p /home/testuser-ssh/.ssh')
438438
run_shell('ssh-keygen -q -t rsa -f /home/testuser-ssh/.ssh/id_rsa -N ""')
439439

440+
# add localhost to known_hosts
441+
run_shell('rm /home/testuser-ssh/.ssh/known_hosts', expect_failures: true)
442+
run_shell('ssh-keyscan localhost >> /home/testuser-ssh/.ssh/known_hosts')
443+
440444
# copy public key to authorized_keys
441445
run_shell('cat /home/testuser-ssh/.ssh/id_rsa.pub > /home/testuser-ssh/.ssh/authorized_keys')
442446
run_shell('echo -e "Host localhost\n\tStrictHostKeyChecking no\n" > /home/testuser-ssh/.ssh/config')
@@ -472,8 +476,12 @@
472476
before(:all) do
473477
# create user
474478
pp = <<-MANIFEST
479+
group { 'testuser-ssh':
480+
ensure => present,
481+
}
475482
user { 'testuser-ssh':
476483
ensure => present,
484+
groups => 'testuser-ssh',
477485
managehome => true,
478486
}
479487
MANIFEST
@@ -483,9 +491,11 @@
483491
run_shell('mkdir -p /home/testuser-ssh/.ssh')
484492
run_shell('ssh-keygen -q -t rsa -f /home/testuser-ssh/.ssh/id_rsa -N ""')
485493

494+
# add localhost to known_hosts
495+
run_shell('ssh-keyscan localhost > /home/testuser-ssh/.ssh/known_hosts')
496+
486497
# copy public key to authorized_keys
487498
run_shell('cat /home/testuser-ssh/.ssh/id_rsa.pub > /home/testuser-ssh/.ssh/authorized_keys')
488-
run_shell('echo -e "Host localhost\n\tStrictHostKeyChecking no\n" > /home/testuser-ssh/.ssh/config')
489499
run_shell('chown -R testuser-ssh:testuser-ssh /home/testuser-ssh/.ssh')
490500
end
491501

@@ -495,6 +505,7 @@
495505
provider => git,
496506
source => "testuser-ssh@localhost:#{tmpdir}/testrepo.git",
497507
identity => '/home/testuser-ssh/.ssh/id_rsa',
508+
user => 'testuser-ssh',
498509
}
499510
MANIFEST
500511
it 'applies the manifest' do

0 commit comments

Comments
 (0)