Search in sources :

Example 6 with ActionRequest

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());
}
Also used : ActionRequest(org.opensearch.action.ActionRequest) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 7 with ActionRequest

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);
}
Also used : ActionRequest(org.opensearch.action.ActionRequest) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 8 with 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;
}
Also used : OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) Resolved(org.opensearch.security.resolver.IndexResolverReplacer.Resolved) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ThreadPool(org.opensearch.threadpool.ThreadPool) HeaderHelper(org.opensearch.security.support.HeaderHelper) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) User(org.opensearch.security.user.User) Settings(org.opensearch.common.settings.Settings) ActionRequest(org.opensearch.action.ActionRequest) SecurityRoles(org.opensearch.security.securityconf.SecurityRoles) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) WildcardMatcher(org.opensearch.security.support.WildcardMatcher) Serializable(java.io.Serializable) Tuple(org.opensearch.common.collect.Tuple) ConfigConstants(org.opensearch.security.support.ConfigConstants) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) Base64Helper(org.opensearch.security.support.Base64Helper) Map(java.util.Map) ClusterService(org.opensearch.cluster.service.ClusterService) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Serializable(java.io.Serializable) Set(java.util.Set) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 9 with ActionRequest

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);
            }
        };
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Task(org.opensearch.tasks.Task) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ActionType(org.opensearch.action.ActionType) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ActionRequest(org.opensearch.action.ActionRequest) IndicesRequest(org.opensearch.action.IndicesRequest) ActionFilters(org.opensearch.action.support.ActionFilters) ActionResponse(org.opensearch.action.ActionResponse) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) ActionRequest(org.opensearch.action.ActionRequest) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 10 with ActionRequest

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());
    }
}
Also used : ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActionRequest(org.opensearch.action.ActionRequest)

Aggregations

ActionRequest (org.opensearch.action.ActionRequest)29 ActionListener (org.opensearch.action.ActionListener)15 IOException (java.io.IOException)12 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)10 StreamOutput (org.opensearch.common.io.stream.StreamOutput)10 Test (org.junit.Test)9 ActionResponse (org.opensearch.action.ActionResponse)9 GetRequest (org.opensearch.action.get.GetRequest)9 ActionType (org.opensearch.action.ActionType)8 SearchRequest (org.opensearch.action.search.SearchRequest)8 Task (org.opensearch.tasks.Task)7 Map (java.util.Map)6 BulkRequest (org.opensearch.action.bulk.BulkRequest)6 Client (org.opensearch.client.Client)6 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)6 Set (java.util.Set)5 DeleteRequest (org.opensearch.action.delete.DeleteRequest)5 IndexRequest (org.opensearch.action.index.IndexRequest)5 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)4