use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class InterVDCTokenCacheHelper method cacheForeignTokenAndKeys.
/**
* saves token artifacts to the cache. The artifacts can be the token & user record, and token key ids.
* Token key ids (TokenKeyBundle) goes to zk. Token and user record goes to cassandra.
*
* @param artifacts
* @param vdcID
*/
public void cacheForeignTokenAndKeys(TokenResponseArtifacts artifacts, String vdcID) {
Token token = artifacts.getToken();
StorageOSUserDAO user = artifacts.getUser();
TokenKeysBundle bundle = artifacts.getTokenKeysBundle();
if (token != null && user != null) {
cacheForeignTokenArtifacts(token, user);
}
if (bundle != null) {
saveTokenKeysBundle(vdcID, bundle);
}
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class InterVDCTokenCacheHelper method cacheForeignTokenArtifacts.
/**
* Saves the token and user dao records to the db. Set the cache expiration time
* to 10 minutes or time left on the token, whichever is sooner.
* Note: this method assumes validity of the token (expiration) has been checked
*
* @param t
* @param user
* @param now current time in minutes
*/
private synchronized void cacheForeignTokenArtifacts(final Token token, final StorageOSUserDAO user) {
long now = System.currentTimeMillis() / (MIN_TO_MSECS);
InterProcessLock tokenLock = null;
try {
tokenLock = coordinator.getLock(token.getId().toString());
if (tokenLock == null) {
log.error("Could not acquire lock for token caching");
throw SecurityException.fatals.couldNotAcquireLockTokenCaching();
}
tokenLock.acquire();
StorageOSUserDAO userToPersist = dbClient.queryObject(StorageOSUserDAO.class, user.getId());
userToPersist = (userToPersist == null) ? new StorageOSUserDAO() : userToPersist;
userToPersist.setAttributes(user.getAttributes());
userToPersist.setCreationTime(user.getCreationTime());
userToPersist.setDistinguishedName(user.getDistinguishedName());
userToPersist.setGroups(user.getGroups());
userToPersist.setId(user.getId());
userToPersist.setIsLocal(user.getIsLocal());
userToPersist.setTenantId(user.getTenantId());
userToPersist.setUserName(user.getUserName());
dbClient.persistObject(userToPersist);
Token tokenToPersist = dbClient.queryObject(Token.class, token.getId());
tokenToPersist = (tokenToPersist == null) ? new Token() : tokenToPersist;
if ((token.getExpirationTime() - now) > maxLifeValuesHolder.getForeignTokenCacheExpirationInMins()) {
tokenToPersist.setCacheExpirationTime(now + maxLifeValuesHolder.getForeignTokenCacheExpirationInMins());
} else {
tokenToPersist.setCacheExpirationTime(token.getExpirationTime());
}
tokenToPersist.setId(token.getId());
// relative index, Id of the userDAO record
tokenToPersist.setUserId(user.getId());
tokenToPersist.setIssuedTime(token.getIssuedTime());
tokenToPersist.setLastAccessTime(now);
tokenToPersist.setExpirationTime(token.getExpirationTime());
tokenToPersist.setIndexed(true);
tokenToPersist.setZoneId(token.getZoneId());
dbClient.persistObject(tokenToPersist);
log.info("Cached user {} and token", user.getUserName());
} catch (Exception ex) {
log.error("Could not acquire lock while trying to get a proxy token.", ex);
} finally {
try {
if (tokenLock != null) {
tokenLock.release();
}
} catch (Exception ex) {
log.error("Unable to release token caching lock", ex);
}
}
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class TokenResponseBuilder method parseTokenResponse.
/**
* Creates a TokenResponseArtifacts holder for items retrieved in a TokenResponse.
* Today, Token and StorageOSUserDAO objects
*
* @param response
* @return
*/
public static TokenResponseArtifacts parseTokenResponse(TokenResponse response) {
String userEncoded = response.getUserDAO();
String tokenEncoded = response.getToken();
String tokenKeysBundleEncoded = response.getTokenKeysBundle();
StorageOSUserDAO user = null;
Token token = null;
TokenKeysBundle tokenKeysBundle = null;
if (StringUtils.isNotBlank(userEncoded)) {
try {
user = (StorageOSUserDAO) SerializerUtils.deserialize(userEncoded);
} catch (UnsupportedEncodingException e) {
log.error("Could not decode user: ", e);
} catch (Exception e) {
log.error("Could not deserialize user: ", e);
}
}
if (StringUtils.isNotBlank(tokenEncoded)) {
try {
token = (Token) SerializerUtils.deserialize(tokenEncoded);
} catch (UnsupportedEncodingException e) {
log.error("Could not decode token: ", e);
} catch (Exception e) {
log.error("Could not deserialize token: ", e);
}
}
if (StringUtils.isNotBlank(tokenKeysBundleEncoded)) {
try {
tokenKeysBundle = (TokenKeysBundle) SerializerUtils.deserialize(tokenKeysBundleEncoded);
} catch (UnsupportedEncodingException e) {
log.error("Could not decode token keys bundle: ", e);
} catch (Exception e) {
log.error("Could not deserialize token keys bundle: ", e);
}
}
return new TokenResponseBuilder.TokenResponseArtifacts(user, token, tokenKeysBundle);
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CassandraTokenValidator method deleteTokenInternal.
/**
* Delete the given token from db, if this is last token referring the userDAO,
* and there are no proxy token associated, mark the userDAO for deletion
*
* @param token
*/
protected void deleteTokenInternal(Token token) {
URI userId = token.getUserId();
_dbClient.removeObject(token);
List<Token> tokens = getTokensForUserId(userId);
List<ProxyToken> pTokens = getProxyTokensForUserId(userId);
if (CollectionUtils.isEmpty(tokens) && CollectionUtils.isEmpty(pTokens)) {
_log.info("There are no more tokens referring to the user id {}, marking it inactive");
StorageOSUserDAO userDAO = _dbClient.queryObject(StorageOSUserDAO.class, userId);
_dbClient.markForDeletion(userDAO);
}
}
use of com.emc.storageos.db.client.model.StorageOSUserDAO in project coprhd-controller by CoprHD.
the class CassandraTokenValidator method getForeignToken.
/**
* Queries the remote VDC for token and userdao objects
*
* @param tw TokenOnWire object
* @param rawToken the rawToken to send to the remote vdc
* @return
*/
private StorageOSUserDAO getForeignToken(TokenOnWire tw, String rawToken) {
StorageOSUserDAO userFromCache = this.foreignTokenCacheLookup(tw);
if (userFromCache != null) {
return userFromCache;
}
try {
String shortVDCid = URIUtil.parseVdcIdFromURI(tw.getTokenId());
TokenResponse response = geoClientCacheMgt.getGeoClient(shortVDCid).getToken(rawToken, null, null);
if (response != null) {
TokenResponseArtifacts artifacts = TokenResponseBuilder.parseTokenResponse(response);
_log.info("Got username for foreign token: {}", artifacts.getUser().getUserName());
_log.debug("Got token object: {}", artifacts.getToken().getId().toString());
interVDCTokenCacheHelper.cacheForeignTokenAndKeys(artifacts, shortVDCid);
return artifacts.getUser();
} else {
_log.error("Null response from getForeignToken call. It's possible remote vdc is not reachable.");
}
} catch (Exception e) {
_log.error("Could not validate foreign token ", e);
}
return null;
}
Aggregations