Java Code Examples for org.elasticsearch.rest.RestStatus#fromCode()

The following examples show how to use org.elasticsearch.rest.RestStatus#fromCode() . 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: OpenshiftAPIService.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
public String userName(final String token) {
    Response response = null;
    try (DefaultOpenShiftClient client = factory.buildClient(token)) {
        Request okRequest = new Request.Builder()
                .url(client.getMasterUrl() + "apis/user.openshift.io/v1/users/~")
                .header("Authorization", "Bearer " + token)
                .header(ACCEPT, APPLICATION_JSON)
                .build();
        response = client.getHttpClient().newCall(okRequest).execute();
        final String body = response.body().string();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Response: code '{}' {}", response.code(), body);
        }
        if(response.code() != RestStatus.OK.getStatus()) {
            throw new ElasticsearchSecurityException("Unable to determine username from the token provided", RestStatus.fromCode(response.code()));
        }
        return JsonPath.read(body,"$.metadata.name");
    } catch (IOException e) {
        LOGGER.error("Error retrieving username from token", e);
        throw new ElasticsearchException(e);
    }        
}
 
Example 2
Source File: OpenshiftAPIService.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a LocalSubectAccessReview
 * 
 * @param token             a token to check
 * @param project           the namespace to check against
 * @param verb              the verb (e.g. view)
 * @param resource          the resource (e.g. pods/log)
 * @param resourceAPIGroup  the group of the resource being checked
 * @param scopes            the scopes:
 *                            null  - use token scopes
 *                            empty - remove scopes
 *                            list  - an array of scopes
 *                            
 * @return  true if the SAR is satisfied
 */
public boolean localSubjectAccessReview(final String token, 
        final String project, final String verb, final String resource, final String resourceAPIGroup, final String [] scopes) {
    try (DefaultOpenShiftClient client = factory.buildClient(token)) {
        XContentBuilder payload = XContentFactory.jsonBuilder()
            .startObject()
                .field("kind","SubjectAccessReview")
                .field("apiVersion","authorization.openshift.io/v1")
                .field("verb", verb)
                .array("scopes", scopes);
        if(resource.startsWith("/")) {
            payload.field("isNonResourceURL", Boolean.TRUE)
                .field("path", resource);
        } else {
            payload.field("resourceAPIGroup", resourceAPIGroup)
                .field("resource", resource)
                .field("namespace", project);
        }
        payload.endObject();
        Request request = new Request.Builder()
                .url(String.format("%sapis/authorization.openshift.io/v1/subjectaccessreviews", client.getMasterUrl(), project))
                .header("Authorization", "Bearer " + token)
                .header(CONTENT_TYPE, APPLICATION_JSON)
                .header(ACCEPT, APPLICATION_JSON)
                .post(RequestBody.create(MediaType.parse(APPLICATION_JSON), payload.string()))
                .build();
        log(request);
        Response response = client.getHttpClient().newCall(request).execute();
        final String body = IOUtils.toString(response.body().byteStream());
        log(response, body);
        if(response.code() != RestStatus.CREATED.getStatus()) {
            throw new ElasticsearchSecurityException("Unable to determine user's operations role", RestStatus.fromCode(response.code()));
        }
        return JsonPath.read(body, "$.allowed");
    } catch (IOException e) {
        LOGGER.error("Error determining user's role", e);
    }
    return false;
}
 
Example 3
Source File: AnomalyDetectorRestTestCase.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
protected RestStatus restStatus(Response response) {
    return RestStatus.fromCode(response.getStatusLine().getStatusCode());
}