Skip to content
Draft
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 @@ -42,15 +42,24 @@ public String getMode() {
}

/**
* Sets the artifact store mode; blank or null values fall back to the
* disk-backed default, and any value other than {@link #MODE_IN_MEMORY}
* selects the disk-backed store.
* Sets the artifact store mode. Blank or null values fall back to the
* disk-backed default; unsupported non-blank modes are rejected so a
* deployment cannot silently persist artifacts to a different store than
* the operator intended.
*
* @param mode artifact store mode
* @throws IllegalArgumentException when a non-blank mode is unsupported
*/
public void setMode(String mode) {
String sanitized = sanitize(mode);
this.mode = sanitized.isEmpty() ? MODE_FILESYSTEM : sanitized;
if (sanitized.isEmpty()) {
this.mode = MODE_FILESYSTEM;
return;
}
if (!MODE_FILESYSTEM.equals(sanitized) && !MODE_IN_MEMORY.equals(sanitized)) {
throw new IllegalArgumentException("unsupported artifact store mode");
}
this.mode = sanitized;
}

/**
Expand Down Expand Up @@ -86,6 +95,9 @@ private static String sanitize(String value) {
if (value == null) {
return "";
}
return value.replace("\u0000", "").strip().toLowerCase(Locale.ROOT);
if (value.indexOf('\u0000') >= 0) {
throw new IllegalArgumentException("unsupported artifact store mode: contains NUL");
}
return value.strip().toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
Expand All @@ -27,6 +28,17 @@ void setModeNormalizesCaseAndWhitespace() {
assertTrue(properties.isInMemoryMode());
}

@Test
void setModeAcceptsExplicitFilesystemMode() {
ArtifactStoreProperties properties = new ArtifactStoreProperties();
properties.setMode(ArtifactStoreProperties.MODE_IN_MEMORY);

properties.setMode(" FILESYSTEM ");

assertEquals(ArtifactStoreProperties.MODE_FILESYSTEM, properties.getMode());
assertFalse(properties.isInMemoryMode());
}

@Test
void setModeFallsBackToFilesystemForNullAndBlankValues() {
ArtifactStoreProperties properties = new ArtifactStoreProperties();
Expand All @@ -39,13 +51,28 @@ void setModeFallsBackToFilesystemForNullAndBlankValues() {
}

@Test
void setModeKeepsUnknownValuesWithoutSelectingInMemory() {
void setModeRejectsUnknownStorageModes() {
ArtifactStoreProperties properties = new ArtifactStoreProperties();

properties.setMode("cloud");
IllegalArgumentException error = assertThrows(
IllegalArgumentException.class,
() -> properties.setMode("cloud://tenant-secret")
);

assertEquals("cloud", properties.getMode());
assertFalse(properties.isInMemoryMode());
assertEquals("unsupported artifact store mode", error.getMessage());
assertEquals(ArtifactStoreProperties.MODE_FILESYSTEM, properties.getMode());
}

@Test
void setModeRejectsNulCorruptedKnownMode() {
ArtifactStoreProperties properties = new ArtifactStoreProperties();

assertThrows(
IllegalArgumentException.class,
() -> properties.setMode("file\u0000system")
);

assertEquals(ArtifactStoreProperties.MODE_FILESYSTEM, properties.getMode());
}

@Test
Expand Down
Loading