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;
}
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);
}
}
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;
}
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");
}
Aggregations