Skip to content

Commit 0f5a2c7

Browse files
spbsolubleclaude
andcommitted
fix(management): guard against IndexOutOfRangeException for K8SNS/K8SCluster alias parsing
HandleCreateOrUpdate (namespace case) and HandleRemove both accessed splitAlias[^2] without a bounds check. An alias without the expected '/' delimiter (e.g. a bare alias like 'testcert') produced a single-element array, causing 'Index was outside the bounds of the array' at runtime. Added Length guards before all splitAlias[^2]/splitAlias[^1] accesses in both methods for K8SNS and K8SCluster store types; returns a clear FailJob with an actionable error message describing the expected alias format. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 82ad516 commit 0f5a2c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

kubernetes-orchestrator-extension/Jobs/Management.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ private JobResult HandleCreateOrUpdate(string secretType, ManagementJobConfigura
548548
jobCertObj.Alias = config.JobCertificate.Alias;
549549
// Split alias by / and get second to last element KubeSecretType
550550
var splitAlias = jobCertObj.Alias.Split("/");
551+
if (splitAlias.Length < 2)
552+
{
553+
var invalidAliasErrMsg =
554+
"Invalid alias format for K8SNS store type. Alias pattern: `<secret_type>/<secret_name>` where `secret_type` is one of 'opaque' or 'tls' and `secret_name` is the name of the secret.";
555+
Logger.LogError(invalidAliasErrMsg);
556+
Logger.LogInformation("End MANAGEMENT job " + config.JobId + " " + invalidAliasErrMsg + " Failed!");
557+
return FailJob(invalidAliasErrMsg, config.JobHistoryId);
558+
}
551559
KubeSecretType = splitAlias[^2];
552560
KubeSecretName = splitAlias[^1];
553561
Logger.LogDebug("Handling managment add job for K8SNS secret type '" + KubeSecretType + "(" + jobCertObj.Alias + ")'...");
@@ -656,6 +664,12 @@ private JobResult HandleRemove(string secretType, ManagementJobConfiguration con
656664
var splitAlias = certAlias.Split("/");
657665
if (Capability.Contains("K8SNS"))
658666
{
667+
if (splitAlias.Length < 2)
668+
{
669+
var errMsg = $"Invalid alias format for K8SNS store type. Expected pattern: 'secrets/<tls|opaque>/<secret_name>' but got '{certAlias}'";
670+
Logger.LogError(errMsg);
671+
return FailJob(errMsg, config.JobHistoryId);
672+
}
659673
// Split alias by / and get second to last element KubeSecretType
660674
KubeSecretType = splitAlias[^2];
661675
KubeSecretName = splitAlias[^1];
@@ -666,6 +680,12 @@ private JobResult HandleRemove(string secretType, ManagementJobConfiguration con
666680
}
667681
else if (Capability.Contains("K8SCluster"))
668682
{
683+
if (splitAlias.Length < 3)
684+
{
685+
var errMsg = $"Invalid alias format for K8SCluster store type. Expected pattern: '<namespace>/secrets/<tls|opaque>/<secret_name>' but got '{certAlias}'";
686+
Logger.LogError(errMsg);
687+
return FailJob(errMsg, config.JobHistoryId);
688+
}
669689
KubeSecretType = splitAlias[^2];
670690
KubeSecretName = splitAlias[^1];
671691
KubeNamespace = splitAlias[0];

0 commit comments

Comments
 (0)