@@ -24,47 +24,42 @@ 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 is nil.
53
40
func ParseCommitWithSignatureCommitter (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
54
- // If no signature just report the committer
41
+ // If no signature, just report the committer
55
42
if c .Signature == nil {
56
43
return & asymkey_model.CommitVerification {
57
44
CommittingUser : committer ,
58
- Verified : false , // Default value
59
- Reason : "gpg.error.not_signed_commit" , // Default value
45
+ Verified : false ,
46
+ Reason : "gpg.error.not_signed_commit" ,
47
+ }
48
+ }
49
+ // to support instance key, we need a fake committer user (not really needed, but legacy code accesses the committer without nil-check)
50
+ if committer == nil {
51
+ committer = & user_model.User {
52
+ Name : c .Committer .Name ,
53
+ Email : c .Committer .Email ,
60
54
}
61
55
}
62
-
63
- // If this a SSH signature handle it differently
64
56
if strings .HasPrefix (c .Signature .Signature , "-----BEGIN SSH SIGNATURE-----" ) {
65
- return ParseCommitWithSSHSignature (ctx , c , committer )
57
+ return parseCommitWithSSHSignature (ctx , c , committer )
66
58
}
59
+ return parseCommitWithGPGSignature (ctx , c , committer )
60
+ }
67
61
62
+ func parseCommitWithGPGSignature (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
68
63
// Parsing signature
69
64
sig , err := asymkey_model .ExtractSignature (c .Signature .Signature )
70
65
if err != nil { // Skipping failed to extract sign
@@ -96,7 +91,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
96
91
}
97
92
98
93
// Now try to associate the signature with the committer, if present
99
- if committer .ID != 0 {
94
+ if committer != nil && committer .ID != 0 {
100
95
keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
101
96
OwnerID : committer .ID ,
102
97
})
@@ -165,7 +160,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
165
160
}
166
161
if err := gpgSettings .LoadPublicKeyContent (); err != nil {
167
162
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 {
163
+ } else if commitVerification := verifyWithGPGSettings (ctx , & gpgSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
169
164
if commitVerification .Reason == asymkey_model .BadSignature {
170
165
defaultReason = asymkey_model .BadSignature
171
166
} else {
@@ -180,7 +175,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
180
175
} else if defaultGPGSettings == nil {
181
176
log .Warn ("Unable to get defaultGPGSettings for unattached commit: %s" , c .ID .String ())
182
177
} else if defaultGPGSettings .Sign {
183
- if commitVerification := VerifyWithGPGSettings (ctx , defaultGPGSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
178
+ if commitVerification := verifyWithGPGSettings (ctx , defaultGPGSettings , sig , c .Signature .Payload , committer , keyID ); commitVerification != nil {
184
179
if commitVerification .Reason == asymkey_model .BadSignature {
185
180
defaultReason = asymkey_model .BadSignature
186
181
} else {
@@ -295,7 +290,7 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
295
290
}
296
291
}
297
292
298
- func VerifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string ) * asymkey_model.CommitVerification {
293
+ func verifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string ) * asymkey_model.CommitVerification {
299
294
// First try to find the key in the db
300
295
if commitVerification := HashAndVerifyForKeyID (ctx , sig , payload , committer , gpgSettings .KeyID , gpgSettings .Name , gpgSettings .Email ); commitVerification != nil {
301
296
return commitVerification
@@ -375,10 +370,10 @@ func verifySSHCommitVerificationByInstanceKey(c *git.Commit, committerUser, sign
375
370
return verifySSHCommitVerification (c .Signature .Signature , c .Signature .Payload , sshPubKey , committerUser , signerUser , committerGitEmail )
376
371
}
377
372
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 {
373
+ // parseCommitWithSSHSignature check if signature is good against keystore.
374
+ func parseCommitWithSSHSignature (ctx context.Context , c * git.Commit , committerUser * user_model.User ) * asymkey_model.CommitVerification {
380
375
// Now try to associate the signature with the committer, if present
381
- if committerUser .ID != 0 {
376
+ if committerUser != nil && committerUser .ID != 0 {
382
377
keys , err := db .Find [asymkey_model.PublicKey ](ctx , asymkey_model.FindPublicKeyOptions {
383
378
OwnerID : committerUser .ID ,
384
379
NotKeytype : asymkey_model .KeyTypePrincipal ,
0 commit comments