mirror of
https://github.com/kestra-io/kestra.git
synced 2025-12-26 14:00:23 -05:00
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:
@@ -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()
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user