Search in sources :

Example 6 with ContextConnectionException

use of com.att.cdp.exceptions.ContextConnectionException in project AJSC by att.

the class AbstractOpenStackIdentityService method getRoles.

/**
 * @see com.att.cdp.zones.IdentityService#getRoles()
 */
@SuppressWarnings("nls")
@Override
public List<String> getRoles() throws ZoneException {
    trackRequest();
    Context context = getContext();
    ArrayList<String> list = new ArrayList<>();
    if (context.isLoggedIn()) {
        try {
            keystoneUrl = context.getProperties().getProperty(ContextFactory.PROPERTY_IDENTITY_URL);
            // tenantName = context.getProperties().getProperty(ContextFactory.PROPERTY_TENANT);
            Keystone keystone = new Keystone(keystoneUrl);
            OpenStackRequest<Roles> request = new OpenStackRequest<>(keystone, HttpMethod.GET, "/users/" + context.getPrincipal() + "/roles", null, Roles.class);
            Roles roles;
            try {
                roles = keystone.execute(request);
            } catch (OpenStackConnectException e) {
                throw new ContextConnectionException(EELFResourceManager.format(OSMsg.PAL_OS_CONNECTION_FAILED, "Identity", keystoneUrl), e);
            }
            for (Role role : roles.getList()) {
                list.add(role.getName());
            }
        } catch (OpenStackResponseException e) {
            if (e.getStatus() == 404) {
                throw new ResourceNotFoundException("Attempt to get roles for user " + context.getPrincipal(), e);
            }
            throw new ZoneException("Attempt to get roles for user " + context.getPrincipal(), e);
        }
    }
    return list;
}
Also used : OpenStackContext(com.att.cdp.openstack.OpenStackContext) Context(com.att.cdp.zones.Context) ArrayList(java.util.ArrayList) OpenStackRequest(com.woorea.openstack.base.client.OpenStackRequest) Roles(com.woorea.openstack.keystone.model.Roles) Role(com.woorea.openstack.keystone.model.Role) ContextConnectionException(com.att.cdp.exceptions.ContextConnectionException) Keystone(com.woorea.openstack.keystone.Keystone) ZoneException(com.att.cdp.exceptions.ZoneException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) ResourceNotFoundException(com.att.cdp.exceptions.ResourceNotFoundException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException)

Example 7 with ContextConnectionException

use of com.att.cdp.exceptions.ContextConnectionException in project AJSC by att.

the class AbstractContext method login.

/**
 * This method delegates to the identity service the request to login. This is a convenience method.
 *
 * @throws ZoneException
 *             If any of the following conditions are true:
 *             <ul>
 *             <li>the user has not successfully logged in to the provider</li>
 *             <li>the context has been closed and this service is requested</li>
 *             <li>the current user does not have the rights to perform this operation</li>
 *             <li>the user and/or credentials are not valid</li>
 *             </ul>
 * @see com.att.cdp.zones.Context#login(java.lang.String, java.lang.String)
 */
@SuppressWarnings("nls")
@Override
public void login(String principal, String credential) throws IllegalStateException, IllegalArgumentException, ZoneException {
    this.principal = principal;
    this.credentials = credential;
    String msg = String.format("About to login principal [%s] to provider [%s] on tenant [%s] ", principal, provider.getName(), tenantName);
    appLogger.debug(msg);
    securityLogger.info(msg);
    IdentityService identity = getIdentityService();
    if (identity == null) {
        msg = EELFResourceManager.format(Msg.NO_PROVIDER_SERVICE, "Identity", provider.getName());
        appLogger.error(msg);
        securityLogger.error(msg);
        throw new IllegalStateException(msg);
    }
    if (principal == null || principal.trim().length() == 0) {
        msg = EELFResourceManager.format(Msg.INVALID_PRINCIPAL, principal, provider.getName());
        appLogger.error(msg);
        securityLogger.error(msg);
        throw new IllegalArgumentException(msg);
    }
    if (credential == null || credential.trim().length() == 0) {
        msg = EELFResourceManager.format(Msg.INVALID_CREDENTIAL, provider.getName());
        appLogger.error(msg);
        securityLogger.error(msg);
        throw new IllegalArgumentException(msg);
    }
    /*
         * This logic was incorrect and not handling the failed login attempts correctly. This has been revised. If we
         * catch a connection exception during authentication, we will attempt recovery in case it is a communications
         * error. If the retries are exhausted, then we will throw an IllegalStateException.
         */
    int attempts = 0;
    while (attempts < getRetryLimit()) {
        try {
            identity.authenticate(principal, credential);
            msg = EELFResourceManager.format(Msg.PRINCIPAL_HAS_BEEN_AUTHENTICATED, principal, provider.getName(), tenantName);
            appLogger.debug(msg);
            securityLogger.info(msg);
            loggedIn = true;
            tenantName = identity.getTenant().getName();
            String providerName = provider.getName();
            appLogger.debug(EELFResourceManager.format(Msg.PROVIDER_LOGIN, principal, providerName));
            securityLogger.debug(EELFResourceManager.format(Msg.PROVIDER_LOGIN, principal, providerName));
            break;
        } catch (ContextConnectionException e) {
            appLogger.error(EELFResourceManager.format(Msg.RETRY_PROVIDER_CONNECTION, identity.getURL(), e.getClass().getSimpleName(), e.getMessage(), Integer.toString(attempts + 1), Integer.toString(getRetryLimit()), Integer.toString(getRetryDelay())));
            try {
                Thread.sleep(getRetryDelay() * 1000L);
            } catch (InterruptedException ex) {
            // ignore
            }
            attempts++;
        }
    }
    if (attempts >= getRetryLimit()) {
        msg = EELFResourceManager.format(Msg.NO_PROVIDER_SERVICE, "Identity", provider.getName());
        appLogger.error(msg);
        securityLogger.error(msg);
        throw new IllegalStateException(msg);
    }
}
Also used : IdentityService(com.att.cdp.zones.IdentityService) ContextConnectionException(com.att.cdp.exceptions.ContextConnectionException)

Example 8 with ContextConnectionException

use of com.att.cdp.exceptions.ContextConnectionException in project AJSC by att.

the class AbstractOpenStackIdentityService method getTenant.

/**
 * All services must be able to return the tenant object that the user has connected to.
 *
 * @return The tenant object
 * @throws ZoneException
 *             If the user has not logged in
 * @see com.att.cdp.zones.Service#getTenant()
 */
@Override
public Tenant getTenant() throws ZoneException {
    checkLoggedIn();
    Context context = getContext();
    trackRequest();
    Keystone keystone = getKeystone();
    keystoneUrl = context.getProperties().getProperty(ContextFactory.PROPERTY_IDENTITY_URL);
    if (tenant == null) {
        com.woorea.openstack.keystone.model.Tenants tenants;
        try {
            tenants = keystone.tenants().list().execute();
        } catch (OpenStackConnectException e) {
            throw new ContextConnectionException(EELFResourceManager.format(OSMsg.PAL_OS_CONNECTION_FAILED, "Identity", keystoneUrl), e);
        } catch (OpenStackResponseException e) {
            throw new ZoneException(EELFResourceManager.format(OSMsg.PAL_OS_REQUEST_FAILURE, "get tenant " + tenantName), e);
        }
        for (com.woorea.openstack.keystone.model.Tenant t : tenants) {
            if (t.getName().equals(tenantName)) {
                tenant = new OpenStackTenant((OpenStackContext) context, t);
                break;
            }
        }
    }
    return tenant;
}
Also used : OpenStackContext(com.att.cdp.openstack.OpenStackContext) Context(com.att.cdp.zones.Context) OpenStackTenant(com.att.cdp.openstack.model.OpenStackTenant) OpenStackContext(com.att.cdp.openstack.OpenStackContext) ContextConnectionException(com.att.cdp.exceptions.ContextConnectionException) Keystone(com.woorea.openstack.keystone.Keystone) ZoneException(com.att.cdp.exceptions.ZoneException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) Tenants(com.woorea.openstack.keystone.model.Tenants) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException)

Example 9 with ContextConnectionException

use of com.att.cdp.exceptions.ContextConnectionException in project AJSC by att.

the class AbstractOpenStackIdentityService method getKeyPairs.

/**
 * @see com.att.cdp.zones.IdentityService#getKeyPairs()
 */
@SuppressWarnings("nls")
@Override
public List<KeyPair> getKeyPairs() throws ZoneException {
    trackRequest();
    Context context = getContext();
    if (context.isLoggedIn()) {
        NovaConnector connector = ((OpenStackContext) context).getNovaConnector();
        KeyPairs pairs = null;
        try {
            pairs = connector.getClient().keyPairs().list().execute();
        } catch (OpenStackConnectException e) {
            throw new ContextConnectionException(EELFResourceManager.format(OSMsg.PAL_OS_CONNECTION_FAILED, "Compute", connector.getEndpoint()), e);
        } catch (OpenStackResponseException e) {
            throw new ZoneException(EELFResourceManager.format(OSMsg.PAL_OS_REQUEST_FAILURE, "get key-pair list"), e);
        }
        ArrayList<KeyPair> list = new ArrayList<>();
        for (com.woorea.openstack.nova.model.KeyPair pair : pairs.getList()) {
            OpenStackKeyPair kp = new OpenStackKeyPair(context, pair);
            list.add(kp);
        }
        return list;
    }
    throw new ZoneException("Unable to retrieve key-pairs when the context has not been logged in and authenticated");
}
Also used : OpenStackContext(com.att.cdp.openstack.OpenStackContext) Context(com.att.cdp.zones.Context) KeyPair(com.att.cdp.zones.model.KeyPair) OpenStackKeyPair(com.att.cdp.openstack.model.OpenStackKeyPair) KeyPairs(com.woorea.openstack.nova.model.KeyPairs) ArrayList(java.util.ArrayList) OpenStackContext(com.att.cdp.openstack.OpenStackContext) ContextConnectionException(com.att.cdp.exceptions.ContextConnectionException) ZoneException(com.att.cdp.exceptions.ZoneException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) OpenStackKeyPair(com.att.cdp.openstack.model.OpenStackKeyPair) NovaConnector(com.att.cdp.openstack.connectors.NovaConnector) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException)

Aggregations

ContextConnectionException (com.att.cdp.exceptions.ContextConnectionException)9 OpenStackContext (com.att.cdp.openstack.OpenStackContext)8 OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)8 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)8 ZoneException (com.att.cdp.exceptions.ZoneException)7 Context (com.att.cdp.zones.Context)7 NovaConnector (com.att.cdp.openstack.connectors.NovaConnector)4 Keystone (com.woorea.openstack.keystone.Keystone)4 OpenStackKeyPair (com.att.cdp.openstack.model.OpenStackKeyPair)3 OpenStackTenant (com.att.cdp.openstack.model.OpenStackTenant)3 Tenants (com.woorea.openstack.keystone.model.Tenants)3 KeyPairs (com.woorea.openstack.nova.model.KeyPairs)2 ArrayList (java.util.ArrayList)2 AuthenticationException (com.att.cdp.exceptions.AuthenticationException)1 ResourceNotFoundException (com.att.cdp.exceptions.ResourceNotFoundException)1 CommonIdentityService (com.att.cdp.openstack.CommonIdentityService)1 IdentityService (com.att.cdp.zones.IdentityService)1 KeyPair (com.att.cdp.zones.model.KeyPair)1 Tenant (com.att.cdp.zones.model.Tenant)1 OpenStackRequest (com.woorea.openstack.base.client.OpenStackRequest)1