List of usage examples for org.apache.lucene.util SetOnce set
AtomicBoolean set
To view the source code for org.apache.lucene.util SetOnce set.
Click Source Link
From source file:org.elasticsearch.ingest.PipelineExecutionServiceTests.java
License:Apache License
public void testExecuteIndexPipelineExistsButFailedParsing() { when(store.get("_id")) .thenReturn(new Pipeline("_id", "stub", null, new CompoundProcessor(new AbstractProcessor("mock") { @Override/*from w w w . j a va 2 s .co m*/ public void execute(IngestDocument ingestDocument) { throw new IllegalStateException("error"); } @Override public String getType() { return null; } }))); SetOnce<Boolean> failed = new SetOnce<>(); IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").source(Collections.emptyMap()) .setPipeline("_id"); Consumer<Exception> failureHandler = (e) -> { assertThat(e.getCause().getClass(), equalTo(IllegalArgumentException.class)); assertThat(e.getCause().getCause().getClass(), equalTo(IllegalStateException.class)); assertThat(e.getCause().getCause().getMessage(), equalTo("error")); failed.set(true); }; Consumer<Boolean> completionHandler = (e) -> failed.set(false); executionService.executeIndexRequest(indexRequest, failureHandler, completionHandler); assertTrue(failed.get()); }
From source file:org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.java
License:Apache License
public void testIndicesDeletedFromRepository() throws Exception { Client client = client();//from w w w .j a va 2 s .c o m logger.info("--> creating repository"); final String repoName = "test-repo"; createAndCheckTestRepository(repoName); createIndex("test-idx-1", "test-idx-2", "test-idx-3"); ensureGreen(); logger.info("--> indexing some data"); for (int i = 0; i < 20; i++) { index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i); index("test-idx-2", "doc", Integer.toString(i), "foo", "baz" + i); index("test-idx-3", "doc", Integer.toString(i), "foo", "baz" + i); } refresh(); logger.info("--> take a snapshot"); CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() .prepareCreateSnapshot(repoName, "test-snap").setWaitForCompletion(true).get(); assertEquals(createSnapshotResponse.getSnapshotInfo().successfulShards(), createSnapshotResponse.getSnapshotInfo().totalShards()); logger.info("--> indexing more data"); for (int i = 20; i < 40; i++) { index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i); index("test-idx-2", "doc", Integer.toString(i), "foo", "baz" + i); index("test-idx-3", "doc", Integer.toString(i), "foo", "baz" + i); } logger.info("--> take another snapshot with only 2 of the 3 indices"); createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, "test-snap2") .setWaitForCompletion(true).setIndices("test-idx-1", "test-idx-2").get(); assertEquals(createSnapshotResponse.getSnapshotInfo().successfulShards(), createSnapshotResponse.getSnapshotInfo().totalShards()); logger.info("--> delete a snapshot"); assertAcked(client().admin().cluster().prepareDeleteSnapshot(repoName, "test-snap").get()); logger.info("--> verify index folder deleted from blob container"); RepositoriesService repositoriesSvc = internalCluster().getInstance(RepositoriesService.class, internalCluster().getMasterName()); ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, internalCluster().getMasterName()); BlobStoreRepository repository = (BlobStoreRepository) repositoriesSvc.repository(repoName); final SetOnce<BlobContainer> indicesBlobContainer = new SetOnce<>(); final SetOnce<RepositoryData> repositoryData = new SetOnce<>(); final CountDownLatch latch = new CountDownLatch(1); threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(() -> { indicesBlobContainer.set(repository.blobStore().blobContainer(repository.basePath().add("indices"))); repositoryData.set(repository.getRepositoryData()); latch.countDown(); }); latch.await(); for (IndexId indexId : repositoryData.get().getIndices().values()) { if (indexId.getName().equals("test-idx-3")) { assertFalse(indicesBlobContainer.get().blobExists(indexId.getId())); // deleted index } } }
From source file:org.elasticsearch.repositories.s3.S3BlobContainer.java
License:Apache License
/** * Uploads a blob using multipart upload requests. *///from w w w.java 2 s . co m void executeMultipartUpload(final S3BlobStore blobStore, final String blobName, final InputStream input, final long blobSize) throws IOException { if (blobSize > MAX_FILE_SIZE_USING_MULTIPART.getBytes()) { throw new IllegalArgumentException("Multipart upload request size [" + blobSize + "] can't be larger than " + MAX_FILE_SIZE_USING_MULTIPART); } if (blobSize < MIN_PART_SIZE_USING_MULTIPART.getBytes()) { throw new IllegalArgumentException("Multipart upload request size [" + blobSize + "] can't be smaller than " + MIN_PART_SIZE_USING_MULTIPART); } final long partSize = blobStore.bufferSizeInBytes(); final Tuple<Long, Long> multiparts = numberOfMultiparts(blobSize, partSize); if (multiparts.v1() > Integer.MAX_VALUE) { throw new IllegalArgumentException( "Too many multipart upload requests, maybe try a larger buffer size?"); } final int nbParts = multiparts.v1().intValue(); final long lastPartSize = multiparts.v2(); assert blobSize == (nbParts - 1) * partSize + lastPartSize : "blobSize does not match multipart sizes"; final SetOnce<String> uploadId = new SetOnce<>(); final String bucketName = blobStore.bucket(); boolean success = false; try { final InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, blobName); initRequest.setStorageClass(blobStore.getStorageClass()); initRequest.setCannedACL(blobStore.getCannedACL()); if (blobStore.serverSideEncryption()) { final ObjectMetadata md = new ObjectMetadata(); md.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); initRequest.setObjectMetadata(md); } uploadId.set(blobStore.client().initiateMultipartUpload(initRequest).getUploadId()); if (Strings.isEmpty(uploadId.get())) { throw new IOException("Failed to initialize multipart upload " + blobName); } final List<PartETag> parts = new ArrayList<>(); long bytesCount = 0; for (int i = 1; i <= nbParts; i++) { final UploadPartRequest uploadRequest = new UploadPartRequest(); uploadRequest.setBucketName(bucketName); uploadRequest.setKey(blobName); uploadRequest.setUploadId(uploadId.get()); uploadRequest.setPartNumber(i); uploadRequest.setInputStream(input); if (i < nbParts) { uploadRequest.setPartSize(partSize); uploadRequest.setLastPart(false); } else { uploadRequest.setPartSize(lastPartSize); uploadRequest.setLastPart(true); } bytesCount += uploadRequest.getPartSize(); final UploadPartResult uploadResponse = blobStore.client().uploadPart(uploadRequest); parts.add(uploadResponse.getPartETag()); } if (bytesCount != blobSize) { throw new IOException("Failed to execute multipart upload for [" + blobName + "], expected " + blobSize + "bytes sent but got " + bytesCount); } CompleteMultipartUploadRequest complRequest = new CompleteMultipartUploadRequest(bucketName, blobName, uploadId.get(), parts); blobStore.client().completeMultipartUpload(complRequest); success = true; } catch (AmazonClientException e) { throw new IOException("Unable to upload object [" + blobName + "] using multipart upload", e); } finally { if (success == false && Strings.hasLength(uploadId.get())) { final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(bucketName, blobName, uploadId.get()); blobStore.client().abortMultipartUpload(abortRequest); } } }
From source file:org.elasticsearch.xpack.security.action.filter.SecurityActionFilterTests.java
License:Open Source License
public void testApplyAsSystemUser() throws Exception { ActionRequest request = mock(ActionRequest.class); ActionListener listener = mock(ActionListener.class); User user = new User("username", "r1", "r2"); Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); SetOnce<Authentication> authenticationSetOnce = new SetOnce<>(); ActionFilterChain chain = (task, action, request1, listener1) -> { authenticationSetOnce.set(threadContext.getTransient(AuthenticationField.AUTHENTICATION_KEY)); };/*from w ww. j a v a2 s .c o m*/ Task task = mock(Task.class); final boolean hasExistingAuthentication = randomBoolean(); final String action = "internal:foo"; if (hasExistingAuthentication) { threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, authentication); threadContext.putHeader(AuthenticationField.AUTHENTICATION_KEY, "foo"); threadContext.putTransient(AuthorizationService.ORIGINATING_ACTION_KEY, "indices:foo"); } else { assertNull(threadContext.getTransient(AuthenticationField.AUTHENTICATION_KEY)); } doAnswer((i) -> { ActionListener callback = (ActionListener) i.getArguments()[3]; callback.onResponse(threadContext.getTransient(AuthenticationField.AUTHENTICATION_KEY)); return Void.TYPE; }).when(authcService).authenticate(eq(action), eq(request), eq(SystemUser.INSTANCE), any(ActionListener.class)); filter.apply(task, action, request, listener, chain); if (hasExistingAuthentication) { assertEquals(authentication, threadContext.getTransient(AuthenticationField.AUTHENTICATION_KEY)); } else { assertNull(threadContext.getTransient(AuthenticationField.AUTHENTICATION_KEY)); } assertNotNull(authenticationSetOnce.get()); assertNotEquals(authentication, authenticationSetOnce.get()); assertEquals(SystemUser.INSTANCE, authenticationSetOnce.get().getUser()); }
From source file:org.elasticsearch.xpack.security.audit.index.IndexAuditTrailTests.java
License:Open Source License
private SearchHit getIndexedAuditMessage(Message message) throws InterruptedException { assertNotNull("no audit message was enqueued", message); final String indexName = IndexNameResolver.resolve(IndexAuditTrailField.INDEX_NAME_PREFIX, message.timestamp, rollover); ensureYellowAndNoInitializingShards(indexName); GetSettingsResponse settingsResponse = getClient().admin().indices().prepareGetSettings(indexName).get(); assertThat(settingsResponse.getSetting(indexName, "index.number_of_shards"), is(Integer.toString(numShards))); assertThat(settingsResponse.getSetting(indexName, "index.number_of_replicas"), is(Integer.toString(numReplicas))); final SetOnce<SearchResponse> searchResponseSetOnce = new SetOnce<>(); final boolean found = awaitBusy(() -> { try {//w ww .j a v a 2s. c o m SearchResponse searchResponse = getClient().prepareSearch(indexName) .setTypes(IndexAuditTrail.DOC_TYPE).get(); if (searchResponse.getHits().getTotalHits() > 0L) { searchResponseSetOnce.set(searchResponse); return true; } } catch (Exception e) { logger.debug("caught exception while executing search", e); } return false; }); assertThat("no audit document exists!", found, is(true)); SearchResponse response = searchResponseSetOnce.get(); assertNotNull(response); assertEquals(1, response.getHits().getTotalHits()); return response.getHits().getHits()[0]; }
From source file:org.elasticsearch.xpack.security.authc.AuthenticationServiceTests.java
License:Open Source License
public void testAutheticateTransportContextAndHeader() throws Exception { User user1 = new User("username", "r1", "r2"); when(firstRealm.token(threadContext)).thenReturn(token); when(firstRealm.supports(token)).thenReturn(true); mockAuthenticate(firstRealm, token, user1); final AtomicBoolean completed = new AtomicBoolean(false); final SetOnce<Authentication> authRef = new SetOnce<>(); final SetOnce<String> authHeaderRef = new SetOnce<>(); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { service.authenticate("_action", message, SystemUser.INSTANCE, ActionListener.wrap(authentication -> { assertThat(authentication, notNullValue()); assertThat(authentication.getUser(), sameInstance(user1)); assertThreadContextContainsAuthentication(authentication); authRef.set(authentication); authHeaderRef.set(threadContext.getHeader(AuthenticationField.AUTHENTICATION_KEY)); setCompletedToTrue(completed); }, this::logAndFail)); }/*from w ww . ja v a 2 s . co m*/ assertTrue(completed.compareAndSet(true, false)); reset(firstRealm); // checking authentication from the context InternalMessage message1 = new InternalMessage(); ThreadPool threadPool1 = new TestThreadPool("testAutheticateTransportContextAndHeader1"); try { ThreadContext threadContext1 = threadPool1.getThreadContext(); service = new AuthenticationService(Settings.EMPTY, realms, auditTrail, new DefaultAuthenticationFailureHandler(), threadPool1, new AnonymousUser(Settings.EMPTY), tokenService); threadContext1.putTransient(AuthenticationField.AUTHENTICATION_KEY, authRef.get()); threadContext1.putHeader(AuthenticationField.AUTHENTICATION_KEY, authHeaderRef.get()); service.authenticate("_action", message1, SystemUser.INSTANCE, ActionListener.wrap(ctxAuth -> { assertThat(ctxAuth, sameInstance(authRef.get())); assertThat(threadContext1.getHeader(AuthenticationField.AUTHENTICATION_KEY), sameInstance(authHeaderRef.get())); setCompletedToTrue(completed); }, this::logAndFail)); assertTrue(completed.compareAndSet(true, false)); verifyZeroInteractions(firstRealm); reset(firstRealm); } finally { terminate(threadPool1); } // checking authentication from the user header ThreadPool threadPool2 = new TestThreadPool("testAutheticateTransportContextAndHeader2"); try { ThreadContext threadContext2 = threadPool2.getThreadContext(); final String header; try (ThreadContext.StoredContext ignore = threadContext2.stashContext()) { service = new AuthenticationService(Settings.EMPTY, realms, auditTrail, new DefaultAuthenticationFailureHandler(), threadPool2, new AnonymousUser(Settings.EMPTY), tokenService); threadContext2.putHeader(AuthenticationField.AUTHENTICATION_KEY, authHeaderRef.get()); BytesStreamOutput output = new BytesStreamOutput(); threadContext2.writeTo(output); StreamInput input = output.bytes().streamInput(); threadContext2 = new ThreadContext(Settings.EMPTY); threadContext2.readHeaders(input); header = threadContext2.getHeader(AuthenticationField.AUTHENTICATION_KEY); } threadPool2.getThreadContext().putHeader(AuthenticationField.AUTHENTICATION_KEY, header); service = new AuthenticationService(Settings.EMPTY, realms, auditTrail, new DefaultAuthenticationFailureHandler(), threadPool2, new AnonymousUser(Settings.EMPTY), tokenService); service.authenticate("_action", new InternalMessage(), SystemUser.INSTANCE, ActionListener.wrap(result -> { assertThat(result, notNullValue()); assertThat(result.getUser(), equalTo(user1)); setCompletedToTrue(completed); }, this::logAndFail)); assertTrue(completed.get()); verifyZeroInteractions(firstRealm); } finally { terminate(threadPool2); } }
From source file:org.elasticsearch.xpack.security.authc.support.CachingUsernamePasswordRealm.java
License:Open Source License
private void authenticateWithCache(UsernamePasswordToken token, ActionListener<AuthenticationResult> listener) { try {/*from www . j a v a 2s. c om*/ final SetOnce<User> authenticatedUser = new SetOnce<>(); final AtomicBoolean createdAndStartedFuture = new AtomicBoolean(false); final ListenableFuture<Tuple<AuthenticationResult, UserWithHash>> future = cache .computeIfAbsent(token.principal(), k -> { final ListenableFuture<Tuple<AuthenticationResult, UserWithHash>> created = new ListenableFuture<>(); if (createdAndStartedFuture.compareAndSet(false, true) == false) { throw new IllegalStateException("something else already started this. how?"); } return created; }); if (createdAndStartedFuture.get()) { doAuthenticate(token, ActionListener.wrap(result -> { if (result.isAuthenticated()) { final User user = result.getUser(); authenticatedUser.set(user); final UserWithHash userWithHash = new UserWithHash(user, token.credentials(), cacheHasher); future.onResponse(new Tuple<>(result, userWithHash)); } else { future.onResponse(new Tuple<>(result, null)); } }, future::onFailure)); } future.addListener(ActionListener.wrap(tuple -> { if (tuple != null) { final UserWithHash userWithHash = tuple.v2(); final boolean performedAuthentication = createdAndStartedFuture.get() && userWithHash != null && tuple.v2().user == authenticatedUser.get(); handleResult(future, createdAndStartedFuture.get(), performedAuthentication, token, tuple, listener); } else { handleFailure(future, createdAndStartedFuture.get(), token, new IllegalStateException("unknown error authenticating"), listener); } }, e -> handleFailure(future, createdAndStartedFuture.get(), token, e, listener)), threadPool.executor(ThreadPool.Names.GENERIC)); } catch (ExecutionException e) { listener.onResponse(AuthenticationResult.unsuccessful("", e)); } }
From source file:org.elasticsearch.xpack.security.rest.action.oauth2.RestGetTokenActionTests.java
License:Open Source License
public void testListenerHandlesExceptionProperly() { FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(); final SetOnce<RestResponse> responseSetOnce = new SetOnce<>(); RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @Override// w w w . j av a 2s. c o m public void sendResponse(RestResponse restResponse) { responseSetOnce.set(restResponse); } }; CreateTokenResponseActionListener listener = new CreateTokenResponseActionListener(restChannel, restRequest, NoOpLogger.INSTANCE); ActionRequestValidationException ve = new CreateTokenRequest(null, null, null, null, null).validate(); listener.onFailure(ve); RestResponse response = responseSetOnce.get(); assertNotNull(response); Map<String, Object> map = XContentHelper .convertToMap(response.content(), false, XContentType.fromMediaType(response.contentType())).v2(); assertThat(map, hasEntry("error", "unsupported_grant_type")); assertThat(map, hasEntry("error_description", ve.getMessage())); assertEquals(2, map.size()); assertEquals(RestStatus.BAD_REQUEST, response.status()); }
From source file:org.elasticsearch.xpack.security.rest.action.oauth2.RestGetTokenActionTests.java
License:Open Source License
public void testSendResponse() { FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(); final SetOnce<RestResponse> responseSetOnce = new SetOnce<>(); RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @Override/*ww w .ja va 2 s .c om*/ public void sendResponse(RestResponse restResponse) { responseSetOnce.set(restResponse); } }; CreateTokenResponseActionListener listener = new CreateTokenResponseActionListener(restChannel, restRequest, NoOpLogger.INSTANCE); CreateTokenResponse createTokenResponse = new CreateTokenResponse(randomAlphaOfLengthBetween(1, 256), TimeValue.timeValueHours(1L), null, randomAlphaOfLength(4)); listener.onResponse(createTokenResponse); RestResponse response = responseSetOnce.get(); assertNotNull(response); Map<String, Object> map = XContentHelper .convertToMap(response.content(), false, XContentType.fromMediaType(response.contentType())).v2(); assertEquals(RestStatus.OK, response.status()); assertThat(map, hasEntry("type", "Bearer")); assertThat(map, hasEntry("access_token", createTokenResponse.getTokenString())); assertThat(map, hasEntry("expires_in", Math.toIntExact(createTokenResponse.getExpiresIn().seconds()))); assertThat(map, hasEntry("refresh_token", createTokenResponse.getRefreshToken())); assertEquals(4, map.size()); }
From source file:org.elasticsearch.xpack.security.rest.SecurityRestFilterTests.java
License:Open Source License
public void testProcessFiltersBodyCorrectly() throws Exception { FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent( new BytesArray( "{\"password\": \"" + SecuritySettingsSourceField.TEST_PASSWORD + "\", \"foo\": \"bar\"}"), XContentType.JSON).build();/* w ww . j a v a 2 s . co m*/ when(channel.request()).thenReturn(restRequest); SetOnce<RestRequest> handlerRequest = new SetOnce<>(); restHandler = new FilteredRestHandler() { @Override public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception { handlerRequest.set(request); } @Override public Set<String> getFilteredFields() { return Collections.singleton("password"); } }; SetOnce<RestRequest> authcServiceRequest = new SetOnce<>(); doAnswer((i) -> { ActionListener callback = (ActionListener) i.getArguments()[1]; authcServiceRequest.set((RestRequest) i.getArguments()[0]); callback.onResponse(new Authentication(XPackUser.INSTANCE, new RealmRef("test", "test", "t"), null)); return Void.TYPE; }).when(authcService).authenticate(any(RestRequest.class), any(ActionListener.class)); filter = new SecurityRestFilter(licenseState, new ThreadContext(Settings.EMPTY), authcService, restHandler, false); filter.handleRequest(restRequest, channel, null); assertEquals(restRequest, handlerRequest.get()); assertEquals(restRequest.content(), handlerRequest.get().content()); Map<String, Object> original = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, handlerRequest.get().content().streamInput()).map(); assertEquals(2, original.size()); assertEquals(SecuritySettingsSourceField.TEST_PASSWORD, original.get("password")); assertEquals("bar", original.get("foo")); assertNotEquals(restRequest, authcServiceRequest.get()); assertNotEquals(restRequest.content(), authcServiceRequest.get().content()); Map<String, Object> map = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, authcServiceRequest.get().content().streamInput()) .map(); assertEquals(1, map.size()); assertEquals("bar", map.get("foo")); }