Java Code Examples for org.elasticsearch.rest.RestRequest#getRemoteAddress()

The following examples show how to use org.elasticsearch.rest.RestRequest#getRemoteAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoginUserContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public LoginUserContext(RestRequest request, ClusterService clusterService) {
    authenticated = false;
    // get username and password
    String auth = request.header("Authorization");
    if (request.param(USERNAME_KEY) != null) {
        loginUsername = request.param(USERNAME_KEY);
        password = request.param(PASSWORD_KEY);
        if (request.hasParam(AUTHENTICATED_KEY)) {
            authenticated = Boolean.parseBoolean(request.param(AUTHENTICATED_KEY));
        }
    } else if ((auth != null) && (auth.length() > BASIC_LENGTH)) {
        auth = auth.substring(BASIC_LENGTH);
        String decodedAuth = new String(Base64.decodeBase64(auth));
        String[] nameAndPass = decodedAuth.split(":");
        if (nameAndPass.length > 0) {
            loginUsername = nameAndPass[0];
        }
        if (nameAndPass.length > 1) {
            password = nameAndPass[1];
        }
    }
    
    if (Strings.isNullOrEmpty(loginUsername)) {
        throw new NoPermissionException(RestStatus.UNAUTHORIZED.getStatus(), "could not parse username from http header or url");
    }
    
    MetaData metaData = clusterService.state().metaData();
    if (UserProperty.getUsernameWithoutTenantFromFullUsername(loginUsername).equalsIgnoreCase(UserProperty.ROOT_NAME)) {
        userProperty = metaData.getUserMetadata().getUserProperties().get(UserProperty.ROOT_NAME);
        UserProperty.Builder userBuilder = new UserProperty.Builder(userProperty);
        userBuilder.changeUsername(UserProperty.ROOT_NAME, UserProperty.getTenantIdFromLoginUserName(loginUsername, metaData.tenantMetadata()));
        userProperty = userBuilder.build();
    } else {
        userProperty = metaData.getUserMetadata().getUserPropertyFromLoginUser(loginUsername, metaData.tenantMetadata());
    }
    if (userProperty == null) {
        throw new NoPermissionException(RestStatus.UNAUTHORIZED.getStatus(), "could not find user " + loginUsername);
    }
    tenantId = userProperty.getTenantId();
    fullUsername = userProperty.getUsernameWithTenant();
    // get sourceAddrs and proxyAddrs
    InetSocketAddress addrs = (InetSocketAddress) request.getRemoteAddress();
    String forwardAddrsList = request.header("X-Forwarded-For");
    if (forwardAddrsList != null && forwardAddrsList.length() > 0) {
        proxyAddrs = addrs.getAddress().getHostAddress();
        sourceAddrs = forwardAddrsList.split(",")[0];
    } else {
        sourceAddrs = addrs.getAddress().getHostAddress();
    }
}
 
Example 2
Source File: RequestUtils.java    From openshift-elasticsearch-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Modify the request of needed
 * @param request the original request
 * @param context the Openshift context
 * @param channel the channel that is processing the request
 * 
 * @return The modified request
 */
public RestRequest modifyRequest(final RestRequest request, OpenshiftRequestContext context, RestChannel channel) {
    
    final String uri = getUri(request, context);
    final BytesReference content = getContent(request, context);
    if(!getUser(request).equals(context.getUser()) || !uri.equals(request.uri()) || content != request.content()) {
        LOGGER.debug("Modifying header '{}' to be '{}'", proxyUserHeader, context.getUser());
        final Map<String, List<String>> modifiedHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        modifiedHeaders.putAll(request.getHeaders());
        modifiedHeaders.put(proxyUserHeader, Arrays.asList(context.getUser()));
        if(request.header("Content-Type") != null && request.header("Content-Type").toLowerCase().endsWith("json")){
            modifiedHeaders.put("Content-Type", Arrays.asList("application/json"));
        }
        RestRequest modified = new RestRequest(request.getXContentRegistry(), uri, modifiedHeaders) {

            @Override
            public Method method() {
                return request.method();
            }

            @Override
            public String uri() {
                return uri;
            }

            @Override
            public boolean hasContent() {
                return content.length() > 0;
            }

            @Override
            public BytesReference content() {
                return content;
            }
            
            @Override
            public SocketAddress getRemoteAddress() {
                return request.getRemoteAddress();
            }

            /**
             * Returns the local address where this request channel is bound to.  The returned
             * {@link SocketAddress} is supposed to be down-cast into more concrete
             * type such as {@link java.net.InetSocketAddress} to retrieve the detailed
             * information.
             */
            @Override
            public SocketAddress getLocalAddress() {
                return request.getRemoteAddress();
            }

            @SuppressWarnings("unused")
            public Channel getChannel() {
                return (Channel) channel;
            }
            
        };
        modified.params().putAll(request.params());
        //HACK - only need to do if we modify the kibana index
        if (uri.contains(defaultKibanaIndex)) {
            modified.params().put("index", context.getKibanaIndex());
        }

        return modified;
    }
    return request;
}