Skip to content

Commit 7a50794

Browse files
committed
git: Support verifying commit signature post-sync
Add a new sync-git-verify-commit-signature option (defaulting to false) that verifies the top commit signature after syncing. The verification is currently done using built-in git routines. The verification passes if the signature is good or untrusted. In the latter case, a warning is printed. In any other case, the verification causes sync to fail and an appropriate error is output. Reviewed-by: Zac Medico <zmedico@gentoo.org>
1 parent 8d99acd commit 7a50794

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎man/portage.5‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,10 @@ See also example for sync-git-clone-env.
10071007
.B sync\-git\-pull\-extra\-opts
10081008
Extra options to give to git when updating repository (git pull).
10091009
.TP
1010+
.B sync\-git\-verify\-commit\-signature = true|false
1011+
Require the top commit in the repository to contain a good OpenPGP
1012+
signature. Defaults to false.
1013+
.TP
10101014
.B sync\-hooks\-only\-on\-change
10111015
If set to true, then sync of a given repository will not trigger postsync
10121016
hooks unless hooks would have executed for a master repository or the

‎pym/portage/sync/modules/git/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2014-2017 Gentoo Foundation
1+
# Copyright 2014-2018 Gentoo Foundation
22
# Distributed under the terms of the GNU General Public License v2
33

44
doc = """Git plug-in module for portage.
@@ -58,6 +58,7 @@ def _check_depth(self, attr):
5858
'sync-git-env',
5959
'sync-git-pull-env',
6060
'sync-git-pull-extra-opts',
61+
'sync-git-verify-commit-signature',
6162
),
6263
}
6364
}

‎pym/portage/sync/modules/git/git.py‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2005-2017 Gentoo Foundation
1+
# Copyright 2005-2018 Gentoo Foundation
22
# Distributed under the terms of the GNU General Public License v2
33

44
import logging
@@ -7,7 +7,7 @@
77
import portage
88
from portage import os
99
from portage.util import writemsg_level, shlex_split
10-
from portage.output import create_color_func
10+
from portage.output import create_color_func, EOutput
1111
good = create_color_func("GOOD")
1212
bad = create_color_func("BAD")
1313
warn = create_color_func("WARN")
@@ -71,6 +71,7 @@ def new(self, **kwargs):
7171
else:
7272
# default
7373
git_cmd_opts += " --depth 1"
74+
7475
if self.repo.module_specific_options.get('sync-git-clone-extra-opts'):
7576
git_cmd_opts += " %s" % self.repo.module_specific_options['sync-git-clone-extra-opts']
7677
git_cmd = "%s clone%s %s ." % (self.bin_command, git_cmd_opts,
@@ -85,6 +86,8 @@ def new(self, **kwargs):
8586
self.logger(self.xterm_titles, msg)
8687
writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
8788
return (exitcode, False)
89+
if not self.verify_head():
90+
return (1, False)
8891
return (os.EX_OK, True)
8992

9093

@@ -125,12 +128,53 @@ def update(self):
125128
self.logger(self.xterm_titles, msg)
126129
writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
127130
return (exitcode, False)
131+
if not self.verify_head():
132+
return (1, False)
128133

129134
current_rev = subprocess.check_output(rev_cmd,
130135
cwd=portage._unicode_encode(self.repo.location))
131136

132137
return (os.EX_OK, current_rev != previous_rev)
133138

139+
def verify_head(self):
140+
if (self.repo.module_specific_options.get(
141+
'sync-git-verify-commit-signature', 'false') != 'true'):
142+
return True
143+
144+
rev_cmd = [self.bin_command, "log", "--pretty=format:%G?", "-1"]
145+
try:
146+
status = (portage._unicode_decode(
147+
subprocess.check_output(rev_cmd,
148+
cwd=portage._unicode_encode(self.repo.location)))
149+
.strip())
150+
except subprocess.CalledProcessError:
151+
return False
152+
153+
out = EOutput()
154+
if status == 'G': # good signature is good
155+
out.einfo('Trusted signature found on top commit')
156+
return True
157+
elif status == 'U': # untrusted
158+
out.ewarn('Top commit signature is valid but not trusted')
159+
return True
160+
else:
161+
if status == 'B':
162+
expl = 'bad signature'
163+
elif status == 'X':
164+
expl = 'expired signature'
165+
elif status == 'Y':
166+
expl = 'expired key'
167+
elif status == 'R':
168+
expl = 'revoked key'
169+
elif status == 'E':
170+
expl = 'unable to verify signature (missing key?)'
171+
elif status == 'N':
172+
expl = 'no signature'
173+
else:
174+
expl = 'unknown issue'
175+
out.eerror('No valid signature found: %s' % (expl,))
176+
return False
177+
134178
def retrieve_head(self, **kwargs):
135179
'''Get information about the head commit'''
136180
if kwargs:

0 commit comments

Comments
 (0)