Skip to content

Commit 5c381cf

Browse files
authored
merge Release 3.1.6 to main
2 parents 4cad753 + 8cf092c commit 5c381cf

File tree

8 files changed

+129
-81
lines changed

8 files changed

+129
-81
lines changed

AzureKeyVault/AzureClient.cs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

AzureKeyVault/AzureKeyVault.csproj

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<AssemblyName>Keyfactor.Extensions.Orchestrators.AKV</AssemblyName>
66
<RootNamespace>Keyfactor.Extensions.Orchestrator.AzureKeyVault</RootNamespace>
7-
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
7+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
88
<SignAssembly>false</SignAssembly>
99
<Copyright />
1010
<PackageLicenseExpression>https://apache.org/licenses/LICENSE-2.0</PackageLicenseExpression>
@@ -18,30 +18,25 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<None Remove="C:\Users\jvanwanzeele\.nuget\packages\keyfactor.extensions.pam.utilities\1.0.2\contentFiles\any\any\Keyfactor.Extensions.Pam.Config.exe.config" />
22-
<None Remove="C:\Users\jvanwanzeele\.nuget\packages\keyfactor.extensions.pam.utilities\1.0.2\contentFiles\any\any\Keyfactor.Extensions.Pam.Utilities.dll.config" />
23-
</ItemGroup>
24-
25-
<ItemGroup>
26-
<PackageReference Include="Azure.Core" Version="1.40.0" />
27-
<PackageReference Include="Azure.Identity" Version="1.12.0" />
28-
<PackageReference Include="Azure.ResourceManager" Version="1.12.0" />
29-
<PackageReference Include="Azure.ResourceManager.KeyVault" Version="1.2.3" />
30-
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.7.3" />
31-
<PackageReference Include="Azure.Security.KeyVault.Administration" Version="4.4.0" />
32-
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.6.0" />
33-
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
34-
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
35-
<PackageReference Include="CSS.Common" Version="1.7.0" />
36-
<PackageReference Include="Keyfactor.Common" Version="2.3.7" />
37-
<PackageReference Include="Keyfactor.Extensions.Pam.Utilities" Version="1.0.2" />
21+
<PackageReference Include="Azure.Core" Version="1.44.1" />
22+
<PackageReference Include="Azure.Identity" Version="1.13.1" />
23+
<PackageReference Include="Azure.ResourceManager" Version="1.13.0" />
24+
<PackageReference Include="Azure.ResourceManager.KeyVault" Version="1.3.0" />
25+
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.9.0" />
26+
<PackageReference Include="Azure.Security.KeyVault.Administration" Version="4.5.0" />
27+
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.7.0" />
28+
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" />
29+
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.2" />
3830
<PackageReference Include="Keyfactor.Logging" Version="1.1.1" />
3931
<PackageReference Include="Keyfactor.Orchestrators.Common" Version="3.2.0" />
4032
<PackageReference Include="Keyfactor.Orchestrators.IOrchestratorJobExtensions" Version="0.7.0" />
4133
<PackageReference Include="Keyfactor.Platform.IPAMProvider" Version="1.0.0" />
4234
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
43-
<PackageReference Include="Microsoft.Identity.Client" Version="4.61.3" />
44-
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.61.3" />
35+
<PackageReference Include="Microsoft.Identity.Client" Version="4.66.1" />
36+
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.66.1" />
37+
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
38+
<PackageReference Include="System.Linq" Version="4.3.0" />
39+
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
4540
</ItemGroup>
4641

4742
<ItemGroup>

0 commit comments

Comments
 (0)