Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,50 +95,4 @@ private static boolean hasEmptyValueFrom(EnvVar envVar) {
&& envVar.getValueFrom().getFieldRef() == null
&& envVar.getValueFrom().getResourceFieldRef() == null;
}

/**
* Validate that environment variables are properly configured.
*
* @param envVars List of environment variables to validate
* @return true if all variables are valid
*/
public static boolean validateEnvVars(List<EnvVar> envVars) {
if (envVars == null) {
return true;
}

for (EnvVar envVar : envVars) {
if (!validateEnvVar(envVar)) {
return false;
}
}
return true;
}

/**
* Validate a single environment variable.
*
* @param envVar Environment variable to validate
* @return true if variable is valid
*/
private static boolean validateEnvVar(EnvVar envVar) {
if (envVar == null || envVar.getName() == null || envVar.getName().isEmpty()) {
LOG.error("Invalid env var: missing name");
return false;
}

// Must have either value or valueFrom, not both
if (envVar.getValue() != null && envVar.getValueFrom() != null) {
LOG.error("Invalid env var {}: cannot have both value and valueFrom", envVar.getName());
return false;
}

// If valueFrom exists, it must have at least one valid reference
if (envVar.getValueFrom() != null && hasEmptyValueFrom(envVar)) {
LOG.error("Invalid env var {}: empty valueFrom", envVar.getName());
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,65 +159,6 @@ void testSanitizeEnvVarsWithMixedTypes() {
assertNotNull(result.get(2).getValueFrom().getConfigMapKeyRef());
}

@Test
void testValidateEnvVarsWithValidVars() {
List<EnvVar> envVars =
Arrays.asList(
new EnvVarBuilder().withName("VAR1").withValue("value1").build(),
new EnvVarBuilder()
.withName("VAR2")
.withValueFrom(
new EnvVarSourceBuilder()
.withConfigMapKeyRef(
new ConfigMapKeySelectorBuilder()
.withName("config")
.withKey("key")
.build())
.build())
.build());

assertTrue(EnvVarUtils.validateEnvVars(envVars));
}

@Test
void testValidateEnvVarsWithEmptyValueFrom() {
List<EnvVar> envVars =
List.of(
new EnvVarBuilder()
.withName("INVALID")
.withValueFrom(new EnvVarSource()) // Empty valueFrom is invalid
.build());

assertFalse(EnvVarUtils.validateEnvVars(envVars));
}

@Test
void testValidateEnvVarsWithBothValueAndValueFrom() {
List<EnvVar> envVars =
List.of(
new EnvVarBuilder()
.withName("INVALID")
.withValue("value")
.withValueFrom(
new EnvVarSourceBuilder()
.withConfigMapKeyRef(
new ConfigMapKeySelectorBuilder()
.withName("config")
.withKey("key")
.build())
.build())
.build());

assertFalse(EnvVarUtils.validateEnvVars(envVars));
}

@Test
void testValidateEnvVarsWithMissingName() {
List<EnvVar> envVars = List.of(new EnvVarBuilder().withValue("value").build());

assertFalse(EnvVarUtils.validateEnvVars(envVars));
}

@Test
void testRealWorldScenarioFromFailedCronOMJob() {
// This recreates the exact scenario from the failed CronOMJob
Expand All @@ -242,15 +183,9 @@ void testRealWorldScenarioFromFailedCronOMJob() {
.withValue("openmetadata-pipelines-test")
.build());

// Before sanitization - validation should fail
assertFalse(EnvVarUtils.validateEnvVars(envVars), "Original env vars should be invalid");

// After sanitization
List<EnvVar> sanitized = EnvVarUtils.sanitizeEnvVars(envVars);

// Should pass validation now
assertTrue(EnvVarUtils.validateEnvVars(sanitized), "Sanitized env vars should be valid");

// Check that all vars are present
assertEquals(6, sanitized.size());

Expand Down
Loading