|
| 1 | +from pathlib import Path |
| 2 | +from subprocess import CalledProcessError |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from gitrevise.odb import Repository |
| 8 | +from gitrevise.utils import sh_run |
| 9 | + |
| 10 | +from .conftest import bash, main |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="function", name="ssh_private_key_path") |
| 14 | +def fixture_ssh_private_key_path(short_tmpdir: Path) -> Generator[Path, None, None]: |
| 15 | + """ |
| 16 | + Creates an SSH key and registers it with ssh-agent. De-registers it during cleanup. |
| 17 | + Yields the Path to the private key file. The corresponding public key file is that path |
| 18 | + with suffix ".pub". |
| 19 | + """ |
| 20 | + short_tmpdir.chmod(0o700) |
| 21 | + private_key_path = short_tmpdir / "test_sshsign" |
| 22 | + sh_run( |
| 23 | + [ |
| 24 | + "ssh-keygen", |
| 25 | + "-q", |
| 26 | + "-N", |
| 27 | + "", |
| 28 | + "-f", |
| 29 | + private_key_path.as_posix(), |
| 30 | + "-C", |
| 31 | + "git-revise: test_sshsign", |
| 32 | + ], |
| 33 | + check=True, |
| 34 | + ) |
| 35 | + |
| 36 | + assert private_key_path.is_file() |
| 37 | + pub_key_path = private_key_path.with_suffix(".pub") |
| 38 | + assert pub_key_path.is_file() |
| 39 | + |
| 40 | + sh_run(["ssh-add", private_key_path.as_posix()], check=True) |
| 41 | + yield private_key_path |
| 42 | + sh_run(["ssh-add", "-d", private_key_path.as_posix()], check=True) |
| 43 | + |
| 44 | + |
| 45 | +def test_sshsign( |
| 46 | + repo: Repository, |
| 47 | + ssh_private_key_path: Path, |
| 48 | +) -> None: |
| 49 | + def commit_has_ssh_signature(refspec: str) -> bool: |
| 50 | + commit = repo.get_commit(refspec) |
| 51 | + assert commit is not None |
| 52 | + assert commit.gpgsig is not None |
| 53 | + assert commit.gpgsig.startswith(b"-----BEGIN SSH SIGNATURE-----") |
| 54 | + return True |
| 55 | + |
| 56 | + bash("git commit --allow-empty -m 'commit 1'") |
| 57 | + assert repo.get_commit("HEAD").gpgsig is None |
| 58 | + |
| 59 | + bash("git config gpg.format ssh") |
| 60 | + bash("git config commit.gpgSign true") |
| 61 | + |
| 62 | + sh_run( |
| 63 | + ["git", "config", "user.signingKey", ssh_private_key_path.as_posix()], |
| 64 | + check=True, |
| 65 | + ) |
| 66 | + main(["HEAD"]) |
| 67 | + assert commit_has_ssh_signature("HEAD"), "can ssh sign given key as path" |
| 68 | + |
| 69 | + pubkey = ssh_private_key_path.with_suffix(".pub").read_text().strip() |
| 70 | + sh_run( |
| 71 | + [ |
| 72 | + "git", |
| 73 | + "config", |
| 74 | + "user.signingKey", |
| 75 | + pubkey, |
| 76 | + ], |
| 77 | + check=True, |
| 78 | + ) |
| 79 | + main(["HEAD"]) |
| 80 | + assert commit_has_ssh_signature("HEAD"), "can ssh sign given literal pubkey" |
| 81 | + |
| 82 | + bash("git config gpg.ssh.program false") |
| 83 | + try: |
| 84 | + main(["HEAD", "--gpg-sign"]) |
| 85 | + assert False, "Overridden gpg.ssh.program should fail" |
| 86 | + except CalledProcessError: |
| 87 | + pass |
| 88 | + bash("git config --unset gpg.ssh.program") |
| 89 | + |
| 90 | + # Check that we can sign multiple commits. |
| 91 | + bash( |
| 92 | + """ |
| 93 | + git -c commit.gpgSign=false commit --allow-empty -m 'commit 2' |
| 94 | + git -c commit.gpgSign=false commit --allow-empty -m 'commit 3' |
| 95 | + git -c commit.gpgSign=false commit --allow-empty -m 'commit 4' |
| 96 | + """ |
| 97 | + ) |
| 98 | + main(["HEAD~~", "--gpg-sign"]) |
| 99 | + assert commit_has_ssh_signature("HEAD~~") |
| 100 | + assert commit_has_ssh_signature("HEAD~") |
| 101 | + assert commit_has_ssh_signature("HEAD~") |
| 102 | + |
| 103 | + # Check that we can remove signatures from multiple commits. |
| 104 | + main(["HEAD~", "--no-gpg-sign"]) |
| 105 | + assert repo.get_commit("HEAD~").gpgsig is None |
| 106 | + assert repo.get_commit("HEAD").gpgsig is None |
| 107 | + |
| 108 | + # Check that we add signatures, even if the target commit already has one. |
| 109 | + assert commit_has_ssh_signature("HEAD~~") |
| 110 | + main(["HEAD~~", "--gpg-sign"]) |
| 111 | + assert commit_has_ssh_signature("HEAD~") |
| 112 | + assert commit_has_ssh_signature("HEAD") |
0 commit comments