Search in sources :

Example 1 with KeystoneAccess

use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.

the class AbstractTest method osv2.

protected OSClientV2 osv2() {
    if (osv2 == null) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        try {
            String json = new String(Streams.readAll(getClass().getResourceAsStream(JSON_ACCESS)));
            LOG.info(getClass().getName());
            //LOG.info(getClass().getName() + ", JSON Access = " + json);
            json = json.replaceAll("127.0.0.1", getHost());
            //LOG.info("JSON Access = " + json);
            KeystoneAccess a = mapper.readValue(json, KeystoneAccess.class);
            a.applyContext(authURL("/v2.0"), new org.openstack4j.openstack.identity.v2.domain.Credentials("test", "test"));
            osv2 = OSFactory.clientFromAccess(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return osv2;
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) KeystoneAccess(org.openstack4j.openstack.identity.v2.domain.KeystoneAccess)

Example 2 with KeystoneAccess

use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.

the class OSAuthenticator method authenticateV2.

private static OSClientV2 authenticateV2(org.openstack4j.openstack.identity.v2.domain.Auth auth, SessionInfo info, Config config) {
    HttpRequest<KeystoneAccess> request = HttpRequest.builder(KeystoneAccess.class).header(ClientConstants.HEADER_OS4J_AUTH, TOKEN_INDICATOR).endpoint(info.endpoint).method(HttpMethod.POST).path("/tokens").config(config).entity(auth).build();
    HttpResponse response = HttpExecutor.create().execute(request);
    if (response.getStatus() >= 400) {
        try {
            throw mapException(response.getStatusMessage(), response.getStatus());
        } finally {
            HttpEntityHandler.closeQuietly(response);
        }
    }
    KeystoneAccess access = response.getEntity(KeystoneAccess.class);
    if (auth.getType() == Type.CREDENTIALS) {
        access = access.applyContext(info.endpoint, (Credentials) auth);
    } else if (auth.getType() == Type.RAX_APIKEY) {
        access = access.applyContext(info.endpoint, (RaxApiKeyCredentials) auth);
    } else {
        access = access.applyContext(info.endpoint, (org.openstack4j.openstack.identity.v2.domain.TokenAuth) auth);
    }
    if (!info.reLinkToExistingSession)
        return OSClientSession.OSClientSessionV2.createSession(access, info.perspective, info.provider, config);
    OSClientSession.OSClientSessionV2 current = (OSClientSessionV2) OSClientSession.getCurrent();
    current.access = access;
    return current;
}
Also used : OSClientSessionV2(org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV2) OSClientSessionV2(org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV2) RaxApiKeyCredentials(org.openstack4j.openstack.identity.v2.domain.RaxApiKeyCredentials) Credentials(org.openstack4j.openstack.identity.v2.domain.Credentials) KeystoneAccess(org.openstack4j.openstack.identity.v2.domain.KeystoneAccess)

Example 3 with KeystoneAccess

use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.

the class OSAuthenticator method reAuthenticate.

/**
     * Re-authenticates/renews the token for the current Session
     */
@SuppressWarnings("rawtypes")
public static void reAuthenticate() {
    LOG.debug("Re-Authenticating session due to expired Token or invalid response");
    OSClientSession session = OSClientSession.getCurrent();
    switch(session.getAuthVersion()) {
        case V2:
            KeystoneAccess access = ((OSClientSessionV2) session).getAccess().unwrap();
            SessionInfo info = new SessionInfo(access.getEndpoint(), session.getPerspective(), true, session.getProvider());
            Auth auth = (Auth) ((access.isCredentialType()) ? access.getCredentials() : access.getTokenAuth());
            authenticateV2((org.openstack4j.openstack.identity.v2.domain.Auth) auth, info, session.getConfig());
            break;
        case V3:
        default:
            Token token = ((OSClientSessionV3) session).getToken();
            info = new SessionInfo(token.getEndpoint(), session.getPerspective(), true, session.getProvider());
            authenticateV3((KeystoneAuth) token.getCredentials(), info, session.getConfig());
            break;
    }
}
Also used : OSClientSessionV3(org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV3) Auth(org.openstack4j.openstack.common.Auth) TokenAuth(org.openstack4j.openstack.identity.v3.domain.TokenAuth) KeystoneAuth(org.openstack4j.openstack.identity.v3.domain.KeystoneAuth) KeystoneToken(org.openstack4j.openstack.identity.v3.domain.KeystoneToken) Token(org.openstack4j.model.identity.v3.Token) KeystoneAccess(org.openstack4j.openstack.identity.v2.domain.KeystoneAccess)

Aggregations

KeystoneAccess (org.openstack4j.openstack.identity.v2.domain.KeystoneAccess)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Token (org.openstack4j.model.identity.v3.Token)1 Auth (org.openstack4j.openstack.common.Auth)1 Credentials (org.openstack4j.openstack.identity.v2.domain.Credentials)1 RaxApiKeyCredentials (org.openstack4j.openstack.identity.v2.domain.RaxApiKeyCredentials)1 KeystoneAuth (org.openstack4j.openstack.identity.v3.domain.KeystoneAuth)1 KeystoneToken (org.openstack4j.openstack.identity.v3.domain.KeystoneToken)1 TokenAuth (org.openstack4j.openstack.identity.v3.domain.TokenAuth)1 OSClientSessionV2 (org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV2)1 OSClientSessionV3 (org.openstack4j.openstack.internal.OSClientSession.OSClientSessionV3)1