use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class TokenManagerTests method testVerifyAndResolveTokens.
/**
* Token tests for verify and resolve
*/
@Test
public void testVerifyAndResolveTokens() throws Exception {
commonDefaultSetupForSingleNodeTests();
// Test - new ticket issue
StorageOSUserDAO userDAO = new StorageOSUserDAO();
userDAO.setUserName("root");
userDAO.setIsLocal(true);
StringSet groups = new StringSet();
groups.add("gr1");
groups.add("gr2");
userDAO.setGroups(groups);
StringSet attributes = new StringSet();
attributes.add("atrr1");
attributes.add("attr2");
userDAO.setAttributes(attributes);
final String token = _tokenManager.getToken(userDAO);
Assert.assertNotNull(token);
Token tokenVerif = (Token) _tokenManager.verifyToken(token);
Assert.assertNotNull(tokenVerif);
StorageOSUserDAO gotUser = _tokenManager.resolveUser(tokenVerif);
Assert.assertTrue(gotUser.getIsLocal());
Assert.assertEquals(userDAO.getUserName(), gotUser.getUserName());
Assert.assertEquals(gotUser.getGroups().size(), groups.size());
Assert.assertEquals(gotUser.getAttributes().size(), attributes.size());
// Try with a non local user, make sure local flag is preserved
StorageOSUserDAO userDAO2 = new StorageOSUserDAO();
userDAO2.setUserName("user@domain.com");
userDAO2.setIsLocal(false);
final String token2 = _tokenManager.getToken(userDAO2);
Assert.assertNotNull(token2);
Token tokenVerif2 = (Token) _tokenManager.verifyToken(token2);
Assert.assertNotNull(tokenVerif2);
// make sure the is local flag checks out
StorageOSUserDAO gotUser2 = _tokenManager.resolveUser(tokenVerif2);
Assert.assertFalse(gotUser2.getIsLocal());
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CustomAuthenticationManagerTest method testUserRefresh.
@Test
public void testUserRefresh() throws Exception {
AuthnProvider authConfig = createValidAuthProviderInDB();
// First try to refresh a user that does not exist in the DB- Should fail with a
// BadRequestException, where the message says that the parameter is not valid
String userName = "iShouldntExistAnywhereInTheWholeWideWorld@sanity.local".toLowerCase();
boolean exceptionWasCaught = false;
try {
_authManager.refreshUser(userName);
} catch (SecurityException e) {
// should not get here.
Assert.fail("Got a securityExcpetion instead of BadRequestException, message is " + e.getLocalizedMessage());
} catch (APIException e) {
// this is what is expected
String errorMessage = "Invalid value " + userName + " for parameter username";
assertServiceError(HttpStatus.SC_BAD_REQUEST, ServiceCode.API_PARAMETER_INVALID, errorMessage, e);
exceptionWasCaught = true;
} finally {
Assert.assertTrue("Refresh user call for a user that does not exist in DB did not throw an exception", exceptionWasCaught);
}
// try to refresh a user that doesn't exist in ldap, but exists in the DB- should
// fail with a BadRequestException- Search for {0} failed for this tenant, or
// could not be found for this tenant. make sure the user gets deleted
StorageOSUserDAO userDAO = new StorageOSUserDAO();
userDAO.setId(URIUtil.createId(StorageOSUserDAO.class));
userDAO.setUserName(userName);
_dbClient.createObject(userDAO);
exceptionWasCaught = false;
try {
_authManager.refreshUser(userName);
} catch (SecurityException e) {
Assert.fail("Got a securityExcpetion instead of BadRequestException, message is " + e.getLocalizedMessage());
} catch (APIException e) {
String errorMessage = "Search for " + userName + " failed for this tenant, or could not be found for this tenant.";
assertServiceError(HttpStatus.SC_BAD_REQUEST, ServiceCode.API_BAD_REQUEST, errorMessage, e);
exceptionWasCaught = true;
} finally {
Assert.assertTrue("Refresh user call for a user that does not exist in LDAP did not throw an exception", exceptionWasCaught);
}
StorageOSUserDAO userDAOAfterRefresh = _dbClient.queryObject(StorageOSUserDAO.class, userDAO.getId());
if (userDAOAfterRefresh != null) {
Assert.assertTrue(userDAOAfterRefresh.getInactive());
}
// disable the authProvider and refresh a user- should fail with a
// BadRequestException - Search for {0} failed for this tenant, or
// could not be found for this tenant. make sure the user gets deleted
cleanupProviders();
userName = "sanity_user@sanity.local".toLowerCase();
userDAO = new StorageOSUserDAO();
userDAO.setId(URIUtil.createId(StorageOSUserDAO.class));
userDAO.setUserName(userName);
_dbClient.createObject(userDAO);
exceptionWasCaught = false;
try {
_authManager.refreshUser(userName);
} catch (SecurityException e) {
Assert.fail("Got a securityExcpetion instead of BadRequestException, message is " + e.getLocalizedMessage());
} catch (APIException e) {
String errorMessage = "Search for " + userName + " failed for this tenant, or could not be found for this tenant.";
assertServiceError(HttpStatus.SC_BAD_REQUEST, ServiceCode.API_BAD_REQUEST, errorMessage, e);
exceptionWasCaught = true;
} finally {
Assert.assertTrue("Refresh user call for a user who is not supported by any authentication handler did not throw an exception", exceptionWasCaught);
}
userDAOAfterRefresh = _dbClient.queryObject(StorageOSUserDAO.class, userDAO.getId());
if (userDAOAfterRefresh != null) {
Assert.assertTrue(userDAOAfterRefresh.getInactive());
}
// enable the authProvider and test user refresh - should not throw
authConfig = createValidAuthProviderInDB();
userDAO = new StorageOSUserDAO();
userDAO.setId(URIUtil.createId(StorageOSUserDAO.class));
userDAO.setUserName(userName);
_dbClient.createObject(userDAO);
try {
// refresh the user
_authManager.refreshUser(userName);
} catch (SecurityException e) {
Assert.fail("Got a FatalSecurityException, message is " + e.getLocalizedMessage());
} catch (APIException e) {
Assert.fail("Got a BadRequestException, message is " + e.getLocalizedMessage());
}
userDAOAfterRefresh = _dbClient.queryObject(StorageOSUserDAO.class, userDAO.getId());
Assert.assertNotNull(userDAOAfterRefresh.getTenantId());
Assert.assertTrue("sanity_user@sanity.local is supposed to be mapped to root tenant", _rootTenantId.toString().equals(userDAOAfterRefresh.getTenantId()));
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CassandraTokenManager method updateDBWithUser.
@Override
public StorageOSUserDAO updateDBWithUser(final StorageOSUserDAO userDAO, final List<StorageOSUserDAO> userRecords) {
StorageOSUserDAO user = null;
for (StorageOSUserDAO record : userRecords) {
if (!record.getInactive()) {
// update the record, most of the cases this is a NO-OP
// because user info does not change much
record.updateFrom(userDAO);
user = record;
_dbClient.persistObject(record);
}
}
return user;
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CassandraTokenManager method getToken.
/**
* Persist/Update the StorageOSUserDAO record
* generates a new token or reuses an existing token.
*
* @return token as a String
*/
@Override
public String getToken(StorageOSUserDAO userDAO) {
try {
// always use lower case username for comparing/saving to db
userDAO.setUserName(userDAO.getUserName().toLowerCase());
// find an active user record, if there is one with an active token
List<StorageOSUserDAO> userRecords = getUserRecords(userDAO.getUserName());
StorageOSUserDAO user = updateDBWithUser(userDAO, userRecords);
// do we have a user account to use?
if (user == null) {
// No, create one
userDAO.setId(URIUtil.createId(StorageOSUserDAO.class));
_dbClient.persistObject(userDAO);
user = userDAO;
} else {
// check count
List<Token> tokensForUserId = getTokensForUserId(user.getId());
int maxTokens = user.getUserName().equalsIgnoreCase(PROXY_USER) ? _maxTokensForProxyUser : _maxTokensPerUserId;
double alertTokensSize = (maxTokens * TOKEN_WARNING_EIGHTY_PERCENT);
if (tokensForUserId.size() >= maxTokens) {
throw APIException.unauthorized.maxNumberOfTokenExceededForUser();
} else if (tokensForUserId.size() == (int) alertTokensSize) {
_log.warn("Prior to creating new token, user {} had {} tokens.", user.getUserName(), tokensForUserId.size());
}
}
return _tokenEncoder.encode(TokenOnWire.createTokenOnWire(createNewToken(user)));
} catch (DatabaseException ex) {
_log.error("Exception while persisting user information {}", userDAO.getUserName(), ex);
} catch (SecurityException e) {
_log.error("Token encoding exception. ", e);
}
return null;
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CassandraTokenValidator method resolveUser.
/**
* Gets a userDAO record from a token or proxytoken
*/
@Override
public StorageOSUserDAO resolveUser(BaseToken token) {
if (token == null) {
return null;
}
URI userId = null;
// Skip expiration verification for proxy tokens.
// verify it is still valid, if not remove it from db and send back null
boolean isProxy = token instanceof ProxyToken;
if (isProxy) {
userId = ((ProxyToken) token).peekLastKnownId();
} else {
userId = ((Token) token).getUserId();
}
StorageOSUserDAO userDAO = _dbClient.queryObject(StorageOSUserDAO.class, userId);
if (userDAO == null) {
_log.error("No user record found or userId: {}", userId.toString());
return null;
}
return userDAO;
}
Aggregations