Skip to content

Commit e29d794

Browse files
authored
Fix #358 Add support for passphrase protected private key files. (#359)
Signed-off-by: Jamie Pate <jpate@fortinet.com>
1 parent 61d45dd commit e29d794

5 files changed

Lines changed: 37 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
2121
-----END RSA PRIVATE KEY-----
2222
```
2323
24-
* `private_key_user`: *Optional.* Enables setting User in the ssh config
24+
* `private_key_user`: *Optional.* Enables setting User in the ssh config.
25+
26+
* `private_key_passphrase`: *Optional.* To unlock `private_key` if it is protected by a passphrase.
2527

2628
* `forward_agent`: *Optional* Enables ForwardAgent SSH option when set to true. Useful when using proxy/jump hosts. Defaults to false.
2729

assets/askpass.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/bin/bash
2-
echo "Private keys with passphrases are not supported." >&2
3-
exit 1
2+
if [ -z "$GIT_SSH_PRIVATE_KEY_PASS" ]; then
3+
echo "Private key has a passphrase but private_key_passphrase has not been set." >&2
4+
exit 1
5+
fi
6+
echo "$GIT_SSH_PRIVATE_KEY_PASS"

assets/common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load_pubkey() {
55
local private_key_path=$TMPDIR/git-resource-private-key
66
local private_key_user=$(jq -r '.source.private_key_user // empty' < $1)
77
local forward_agent=$(jq -r '.source.forward_agent // false' < $1)
8+
local passphrase="$(jq -r '.source.private_key_passphrase // empty' < $1)"
89

910
(jq -r '.source.private_key // empty' < $1) > $private_key_path
1011

@@ -13,8 +14,7 @@ load_pubkey() {
1314

1415
eval $(ssh-agent) >/dev/null 2>&1
1516
trap "kill $SSH_AGENT_PID" EXIT
16-
17-
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh DISPLAY= ssh-add $private_key_path >/dev/null
17+
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh GIT_SSH_PRIVATE_KEY_PASS="$passphrase" DISPLAY= ssh-add $private_key_path >/dev/null
1818

1919
mkdir -p ~/.ssh
2020
cat > ~/.ssh/config <<EOF

test/check.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ it_can_check_from_head_only_fetching_single_branch() {
2626
! git -C $cachedir rev-parse origin/bogus
2727
}
2828

29-
it_fails_if_key_has_password() {
29+
it_fails_if_key_has_password_not_provided() {
3030
local repo=$(init_repo)
3131
local ref=$(make_commit $repo)
3232

@@ -39,9 +39,22 @@ it_fails_if_key_has_password() {
3939
return 1
4040
fi
4141

42-
grep "Private keys with passphrases are not supported." $failed_output
42+
grep "Private key has a passphrase but private_key_passphrase has not been set." $failed_output
4343
}
4444

45+
it_can_unlock_key_with_password() {
46+
local repo=$(init_repo)
47+
local ref=$(make_commit $repo)
48+
local passphrase='some passphrase with spaces!'
49+
50+
local key=$TMPDIR/key-with-passphrase
51+
ssh-keygen -f $key -N "$passphrase"
52+
53+
local failed_output=$TMPDIR/failed-output
54+
check_uri_with_key_and_passphrase $repo $key "$passphrase" 2>$failed_output
55+
}
56+
57+
4558
it_configures_forward_agent() {
4659
local repo=$(init_repo)
4760
local key=$TMPDIR/key-no-passphrase
@@ -956,7 +969,8 @@ run it_skips_excluded_commits_conventional
956969
run it_skips_non_included_commits
957970
run it_skips_non_included_and_excluded_commits
958971
run it_does_not_skip_marked_commits_when_disable_skip_configured
959-
run it_fails_if_key_has_password
972+
run it_fails_if_key_has_password_not_provided
973+
run it_can_unlock_key_with_password
960974
run it_configures_forward_agent
961975
run it_skips_forward_agent_configuration
962976
run it_can_check_with_credentials

test/helpers.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ check_uri_with_key() {
298298
}" | ${resource_dir}/check | tee /dev/stderr
299299
}
300300

301+
check_uri_with_key_and_passphrase() {
302+
jq -n "{
303+
source: {
304+
uri: $(echo $1 | jq -R .),
305+
private_key: $(cat $2 | jq -s -R .),
306+
private_key_passphrase: $(echo $3 | jq -R .)
307+
}
308+
}" | ${resource_dir}/check | tee /dev/stderr
309+
}
310+
301311
check_uri_with_credentials() {
302312
jq -n "{
303313
source: {

0 commit comments

Comments
 (0)