Skip to content

Commit 2ff2132

Browse files
committed
Bring back GIT_SSH support for old git versions
Older versions (<2.3) do not support GIT_SSH_COMMAND yet. Fall back to GIT_SSH with a wrapper script.
1 parent fa05b22 commit 2ff2132

File tree

1 file changed

+49
-15
lines changed
  • lib/puppet/provider/vcsrepo

1 file changed

+49
-15
lines changed

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -666,26 +666,60 @@ def git_with_identity(*args)
666666
end
667667

668668
if @resource.value(:identity)
669-
ssh_opts = {
670-
IgnoreUnknown: 'IdentityAgent',
671-
IdentitiesOnly: 'yes',
672-
IdentityAgent: 'none',
673-
PasswordAuthentication: 'no',
674-
KbdInteractiveAuthentication: 'no',
675-
}
676-
ssh_command = "ssh -i #{@resource.value(:identity)} "
677-
ssh_command += ssh_opts.map { |option, value| "-o \"#{option} #{value}\"" }.join ' '
678-
679-
env_git_ssh_command_save = ENV['GIT_SSH_COMMAND']
680-
ENV['GIT_SSH_COMMAND'] = ssh_command
669+
git_ver = git_version
670+
if Gem::Version.new(git_ver) >= Gem::Version.new('2.3.0')
671+
# GIT_SSH_COMMAND was introduced in version 2.3.0.
672+
git_ssh_with_identity_ssh_command(*args)
673+
else
674+
git_ssh_with_identity_ssh_file(*args)
675+
end
676+
else
677+
exec_git(*args)
678+
end
679+
end
680+
681+
# @!visibility private
682+
def git_ssh_with_identity_ssh_command(*args)
683+
ssh_opts = {
684+
IgnoreUnknown: 'IdentityAgent',
685+
IdentitiesOnly: 'yes',
686+
IdentityAgent: 'none',
687+
PasswordAuthentication: 'no',
688+
KbdInteractiveAuthentication: 'no',
689+
}
690+
ssh_command = "ssh -i #{@resource.value(:identity)} "
691+
ssh_command += ssh_opts.map { |option, value| "-o \"#{option} #{value}\"" }.join ' '
692+
693+
env_git_ssh_command_save = ENV['GIT_SSH_COMMAND']
694+
ENV['GIT_SSH_COMMAND'] = ssh_command
695+
696+
ret = exec_git(*args)
697+
698+
ENV['GIT_SSH_COMMAND'] = env_git_ssh_command_save
699+
700+
ret
701+
end
702+
703+
# @!visiblity private
704+
def git_ssh_with_identity_ssh_file(*args)
705+
Tempfile.open('git-helper', Puppet[:statedir]) do |f|
706+
f.puts '#!/bin/sh'
707+
f.puts 'SSH_AUTH_SOCKET='
708+
f.puts 'export SSH_AUTH_SOCKET'
709+
f.puts 'exec ssh -oStrictHostKeyChecking=no -oPasswordAuthentication=no -oKbdInteractiveAuthentication=no ' \
710+
"-oChallengeResponseAuthentication=no -oConnectTimeout=120 -i #{@resource.value(:identity)} $*"
711+
f.close
712+
713+
FileUtils.chmod(0o755, f.path)
714+
715+
env_git_ssh_save = ENV['GIT_SSH']
716+
ENV['GIT_SSH'] = f.path
681717

682718
ret = exec_git(*args)
683719

684-
ENV['GIT_SSH_COMMAND'] = env_git_ssh_command_save
720+
ENV['GIT_SSH'] = env_git_ssh_save
685721

686722
ret
687-
else
688-
exec_git(*args)
689723
end
690724
end
691725

0 commit comments

Comments
 (0)