Search in sources :

Example 1 with AccessToken

use of com.sequenceiq.cloudbreak.client.AccessToken in project cloudbreak by hortonworks.

the class AutoscaleClient method refreshIfNeededAndGet.

private synchronized <T> T refreshIfNeededAndGet(Class<T> clazz) {
    String token = tokenCache.get(TOKEN_KEY);
    if (token == null || endpointHolder == null) {
        AccessToken accessToken;
        accessToken = secret != null ? identityClient.getToken(secret) : identityClient.getToken(user, password);
        token = accessToken.getToken();
        int exp = (int) (accessToken.getExpiresIn() * TOKEN_EXPIRATION_FACTOR);
        logger.info("Token has been renewed and expires in {} seconds", exp);
        tokenCache.put(TOKEN_KEY, accessToken.getToken(), ExpirationPolicy.CREATED, exp, TimeUnit.SECONDS);
        MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
        headers.add("Authorization", "Bearer " + token);
        webTarget = client.target(autoscaleAddress).path(AutoscaleApi.API_ROOT_CONTEXT);
        endpointHolder = new EndpointHolder(newEndpoint(AlertEndpoint.class, headers), newEndpoint(AutoScaleClusterV1Endpoint.class, headers), newEndpoint(ConfigurationEndpoint.class, headers), newEndpoint(HistoryEndpoint.class, headers), newEndpoint(PolicyEndpoint.class, headers));
        logger.info("Endpoints have been renewed for AutoscaleClient");
    }
    return (T) endpointHolder.endpoints.stream().filter(e -> e.getClass().equals(clazz)).findFirst().get();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) AccessToken(com.sequenceiq.cloudbreak.client.AccessToken) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Form(javax.ws.rs.core.Form) HistoryEndpoint(com.sequenceiq.periscope.api.endpoint.v1.HistoryEndpoint) Client(javax.ws.rs.client.Client) LoggerFactory(org.slf4j.LoggerFactory) ExpiringMap(net.jodah.expiringmap.ExpiringMap) ConfigurationEndpoint(com.sequenceiq.periscope.api.endpoint.v1.ConfigurationEndpoint) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) AlertEndpoint(com.sequenceiq.periscope.api.endpoint.v1.AlertEndpoint) IdentityClient(com.sequenceiq.cloudbreak.client.IdentityClient) TimeUnit(java.util.concurrent.TimeUnit) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) WebResourceFactory(org.glassfish.jersey.client.proxy.WebResourceFactory) RestClientUtil(com.sequenceiq.cloudbreak.client.RestClientUtil) ExpirationPolicy(net.jodah.expiringmap.ExpirationPolicy) WebTarget(javax.ws.rs.client.WebTarget) ConfigKey(com.sequenceiq.cloudbreak.client.ConfigKey) PolicyEndpoint(com.sequenceiq.periscope.api.endpoint.v1.PolicyEndpoint) Collections(java.util.Collections) AutoscaleApi(com.sequenceiq.periscope.api.AutoscaleApi) AutoScaleClusterV1Endpoint(com.sequenceiq.periscope.api.endpoint.v1.AutoScaleClusterV1Endpoint) AccessToken(com.sequenceiq.cloudbreak.client.AccessToken) HistoryEndpoint(com.sequenceiq.periscope.api.endpoint.v1.HistoryEndpoint) ConfigurationEndpoint(com.sequenceiq.periscope.api.endpoint.v1.ConfigurationEndpoint) AlertEndpoint(com.sequenceiq.periscope.api.endpoint.v1.AlertEndpoint) PolicyEndpoint(com.sequenceiq.periscope.api.endpoint.v1.PolicyEndpoint) AutoScaleClusterV1Endpoint(com.sequenceiq.periscope.api.endpoint.v1.AutoScaleClusterV1Endpoint)

Example 2 with AccessToken

use of com.sequenceiq.cloudbreak.client.AccessToken in project cloudbreak by hortonworks.

the class CachedUserDetailsService method getIdentityUser.

private IdentityUser getIdentityUser(String username, UserFilterField filterField, String clientSecret) {
    WebTarget target;
    LOGGER.info("Load user details: {}", username);
    switch(filterField) {
        case USERNAME:
            target = identityWebTarget.queryParam("filter", "userName eq \"" + username + '"');
            break;
        case USERID:
            target = identityWebTarget.path(username);
            break;
        default:
            throw new UserDetailsUnavailableException("User details cannot be retrieved.");
    }
    AccessToken accessToken = identityClient.getToken(clientSecret);
    String scimResponse = target.request(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken.getToken()).get(String.class);
    try {
        JsonNode root = JsonUtil.readTree(scimResponse);
        List<IdentityUserRole> roles = new ArrayList<>();
        String account = null;
        JsonNode userNode = root;
        if (UserFilterField.USERNAME.equals(filterField)) {
            userNode = root.get("resources").get(0);
        }
        if (userNode == null) {
            throw new UserDetailsUnavailableException("User details cannot be retrieved from identity server.");
        }
        for (JsonNode node : userNode.get("groups")) {
            String group = node.get("display").asText();
            if (group.startsWith("sequenceiq.account")) {
                String[] parts = group.split("\\.");
                if (account != null && !account.equals(parts[ACCOUNT_PART])) {
                    throw new IllegalStateException("A user can belong to only one account.");
                }
                account = parts[ACCOUNT_PART];
            } else if (group.startsWith("sequenceiq.cloudbreak")) {
                String[] parts = group.split("\\.");
                roles.add(IdentityUserRole.fromString(parts[ROLE_PART]));
            }
        }
        return createIdentityUser(roles, account, userNode);
    } catch (IOException e) {
        throw new UserDetailsUnavailableException("User details cannot be retrieved from identity server.", e);
    }
}
Also used : IdentityUserRole(com.sequenceiq.cloudbreak.common.model.user.IdentityUserRole) AccessToken(com.sequenceiq.cloudbreak.client.AccessToken) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException)

Aggregations

AccessToken (com.sequenceiq.cloudbreak.client.AccessToken)2 WebTarget (javax.ws.rs.client.WebTarget)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ConfigKey (com.sequenceiq.cloudbreak.client.ConfigKey)1 IdentityClient (com.sequenceiq.cloudbreak.client.IdentityClient)1 RestClientUtil (com.sequenceiq.cloudbreak.client.RestClientUtil)1 IdentityUserRole (com.sequenceiq.cloudbreak.common.model.user.IdentityUserRole)1 AutoscaleApi (com.sequenceiq.periscope.api.AutoscaleApi)1 AlertEndpoint (com.sequenceiq.periscope.api.endpoint.v1.AlertEndpoint)1 AutoScaleClusterV1Endpoint (com.sequenceiq.periscope.api.endpoint.v1.AutoScaleClusterV1Endpoint)1 ConfigurationEndpoint (com.sequenceiq.periscope.api.endpoint.v1.ConfigurationEndpoint)1 HistoryEndpoint (com.sequenceiq.periscope.api.endpoint.v1.HistoryEndpoint)1 PolicyEndpoint (com.sequenceiq.periscope.api.endpoint.v1.PolicyEndpoint)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Client (javax.ws.rs.client.Client)1