diff options
author | Thomas Nelson Jr (arthurdent3) <tn1381@att.com> | 2018-02-23 15:14:24 -0500 |
---|---|---|
committer | Thomas Nelson Jr (arthurdent3) <tn1381@att.com> | 2018-02-23 15:29:08 -0500 |
commit | b6a14a0dc52c9ed43b035aff79831fc32fd52fe3 (patch) | |
tree | 400ba474c44d779632406d3d998e65038816eed1 /src/main/java/org/onap/music/main | |
parent | 3acbae6bf3be04f352bfaac340b8303064a2f7c4 (diff) |
Changes related to Authentication and Atomic
Authentication Fixes
Atomic with delete added.
Issue-ID: MUSIC-38,MUSIC-39
Change-Id: Ide2974a42ff8618d8a94ef7e8a1b0abde3630550
Signed-off-by: Thomas Nelson Jr (arthurdent3) <tn1381@att.com>
Diffstat (limited to 'src/main/java/org/onap/music/main')
-rwxr-xr-x | src/main/java/org/onap/music/main/CachingUtil.java | 60 | ||||
-rw-r--r-- | src/main/java/org/onap/music/main/MusicCore.java | 135 | ||||
-rwxr-xr-x | src/main/java/org/onap/music/main/MusicUtil.java | 2 |
3 files changed, 141 insertions, 56 deletions
diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index cd1d565d..4b2b4824 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -57,6 +57,7 @@ public class CachingUtil implements Runnable { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CachingUtil.class); private static CacheAccess<String, String> musicCache = JCS.getInstance("musicCache"); + private static CacheAccess<String, String> musicLockCache = JCS.getInstance("musicLockCache"); private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache"); private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache"); private static Map<String, Number> userAttempts = new HashMap<>(); @@ -76,7 +77,7 @@ public class CachingUtil implements Runnable { public void initializeAafCache() throws MusicServiceException { logger.info(EELFLoggerDelegate.applicationLogger,"Resetting and initializing AAF Cache..."); - String query = "SELECT application_name, keyspace_name, username, password FROM admin.keyspace_master WHERE is_api = ? allow filtering"; + String query = "SELECT uuid, application_name, keyspace_name, username, password FROM admin.keyspace_master WHERE is_api = ? allow filtering"; PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString(query); try { @@ -94,6 +95,7 @@ public class CachingUtil implements Runnable { String userId = row.getString("username"); String password = row.getString("password"); String keySpace = row.getString("application_name"); + String uuid = row.getUUID("uuid").toString(); try { userAttempts.put(nameSpace, 0); AAFResponse responseObj = triggerAAF(nameSpace, userId, password); @@ -102,6 +104,7 @@ public class CachingUtil implements Runnable { map.put(userId, password); aafCache.put(nameSpace, map); musicCache.put(nameSpace, keySpace); + musicLockCache.put(nameSpace, uuid); logger.debug("Cronjob: Cache Updated with AAF response for namespace " + nameSpace); } @@ -130,7 +133,7 @@ public class CachingUtil implements Runnable { String keySpace) throws Exception { if (aafCache.get(nameSpace) != null) { - if (!musicCache.get(nameSpace).equals(keySpace)) { + if (keySpace != null && !musicCache.get(nameSpace).equals(keySpace)) { logger.debug("Create new application for the same namespace."); } else if (aafCache.get(nameSpace).get(userId).equals(password)) { logger.debug("Authenticated with cache value.."); @@ -161,9 +164,12 @@ public class CachingUtil implements Runnable { AAFResponse responseObj = triggerAAF(nameSpace, userId, password); if (responseObj.getNs().size() > 0) { - if (responseObj.getNs().get(0).getAdmin().contains(userId)) - return true; - + if (responseObj.getNs().get(0).getAdmin().contains(userId)) { + //Map<String, String> map = new HashMap<>(); + //map.put(userId, password); + //aafCache.put(nameSpace, map); + return true; + } } logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated"); return false; @@ -250,6 +256,43 @@ public class CachingUtil implements Runnable { resultMap.put("aid", uuid); return resultMap; } + + + public static Map<String, Object> authenticateAIDUserLock(String aid, String nameSpace) + throws Exception { + Map<String, Object> resultMap = new HashMap<>(); + String uuid = null; + + if (musicLockCache.get(nameSpace) == null) { + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "SELECT uuid from admin.keyspace_master where application_name = '" + + nameSpace + "' allow filtering"); + Row rs = MusicCore.get(pQuery).one(); + try { + uuid = rs.getUUID("uuid").toString(); + musicLockCache.put(nameSpace, uuid); + } catch (Exception e) { + logger.error("Exception occured during uuid retrieval from DB." + e.getMessage()); + resultMap.put("Exception", "Unauthorized operation. Check AID and Namespace. "); + return resultMap; + } + if (!musicLockCache.get(nameSpace).toString().equals(aid)) { + resultMap.put("Exception Message", + "Unauthorized operation. Invalid AID for the Namespace"); + return resultMap; + } + } else if (musicLockCache.get(nameSpace) != null + && !musicLockCache.get(nameSpace).toString().equals(aid)) { + resultMap.put("Exception Message", + "Unauthorized operation. Invalid AID for the Namespace"); + return resultMap; + } + return resultMap; + } + + + public static void updateMusicCache(String aid, String keyspace) { logger.info("Updating musicCache for keyspace " + keyspace + " with aid " + aid); @@ -346,14 +389,15 @@ public class CachingUtil implements Runnable { } PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString( - "select * from admin.keyspace_master where application_name=? and username=? allow filtering"); + "select * from admin.keyspace_master where application_name = ? and username = ? and password = ? allow filtering"); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); Row rs = MusicCore.get(queryObject).one(); if (rs == null) { - logger.error(EELFLoggerDelegate.errorLogger,"Namespace and UserId doesn't match. namespace: "+ns+" and userId: "+userId); + logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); - resultMap.put("Exception", "Namespace and UserId doesn't match. namespace: "+ns+" and userId: "+userId); + resultMap.put("Exception", "Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); } else { boolean is_aaf = rs.getBool("is_aaf"); String keyspace = rs.getString("keyspace_name"); diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index 96056160..3d5cf4e5 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -385,44 +385,7 @@ public class MusicCore { } - /** - * this function is mainly for the benchmarks to see the effect of lock deletion. - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param conditionInfo - * @return - */ - public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, - String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) { - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - deleteLock(key); - long lockDeleteTime = System.currentTimeMillis(); - String timingInfo = "|lock creation time:" + (lockCreationTime - start) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|critical put time:" + (criticalPutTime - lockAcqTime) - + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; - criticalPutResult.setTimingInfo(timingInfo); - return criticalPutResult; - } else { - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - deleteLock(key); - return lockAcqResult; - } - } + /** * @@ -515,6 +478,10 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to release lock:" + (end - start) + " ms"); return mls; } + + public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{ + getLockingServiceHandle().unlockAndDeleteId(lockId); + } /** * @@ -734,22 +701,63 @@ public class MusicCore { * @param primaryKey primary key value * @param queryObject query object containing prepared query and values * @return ReturnType + * @throws MusicLockingException */ public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) { + PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { + + long start = System.currentTimeMillis(); + String key = keyspaceName + "." + tableName + "." + primaryKey; + String lockId = createLockReference(key); + long lockCreationTime = System.currentTimeMillis(); + ReturnType lockAcqResult = acquireLock(key, lockId); + long lockAcqTime = System.currentTimeMillis(); + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); + ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, + queryObject, lockId, conditionInfo); + long criticalPutTime = System.currentTimeMillis(); + voluntaryReleaseLock(lockId); + long lockDeleteTime = System.currentTimeMillis(); + String timingInfo = "|lock creation time:" + (lockCreationTime - start) + + "|lock accquire time:" + (lockAcqTime - lockCreationTime) + + "|critical put time:" + (criticalPutTime - lockAcqTime) + + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; + criticalPutResult.setTimingInfo(timingInfo); + return criticalPutResult; + } else { + logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); + destroyLockRef(lockId); + return lockAcqResult; + } + } + + /** + * this function is mainly for the benchmarks to see the effect of lock deletion. + * + * @param keyspaceName + * @param tableName + * @param primaryKey + * @param queryObject + * @param conditionInfo + * @return + * @throws MusicLockingException + */ + public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, + String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { + long start = System.currentTimeMillis(); String key = keyspaceName + "." + tableName + "." + primaryKey; String lockId = createLockReference(key); long lockCreationTime = System.currentTimeMillis(); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = acquireLock(key, lockId); long lockAcqTime = System.currentTimeMillis(); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, queryObject, lockId, conditionInfo); long criticalPutTime = System.currentTimeMillis(); - boolean voluntaryRelease = true; deleteLock(key); long lockDeleteTime = System.currentTimeMillis(); String timingInfo = "|lock creation time:" + (lockCreationTime - start) @@ -760,10 +768,12 @@ public class MusicCore { return criticalPutResult; } else { logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - destroyLockRef(lockId); + deleteLock(key); return lockAcqResult; } } + + /** @@ -775,25 +785,48 @@ public class MusicCore { * @param queryObject query object containing prepared query and values * @return ResultSet * @throws MusicServiceException + * @throws MusicLockingException */ public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException { + PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { String key = keyspaceName + "." + tableName + "." + primaryKey; String lockId = createLockReference(key); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = acquireLock(key, lockId); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - boolean voluntaryRelease = true; - releaseLock(lockId, voluntaryRelease); + voluntaryReleaseLock(lockId); return result; } else { + destroyLockRef(lockId); logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); return null; } } + + public static ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey, + PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { + String key = keyspaceName + "." + tableName + "." + primaryKey; + String lockId = createLockReference(key); + long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); + + ReturnType lockAcqResult = acquireLock(key, lockId); + + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId); + ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); + deleteLock(key); + return result; + } else { + deleteLock(key); + logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId); + return null; + } + } + + /** * authenticate user logic @@ -822,7 +855,13 @@ public class MusicCore { resultMap.put("Exception", "Aid is mandatory for nonAAF applications "); return resultMap; } - resultMap = CachingUtil.authenticateAIDUser(aid, keyspace); + if(operation.contains("Lock")) { + resultMap = CachingUtil.authenticateAIDUserLock(aid, nameSpace); + } + else { + resultMap = CachingUtil.authenticateAIDUser(aid, keyspace); + } + if (!resultMap.isEmpty()) return resultMap; } @@ -837,7 +876,7 @@ public class MusicCore { if (isAAF && nameSpace != null && userId != null && password != null) { boolean isValid = true; try { - isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); + isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace); resultMap.put("Exception", e.getMessage()); diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 47e23973..317f2560 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -66,6 +66,8 @@ public class MusicUtil { public static final String ATOMIC = "atomic"; public static final String EVENTUAL = "eventual"; public static final String CRITICAL = "critical"; + public static final String ATOMICDELETELOCK = "atomic_delete_lock"; + public static final String DEFAULTKEYSPACENAME = "TBD"; private static String cassName = "cassandra"; private static String cassPwd = "cassandra"; |