@@ -24,47 +24,43 @@ import (
24
24
25
25
// ParseCommitWithSignature check if signature is good against keystore.
26
26
func ParseCommitWithSignature (ctx context.Context , c * git.Commit ) * asymkey_model.CommitVerification {
27
- var committer * user_model.User
28
- if c .Committer != nil {
29
- var err error
30
- // Find Committer account
31
- committer , err = user_model .GetUserByEmail (ctx , c .Committer .Email ) // This finds the user by primary email or activated email so commit will not be valid if email is not
32
- if err != nil { // Skipping not user for committer
33
- committer = & user_model.User {
34
- Name : c .Committer .Name ,
35
- Email : c .Committer .Email ,
36
- }
37
- // We can expect this to often be an ErrUserNotExist. in the case
38
- // it is not, however, it is important to log it.
39
- if ! user_model .IsErrUserNotExist (err ) {
40
- log .Error ("GetUserByEmail: %v" , err )
41
- return & asymkey_model.CommitVerification {
42
- CommittingUser : committer ,
43
- Verified : false ,
44
- Reason : "gpg.error.no_committer_account" ,
45
- }
46
- }
27
+ committer , err := user_model .GetUserByEmail (ctx , c .Committer .Email )
28
+ if err != nil && ! user_model .IsErrUserNotExist (err ) {
29
+ log .Error ("GetUserByEmail: %v" , err )
30
+ return & asymkey_model.CommitVerification {
31
+ Verified : false ,
32
+ Reason : "gpg.error.no_committer_account" , // this error is not right, but such error should seldom happen
47
33
}
48
34
}
49
-
50
35
return ParseCommitWithSignatureCommitter (ctx , c , committer )
51
36
}
52
37
38
+ // ParseCommitWithSignatureCommitter parses a commit's GPG or SSH signature.
39
+ // If the commit is singed by an instance key, then committer can be nil.
40
+ // If the signature exists, even if committer is nil, the returned CommittingUser will be a non-nil fake user.
53
41
func ParseCommitWithSignatureCommitter (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
54
- // If no signature just report the committer
42
+ // If no signature, just report the committer
55
43
if c .Signature == nil {
56
44
return & asymkey_model.CommitVerification {
57
45
CommittingUser : committer ,
58
- Verified : false , // Default value
59
- Reason : "gpg.error.not_signed_commit" , // Default value
46
+ Verified : false ,
47
+ Reason : "gpg.error.not_signed_commit" ,
48
+ }
49
+ }
50
+ // to support instance key, we need a fake committer user (not really needed, but legacy code accesses the committer without nil-check)
51
+ if committer == nil {
52
+ committer = & user_model.User {
53
+ Name : c .Committer .Name ,
54
+ Email : c .Committer .Email ,
60
55
}
61
56
}
62
-
63
- // If this a SSH signature handle it differently
64
57
if strings .HasPrefix (c .Signature .Signature , "-----BEGIN SSH SIGNATURE-----" ) {
65
- return ParseCommitWithSSHSignature (ctx , c , committer )
58
+ return parseCommitWithSSHSignature (ctx , c , committer )
66
59
}
60
+ return parseCommitWithGPGSignature (ctx , c , committer )
61
+ }
67
62
63
+ func parseCommitWithGPGSignature (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
68
64
// Parsing signature
69
65
sig , err := asymkey_model .ExtractSignature (c .Signature .Signature )
70
66
if err != nil { // Skipping failed to extract sign
@@ -165,7 +161,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
165
161
}
166
162
if err := gpgSettings .LoadPublicKeyContent (); err != nil {
167
163
log .Error ("Error getting default signing key: %s %v" , gpgSettings .KeyID , err )
168
- } else if commitVerification := VerifyWithGPGSettings (ctx , & gpgSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
164
+ } else if commitVerification := verifyWithGPGSettings (ctx , & gpgSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
169
165
if commitVerification .Reason == asymkey_model .BadSignature {
170
166
defaultReason = asymkey_model .BadSignature
171
167
} else {
@@ -180,7 +176,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
180
176
} else if defaultGPGSettings == nil {
181
177
log .Warn ("Unable to get defaultGPGSettings for unattached commit: %s" , c .ID .String ())
182
178
} else if defaultGPGSettings .Sign {
183
- if commitVerification := VerifyWithGPGSettings (ctx , defaultGPGSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
179
+ if commitVerification := verifyWithGPGSettings (ctx , defaultGPGSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
184
180
if commitVerification .Reason == asymkey_model .BadSignature {
185
181
defaultReason = asymkey_model .BadSignature
186
182
} else {
@@ -295,7 +291,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
295
291
}
296
292
}
297
293
298
- func VerifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string ) * asymkey_model.CommitVerification {
294
+ func verifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string ) * asymkey_model.CommitVerification {
299
295
// First try to find the key in the db
300
296
if commitVerification := HashAndVerifyForKeyID (ctx , sig , payload , committer , gpgSettings .KeyID , gpgSettings .Name , gpgSettings .Email ); commitVerification != nil {
301
297
return commitVerification
@@ -375,8 +371,8 @@ func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, sign
375
371
return verifySSHCommitVerification (c .Signature .Signature , c .Signature .Payload , sshPubKey , committerUser , signerUser , committerGitEmail )
376
372
}
377
373
378
- // ParseCommitWithSSHSignature check if signature is good against keystore.
379
- func ParseCommitWithSSHSignature (ctx context.Context , c * git.Commit , committerUser * user_model.User ) * asymkey_model.CommitVerification {
374
+ // parseCommitWithSSHSignature check if signature is good against keystore.
375
+ func parseCommitWithSSHSignature (ctx context.Context , c * git.Commit , committerUser * user_model.User ) * asymkey_model.CommitVerification {
380
376
// Now try to associate the signature with the committer, if present
381
377
if committerUser .ID != 0 {
382
378
keys , err := db .Find [asymkey_model.PublicKey ](ctx , asymkey_model.FindPublicKeyOptions {
0 commit comments