-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add SSH commit signing for HEAD commits #2789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kausters
wants to merge
5
commits into
git-up:master
Choose a base branch
from
kausters:ssh-signed-commits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9615e43
Add SSH commit signing for HEAD commits
kausters 82af949
Test SSH commit signing for HEAD commits
kausters 98851ac
Extract SSH commit signing helper
kausters b5d973f
Split commit signing tests
kausters 77f9890
Hide amend parent handling in signing helper
kausters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| // Copyright (C) 2015-2019 Pierre-Olivier Latour <info@pol-online.net> | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| #if !__has_feature(objc_arc) | ||
| #error This file requires ARC | ||
| #endif | ||
|
|
||
| #import "GCTestCase.h" | ||
| #import "GCRepository+Index.h" | ||
|
|
||
| static NSString* _CommitSignature(GCCommit* commit) { | ||
| git_buf buffer = {0}; | ||
| int status = git_commit_header_field(&buffer, commit.private, "gpgsig"); | ||
| if (status != GIT_OK) { | ||
| git_buf_free(&buffer); | ||
| return nil; | ||
| } | ||
| NSString* signature = [[NSString alloc] initWithBytes:buffer.ptr length:buffer.size encoding:NSUTF8StringEncoding]; | ||
| git_buf_free(&buffer); | ||
| return signature; | ||
| } | ||
|
|
||
| static BOOL _WriteLocalConfigOption(GCRepository* repository, NSString* variable, NSString* value) { | ||
| return [repository writeConfigOptionForLevel:kGCConfigLevel_Local variable:variable withValue:value error:NULL]; | ||
| } | ||
|
|
||
| static BOOL _CommitHasSSHSignature(GCCommit* commit) { | ||
| return [_CommitSignature(commit) containsString:@"BEGIN SSH SIGNATURE"]; | ||
| } | ||
|
|
||
| static BOOL _ConfigureSSHSigningWithKeyPath(GCRepository* repository, NSString* keyPath) { | ||
| if (!_WriteLocalConfigOption(repository, @"commit.gpgsign", @"true")) { | ||
| return NO; | ||
| } | ||
| if (!_WriteLocalConfigOption(repository, @"gpg.format", @"ssh")) { | ||
| return NO; | ||
| } | ||
| return _WriteLocalConfigOption(repository, @"user.signingkey", keyPath); | ||
| } | ||
|
|
||
| static NSString* _CreateFakeSSHSigner(NSString* directory, int exitStatus) { | ||
| NSString* path = [directory stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; | ||
| NSString* contents; | ||
|
|
||
| if (exitStatus == 0) { | ||
| NSArray* lines = @[ | ||
| @"#!/bin/sh", | ||
| @"cat >/dev/null", | ||
| @"printf '%s\\n' '-----BEGIN SSH SIGNATURE-----' 'fake-signature' '-----END SSH SIGNATURE-----'", | ||
| @"" | ||
| ]; | ||
| contents = [lines componentsJoinedByString:@"\n"]; | ||
| } else { | ||
| NSArray* lines = @[ | ||
| @"#!/bin/sh", | ||
| @"cat >/dev/null", | ||
| @"echo signer failed >&2", | ||
| [NSString stringWithFormat:@"exit %i", exitStatus], | ||
| @"" | ||
| ]; | ||
| contents = [lines componentsJoinedByString:@"\n"]; | ||
| } | ||
|
|
||
| if (![contents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL]) { | ||
| return nil; | ||
| } | ||
| NSDictionary* attributes = @{NSFilePosixPermissions : @(0755)}; | ||
| if (![[NSFileManager defaultManager] setAttributes:attributes ofItemAtPath:path error:NULL]) { | ||
| return nil; | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| static GCCommit* _CreateCommitFromRepositoryIndex(GCRepository* repository, NSString* message, NSError** error) { | ||
| GCCommit* commit = nil; | ||
| git_index* index = NULL; | ||
| git_tree* tree = NULL; | ||
|
|
||
| index = [repository reloadRepositoryIndex:error]; | ||
| if (!index) { | ||
| goto cleanup; | ||
| } | ||
|
|
||
| git_oid oid; | ||
| CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_index_write_tree_to, &oid, index, repository.private); | ||
| CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_tree_lookup, &tree, repository.private, &oid); | ||
| commit = GCCreateCommitFromTreeWithOptionalSignature(repository, | ||
| tree, | ||
| NULL, | ||
| 0, | ||
| NULL, | ||
| message, | ||
| error); | ||
|
|
||
| cleanup: | ||
| git_tree_free(tree); | ||
| git_index_free(index); | ||
| return commit; | ||
| } | ||
|
|
||
| @implementation GCEmptyRepositoryTests (GCCommitSigning) | ||
|
|
||
| - (void)testCommitSigningLeavesCommitsUnsignedWhenDisabledOrUnsupported { | ||
| [self updateFileAtPath:@"unsigned.txt" withString:@"unsigned\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"unsigned.txt" error:NULL]); | ||
|
|
||
| GCCommit* unsignedCommit = _CreateCommitFromRepositoryIndex(self.repository, @"Unsigned", NULL); | ||
| XCTAssertNotNil(unsignedCommit); | ||
| XCTAssertNil(_CommitSignature(unsignedCommit)); | ||
|
|
||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"commit.gpgsign", @"true")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.format", @"openpgp")); | ||
| [self updateFileAtPath:@"openpgp.txt" withString:@"openpgp\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"openpgp.txt" error:NULL]); | ||
|
|
||
| GCCommit* openPGPCommit = _CreateCommitFromRepositoryIndex(self.repository, @"OpenPGP config remains unsigned", NULL); | ||
| XCTAssertNotNil(openPGPCommit); | ||
| XCTAssertNil(_CommitSignature(openPGPCommit)); | ||
| } | ||
|
|
||
| - (void)testCommitSigningRequiresSSHKey { | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"commit.gpgsign", @"true")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.format", @"ssh")); | ||
| [self updateFileAtPath:@"missing-key.txt" withString:@"missing key\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"missing-key.txt" error:NULL]); | ||
|
|
||
| NSError* error; | ||
| XCTAssertNil(_CreateCommitFromRepositoryIndex(self.repository, @"Missing key", &error)); | ||
| XCTAssertTrue([error.localizedDescription containsString:@"user.signingkey"]); | ||
| } | ||
|
|
||
| - (void)testCommitSigningSupportsInlineKeyAndDefaultKeyCommand { | ||
| NSString* signer = _CreateFakeSSHSigner(self.temporaryPath, 0); | ||
| NSString* inlineKey = @"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFakeInlineKey test@example.com"; | ||
| NSString* defaultKeyCommand = @"printf 'key::ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDefaultCommandKey test@example.com\\n'"; | ||
|
|
||
| XCTAssertNotNil(signer); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"commit.gpgsign", @"true")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.format", @"ssh")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.ssh.program", signer)); | ||
|
|
||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"user.signingkey", inlineKey)); | ||
| [self updateFileAtPath:@"inline-key.txt" withString:@"inline key\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"inline-key.txt" error:NULL]); | ||
|
|
||
| GCCommit* inlineCommit = _CreateCommitFromRepositoryIndex(self.repository, @"Inline key", NULL); | ||
| XCTAssertNotNil(inlineCommit); | ||
| XCTAssertTrue(_CommitHasSSHSignature(inlineCommit)); | ||
|
|
||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"user.signingkey", nil)); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.ssh.defaultKeyCommand", defaultKeyCommand)); | ||
| [self updateFileAtPath:@"default-key-command.txt" withString:@"default key command\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"default-key-command.txt" error:NULL]); | ||
|
|
||
| GCCommit* defaultCommandCommit = _CreateCommitFromRepositoryIndex(self.repository, @"Default key command", NULL); | ||
| XCTAssertNotNil(defaultCommandCommit); | ||
| XCTAssertTrue(_CommitHasSSHSignature(defaultCommandCommit)); | ||
| } | ||
|
|
||
| - (void)testCommitSigningSupportsKeyPath { | ||
| NSString* keyPath = [self.temporaryPath stringByAppendingPathComponent:@"signing_key"]; | ||
| GCTask* keygen = [[GCTask alloc] initWithExecutablePath:@"/usr/bin/ssh-keygen"]; | ||
| int status; | ||
| NSArray* keygenArguments = @[ @"-t", @"ed25519", @"-f", keyPath, @"-N", @"", @"-q" ]; | ||
| BOOL keygenSuccess = [keygen runWithArguments:keygenArguments stdin:nil stdout:NULL stderr:NULL exitStatus:&status error:NULL]; | ||
| XCTAssertTrue(keygenSuccess); | ||
| XCTAssertEqual(status, 0); | ||
| XCTAssertTrue(_ConfigureSSHSigningWithKeyPath(self.repository, keyPath)); | ||
|
|
||
| [self updateFileAtPath:@"path-key.txt" withString:@"path key\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"path-key.txt" error:NULL]); | ||
|
|
||
| GCCommit* commit = _CreateCommitFromRepositoryIndex(self.repository, @"Path key", NULL); | ||
| XCTAssertNotNil(commit); | ||
| XCTAssertTrue(_CommitHasSSHSignature(commit)); | ||
|
|
||
| NSString* publicKey = [NSString stringWithContentsOfFile:[keyPath stringByAppendingString:@".pub"] encoding:NSUTF8StringEncoding error:NULL]; | ||
| NSString* allowedSignersPath = [self.temporaryPath stringByAppendingPathComponent:@"allowed_signers"]; | ||
| NSString* allowedSigners = [NSString stringWithFormat:@"bot@example.com %@", publicKey]; | ||
| XCTAssertTrue([allowedSigners writeToFile:allowedSignersPath atomically:YES encoding:NSUTF8StringEncoding error:NULL]); | ||
|
|
||
| NSString* allowedSignersConfig = [NSString stringWithFormat:@"gpg.ssh.allowedSignersFile=%@", allowedSignersPath]; | ||
| NSString* verifyOutput = [self runGitCLTWithRepository:self.repository command:@"-c", allowedSignersConfig, @"verify-commit", commit.SHA1, nil]; | ||
| XCTAssertNotNil(verifyOutput); | ||
| } | ||
|
|
||
| - (void)testCommitSigningFailsOnSignerFailure { | ||
| NSString* signer = _CreateFakeSSHSigner(self.temporaryPath, 7); | ||
| NSString* signingKey = @"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFailingSignerKey test@example.com"; | ||
|
|
||
| XCTAssertNotNil(signer); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"commit.gpgsign", @"true")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.format", @"ssh")); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"gpg.ssh.program", signer)); | ||
| XCTAssertTrue(_WriteLocalConfigOption(self.repository, @"user.signingkey", signingKey)); | ||
| [self updateFileAtPath:@"failing-signer.txt" withString:@"failing signer\n"]; | ||
| XCTAssertTrue([self.repository addFileToIndex:@"failing-signer.txt" error:NULL]); | ||
|
|
||
| NSError* error; | ||
| XCTAssertNil(_CreateCommitFromRepositoryIndex(self.repository, @"Failing signer", &error)); | ||
| XCTAssertTrue([error.localizedDescription containsString:@"non-zero status"]); | ||
| } | ||
|
|
||
| @end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_CommitSignature()is copy-pasted character-for-character intoGCRepository+HEAD-Tests.m(lines 581–591 of that file)._ConfigureSSHSigningWithKeyPathalso appears in both files, with slightly different implementations (one uses a_WriteLocalConfigOptionwrapper, the other callswriteConfigOptionForLevel:variable:withValue:error:directly). If either helper needs to change — for example, to read a different signature header field for a future signing format — the two copies will diverge silently.Both helpers should be extracted into
GCTestCase(or a dedicated signing test utility category) so they are shared by both test files.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucasderraugh not sure what the project convention is for shared test helpers across files, happy to adjust or leave it as-is, up to you
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think reusing this same call would be ideal. A small file with helpers in the test target would be fine.