@@ -24,47 +24,43 @@ import (
2424
2525// ParseCommitWithSignature check if signature is good against keystore.
2626func 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
4733 }
4834 }
49-
5035 return ParseCommitWithSignatureCommitter (ctx , c , committer )
5136}
5237
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.
5341func 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
5543 if c .Signature == nil {
5644 return & asymkey_model.CommitVerification {
5745 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 ,
6055 }
6156 }
62-
63- // If this a SSH signature handle it differently
6457 if strings .HasPrefix (c .Signature .Signature , "-----BEGIN SSH SIGNATURE-----" ) {
65- return ParseCommitWithSSHSignature (ctx , c , committer )
58+ return parseCommitWithSSHSignature (ctx , c , committer )
6659 }
60+ return parseCommitWithGPGSignature (ctx , c , committer )
61+ }
6762
63+ func parseCommitWithGPGSignature (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
6864 // Parsing signature
6965 sig , err := asymkey_model .ExtractSignature (c .Signature .Signature )
7066 if err != nil { // Skipping failed to extract sign
@@ -165,7 +161,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
165161 }
166162 if err := gpgSettings .LoadPublicKeyContent (); err != nil {
167163 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 {
169165 if commitVerification .Reason == asymkey_model .BadSignature {
170166 defaultReason = asymkey_model .BadSignature
171167 } else {
@@ -180,7 +176,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
180176 } else if defaultGPGSettings == nil {
181177 log .Warn ("Unable to get defaultGPGSettings for unattached commit: %s" , c .ID .String ())
182178 } 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 {
184180 if commitVerification .Reason == asymkey_model .BadSignature {
185181 defaultReason = asymkey_model .BadSignature
186182 } else {
@@ -295,7 +291,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
295291 }
296292}
297293
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 {
299295 // First try to find the key in the db
300296 if commitVerification := HashAndVerifyForKeyID (ctx , sig , payload , committer , gpgSettings .KeyID , gpgSettings .Name , gpgSettings .Email ); commitVerification != nil {
301297 return commitVerification
@@ -375,8 +371,8 @@ func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, sign
375371 return verifySSHCommitVerification (c .Signature .Signature , c .Signature .Payload , sshPubKey , committerUser , signerUser , committerGitEmail )
376372}
377373
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 {
380376 // Now try to associate the signature with the committer, if present
381377 if committerUser .ID != 0 {
382378 keys , err := db .Find [asymkey_model.PublicKey ](ctx , asymkey_model.FindPublicKeyOptions {
0 commit comments