use of com.emc.storageos.db.client.model.ProxyToken in project coprhd-controller by CoprHD.
the class CassandraTokenValidator method fetchTokenLocal.
/**
* Retrieves a token and checks expiration
*
* @param tw
* @return
*/
private BaseToken fetchTokenLocal(TokenOnWire tw) {
BaseToken verificationToken = null;
URI tkId = tw.getTokenId();
if (!tw.isProxyToken()) {
verificationToken = _dbClient.queryObject(Token.class, tkId);
if (null != verificationToken && !checkExpiration(((Token) verificationToken), true)) {
_log.warn("Token found in database but is expired: {}", verificationToken.getId());
return null;
}
} else {
verificationToken = _dbClient.queryObject(ProxyToken.class, tkId);
if (null != verificationToken && !checkExpiration((ProxyToken) verificationToken)) {
_log.warn("ProxyToken found in database but is expired: {}", verificationToken.getId());
return null;
}
}
if (verificationToken == null) {
_log.error("Could not find token with id {} for validation", tkId);
}
return verificationToken;
}
use of com.emc.storageos.db.client.model.ProxyToken in project coprhd-controller by CoprHD.
the class CassandraTokenManager method getProxyToken.
/**
* Gets a proxy token for the given user
* If a proxy token for the given user already exists, it will be reused
*
* @return proxy-token
*/
@Override
public String getProxyToken(StorageOSUserDAO userDAO) {
InterProcessLock userLock = null;
try {
userLock = _coordinator.getLock(userDAO.getUserName());
if (userLock == null) {
_log.error("Could not acquire lock for user: {}", userDAO.getUserName());
throw SecurityException.fatals.couldNotAcquireLockForUser(userDAO.getUserName());
}
userLock.acquire();
// Look for proxy tokens based on that username.
// If any is found, use that. Else, create a new one.
ProxyToken proxyToken = getProxyTokenForUserName(userDAO.getUserName());
if (proxyToken != null) {
_log.debug("Found proxy token {} for user {}. Reusing...", proxyToken.getId(), userDAO.getUserName());
return _tokenEncoder.encode(TokenOnWire.createTokenOnWire(proxyToken));
}
// No proxy token found for this user. Create a new one.
// Create the actual proxy token
ProxyToken pToken = new ProxyToken();
pToken.setId(URIUtil.createId(ProxyToken.class));
pToken.addKnownId(userDAO.getId());
pToken.setUserName(userDAO.getUserName());
// for now
pToken.setZoneId("zone1");
pToken.setIssuedTime(getCurrentTimeInMins());
pToken.setLastValidatedTime(getCurrentTimeInMins());
_dbClient.persistObject(pToken);
return _tokenEncoder.encode(TokenOnWire.createTokenOnWire(pToken));
} catch (DatabaseException ex) {
_log.error("DatabaseException while persisting proxy token", ex);
} catch (SecurityException ex) {
_log.error("Proxy Token encoding exception. ", ex);
} catch (Exception ex) {
_log.error("Could not acquire lock while trying to get a proxy token.", ex);
} finally {
try {
if (userLock != null) {
userLock.release();
}
} catch (Exception ex) {
_log.error("Unable to release proxytoken creation lock", ex);
}
}
return null;
}
use of com.emc.storageos.db.client.model.ProxyToken in project coprhd-controller by CoprHD.
the class CassandraTokenManager method deleteAllTokensForUser.
/**
* Delete all tokens belonging to the user and mark all the user records for this user for deletion.
*/
@Override
public void deleteAllTokensForUser(String userName, boolean includeProxyTokens) {
try {
List<StorageOSUserDAO> userRecords = getUserRecords(userName.toLowerCase());
for (StorageOSUserDAO userRecord : userRecords) {
List<Token> tokensToDelete = getTokensForUserId(userRecord.getId());
for (Token token : tokensToDelete) {
_log.info("Removing token {} using userDAO {} for username {}", new String[] { token.getId().toString(), userRecord.getId().toString(), userName });
_dbClient.removeObject(token);
cleanUpRequestedTokenMap(token);
}
// making proxy token deletion optional
List<ProxyToken> pTokensToDelete = getProxyTokensForUserId(userRecord.getId());
if (includeProxyTokens) {
for (ProxyToken token : pTokensToDelete) {
_log.info("Removing proxy token {} using userDAO {} for username {}", new String[] { token.getId().toString(), userRecord.getId().toString(), userName });
_dbClient.removeObject(token);
}
_log.info("Marking for deletion: user record {} for username {}", userRecord.getId().toString(), userName);
_dbClient.markForDeletion(userRecord);
} else if (pTokensToDelete.isEmpty()) {
_log.info("No proxy tokens found. Marking for deletion: user record {} for username {}", userRecord.getId().toString(), userName);
_dbClient.markForDeletion(userRecord);
}
}
} catch (DatabaseException ex) {
throw SecurityException.fatals.exceptionDuringTokenDeletionForUser(userName, ex);
}
}
use of com.emc.storageos.db.client.model.ProxyToken in project coprhd-controller by CoprHD.
the class TokenManagerTests method getProxyTokenCountForUser.
/**
* returns number of current proxytokens for username
*
* @param username
* @return
*/
private int getProxyTokenCountForUser(String username) throws IOException {
URIQueryResultList tokens = new URIQueryResultList();
_dbClient.queryByConstraint(AlternateIdConstraint.Factory.getProxyTokenUserNameConstraint(username), tokens);
List<URI> uris = new ArrayList<URI>();
for (Iterator<URI> it = tokens.iterator(); it.hasNext(); ) {
uris.add(it.next());
}
List<ProxyToken> toReturn = _dbClient.queryObject(ProxyToken.class, uris);
if (toReturn == null) {
return 0;
}
return toReturn.size();
}
Aggregations