@@ -37,8 +37,8 @@ private Uri AzureCloudEndpoint
3737
3838 case "china" :
3939 return AzureAuthorityHosts . AzureChina ;
40- case "germany" :
41- return AzureAuthorityHosts . AzureGermany ;
40+ // case "germany":
41+ // return AzureAuthorityHosts.AzureGermany; // germany is no longer a valid azure authority host as of 2021
4242 case "government" :
4343 return AzureAuthorityHosts . AzureGovernment ;
4444 default :
@@ -79,7 +79,7 @@ private protected virtual CertificateClient CertClient
7979 {
8080 logger . LogTrace ( "Using a service principal to authenticate, generating the credentials" ) ;
8181 cred = new ClientSecretCredential ( VaultProperties . TenantId , VaultProperties . ClientId , VaultProperties . ClientSecret , new ClientSecretCredentialOptions ( ) { AuthorityHost = AzureCloudEndpoint , AdditionallyAllowedTenants = { "*" } } ) ;
82- logger . LogTrace ( "generated credentials" , cred ) ;
82+ logger . LogTrace ( "generated credentials" ) ;
8383 }
8484 _certClient = new CertificateClient ( new Uri ( VaultProperties . VaultURL ) , credential : cred ) ;
8585
@@ -106,13 +106,13 @@ internal protected virtual ArmClient getArmClient(string tenantId)
106106 }
107107 else
108108 {
109- logger . LogTrace ( "getting credentials for a service principal identity" ) ;
109+ logger . LogTrace ( $ "getting credentials for a service principal identity with id { VaultProperties . ClientId } in Azure Tenant { credentialOptions . TenantId } ") ;
110110 credential = new ClientSecretCredential ( tenantId , VaultProperties . ClientId , VaultProperties . ClientSecret , credentialOptions ) ;
111- logger . LogTrace ( "got credentials for service principal identity" , credential ) ;
111+ logger . LogTrace ( "got credentials for service principal identity" ) ;
112112 }
113113
114114 _mgmtClient = new ArmClient ( credential ) ;
115- logger . LogTrace ( "created management client" , _mgmtClient ) ;
115+ logger . LogTrace ( "created management client" ) ;
116116 return _mgmtClient ;
117117 }
118118
@@ -149,7 +149,7 @@ public virtual async Task<KeyVaultResource> CreateVault()
149149 {
150150 try
151151 {
152- logger . LogInformation ( $ "Begin create vault in Subscription { VaultProperties . SubscriptionId } with storepath = { VaultProperties . StorePath } ") ;
152+ logger . LogTrace ( $ "Begin create vault in Subscription { VaultProperties . SubscriptionId } with storepath = { VaultProperties . StorePath } ") ;
153153
154154 logger . LogTrace ( $ "getting subscription info for provided subscription id { VaultProperties . SubscriptionId } ") ;
155155
@@ -170,7 +170,7 @@ public virtual async Task<KeyVaultResource> CreateVault()
170170 }
171171 catch ( Exception ex )
172172 {
173- logger . LogError ( $ "error retrieving default Azure Location: { ex . Message } ", ex ) ;
173+ logger . LogError ( $ "error retrieving default Azure Location: { ex . Message } ") ;
174174 throw ;
175175 }
176176 }
@@ -189,10 +189,9 @@ public virtual async Task<KeyVaultResource> CreateVault()
189189 }
190190 catch ( Exception ex )
191191 {
192- logger . LogError ( "Error when trying to create Azure Keyvault" , ex ) ;
192+ logger . LogError ( $ "Error when trying to create Azure Keyvault { ex . Message } " ) ;
193193 throw ;
194194 }
195-
196195 }
197196
198197 public virtual async Task < KeyVaultCertificateWithPolicy > ImportCertificateAsync ( string certName , string contents , string pfxPassword )
@@ -228,7 +227,7 @@ public virtual async Task<KeyVaultCertificateWithPolicy> ImportCertificateAsync(
228227 }
229228 catch ( Exception ex )
230229 {
231- logger . LogError ( ex . Message ) ;
230+ logger . LogError ( $ "There was an error importing the certificate: { ex . Message } " ) ;
232231 throw ;
233232 }
234233 }
@@ -244,7 +243,7 @@ public virtual async Task<KeyVaultCertificateWithPolicy> GetCertificate(string a
244243 if ( rEx . ErrorCode == "CertificateNotFound" )
245244 {
246245 // the request was successful, the cert does not exist.
247- logger . LogTrace ( "The certificate was not found. " ) ;
246+ logger . LogTrace ( $ "The certificate with alias { alias } was not found: { rEx . Message } ") ;
248247 return null ;
249248 }
250249 }
@@ -263,38 +262,68 @@ public virtual async Task<IEnumerable<CurrentInventoryItem>> GetCertificatesAsyn
263262 AsyncPageable < CertificateProperties > inventory = null ;
264263 try
265264 {
266- logger . LogTrace ( "calling GetPropertiesOfCertificates() on the Certificate Client" , CertClient ) ;
265+ logger . LogTrace ( "calling GetPropertiesOfCertificates() on the Certificate Client" ) ;
267266 inventory = CertClient . GetPropertiesOfCertificatesAsync ( ) ;
268267
269- logger . LogTrace ( "got a response" , inventory ) ;
268+ logger . LogTrace ( $ "got a pageable response") ;
270269 }
271270 catch ( Exception ex )
272271 {
273272 logger . LogError ( $ "Error performing inventory. { ex . Message } ", ex ) ;
274273 throw ;
275274 }
276275
277- logger . LogTrace ( "retrieving each certificate from the response" ) ;
276+ logger . LogTrace ( "iterating over result pages for complete list.." ) ;
277+
278+ var fullInventoryList = new List < CertificateProperties > ( ) ;
279+ var failedCount = 0 ;
280+ Exception innerException = null ;
281+
282+ await foreach ( var cert in inventory ) {
283+ logger . LogTrace ( $ "adding cert with ID: { cert . Id } to the list.") ;
284+ fullInventoryList . Add ( cert ) ; // convert to list from pages
285+ }
286+
287+ logger . LogTrace ( $ "compiled full inventory list of { fullInventoryList . Count ( ) } certificate(s)") ;
278288
279- await foreach ( var certificate in inventory )
289+ foreach ( var certificate in fullInventoryList )
280290 {
281- logger . LogTrace ( "getting details for the individual certificate" , certificate ) ;
282- var cert = await CertClient . GetCertificateAsync ( certificate . Name ) ;
283- logger . LogTrace ( "got certificate response" , cert ) ;
291+ logger . LogTrace ( $ "getting details for the individual certificate with id: { certificate . Id } and name: { certificate . Name } ") ;
292+ try
293+ {
294+ var cert = await CertClient . GetCertificateAsync ( certificate . Name ) ;
295+ logger . LogTrace ( $ "got certificate details") ;
284296
285- inventoryItems . Add ( new CurrentInventoryItem ( )
297+ inventoryItems . Add ( new CurrentInventoryItem ( )
298+ {
299+ Alias = cert . Value . Name ,
300+ PrivateKeyEntry = true ,
301+ ItemStatus = OrchestratorInventoryItemStatus . Unknown ,
302+ UseChainLevel = true ,
303+ Certificates = new List < string > ( ) { Convert . ToBase64String ( cert . Value . Cer ) }
304+ } ) ;
305+ }
306+ catch ( Exception ex )
286307 {
287- Alias = cert . Value . Name ,
288- PrivateKeyEntry = true ,
289- ItemStatus = OrchestratorInventoryItemStatus . Unknown ,
290- UseChainLevel = true ,
291- Certificates = new string [ ] { Convert . ToBase64String ( cert . Value . Cer ) }
292- } ) ;
308+ failedCount ++ ;
309+ innerException = ex ;
310+ logger . LogError ( $ "Failed to retreive details for certificate { certificate . Name } . Exception: { ex . Message } ") ;
311+ // continuing with inventory instead of throwing, in case there's an issue with a single certificate
312+ }
313+ }
314+
315+ if ( failedCount == fullInventoryList . Count ( ) ) {
316+ throw new Exception ( "Unable to retreive details for certificates." , innerException ) ;
293317 }
318+
319+ if ( failedCount > 0 ) {
320+ logger . LogWarning ( $ "{ failedCount } of { fullInventoryList . Count ( ) } certificates were not able to be retreieved. Please review the errors.") ;
321+ }
322+
294323 return inventoryItems ;
295324 }
296325
297- public virtual async Task < ( List < string > , List < string > ) > GetVaults ( )
326+ public virtual ( List < string > , List < string > ) GetVaults ( )
298327 {
299328 var vaultNames = new List < string > ( ) ;
300329 var warnings = new List < string > ( ) ;
@@ -333,6 +362,8 @@ public virtual async Task<IEnumerable<CurrentInventoryItem>> GetCertificatesAsyn
333362 var subId = splitId [ 1 ] ;
334363 var resourceGroupName = splitId [ 3 ] ;
335364 var vaultName = splitId . Last ( ) ;
365+ var vaultStorePath = $ "{ subId } :{ resourceGroupName } :{ vaultName } ";
366+ logger . LogTrace ( $ "found keyvault, using storepath { vaultStorePath } ") ;
336367 vaultNames . Add ( $ "{ subId } :{ resourceGroupName } :{ vaultName } ") ;
337368 }
338369 }
0 commit comments