Skip to content

Commit 8f5df3b

Browse files
committed
fix: replace unsafe dictionary indexers in ValidateCAConnectionInfo and ValidateProductInfo
Direct Dictionary.get_Item calls throw KeyNotFoundException when the gateway does not populate all parameter keys before calling validation (observed on PUT/POST config/configuration). Replace with TryGetValue throughout. Also relaxes the ValidateProductInfo RoleName check: RoleName is optional since the Enroll path already falls back to ProductID when RoleName is absent, so the validator no longer rejects configs that omit it.
1 parent e973ff6 commit 8f5df3b

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

hashicorp-vault-cagateway/HashicorpVaultCAConnector.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,23 @@ public async Task ValidateCAConnectionInfo(Dictionary<string, object> connection
340340
List<string> errors = new List<string>();
341341

342342
// then, we make sure required fields are defined..
343-
if (string.IsNullOrEmpty(connectionInfo[Constants.CAConfig.HOST] as string))
343+
connectionInfo.TryGetValue(Constants.CAConfig.HOST, out var hostVal);
344+
if (string.IsNullOrEmpty(hostVal as string))
344345
{
345346
errors.Add($"The '{Constants.CAConfig.HOST}' is required.");
346347
}
347348

348-
if (string.IsNullOrEmpty(connectionInfo[Constants.CAConfig.MOUNTPOINT] as string))
349+
connectionInfo.TryGetValue(Constants.CAConfig.MOUNTPOINT, out var mountVal);
350+
if (string.IsNullOrEmpty(mountVal as string))
349351
{
350352
errors.Add($"The '{Constants.CAConfig.MOUNTPOINT}' is required.");
351353
}
352354

353355
// make sure an authentication mechanism is defined (either certificate or token)
354-
var token = connectionInfo[Constants.CAConfig.TOKEN] as string;
355-
var cert = connectionInfo[Constants.CAConfig.CLIENTCERT] as string;
356+
connectionInfo.TryGetValue(Constants.CAConfig.TOKEN, out var tokenVal);
357+
connectionInfo.TryGetValue(Constants.CAConfig.CLIENTCERT, out var certVal);
358+
var token = tokenVal as string;
359+
var cert = certVal as string;
356360

357361
if (string.IsNullOrEmpty(token) && string.IsNullOrEmpty(cert))
358362
{
@@ -439,10 +443,11 @@ public Task ValidateProductInfo(EnrollmentProductInfo productInfo, Dictionary<st
439443
logger.LogError(LogHandler.FlattenException(ex));
440444
throw;
441445
}
442-
// make sure Role Name is present in the template config
443-
if (string.IsNullOrEmpty(productInfo.ProductParameters[Constants.TemplateConfig.ROLENAME] as string))
446+
// RoleName is optional — if absent or empty, ProductID is used as the role name (see Enroll).
447+
productInfo.ProductParameters.TryGetValue(Constants.TemplateConfig.ROLENAME, out var roleNameVal);
448+
if (roleNameVal != null && string.IsNullOrEmpty(roleNameVal as string))
444449
{
445-
errors.Add($"The '{Constants.TemplateConfig.ROLENAME}' is required.");
450+
errors.Add($"The '{Constants.TemplateConfig.ROLENAME}' must not be empty if provided.");
446451
}
447452

448453
// if any errors, throw

0 commit comments

Comments
 (0)