@@ -18,7 +18,7 @@ import (
1818)
1919
2020// ParseCommitWithSignature check if signature is good against keystore.
21- func ParseCommitWithSignature (ctx context.Context , c * git.Commit ) * asymkey_model.CommitVerification {
21+ func ParseCommitWithSignature (ctx context.Context , c * git.Commit , keysCache map [ string ][] * asymkey_model. GPGKey ) * asymkey_model.CommitVerification {
2222 var committer * user_model.User
2323 if c .Committer != nil {
2424 var err error
@@ -42,10 +42,10 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model
4242 }
4343 }
4444
45- return ParseCommitWithSignatureCommitter (ctx , c , committer )
45+ return ParseCommitWithSignatureCommitter (ctx , c , committer , keysCache )
4646}
4747
48- func ParseCommitWithSignatureCommitter (ctx context.Context , c * git.Commit , committer * user_model.User ) * asymkey_model.CommitVerification {
48+ func ParseCommitWithSignatureCommitter (ctx context.Context , c * git.Commit , committer * user_model.User , keysCache map [ string ][] * asymkey_model. GPGKey ) * asymkey_model.CommitVerification {
4949 // If no signature just report the committer
5050 if c .Signature == nil {
5151 return & asymkey_model.CommitVerification {
@@ -82,7 +82,8 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
8282 committer ,
8383 keyID ,
8484 setting .AppName ,
85- "" ); commitVerification != nil {
85+ "" ,
86+ keysCache ); commitVerification != nil {
8687 if commitVerification .Reason == asymkey_model .BadSignature {
8788 defaultReason = asymkey_model .BadSignature
8889 } else {
@@ -160,7 +161,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
160161 }
161162 if err := gpgSettings .LoadPublicKeyContent (); err != nil {
162163 log .Error ("Error getting default signing key: %s %v" , gpgSettings .KeyID , err )
163- } 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 , keysCache ); commitVerification != nil {
164165 if commitVerification .Reason == asymkey_model .BadSignature {
165166 defaultReason = asymkey_model .BadSignature
166167 } else {
@@ -175,7 +176,7 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
175176 } else if defaultGPGSettings == nil {
176177 log .Warn ("Unable to get defaultGPGSettings for unattached commit: %s" , c .ID .String ())
177178 } else if defaultGPGSettings .Sign {
178- 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 , keysCache ); commitVerification != nil {
179180 if commitVerification .Reason == asymkey_model .BadSignature {
180181 defaultReason = asymkey_model .BadSignature
181182 } else {
@@ -225,39 +226,48 @@ func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GP
225226 return false , email
226227}
227228
228- func HashAndVerifyForKeyID (ctx context.Context , sig * packet.Signature , payload string , committer * user_model.User , keyID , name , email string ) * asymkey_model.CommitVerification {
229+ func HashAndVerifyForKeyID (ctx context.Context , sig * packet.Signature , payload string , committer * user_model.User , keyID , name , email string , keysCache map [ string ][] * asymkey_model. GPGKey ) * asymkey_model.CommitVerification {
229230 if keyID == "" {
230231 return nil
231232 }
232- keys , err := db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
233- KeyID : keyID ,
234- IncludeSubKeys : true ,
235- })
236- if err != nil {
237- log .Error ("GetGPGKeysByKeyID: %v" , err )
238- return & asymkey_model.CommitVerification {
239- CommittingUser : committer ,
240- Verified : false ,
241- Reason : "gpg.error.failed_retrieval_gpg_keys" ,
233+ var err error
234+ keys , ok := keysCache [keyID ]
235+ if ! ok {
236+ keys , err = db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
237+ KeyID : keyID ,
238+ IncludeSubKeys : true ,
239+ })
240+ if err != nil {
241+ log .Error ("GetGPGKeysByKeyID: %v" , err )
242+ return & asymkey_model.CommitVerification {
243+ CommittingUser : committer ,
244+ Verified : false ,
245+ Reason : "gpg.error.failed_retrieval_gpg_keys" ,
246+ }
242247 }
248+ keysCache [keyID ] = keys
243249 }
244250 if len (keys ) == 0 {
245251 return nil
246252 }
247253 for _ , key := range keys {
248254 var primaryKeys []* asymkey_model.GPGKey
249255 if key .PrimaryKeyID != "" {
250- primaryKeys , err = db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
251- KeyID : key .PrimaryKeyID ,
252- IncludeSubKeys : true ,
253- })
254- if err != nil {
255- log .Error ("GetGPGKeysByKeyID: %v" , err )
256- return & asymkey_model.CommitVerification {
257- CommittingUser : committer ,
258- Verified : false ,
259- Reason : "gpg.error.failed_retrieval_gpg_keys" ,
256+ primaryKeys , ok = keysCache [keyID ]
257+ if ! ok {
258+ primaryKeys , err = db .Find [asymkey_model.GPGKey ](ctx , asymkey_model.FindGPGKeyOptions {
259+ KeyID : key .PrimaryKeyID ,
260+ IncludeSubKeys : true ,
261+ })
262+ if err != nil {
263+ log .Error ("GetGPGKeysByKeyID: %v" , err )
264+ return & asymkey_model.CommitVerification {
265+ CommittingUser : committer ,
266+ Verified : false ,
267+ Reason : "gpg.error.failed_retrieval_gpg_keys" ,
268+ }
260269 }
270+ keysCache [keyID ] = primaryKeys
261271 }
262272 }
263273
@@ -297,9 +307,9 @@ func HashAndVerifyForKeyID(ctx context.Context, sig *packet.Signature, payload s
297307 }
298308}
299309
300- func VerifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string ) * asymkey_model.CommitVerification {
310+ func VerifyWithGPGSettings (ctx context.Context , gpgSettings * git.GPGSettings , sig * packet.Signature , payload string , committer * user_model.User , keyID string , keysCache map [ string ][] * asymkey_model. GPGKey ) * asymkey_model.CommitVerification {
301311 // First try to find the key in the db
302- if commitVerification := HashAndVerifyForKeyID (ctx , sig , payload , committer , gpgSettings .KeyID , gpgSettings .Name , gpgSettings .Email ); commitVerification != nil {
312+ if commitVerification := HashAndVerifyForKeyID (ctx , sig , payload , committer , gpgSettings .KeyID , gpgSettings .Name , gpgSettings .Email , keysCache ); commitVerification != nil {
303313 return commitVerification
304314 }
305315
0 commit comments