Runway 1.1.0
This commit is contained in:
1 parent
ce217fc952
commit
07d5bd7e9a
223 files changed
+21147
-255
No files matched your search
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>mx.quest</groupId>
|
||||
<artifactId>runway</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.1.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>runway-spi-testkit</artifactId>
|
||||
|
||||
+15
-1
@@ -17,7 +17,8 @@ import mx.quest.runway.spi.service.PluginSettings;
|
||||
* FakePluginSettings settings = new FakePluginSettings()
|
||||
* .defaults(Map.of("enabled", true, "limit", 20))
|
||||
* .site("acme", Map.of("limit", 50))
|
||||
* .secret("acme", "smtpPassword", "s3cret");
|
||||
* .secret("acme", "smtpPassword", "s3cret")
|
||||
* .appSecret("password", "app-s3cret");
|
||||
* MyWidget widget = new MyWidget(settings);
|
||||
* }</pre>
|
||||
*
|
||||
@@ -32,6 +33,7 @@ public final class FakePluginSettings implements PluginSettings {
|
||||
private volatile Map<String, Object> defaults = Map.of();
|
||||
private final Map<String, Map<String, Object>> sites = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> secrets = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> appSecrets = new ConcurrentHashMap<>();
|
||||
|
||||
/** The declared {@code siteSettings:} defaults (replaces any set before). */
|
||||
public FakePluginSettings defaults(Map<String, ?> declaredDefaults) {
|
||||
@@ -51,6 +53,12 @@ public final class FakePluginSettings implements PluginSettings {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** An app-level secret's clear value, as the running environment would hand it out. */
|
||||
public FakePluginSettings appSecret(String key, String clearValue) {
|
||||
appSecrets.put(key, clearValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SettingsView site(String siteId) {
|
||||
Map<String, Object> values = new LinkedHashMap<>(defaults);
|
||||
@@ -69,6 +77,12 @@ public final class FakePluginSettings implements PluginSettings {
|
||||
return value == null || value.isBlank() ? Optional.empty() : Optional.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> appSecret(String key) {
|
||||
String value = key == null ? null : appSecrets.get(key);
|
||||
return value == null || value.isBlank() ? Optional.empty() : Optional.of(value);
|
||||
}
|
||||
|
||||
private static String requireSafe(String siteId) {
|
||||
if (siteId == null || siteId.equals(".") || siteId.equals("..") || !SAFE_ID.matcher(siteId).matches()) {
|
||||
throw new IllegalArgumentException("unsafe site id: " + siteId);
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
@@ -65,10 +66,17 @@ public final class TempDirSiteFiles implements PluginSiteFiles {
|
||||
@Override
|
||||
public Optional<String> readText(String siteId, String relPath) throws IOException {
|
||||
Path file = resolve(siteId, relPath, false);
|
||||
if (!Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
|
||||
BasicFileAttributes attrs;
|
||||
try {
|
||||
attrs = Files.readAttributes(file, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
} catch (NoSuchFileException absent) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (Files.size(file) > maxBytes) {
|
||||
// As the host: a folder that cannot be searched throws; only a file certainly not there reads as empty.
|
||||
if (!attrs.isRegularFile()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (attrs.size() > maxBytes) {
|
||||
throw new IOException("'" + relPath + "' is larger than the " + maxBytes + "-byte limit");
|
||||
}
|
||||
try (InputStream in = Files.newInputStream(file, LinkOption.NOFOLLOW_LINKS)) {
|
||||
@@ -142,7 +150,8 @@ public final class TempDirSiteFiles implements PluginSiteFiles {
|
||||
@Override
|
||||
public List<FileEntry> list(String siteId, String relDir) throws IOException {
|
||||
Path dir = resolve(siteId, relDir, true);
|
||||
if (!Files.isDirectory(dir, LinkOption.NOFOLLOW_LINKS)) {
|
||||
Optional<FileEntry> self = entryOf(dir); // a folder that cannot be searched throws, as in Runway
|
||||
if (self.isEmpty() || !self.get().directory()) {
|
||||
return List.of();
|
||||
}
|
||||
List<FileEntry> entries = new ArrayList<>();
|
||||
@@ -161,10 +170,12 @@ public final class TempDirSiteFiles implements PluginSiteFiles {
|
||||
}
|
||||
|
||||
private static Optional<FileEntry> entryOf(Path path) throws IOException {
|
||||
if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
|
||||
BasicFileAttributes attrs;
|
||||
try {
|
||||
attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
} catch (NoSuchFileException absent) {
|
||||
return Optional.empty();
|
||||
}
|
||||
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||
if (attrs.isSymbolicLink() || !(attrs.isRegularFile() || attrs.isDirectory())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
+9
@@ -47,4 +47,13 @@ class FakePluginSettingsTest {
|
||||
assertThrows(IllegalArgumentException.class, () -> settings.secret(bad, "k"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void appLevelSecretsAreReturnedInClearAndBlankOnesAreAbsent() {
|
||||
FakePluginSettings settings = new FakePluginSettings().appSecret("password", "s3cret").appSecret("blank", " ");
|
||||
assertEquals(Optional.of("s3cret"), settings.appSecret("password"));
|
||||
assertEquals(Optional.empty(), settings.appSecret("blank"));
|
||||
assertEquals(Optional.empty(), settings.appSecret("other"));
|
||||
assertEquals(Optional.empty(), settings.appSecret(null));
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,26 @@ class K2FakesTest {
|
||||
assertFalse(files.deleteTree("acme", "nothing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tempDirSiteFilesTellsCannotSearchFromAbsentLikeTheKernel() throws IOException {
|
||||
TempDirSiteFiles files = new TempDirSiteFiles(sites, "contact", 32);
|
||||
Files.createDirectories(sites.resolve("acme"));
|
||||
files.writeText("acme", "dir/a.txt", "x");
|
||||
Path dir = sites.resolve("acme").resolve("plugins").resolve("contact").resolve("dir");
|
||||
var before = Files.getPosixFilePermissions(dir);
|
||||
Files.setPosixFilePermissions(dir, java.util.Set.of());
|
||||
try {
|
||||
org.junit.jupiter.api.Assumptions.assumeFalse(Files.isReadable(dir.resolve("a.txt")),
|
||||
"running as a user that ignores permissions");
|
||||
assertThrows(IOException.class, () -> files.stat("acme", "dir/a.txt"));
|
||||
assertThrows(IOException.class, () -> files.list("acme", "dir"));
|
||||
assertEquals(Optional.empty(), files.stat("acme", "nope"));
|
||||
assertEquals(List.of(), files.list("acme", "nope"));
|
||||
} finally {
|
||||
Files.setPosixFilePermissions(dir, before);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void fakeSiteServicesBehaveLikeTheKernel() {
|
||||
SiteView acme = new SiteView("acme", "Acme", Locale.ENGLISH, List.of(Locale.ENGLISH), null);
|
||||
|
||||
Reference in new issue
Block a user