@@ -290,18 +290,24 @@ func recoverPrivateKey(ctx context.Context, client *kfc.APIClient, id int64, thu
290290 return "" , nil , nil , fmt .Errorf ("failed to recover private key for certificate" )
291291}
292292
293- func readCertificateById (ctx context.Context , cID int , client * kfc.APIClient ) (* kfc.ModelsCertificateRetrievalResponse , * http.Response , error ) {
293+ func readCertificateById (ctx context.Context , cID int , client * kfc.APIClient , collectionID int64 ) (* kfc.ModelsCertificateRetrievalResponse , * http.Response , error ) {
294294 ctx = tflog .SetField (ctx , "certificate_id" , cID )
295295 tflog .Debug (ctx , fmt .Sprintf ("Calling GET %s/Certificate/%d" , client .GetConfig ().Host , cID ))
296- clientResp , httpResp , respErr := client .CertificateApi .CertificateGetCertificate (ctx , int32 (cID )).
296+ req := client .CertificateApi .CertificateGetCertificate (ctx , int32 (cID )).
297297 IncludeMetadata (true ).
298- IncludeLocations (true ).
299- Execute ()
298+ IncludeLocations (true )
299+
300+ if collectionID > 0 {
301+ ctx = tflog .SetField (ctx , "collection_id" , collectionID )
302+ tflog .Debug (ctx , "Adding collection_id to request" )
303+ req .CollectionId (int32 (collectionID ))
304+ }
305+ clientResp , httpResp , respErr := req .Execute ()
300306 logCommandAPIResponse (ctx , httpResp , respErr )
301307
302308 if httpResp .StatusCode == http .StatusNotFound {
303309 tflog .Warn (ctx , fmt .Sprintf ("Unable to find certificate %d using Keyfactor Command certificate Id. Attempting to search as serial number." , cID ))
304- clientResp , httpResp , respErr = lookupCertificate (ctx , CertificateSNFieldName , fmt .Sprintf ("%d" , cID ), client )
310+ clientResp , httpResp , respErr = lookupCertificate (ctx , CertificateSNFieldName , fmt .Sprintf ("%d" , cID ), client , collectionID )
305311 }
306312
307313 var (
@@ -311,20 +317,20 @@ func readCertificateById(ctx context.Context, cID int, client *kfc.APIClient) (*
311317 )
312318 tp := clientResp .Thumbprint
313319 if tp != nil {
314- detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateThumbprintFieldName , tp , client )
320+ detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateThumbprintFieldName , tp , client , collectionID )
315321 } else {
316322 tflog .Warn (ctx , fmt .Sprintf ("Thumbprint for certificate %d is nil. Attempting to search as serial number." , cID ))
317323 sn := clientResp .SerialNumber
318324 if sn == nil {
319325 tflog .Warn (ctx , fmt .Sprintf ("Serial number for certificate %d is nil. Attempting to search as Issuer DN." , cID ))
320326 dn := clientResp .IssuedDN
321327 if dn .IsSet () {
322- detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateDNFieldName , dn , client )
328+ detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateDNFieldName , dn , client , collectionID )
323329 } else {
324330 tflog .Warn (ctx , fmt .Sprintf ("Unable to lookup locations, private key and metadata for %d" , cID ))
325331 }
326332 } else {
327- detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateSNFieldName , sn , client )
333+ detailedClientResp , detailedHttpResp , detailedRespErr = lookupCertificate (ctx , CertificateSNFieldName , sn , client , collectionID )
328334 }
329335 }
330336 if detailedClientResp != nil && detailedRespErr == nil {
@@ -338,11 +344,12 @@ func readCertificateById(ctx context.Context, cID int, client *kfc.APIClient) (*
338344 return clientResp , httpResp , respErr
339345}
340346
341- func lookupCertificate (ctx context.Context , fieldName string , fieldValue interface {}, client * kfc.APIClient ) (* kfc.ModelsCertificateRetrievalResponse , * http.Response , error ) {
347+ func lookupCertificate (ctx context.Context , fieldName string , fieldValue interface {}, client * kfc.APIClient , collectionId int64 ) (* kfc.ModelsCertificateRetrievalResponse , * http.Response , error ) {
342348 var (
343349 q string
344350 notFoundErr error
345351 )
352+
346353 switch fieldValue .(type ) {
347354 case string :
348355 q = fmt .Sprintf (`%s -eq "%s"` , fieldName , fieldValue .(string ))
@@ -367,12 +374,18 @@ func lookupCertificate(ctx context.Context, fieldName string, fieldValue interfa
367374 tflog .Info (ctx , fmt .Sprintf ("Looking up cert by '%s'" , fieldName ))
368375 ctx = tflog .SetField (ctx , "query_string" , q )
369376 tflog .Debug (ctx , fmt .Sprintf ("Calling GET %s/Certificate?QueryString=%s" , client .GetConfig ().Host , q ))
370- certsResp , httpResp , respErr := client .CertificateApi .CertificateQueryCertificates (ctx ).
377+ req := client .CertificateApi .CertificateQueryCertificates (ctx ).
371378 IncludeMetadata (true ).
372379 IncludeLocations (true ).
373380 IncludeHasPrivateKey (true ).
374- PqQueryString (q ).
375- Execute ()
381+ PqQueryString (q )
382+ if collectionId > 0 {
383+ ctx = tflog .SetField (ctx , "collection_id" , collectionId )
384+ tflog .Debug (ctx , fmt .Sprintf ("Adding collection_id to request" ))
385+ req .CollectionId (int32 (collectionId ))
386+ }
387+ certsResp , httpResp , respErr := req .Execute ()
388+
376389 logCommandAPIResponse (ctx , httpResp , respErr )
377390
378391 if len (certsResp ) > 0 {
0 commit comments