Skip to content

Commit

Permalink
Merge fa9b396 into d118ce6
Browse files Browse the repository at this point in the history
  • Loading branch information
emaV committed Apr 6, 2020
2 parents d118ce6 + fa9b396 commit 7fc38d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 1 addition & 2 deletions bin/git-ssh-wrapper.sh
@@ -1,3 +1,2 @@
#!/bin/sh

ssh -i $GIT_SSH_KEY -p $GIT_SSH_PORT -o StrictHostKeyChecking=no -o IdentitiesOnly=yes "$@"
ssh -i $GIT_SSH_KEY -p $GIT_SSH_PORT $GIT_SSH_OPT_UserKnownHostsFile -o StrictHostKeyChecking=no -o IdentitiesOnly=yes "$@"
15 changes: 14 additions & 1 deletion src/GitWrapper.php
Expand Up @@ -141,8 +141,10 @@ public function getTimeout(): int
*
* @param string|null $wrapper Path the the GIT_SSH wrapper script, defaults to null which uses the
* script included with this library.
* @param string|null $UserKnownHostsFile PATH to the known_hosts file for ssh authentication, defaults
* to null which doesn't inject the option in the SSH command
*/
public function setPrivateKey(string $privateKey, int $port = 22, ?string $wrapper = null): void
public function setPrivateKey(string $privateKey, int $port = 22, ?string $wrapper = null, ?string $UserKnownHostsFile = null): void
{
if ($wrapper === null) {
$wrapper = __DIR__ . '/../bin/git-ssh-wrapper.sh';
Expand All @@ -156,9 +158,19 @@ public function setPrivateKey(string $privateKey, int $port = 22, ?string $wrapp
throw new GitException('Path private key could not be resolved: ' . $privateKey);
}

if ($UserKnownHostsFile === null) {
$optUserKnownHostsFile = '';
} else {
if (! $UserKnownHostsFilePath = realpath($UserKnownHostsFile)) {
throw new GitException('Path to UserKnownHostsFile could not be resolved: ' . $UserKnownHostsFile);
}
$optUserKnownHostsFile = '-o UserKnownHostsFile=' . $UserKnownHostsFile;
}

$this->setEnvVar('GIT_SSH', $wrapperPath);
$this->setEnvVar('GIT_SSH_KEY', $privateKeyPath);
$this->setEnvVar('GIT_SSH_PORT', $port);
$this->setEnvVar('GIT_SSH_OPT_UserKnownHostsFile', $optUserKnownHostsFile);
}

/**
Expand All @@ -169,6 +181,7 @@ public function unsetPrivateKey(): void
$this->unsetEnvVar('GIT_SSH');
$this->unsetEnvVar('GIT_SSH_KEY');
$this->unsetEnvVar('GIT_SSH_PORT');
$this->unsetEnvVar('GIT_SSH_OPT_UserKnownHostsFile');
}

public function addOutputEventSubscriber(AbstractOutputEventSubscriber $gitOutputEventSubscriber): void
Expand Down

0 comments on commit 7fc38d7

Please sign in to comment.