Search in sources :

Example 1 with RepositoryException

use of org.opensearch.repositories.RepositoryException in project OpenSearch by opensearch-project.

the class URLRepository method checkURL.

/**
 * Makes sure that the url is white listed or if it points to the local file system it matches one on of the root path in path.repo
 */
private URL checkURL(URL url) {
    String protocol = url.getProtocol();
    if (protocol == null) {
        throw new RepositoryException(getMetadata().name(), "unknown url protocol from URL [" + url + "]");
    }
    for (String supportedProtocol : supportedProtocols) {
        if (supportedProtocol.equals(protocol)) {
            try {
                if (URIPattern.match(urlAllowList, url.toURI())) {
                    // URL matches white list - no additional processing is needed
                    return url;
                }
            } catch (URISyntaxException ex) {
                logger.warn("cannot parse the specified url [{}]", url);
                throw new RepositoryException(getMetadata().name(), "cannot parse the specified url [" + url + "]");
            }
            // We didn't match white list - try to resolve against path.repo
            URL normalizedUrl = environment.resolveRepoURL(url);
            if (normalizedUrl == null) {
                String logMessage = "The specified url [{}] doesn't start with any repository paths specified by the " + "path.repo setting or by {} setting: [{}] ";
                logger.warn(logMessage, url, ALLOWED_URLS_SETTING.getKey(), environment.repoFiles());
                String exceptionMessage = "file url [" + url + "] doesn't match any of the locations specified by path.repo or " + ALLOWED_URLS_SETTING.getKey();
                throw new RepositoryException(getMetadata().name(), exceptionMessage);
            }
            return normalizedUrl;
        }
    }
    throw new RepositoryException(getMetadata().name(), "unsupported url protocol [" + protocol + "] from URL [" + url + "]");
}
Also used : RepositoryException(org.opensearch.repositories.RepositoryException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL)

Example 2 with RepositoryException

use of org.opensearch.repositories.RepositoryException in project OpenSearch by opensearch-project.

the class URLRepositoryTests method testNonNormalizedUrl.

public void testNonNormalizedUrl() throws IOException {
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(URLRepository.ALLOWED_URLS_SETTING.getKey(), "file:/tmp/").put(URLRepository.REPOSITORIES_URL_SETTING.getKey(), "file:/var/").build();
    RepositoryMetadata repositoryMetadata = new RepositoryMetadata("url", URLRepository.TYPE, baseSettings);
    final URLRepository repository = createRepository(baseSettings, repositoryMetadata);
    repository.start();
    try {
        repository.blobContainer();
        fail("RepositoryException should have been thrown.");
    } catch (RepositoryException e) {
        assertEquals("[url] file url [file:/var/] doesn't match any of the locations " + "specified by path.repo or repositories.url.allowed_urls", e.getMessage());
    }
}
Also used : RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) RepositoryException(org.opensearch.repositories.RepositoryException) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings)

Example 3 with RepositoryException

use of org.opensearch.repositories.RepositoryException in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testSnapshotSucceedsAfterSnapshotFailure.

public void testSnapshotSucceedsAfterSnapshotFailure() throws Exception {
    // TODO: Fix repo cleanup logic to handle these leaked snap-file and only exclude test-repo (the mock repo) here.
    disableRepoConsistencyCheck("This test uses a purposely broken repository implementation that results in leaking snap-{uuid}.dat files");
    logger.info("--> creating repository");
    final Path repoPath = randomRepoPath();
    final Client client = client();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("mock").setVerify(false).setSettings(Settings.builder().put("location", repoPath).put("random_control_io_exception_rate", randomIntBetween(5, 20) / 100f).put("random", randomAlphaOfLength(10))));
    assertAcked(prepareCreate("test-idx").setSettings(// that caused problems with writing subsequent snapshots if they happened to be lingering in the repository
    indexSettingsNoReplicas(1)));
    ensureGreen();
    final int numDocs = randomIntBetween(1, 5);
    indexRandomDocs("test-idx", numDocs);
    logger.info("--> snapshot with potential I/O failures");
    try {
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
        if (createSnapshotResponse.getSnapshotInfo().totalShards() != createSnapshotResponse.getSnapshotInfo().successfulShards()) {
            assertThat(getFailureCount("test-repo"), greaterThan(0L));
            assertThat(createSnapshotResponse.getSnapshotInfo().shardFailures().size(), greaterThan(0));
            for (SnapshotShardFailure shardFailure : createSnapshotResponse.getSnapshotInfo().shardFailures()) {
                assertThat(shardFailure.reason(), endsWith("; nested: IOException[Random IOException]"));
            }
        }
    } catch (SnapshotException | RepositoryException ex) {
        // sometimes, the snapshot will fail with a top level I/O exception
        assertThat(ExceptionsHelper.stackTrace(ex), containsString("Random IOException"));
    }
    logger.info("--> snapshot with no I/O failures");
    createRepository("test-repo-2", "mock", repoPath);
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo-2", "test-snap-2").setWaitForCompletion(true).setIndices("test-idx").get();
    assertEquals(0, createSnapshotResponse.getSnapshotInfo().failedShards());
    assertEquals(SnapshotState.SUCCESS, getSnapshot("test-repo-2", "test-snap-2").state());
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoryException(org.opensearch.repositories.RepositoryException) Client(org.opensearch.client.Client)

Example 4 with RepositoryException

use of org.opensearch.repositories.RepositoryException in project OpenSearch by opensearch-project.

the class RepositoriesIT method testMisconfiguredRepository.

public void testMisconfiguredRepository() throws Exception {
    Client client = client();
    logger.info("--> trying creating repository with incorrect settings");
    try {
        client.admin().cluster().preparePutRepository("test-repo").setType("fs").get();
        fail("Shouldn't be here");
    } catch (RepositoryException ex) {
        assertThat(ex.toString(), containsString("missing location"));
    }
    logger.info("--> trying creating fs repository with location that is not registered in path.repo setting");
    Path invalidRepoPath = createTempDir().toAbsolutePath();
    String location = invalidRepoPath.toString();
    try {
        client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", location)).get();
        fail("Shouldn't be here");
    } catch (RepositoryException ex) {
        assertThat(ex.toString(), containsString("location [" + location + "] doesn't match any of the locations specified " + "by path.repo"));
    }
}
Also used : Path(java.nio.file.Path) RepositoryException(org.opensearch.repositories.RepositoryException) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client)

Example 5 with RepositoryException

use of org.opensearch.repositories.RepositoryException in project OpenSearch by opensearch-project.

the class CorruptedBlobStoreRepositoryIT method assertRepositoryBlocked.

private void assertRepositoryBlocked(Client client, String repo, String existingSnapshot) {
    logger.info("--> try to delete snapshot");
    final RepositoryException repositoryException3 = expectThrows(RepositoryException.class, () -> client.admin().cluster().prepareDeleteSnapshot(repo, existingSnapshot).execute().actionGet());
    assertThat(repositoryException3.getMessage(), containsString("Could not read repository data because the contents of the repository do not match its expected state."));
    logger.info("--> try to create snapshot");
    final RepositoryException repositoryException4 = expectThrows(RepositoryException.class, () -> client.admin().cluster().prepareCreateSnapshot(repo, existingSnapshot).execute().actionGet());
    assertThat(repositoryException4.getMessage(), containsString("Could not read repository data because the contents of the repository do not match its expected state."));
}
Also used : RepositoryException(org.opensearch.repositories.RepositoryException)

Aggregations

RepositoryException (org.opensearch.repositories.RepositoryException)24 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)9 Settings (org.opensearch.common.settings.Settings)9 List (java.util.List)8 Set (java.util.Set)7 Executor (java.util.concurrent.Executor)7 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)7 Collection (java.util.Collection)6 Collections (java.util.Collections)6 Map (java.util.Map)6 Optional (java.util.Optional)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Consumer (java.util.function.Consumer)6 Function (java.util.function.Function)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 LogManager (org.apache.logging.log4j.LogManager)6 Logger (org.apache.logging.log4j.Logger)6 NoSuchFileException (java.nio.file.NoSuchFileException)5