use of org.opensearch.action.ActionRequest in project ml-commons by opensearch-project.
the class MLPredictionTaskRequestTest method fromActionRequest_Success_WithNonMLPredictionTaskRequest.
private void fromActionRequest_Success_WithNonMLPredictionTaskRequest(MLInput mlInput) {
MLPredictionTaskRequest request = MLPredictionTaskRequest.builder().mlInput(mlInput).build();
ActionRequest actionRequest = new ActionRequest() {
@Override
public ActionRequestValidationException validate() {
return null;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
request.writeTo(out);
}
};
MLPredictionTaskRequest result = MLPredictionTaskRequest.fromActionRequest(actionRequest);
assertNotSame(result, request);
assertEquals(request.getMlInput().getAlgorithm(), result.getMlInput().getAlgorithm());
assertEquals(request.getMlInput().getInputDataset().getInputDataType(), result.getMlInput().getInputDataset().getInputDataType());
}
use of org.opensearch.action.ActionRequest in project ml-commons by opensearch-project.
the class MLPredictionTaskRequestTest method fromActionRequest_IOException.
@Test(expected = UncheckedIOException.class)
public void fromActionRequest_IOException() {
ActionRequest actionRequest = new ActionRequest() {
@Override
public ActionRequestValidationException validate() {
return null;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
throw new IOException("test");
}
};
MLPredictionTaskRequest.fromActionRequest(actionRequest);
}
use of org.opensearch.action.ActionRequest in project security by opensearch-project.
the class DlsFlsEvaluator method evaluate.
public PrivilegesEvaluatorResponse evaluate(final ActionRequest request, final ClusterService clusterService, final IndexNameExpressionResolver resolver, final Resolved requestedResolved, final User user, final SecurityRoles securityRoles, final PrivilegesEvaluatorResponse presponse) {
ThreadContext threadContext = threadPool.getThreadContext();
// maskedFields
final Map<String, Set<String>> maskedFieldsMap = securityRoles.getMaskedFields(user, resolver, clusterService);
final boolean isDebugEnabled = log.isDebugEnabled();
if (maskedFieldsMap != null && !maskedFieldsMap.isEmpty()) {
if (request instanceof ClusterSearchShardsRequest && HeaderHelper.isTrustedClusterRequest(threadContext)) {
threadContext.addResponseHeader(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER, Base64Helper.serializeObject((Serializable) maskedFieldsMap));
if (isDebugEnabled) {
log.debug("Added response header for masked fields info: {}", maskedFieldsMap);
}
} else {
if (threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER) != null) {
if (!maskedFieldsMap.equals(Base64Helper.deserializeObject(threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER)))) {
throw new OpenSearchSecurityException(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER + " does not match ");
} else {
if (isDebugEnabled) {
log.debug("Header {} already set", ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER);
}
}
} else {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_MASKED_FIELD_HEADER, Base64Helper.serializeObject((Serializable) maskedFieldsMap));
if (isDebugEnabled) {
log.debug("Attach masked fields info: {}", maskedFieldsMap);
}
}
}
presponse.maskedFields = maskedFieldsMap.entrySet().stream().filter(requestedResolved.isLocalAll() || requestedResolved.getAllIndices().isEmpty() ? entry -> true : entry -> WildcardMatcher.from(entry.getKey()).matchAny(requestedResolved.getAllIndices())).collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
}
// attach dls/fls map if not already done
final Tuple<Map<String, Set<String>>, Map<String, Set<String>>> dlsFls = securityRoles.getDlsFls(user, resolver, clusterService);
final Map<String, Set<String>> dlsQueries = dlsFls.v1();
final Map<String, Set<String>> flsFields = dlsFls.v2();
if (!dlsQueries.isEmpty()) {
if (request instanceof ClusterSearchShardsRequest && HeaderHelper.isTrustedClusterRequest(threadContext)) {
threadContext.addResponseHeader(ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER, Base64Helper.serializeObject((Serializable) dlsQueries));
if (isDebugEnabled) {
log.debug("Added response header for DLS info: {}", dlsQueries);
}
} else {
if (threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER) != null) {
if (!dlsQueries.equals(Base64Helper.deserializeObject(threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER)))) {
throw new OpenSearchSecurityException(ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER + " does not match (SG 900D)");
}
} else {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_DLS_QUERY_HEADER, Base64Helper.serializeObject((Serializable) dlsQueries));
if (isDebugEnabled) {
log.debug("Attach DLS info: {}", dlsQueries);
}
}
}
presponse.queries = dlsQueries.entrySet().stream().filter(requestedResolved.isLocalAll() || requestedResolved.getAllIndices().isEmpty() ? entry -> true : entry -> WildcardMatcher.from(entry.getKey()).matchAny(requestedResolved.getAllIndices())).collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
}
if (!flsFields.isEmpty()) {
if (request instanceof ClusterSearchShardsRequest && HeaderHelper.isTrustedClusterRequest(threadContext)) {
threadContext.addResponseHeader(ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER, Base64Helper.serializeObject((Serializable) flsFields));
if (isDebugEnabled) {
log.debug("Added response header for FLS info: {}", flsFields);
}
} else {
if (threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER) != null) {
if (!flsFields.equals(Base64Helper.deserializeObject(threadContext.getHeader(ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER)))) {
throw new OpenSearchSecurityException(ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER + " does not match ");
} else {
if (isDebugEnabled) {
log.debug("Header {} already set", ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER);
}
}
} else {
threadContext.putHeader(ConfigConstants.OPENDISTRO_SECURITY_FLS_FIELDS_HEADER, Base64Helper.serializeObject((Serializable) flsFields));
if (isDebugEnabled) {
log.debug("Attach FLS info: {}", flsFields);
}
}
}
presponse.allowedFlsFields = flsFields.entrySet().stream().filter(requestedResolved.isLocalAll() || requestedResolved.getAllIndices().isEmpty() ? entry -> true : entry -> WildcardMatcher.from(entry.getKey()).matchAny(requestedResolved.getAllIndices())).collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
}
return presponse;
}
use of org.opensearch.action.ActionRequest in project OpenSearch by opensearch-project.
the class TransportBulkActionTookTests method createAction.
private TransportBulkAction createAction(boolean controlled, AtomicLong expected) {
CapturingTransport capturingTransport = new CapturingTransport();
TransportService transportService = capturingTransport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null, Collections.emptySet());
transportService.start();
transportService.acceptIncomingRequests();
IndexNameExpressionResolver resolver = new Resolver();
ActionFilters actionFilters = new ActionFilters(new HashSet<>());
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
listener.onResponse((Response) new CreateIndexResponse(false, false, null));
}
};
if (controlled) {
return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, expected::get) {
@Override
void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
expected.set(1000000);
super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
}
};
} else {
return new TestTransportBulkAction(threadPool, transportService, clusterService, null, client, actionFilters, resolver, null, System::nanoTime) {
@Override
void executeBulk(Task task, BulkRequest bulkRequest, long startTimeNanos, ActionListener<BulkResponse> listener, AtomicArray<BulkItemResponse> responses, Map<String, IndexNotFoundException> indicesThatCannotBeCreated) {
long elapsed = spinForAtLeastOneMillisecond();
expected.set(elapsed);
super.executeBulk(task, bulkRequest, startTimeNanos, listener, responses, indicesThatCannotBeCreated);
}
};
}
}
use of org.opensearch.action.ActionRequest in project OpenSearch by opensearch-project.
the class RestHighLevelClientTests method testRequestValidation.
public void testRequestValidation() {
ActionRequestValidationException validationException = new ActionRequestValidationException();
validationException.addValidationError("validation error");
ActionRequest request = new ActionRequest() {
@Override
public ActionRequestValidationException validate() {
return validationException;
}
};
{
ActionRequestValidationException actualException = expectThrows(ActionRequestValidationException.class, () -> restHighLevelClient.performRequest(request, null, RequestOptions.DEFAULT, null, null));
assertSame(validationException, actualException);
}
{
TrackingActionListener trackingActionListener = new TrackingActionListener();
restHighLevelClient.performRequestAsync(request, null, RequestOptions.DEFAULT, null, trackingActionListener, null);
assertSame(validationException, trackingActionListener.exception.get());
}
}
Aggregations