X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fmusic%2Fmain%2FCachingUtil.java;h=a81887a00bc73fdab15d4918939ed435cc6e2693;hb=3fbf486ff0add3ffb5a08d0a57b80802bde1003f;hp=2c46efbcca538ced628f0f83ea1b9af750460666;hpb=67da04ebd086a4e79c9f9acf4d1084729306b2d6;p=music.git diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index 2c46efbc..a81887a0 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -21,30 +21,28 @@ */ package org.onap.music.main; -import java.util.Arrays; import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.UUID; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import org.apache.commons.codec.binary.Base64; import org.apache.commons.jcs.JCS; import org.apache.commons.jcs.access.CacheAccess; -import org.codehaus.jackson.map.ObjectMapper; +import org.mindrot.jbcrypt.BCrypt; import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.datastore.jsonobjects.AAFResponse; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicServiceException; - -import com.att.eelf.configuration.EELFLogger; +import org.onap.music.datastore.jsonobjects.JsonCallback; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; +import com.datastax.driver.core.exceptions.InvalidQueryException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; @@ -62,6 +60,9 @@ public class CachingUtil implements Runnable { private static CacheAccess musicCache = JCS.getInstance("musicCache"); private static CacheAccess> aafCache = JCS.getInstance("aafCache"); private static CacheAccess appNameCache = JCS.getInstance("appNameCache"); + private static CacheAccess> musicValidateCache = JCS.getInstance("musicValidateCache"); + private static CacheAccess callBackCache = JCS.getInstance("callBackCache"); + private static CacheAccess> callbackNotifyList = JCS.getInstance("callbackNotifyList"); private static Map userAttempts = new HashMap<>(); private static Map lastFailedTime = new HashMap<>(); @@ -70,7 +71,25 @@ public class CachingUtil implements Runnable { return true; return false; } + + public static void updateCallBackCache(String appName, JsonCallback jsonCallBack) { + logger.info("updateCallBackCache: updating cache....."); + callBackCache.put(appName, jsonCallBack); + } + + public static JsonCallback getCallBackCache(String appName) { + return callBackCache.get(appName); + } + public static void updateCallbackNotifyList(List notifyList) { + logger.info("callbackNotifyList: updating cache....."); + callbackNotifyList.put("callbackNotify", notifyList); + } + + public static List getCallbackNotifyList() { + return callbackNotifyList.get("callbackNotify"); + } + public void initializeMusicCache() { logger.info(EELFLoggerDelegate.applicationLogger,"Initializing Music Cache..."); musicCache.put("isInitialized", "true"); @@ -86,7 +105,6 @@ public class CachingUtil implements Runnable { pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), false)); } catch (Exception e1) { logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - e1.printStackTrace(); } ResultSet rs = MusicCore.get(pQuery); Iterator it = rs.iterator(); @@ -99,19 +117,18 @@ public class CachingUtil implements Runnable { String keySpace = row.getString("application_name"); try { userAttempts.put(nameSpace, 0); - AAFResponse responseObj = triggerAAF(nameSpace, userId, password); - if (responseObj.getNs().size() > 0) { + boolean responseObj = triggerAAF(nameSpace, userId, password); + if (responseObj) { map = new HashMap<>(); map.put(userId, password); aafCache.put(nameSpace, map); - musicCache.put(nameSpace, keySpace); + musicCache.put(keySpace, nameSpace); logger.debug("Cronjob: Cache Updated with AAF response for namespace " + nameSpace); } } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Something at AAF was changed for ns: " + nameSpace+" So not updating Cache for the namespace. "); - e.printStackTrace(); } } @@ -130,8 +147,8 @@ public class CachingUtil implements Runnable { public static boolean authenticateAAFUser(String nameSpace, String userId, String password, String keySpace) throws Exception { - if (aafCache.get(nameSpace) != null) { - if (keySpace != null && !musicCache.get(nameSpace).equals(keySpace)) { + if (aafCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) { + if (keySpace != null && !musicCache.get(keySpace).equals(nameSpace)) { logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace."); } else if (aafCache.get(nameSpace).get(userId).equals(password)) { logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value.."); @@ -161,20 +178,21 @@ public class CachingUtil implements Runnable { } } - AAFResponse responseObj = triggerAAF(nameSpace, userId, password); - if (responseObj.getNs().size() > 0) { - if (responseObj.getNs().get(0).getAdmin().contains(userId)) { - //Map map = new HashMap<>(); - //map.put(userId, password); - //aafCache.put(nameSpace, map); + boolean responseObj = triggerAAF(nameSpace, userId, password); + if (responseObj) { + //if (responseObj.getNs().get(0).getAdmin().contains(userId)) { + Map map = new HashMap<>(); + map.put(userId, password); + aafCache.put(nameSpace, map); + CachingUtil.updateMusicCache(keySpace, nameSpace); return true; - } + //} } logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated"); return false; } - private static AAFResponse triggerAAF(String nameSpace, String userId, String password) + private static boolean triggerAAF(String nameSpace, String userId, String password) throws Exception { if (MusicUtil.getAafEndpointUrl() == null) { logger.error(EELFLoggerDelegate.errorLogger,"",AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); @@ -207,62 +225,28 @@ public class CachingUtil implements Runnable { // TODO Allow for 2-3 times and forbid any attempt to trigger AAF with invalid values // for specific time. } - response.getHeaders().put(HttpHeaders.CONTENT_TYPE, + /*response.getHeaders().put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON)); // AAFResponse output = response.getEntity(AAFResponse.class); response.bufferEntity(); String x = response.getEntity(String.class); - AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class); + AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class);*/ - return responseObj; + return true; } - public static Map authenticateAIDUser(String aid, String keyspace) - throws Exception { - Map resultMap = new HashMap<>(); - String uuid = null; - /* - * if(aid == null || aid.length() == 0) { resultMap.put("Exception Message", - * "AID is missing for the keyspace requested."); //create a new AID ?? } else - */ - if (musicCache.get(keyspace) == null) { - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "SELECT uuid from admin.keyspace_master where keyspace_name = '" - + keyspace + "' allow filtering"); - Row rs = MusicCore.get(pQuery).one(); - try { - uuid = rs.getUUID("uuid").toString(); - musicCache.put(keyspace, uuid); - } catch (Exception e) { - String msg = e.getMessage(); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - resultMap.put("Exception", "Unauthorized operation. Check AID and Keyspace. " - + "Exception from MUSIC is: " - + (msg == null ? "Keyspace is new so no AID should be passed in Header." - : msg)); - return resultMap; - } - if (!musicCache.get(keyspace).toString().equals(aid)) { - resultMap.put("Exception", - "Unauthorized operation. Invalid AID for the keyspace"); - return resultMap; - } - } else if (musicCache.get(keyspace) != null - && !musicCache.get(keyspace).toString().equals(aid)) { - resultMap.put("Exception Message", - "Unauthorized operation. Invalid AID for the keyspace"); - return resultMap; - } - resultMap.put("aid", uuid); - return resultMap; + public static void updateMusicCache(String keyspace, String nameSpace) { + logger.info(EELFLoggerDelegate.applicationLogger,"Updating musicCache for keyspace " + keyspace + " with nameSpace " + nameSpace); + musicCache.put(keyspace, nameSpace); } - public static void updateMusicCache(String aid, String keyspace) { - logger.info(EELFLoggerDelegate.applicationLogger,"Updating musicCache for keyspace " + keyspace + " with aid " + aid); - musicCache.put(keyspace, aid); + public static void updateMusicValidateCache(String nameSpace, String userId, String password) { + logger.info(EELFLoggerDelegate.applicationLogger,"Updating musicCache for nameSpacce " + nameSpace + " with userId " + userId); + Map map = new HashMap<>(); + map.put(userId, password); + musicValidateCache.put(nameSpace, map); } - + public static void updateisAAFCache(String namespace, String isAAF) { appNameCache.put(namespace, isAAF); } @@ -274,21 +258,26 @@ public class CachingUtil implements Runnable { pQuery.appendQueryString( "SELECT is_aaf from admin.keyspace_master where application_name = '" + namespace + "' allow filtering"); - Row rs = MusicCore.get(pQuery).one(); + Row rs = null; + try { + rs = MusicCore.get(pQuery).one(); + } catch(InvalidQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage()); + throw new MusicServiceException("Please make sure admin.keyspace_master table is configured."); + } try { isAAF = String.valueOf(rs.getBool("is_aaf")); if(isAAF != null) appNameCache.put(namespace, isAAF); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - e.printStackTrace(); } } return isAAF; } public static String getUuidFromMusicCache(String keyspace) throws MusicServiceException { - String uuid = musicCache.get(keyspace); + String uuid = null; if (uuid == null) { PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString( @@ -297,10 +286,8 @@ public class CachingUtil implements Runnable { Row rs = MusicCore.get(pQuery).one(); try { uuid = rs.getUUID("uuid").toString(); - musicCache.put(keyspace, uuid); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,"Exception occured during uuid retrieval from DB."+e.getMessage()); - e.printStackTrace(); } } return uuid; @@ -317,7 +304,6 @@ public class CachingUtil implements Runnable { appName = rs.getString("application_name"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - e.printStackTrace(); } return appName; } @@ -364,31 +350,92 @@ public class CachingUtil implements Runnable { rs = MusicCore.get(queryObject).one(); } catch (MusicServiceException e) { // TODO Auto-generated catch block - e.printStackTrace(); resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage()); return resultMap; - } + } catch (InvalidQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage()); + resultMap.put("Exception", "Please make sure admin.keyspace_master table is configured."); + return resultMap; + } if (rs == null) { logger.error(EELFLoggerDelegate.errorLogger,"Application is not onboarded. Please contact admin."); resultMap.put("Exception", "Application is not onboarded. Please contact admin."); } else { - if(!(rs.getString("username").equals(userId)) && !(rs.getString("password").equals("password"))) { + if(!(rs.getString("username").equals(userId)) || !(BCrypt.checkpw(password, rs.getString("password")))) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); resultMap.put("Exception", "Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); return resultMap; } - boolean is_aaf = rs.getBool("is_aaf"); - String keyspace = rs.getString("keyspace_name"); - if (!is_aaf) { - if (!keyspace.equals(MusicUtil.DEFAULTKEYSPACENAME)) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.DATAERROR); - logger.error(EELFLoggerDelegate.errorLogger,"Non AAF applications are allowed to have only one keyspace per application."); - resultMap.put("Exception", - "Non AAF applications are allowed to have only one keyspace per application."); - } + } + return resultMap; + } + + public static Map authenticateAIDUser(String nameSpace, String userId, String password, + String keyspace) { + Map resultMap = new HashMap<>(); + String pwd = null; + if((musicCache.get(keyspace) != null) && (musicValidateCache.get(nameSpace) != null) + && (musicValidateCache.get(nameSpace).containsKey(userId))) { + if(!musicCache.get(keyspace).equals(nameSpace)) { + resultMap.put("Exception", "Namespace and keyspace doesn't match"); + return resultMap; } + if(!BCrypt.checkpw(password,musicValidateCache.get(nameSpace).get(userId))) { + resultMap.put("Exception", "Namespace, userId and password doesn't match"); + return resultMap; + } + return resultMap; + } + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString( + "select * from admin.keyspace_master where keyspace_name = ? allow filtering"); + try { + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace)); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + } + Row rs = null; + try { + rs = MusicCore.get(queryObject).one(); + } catch (MusicServiceException e) { + resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage()); + return resultMap; } + if(rs == null) { + resultMap.put("Exception", "Please make sure keyspace:"+keyspace+" exists."); + return resultMap; + } + else { + String user = rs.getString("username"); + pwd = rs.getString("password"); + String ns = rs.getString("application_name"); + if(!ns.equals(nameSpace)) { + resultMap.put("Exception", "Namespace and keyspace doesn't match"); + return resultMap; + } + if(!user.equals(userId)) { + resultMap.put("Exception", "Invalid userId :"+userId); + return resultMap; + } + if(!BCrypt.checkpw(password, pwd)) { + resultMap.put("Exception", "Invalid password"); + return resultMap; + } + } + CachingUtil.updateMusicCache(keyspace, nameSpace); + CachingUtil.updateMusicValidateCache(nameSpace, userId, pwd); return resultMap; } + + public static void deleteKeysFromDB(String deleteKeys) { + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")"); + try { + MusicCore.nonKeyRelatedPut(pQuery, "eventual"); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, "Error in deleteKeysFromDB"); + } + } }