[7.10] Move RestRequestFilter to core ()

Move RestRequestFilter to core so that Rest requests outside xpack can use 
it to filter fields and expand its usage.

Backport of 
This commit is contained in:
Ioannis Kakavas 2020-10-16 13:57:52 +03:00 committed by GitHub
parent f4a856d423
commit 364511395d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 104 additions and 20 deletions
modules/reindex/src/main/java/org/elasticsearch/index/reindex
server/src/main/java/org/elasticsearch/rest
x-pack/plugin

@ -24,9 +24,12 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static java.util.Collections.singletonList;
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
@ -35,7 +38,7 @@ import static org.elasticsearch.rest.RestRequest.Method.POST;
/**
* Expose reindex over rest.
*/
public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexRequest, ReindexAction> {
public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexRequest, ReindexAction> implements RestRequestFilter {
public RestReindexAction() {
super(ReindexAction.INSTANCE);
@ -77,4 +80,11 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
return internal;
}
private static final Set<String> FILTERED_FIELDS = Collections.singleton("source.remote.host.password");
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -1,9 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.xpack.core.security.rest;
package org.elasticsearch.rest;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings;
@ -13,7 +27,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.rest.RestRequest;
import java.io.IOException;
import java.util.Map;

@ -31,19 +31,22 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestActions;
import org.elasticsearch.rest.action.RestBuilderListener;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static org.elasticsearch.rest.RestRequest.Method.POST;
public final class RestReloadSecureSettingsAction extends BaseRestHandler {
public final class RestReloadSecureSettingsAction extends BaseRestHandler implements RestRequestFilter {
static final ObjectParser<NodesReloadSecureSettingsRequest, String> PARSER =
new ObjectParser<>("reload_secure_settings", NodesReloadSecureSettingsRequest::new);
@ -101,4 +104,10 @@ public final class RestReloadSecureSettingsAction extends BaseRestHandler {
return false;
}
private static final Set<String> FILTERED_FIELDS = Collections.singleton("secure_settings_password");
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -21,12 +21,14 @@ import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestRequest.Method;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.xpack.security.authc.AuthenticationService;
import org.elasticsearch.xpack.security.authc.support.SecondaryAuthenticator;
import org.elasticsearch.xpack.security.transport.SSLEngineUtils;
import java.io.IOException;
import java.util.List;
public class SecurityRestFilter implements RestHandler {

@ -14,6 +14,7 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.license.XPackLicenseState;
@ -23,11 +24,13 @@ import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequestBuilder;
import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction;
import org.elasticsearch.xpack.core.security.action.GrantApiKeyRequest;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -36,7 +39,7 @@ import static org.elasticsearch.rest.RestRequest.Method.PUT;
* Rest action to create an API key on behalf of another user. Loosely mimics the API of
* {@link org.elasticsearch.xpack.security.rest.action.oauth2.RestGetTokenAction} combined with {@link RestCreateApiKeyAction}
*/
public final class RestGrantApiKeyAction extends ApiKeyBaseRestHandler {
public final class RestGrantApiKeyAction extends ApiKeyBaseRestHandler implements RestRequestFilter {
static final ObjectParser<GrantApiKeyRequest, Void> PARSER = new ObjectParser<>("grant_api_key_request", GrantApiKeyRequest::new);
static {
@ -92,4 +95,11 @@ public final class RestGrantApiKeyAction extends ApiKeyBaseRestHandler {
}));
}
}
private static final Set<String> FILTERED_FIELDS = Collections.unmodifiableSet(Sets.newHashSet("password", "access_token"));
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -14,6 +14,7 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -27,6 +28,7 @@ import org.elasticsearch.xpack.core.security.action.token.CreateTokenAction;
import org.elasticsearch.xpack.core.security.action.token.CreateTokenRequest;
import org.elasticsearch.xpack.core.security.action.token.CreateTokenResponse;
import org.elasticsearch.xpack.core.security.action.token.RefreshTokenAction;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.xpack.security.authc.kerberos.KerberosAuthenticationToken;
import java.io.IOException;
@ -34,6 +36,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.POST;
@ -44,7 +47,7 @@ import static org.elasticsearch.rest.RestRequest.Method.POST;
* specification as this aspect does not make the most sense since the response body is
* expected to be JSON
*/
public final class RestGetTokenAction extends TokenBaseRestHandler {
public final class RestGetTokenAction extends TokenBaseRestHandler implements RestRequestFilter {
static final ConstructingObjectParser<CreateTokenRequest, Void> PARSER = new ConstructingObjectParser<>("token_request",
a -> new CreateTokenRequest((String) a[0], (String) a[1], (SecureString) a[2], (SecureString) a[3], (String) a[4],
@ -242,4 +245,12 @@ public final class RestGetTokenAction extends TokenBaseRestHandler {
*/
_UNAUTHORIZED,
}
private static final Set<String> FILTERED_FIELDS = Collections.unmodifiableSet(
Sets.newHashSet("password", "kerberos_ticket", "refresh_token"));
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -9,6 +9,7 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -20,17 +21,19 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.security.action.token.InvalidateTokenAction;
import org.elasticsearch.xpack.core.security.action.token.InvalidateTokenRequest;
import org.elasticsearch.xpack.core.security.action.token.InvalidateTokenResponse;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
/**
* Rest handler for handling access token invalidation requests
*/
public final class RestInvalidateTokenAction extends TokenBaseRestHandler {
public final class RestInvalidateTokenAction extends TokenBaseRestHandler implements RestRequestFilter {
static final ConstructingObjectParser<InvalidateTokenRequest, Void> PARSER =
new ConstructingObjectParser<>("invalidate_token", a -> {
@ -97,4 +100,11 @@ public final class RestInvalidateTokenAction extends TokenBaseRestHandler {
});
}
}
private static final Set<String> FILTERED_FIELDS = Collections.unmodifiableSet(Sets.newHashSet("token", "refresh_token"));
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -22,9 +22,12 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateAction;
import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateRequest;
import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateResponse;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static java.util.Collections.singletonList;
import static org.elasticsearch.rest.RestRequest.Method.POST;
@ -32,7 +35,7 @@ import static org.elasticsearch.rest.RestRequest.Method.POST;
/**
* Rest handler that authenticates the user based on the information provided as parameters of the redirect_uri
*/
public class RestOpenIdConnectAuthenticateAction extends OpenIdConnectBaseRestHandler {
public class RestOpenIdConnectAuthenticateAction extends OpenIdConnectBaseRestHandler implements RestRequestFilter {
private static final Logger logger = LogManager.getLogger();
static final ObjectParser<OpenIdConnectAuthenticateRequest, Void> PARSER = new ObjectParser<>("oidc_authn",
@ -80,4 +83,11 @@ public class RestOpenIdConnectAuthenticateAction extends OpenIdConnectBaseRestHa
public String getName() {
return "security_oidc_authenticate_action";
}
private static final Set<String> FILTERED_FIELDS = Collections.singleton("redirect_uri");
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -23,18 +23,20 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.security.action.saml.SamlAuthenticateRequestBuilder;
import org.elasticsearch.xpack.core.security.action.saml.SamlAuthenticateResponse;
import org.elasticsearch.xpack.core.security.client.SecurityClient;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.POST;
/**
* A REST handler that attempts to authenticate a user based on the provided SAML response/assertion.
*/
public class RestSamlAuthenticateAction extends SamlBaseRestHandler {
public class RestSamlAuthenticateAction extends SamlBaseRestHandler implements RestRequestFilter {
private static final Logger logger = LogManager.getLogger();
static class Input {
@ -119,4 +121,11 @@ public class RestSamlAuthenticateAction extends SamlBaseRestHandler {
throw e;
}
}
private static final Set<String> FILTERED_FIELDS = Collections.singleton("content");
@Override
public Set<String> getFilteredFields() {
return FILTERED_FIELDS;
}
}

@ -19,7 +19,7 @@ import org.elasticsearch.xpack.core.security.SecurityContext;
import org.elasticsearch.xpack.core.security.action.user.ChangePasswordResponse;
import org.elasticsearch.xpack.core.security.authc.support.Hasher;
import org.elasticsearch.xpack.core.security.client.SecurityClient;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.xpack.core.security.user.User;
import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler;

@ -14,13 +14,13 @@ import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.security.action.user.PutUserRequestBuilder;
import org.elasticsearch.xpack.core.security.action.user.PutUserResponse;
import org.elasticsearch.xpack.core.security.authc.support.Hasher;
import org.elasticsearch.xpack.core.security.client.SecurityClient;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler;
import java.io.IOException;
@ -80,7 +80,7 @@ public class RestPutUserAction extends SecurityBaseRestHandler implements RestRe
});
}
private static final Set<String> FILTERED_FIELDS = Collections.unmodifiableSet(Sets.newHashSet("password", "passwordHash"));
private static final Set<String> FILTERED_FIELDS = Collections.unmodifiableSet(Sets.newHashSet("password", "password_hash"));
@Override
public Set<String> getFilteredFields() {

@ -13,7 +13,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.rest.RestRequestFilter;
import java.io.IOException;
import java.net.InetAddress;

@ -32,7 +32,7 @@ import org.elasticsearch.xpack.core.security.SecurityContext;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
import org.elasticsearch.xpack.core.security.authc.support.SecondaryAuthentication;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.xpack.core.security.user.XPackUser;
import org.elasticsearch.xpack.security.authc.AuthenticationService;
import org.elasticsearch.xpack.security.authc.support.SecondaryAuthenticator;

@ -17,7 +17,7 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode;
import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams;

@ -15,8 +15,8 @@ import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.RestRequestFilter;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.core.security.rest.RestRequestFilter;
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;