use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method testSomethingWorks.
public void testSomethingWorks() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
String pluginZip = createPluginUrl("fake", pluginDir);
installPlugin(pluginZip, env.v1());
assertPlugin("fake", pluginDir, env.v2());
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method assertInstallPluginFromUrl.
void assertInstallPluginFromUrl(final String pluginId, final String name, final String url, final String stagingHash, final boolean isSnapshot, final String shaExtension, final Function<byte[], String> shaCalculator, final PGPSecretKey secretKey, final BiFunction<byte[], PGPSecretKey, String> signature) throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Path pluginZip = createPlugin(name, pluginDir);
InstallPluginCommand command = new InstallPluginCommand() {
@Override
Path downloadZip(Terminal terminal, String urlString, Path tmpDir, boolean isBatch) throws IOException {
assertEquals(url, urlString);
Path downloadedPath = tmpDir.resolve("downloaded.zip");
Files.copy(pluginZip, downloadedPath);
return downloadedPath;
}
@Override
URL openUrl(String urlString) throws IOException {
if ((url + shaExtension).equals(urlString)) {
// calc sha an return file URL to it
Path shaFile = temp.apply("shas").resolve("downloaded.zip" + shaExtension);
byte[] zipbytes = Files.readAllBytes(pluginZip);
String checksum = shaCalculator.apply(zipbytes);
Files.write(shaFile, checksum.getBytes(StandardCharsets.UTF_8));
return shaFile.toUri().toURL();
} else if ((url + ".sig").equals(urlString)) {
final Path ascFile = temp.apply("sig").resolve("downloaded.zip" + ".sig");
final byte[] zipBytes = Files.readAllBytes(pluginZip);
final String asc = signature.apply(zipBytes, secretKey);
Files.write(ascFile, asc.getBytes(StandardCharsets.UTF_8));
return ascFile.toUri().toURL();
}
return null;
}
@Override
void verifySignature(Path zip, String urlString) throws IOException, PGPException {
if (InstallPluginCommand.OFFICIAL_PLUGINS.contains(name)) {
super.verifySignature(zip, urlString);
} else {
throw new UnsupportedOperationException("verify signature should not be called for unofficial plugins");
}
}
@Override
InputStream pluginZipInputStream(Path zip) throws IOException {
return new ByteArrayInputStream(Files.readAllBytes(zip));
}
@Override
String getPublicKeyId() {
return Long.toHexString(secretKey.getKeyID()).toUpperCase(Locale.ROOT);
}
@Override
InputStream getPublicKey() {
try {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final ArmoredOutputStream armored = new ArmoredOutputStream(output);
secretKey.getPublicKey().encode(armored);
armored.close();
return new ByteArrayInputStream(output.toByteArray());
} catch (final IOException e) {
throw new AssertionError(e);
}
}
@Override
boolean urlExists(Terminal terminal, String urlString) throws IOException {
return urlString.equals(url);
}
@Override
String getStagingHash() {
return stagingHash;
}
@Override
boolean isSnapshot() {
return isSnapshot;
}
@Override
void jarHellCheck(PluginInfo candidateInfo, Path candidate, Path pluginsDir, Path modulesDir) throws Exception {
// no jarhell check
}
};
installPlugin(pluginId, env.v1(), command);
assertPlugin(name, pluginDir, env.v2());
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method testBuiltinModule.
public void testBuiltinModule() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
String pluginZip = createPluginUrl("lang-painless", pluginDir);
UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
assertTrue(e.getMessage(), e.getMessage().contains("is a system module"));
assertInstallCleaned(env.v2());
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method testPluginsDirReadOnly.
public void testPluginsDirReadOnly() throws Exception {
assumeTrue("posix and filesystem", isPosix && isReal);
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
try (PosixPermissionsResetter pluginsAttrs = new PosixPermissionsResetter(env.v2().pluginsFile())) {
pluginsAttrs.setPermissions(new HashSet<>());
String pluginZip = createPluginUrl("fake", pluginDir);
IOException e = expectThrows(IOException.class, () -> installPlugin(pluginZip, env.v1()));
assertTrue(e.getMessage(), e.getMessage().contains(env.v2().pluginsFile().toString()));
}
assertInstallCleaned(env.v2());
}
use of org.opensearch.env.Environment in project OpenSearch by opensearch-project.
the class InstallPluginCommandTests method testInstallMisspelledOfficialPlugins.
public void testInstallMisspelledOfficialPlugins() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
UserException e = expectThrows(UserException.class, () -> installPlugin("analysis-smartnc", env.v1()));
assertThat(e.getMessage(), containsString("Unknown plugin analysis-smartnc, did you mean [analysis-smartcn]?"));
e = expectThrows(UserException.class, () -> installPlugin("repository", env.v1()));
assertThat(e.getMessage(), containsString("Unknown plugin repository, did you mean any of [repository-s3, repository-gcs]?"));
e = expectThrows(UserException.class, () -> installPlugin("unknown_plugin", env.v1()));
assertThat(e.getMessage(), containsString("Unknown plugin unknown_plugin"));
}
Aggregations