fix(core): add validation to prevent empty label values in Labels task (#11273)

part of #11227

---------

Co-authored-by: harshinfomaticae <harsh.thakare@infomaticae.co.in>
Co-authored-by: Miloš Paunović <paun992@hotmail.com>
This commit is contained in:
HARSH THAKARE
2025-09-16 13:56:46 +05:30
committed by GitHub
parent 700c6de411
commit e1d2c30e54
2 changed files with 42 additions and 3 deletions

View File

@@ -127,9 +127,24 @@ public class Labels extends Task implements ExecutionUpdatableTask {
}
// check for system labels: none can be passed at runtime
Optional<Map.Entry<String, String>> first = labelsAsMap.entrySet().stream().filter(entry -> entry.getKey().startsWith(SYSTEM_PREFIX)).findFirst();
if (first.isPresent()) {
throw new IllegalArgumentException("System labels can only be set by Kestra itself, offending label: " + first.get().getKey() + "=" + first.get().getValue());
Optional<Map.Entry<String, String>> systemLabel = labelsAsMap.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(SYSTEM_PREFIX))
.findFirst();
if (systemLabel.isPresent()) {
throw new IllegalArgumentException(
"System labels can only be set by Kestra itself, offending label: " +
systemLabel.get().getKey() + "=" + systemLabel.get().getValue()
);
}
// check for empty label values
Optional<Map.Entry<String, String>> emptyValue = labelsAsMap.entrySet().stream()
.filter(entry -> entry.getValue().isEmpty())
.findFirst();
if (emptyValue.isPresent()) {
throw new IllegalArgumentException(
"Label values cannot be empty, offending label: " + emptyValue.get().getKey()
);
}
Map<String, String> newLabels = ListUtils.emptyOnNull(execution.getLabels()).stream()

View File

@@ -10,6 +10,10 @@ import io.kestra.core.runners.RunContextFactory;
import io.kestra.plugin.core.trigger.Schedule;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import io.kestra.plugin.core.execution.Labels;
import io.kestra.core.models.executions.Execution;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Collections;
import java.util.List;
@@ -79,4 +83,24 @@ class LabelServiceTest {
assertTrue(LabelService.containsAll(List.of(new Label("key1", "value1")), List.of(new Label("key1", "value1"))));
assertTrue(LabelService.containsAll(List.of(new Label("key1", "value1"), new Label("key2", "value2")), List.of(new Label("key1", "value1"))));
}
@Test
void shouldThrowExceptionOnEmptyLabelValueInLabelsTask() throws Exception {
Labels task = Labels.builder()
.id("test")
.type(Labels.class.getName())
.labels(Map.of("invalidLabel", "")) // empty value
.build();
RunContext runContext = runContextFactory.of();
Execution execution = Execution.builder()
.id("execId")
.namespace("test.ns")
.build();
assertThatThrownBy(() -> task.update(execution, runContext))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Label values cannot be empty");
}
}