Search in sources :

Example 6 with Environment

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());
}
Also used : Path(java.nio.file.Path) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 7 with Environment

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());
}
Also used : Path(java.nio.file.Path) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MockTerminal(org.opensearch.cli.MockTerminal) Terminal(org.opensearch.cli.Terminal) ByteArrayInputStream(java.io.ByteArrayInputStream) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment)

Example 8 with Environment

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());
}
Also used : Path(java.nio.file.Path) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 9 with Environment

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());
}
Also used : Path(java.nio.file.Path) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) PosixPermissionsResetter(org.opensearch.test.PosixPermissionsResetter)

Example 10 with Environment

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"));
}
Also used : Path(java.nio.file.Path) TestEnvironment(org.opensearch.env.TestEnvironment) Environment(org.opensearch.env.Environment) UserException(org.opensearch.cli.UserException)

Aggregations

Environment (org.opensearch.env.Environment)142 TestEnvironment (org.opensearch.env.TestEnvironment)98 Path (java.nio.file.Path)80 Settings (org.opensearch.common.settings.Settings)79 Matchers.containsString (org.hamcrest.Matchers.containsString)69 Matchers.hasToString (org.hamcrest.Matchers.hasToString)40 NodeEnvironment (org.opensearch.env.NodeEnvironment)32 IOException (java.io.IOException)27 UserException (org.opensearch.cli.UserException)23 ClusterState (org.opensearch.cluster.ClusterState)22 IndexSettings (org.opensearch.index.IndexSettings)22 MockTerminal (org.opensearch.cli.MockTerminal)19 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)19 OpenSearchException (org.opensearch.OpenSearchException)18 Map (java.util.Map)16 OptionSet (joptsimple.OptionSet)15 DiscoverySettings (org.opensearch.node.Node.DiscoverySettings)14 Version (org.opensearch.Version)13 Files (java.nio.file.Files)11 Arrays (java.util.Arrays)11