Search in sources :

Example 26 with NodeClient

use of org.opensearch.client.node.NodeClient in project security by opensearch-project.

the class SecuritySSLInfoAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    return new RestChannelConsumer() {

        final Boolean showDn = request.paramAsBoolean("show_dn", Boolean.FALSE);

        @Override
        public void accept(RestChannel channel) throws Exception {
            XContentBuilder builder = channel.newBuilder();
            BytesRestResponse response = null;
            try {
                SSLInfo sslInfo = SSLRequestHelper.getSSLInfo(settings, configPath, request, principalExtractor);
                X509Certificate[] certs = sslInfo == null ? null : sslInfo.getX509Certs();
                X509Certificate[] localCerts = sslInfo == null ? null : sslInfo.getLocalCertificates();
                builder.startObject();
                builder.field("principal", sslInfo == null ? null : sslInfo.getPrincipal());
                builder.field("peer_certificates", certs != null && certs.length > 0 ? certs.length + "" : "0");
                if (showDn == Boolean.TRUE) {
                    builder.field("peer_certificates_list", certs == null ? null : Arrays.stream(certs).map(c -> c.getSubjectDN().getName()).collect(Collectors.toList()));
                    builder.field("local_certificates_list", localCerts == null ? null : Arrays.stream(localCerts).map(c -> c.getSubjectDN().getName()).collect(Collectors.toList()));
                }
                builder.field("ssl_protocol", sslInfo == null ? null : sslInfo.getProtocol());
                builder.field("ssl_cipher", sslInfo == null ? null : sslInfo.getCipher());
                builder.field("ssl_openssl_available", OpenSsl.isAvailable());
                builder.field("ssl_openssl_version", OpenSsl.version());
                builder.field("ssl_openssl_version_string", OpenSsl.versionString());
                Throwable openSslUnavailCause = OpenSsl.unavailabilityCause();
                builder.field("ssl_openssl_non_available_cause", openSslUnavailCause == null ? "" : openSslUnavailCause.toString());
                builder.field("ssl_openssl_supports_key_manager_factory", OpenSsl.supportsKeyManagerFactory());
                builder.field("ssl_openssl_supports_hostname_validation", OpenSsl.supportsHostnameValidation());
                builder.field("ssl_provider_http", sks.getHTTPProviderName());
                builder.field("ssl_provider_transport_server", sks.getTransportServerProviderName());
                builder.field("ssl_provider_transport_client", sks.getTransportClientProviderName());
                builder.endObject();
                response = new BytesRestResponse(RestStatus.OK, builder);
            } catch (final Exception e1) {
                log.error("Error handle request ", e1);
                builder = channel.newBuilder();
                builder.startObject();
                builder.field("error", e1.toString());
                builder.endObject();
                response = new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, builder);
            } finally {
                if (builder != null) {
                    builder.close();
                }
            }
            channel.sendResponse(response);
        }
    };
}
Also used : X509Certificate(java.security.cert.X509Certificate) OpenSsl(io.netty.handler.ssl.OpenSsl) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) NodeClient(org.opensearch.client.node.NodeClient) RestController(org.opensearch.rest.RestController) RestRequest(org.opensearch.rest.RestRequest) LoggerFactory(org.slf4j.LoggerFactory) SecurityKeyStore(org.opensearch.security.ssl.SecurityKeyStore) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) BytesRestResponse(org.opensearch.rest.BytesRestResponse) SSLRequestHelper(org.opensearch.security.ssl.util.SSLRequestHelper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) PrincipalExtractor(org.opensearch.security.ssl.transport.PrincipalExtractor) RestChannel(org.opensearch.rest.RestChannel) Method(org.opensearch.rest.RestRequest.Method) SSLInfo(org.opensearch.security.ssl.util.SSLRequestHelper.SSLInfo) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Path(java.nio.file.Path) Collections(java.util.Collections) SSLInfo(org.opensearch.security.ssl.util.SSLRequestHelper.SSLInfo) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestChannel(org.opensearch.rest.RestChannel) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException)

Example 27 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestPutIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));
    if (request.hasParam("template")) {
        deprecationLogger.deprecate("put_index_template_deprecated_parameter", "Deprecated parameter [template] used, replaced by [index_patterns]");
        putRequest.patterns(Collections.singletonList(request.param("template")));
    } else {
        putRequest.patterns(Arrays.asList(request.paramAsStringArray("index_patterns", Strings.EMPTY_ARRAY)));
    }
    putRequest.order(request.paramAsInt("order", putRequest.order()));
    putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
    putRequest.create(request.paramAsBoolean("create", false));
    putRequest.cause(request.param("cause", ""));
    Map<String, Object> sourceAsMap = XContentHelper.convertToMap(request.requiredContent(), false, request.getXContentType()).v2();
    sourceAsMap = RestCreateIndexAction.prepareMappings(sourceAsMap);
    putRequest.source(sourceAsMap);
    return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) PutIndexTemplateRequest(org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest) IOException(java.io.IOException) Strings(org.opensearch.common.Strings) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) XContentHelper(org.opensearch.common.xcontent.XContentHelper) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) Collections(java.util.Collections) PutIndexTemplateRequest(org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest)

Example 28 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestRecoveryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final RecoveryRequest recoveryRequest = new RecoveryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    recoveryRequest.detailed(request.paramAsBoolean("detailed", false));
    recoveryRequest.activeOnly(request.paramAsBoolean("active_only", false));
    recoveryRequest.indicesOptions(IndicesOptions.fromRequest(request, recoveryRequest.indicesOptions()));
    return channel -> client.admin().indices().recoveries(recoveryRequest, new RestToXContentListener<>(channel));
}
Also used : List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) BaseRestHandler(org.opensearch.rest.BaseRestHandler) RecoveryRequest(org.opensearch.action.admin.indices.recovery.RecoveryRequest) Strings(org.opensearch.common.Strings) RecoveryRequest(org.opensearch.action.admin.indices.recovery.RecoveryRequest)

Example 29 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestForceMergeAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index")));
    mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions()));
    mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
    mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
    mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
    if (mergeRequest.onlyExpungeDeletes() && mergeRequest.maxNumSegments() != ForceMergeRequest.Defaults.MAX_NUM_SEGMENTS) {
        deprecationLogger.deprecate("force_merge_expunge_deletes_and_max_num_segments_deprecation", "setting only_expunge_deletes and max_num_segments at the same time is deprecated and will be rejected in a future version");
    }
    return channel -> client.admin().indices().forceMerge(mergeRequest, new RestToXContentListener<>(channel));
}
Also used : ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) Strings(org.opensearch.common.Strings) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 30 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestGetIndexTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] names = Strings.splitStringByCommaToArray(request.param("name"));
    final GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
    getIndexTemplatesRequest.local(request.paramAsBoolean("local", getIndexTemplatesRequest.local()));
    getIndexTemplatesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout()));
    final boolean implicitAll = getIndexTemplatesRequest.names().length == 0;
    return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<GetIndexTemplatesResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetIndexTemplatesResponse response) {
            final boolean templateExists = response.getIndexTemplates().isEmpty() == false;
            return (templateExists || implicitAll) ? OK : NOT_FOUND;
        }
    });
}
Also used : OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) Strings(org.opensearch.common.Strings) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest) NOT_FOUND(org.opensearch.rest.RestStatus.NOT_FOUND) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) HEAD(org.opensearch.rest.RestRequest.Method.HEAD) RestStatus(org.opensearch.rest.RestStatus) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesRequest)

Aggregations

NodeClient (org.opensearch.client.node.NodeClient)182 RestRequest (org.opensearch.rest.RestRequest)142 List (java.util.List)140 IOException (java.io.IOException)137 BaseRestHandler (org.opensearch.rest.BaseRestHandler)111 Arrays.asList (java.util.Arrays.asList)76 Collections.unmodifiableList (java.util.Collections.unmodifiableList)76 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 Strings (org.opensearch.common.Strings)66 GET (org.opensearch.rest.RestRequest.Method.GET)64 POST (org.opensearch.rest.RestRequest.Method.POST)47 RestResponse (org.opensearch.rest.RestResponse)39 IndicesOptions (org.opensearch.action.support.IndicesOptions)35 Settings (org.opensearch.common.settings.Settings)34 Set (java.util.Set)31 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)31 BytesRestResponse (org.opensearch.rest.BytesRestResponse)29 Collections (java.util.Collections)28 Collections.singletonList (java.util.Collections.singletonList)28 RestStatus (org.opensearch.rest.RestStatus)28