import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.security.InvalidKeyException;
import java.security.Key;
+import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
+import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
import org.onap.policy.rest.XACMLRestProperties;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.dao.PolicyDBException;
import org.onap.policy.rest.jpa.ActionBodyEntity;
import org.onap.policy.rest.jpa.ConfigurationDataEntity;
import org.onap.policy.rest.jpa.DatabaseLockEntity;
* @return The new instance of PolicyDBDao or throw exception if the given emf is null.
* @throws IllegalStateException if a PolicyDBDao has already been constructed. Call getPolicyDBDaoInstance() to get this.
*/
- public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf) throws Exception{
+ public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf){
logger.debug("getPolicyDBDaoInstance(EntityManagerFactory emf) as getPolicyDBDaoInstance("+emf+") called");
if(currentInstance == null){
if(emf != null){
* @return The instance of PolicyDBDao or throws exception if the given instance is null.
* @throws IllegalStateException if a PolicyDBDao instance is null. Call createPolicyDBDaoInstance(EntityManagerFactory emf) to get this.
*/
- public static PolicyDBDao getPolicyDBDaoInstance() throws Exception{
+ public static PolicyDBDao getPolicyDBDaoInstance(){
logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called");
if(currentInstance != null){
return currentInstance;
try{
em.getTransaction().commit();
} catch(Exception e){
+ logger.warn("List of remote PolicyDBDaos will be empty", e);
try{
em.getTransaction().rollback();
} catch(Exception e2){
return urlUserPass;
}
- private static String encryptPassword(String password) throws Exception{
+ private static String encryptPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey());
byte[] encryption = cipher.doFinal(password.getBytes("UTF-8"));
- System.out.println(encryption);
+ logger.debug("Encryption value is " + encryption);
return new String(Base64.getMimeEncoder().encode(encryption),"UTF-8");
}
- private static String decryptPassword(String encryptedPassword) throws Exception{
+ private static String decryptPassword(String encryptedPassword) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey());
byte[] password = cipher.doFinal(Base64.getDecoder().decode(encryptedPassword.getBytes("UTF-8")));
try{
newPolicyDBDaoEntity.setPassword(encryptPassword(url[2]));
} catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
}
try{
em.getTransaction().commit();
} catch(Exception e){
+ logger.debug(e);
try{
em.getTransaction().rollback();
} catch(Exception e2){
+ logger.debug(e2);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not add new PolicyDBDao to the database");
}
}
try{
encryptedPassword = encryptPassword(url[2]);
} catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
}
if(url[1] != null && !stringEquals(url[1], foundPolicyDBDaoEntity.getUsername())){
try{
em.getTransaction().commit();
} catch(Exception e){
+ logger.debug(e);
try{
em.getTransaction().rollback();
} catch(Exception e2){
+ logger.debug(e2);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not update PolicyDBDao in the database");
}
}
try{
password = decryptPassword(dbdEntity.getPassword());
} catch(Exception e){
+ logger.debug(e);
//if we can't decrypt, might as well try it anyway
password = dbdEntity.getPassword();
}
readTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_NOTIFY_TIMEOUT));
} catch(Exception e){
- logger.error("xacml.rest.pap.notify.timeoutms property not set, using a default.");
+ logger.error("xacml.rest.pap.notify.timeoutms property not set, using a default.", e);
readTimeout = 10000;
}
connection.setReadTimeout(readTimeout);
handleIncomingPolicyChange(url, entityId,extraData);
break;
} catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPolicyChange("+url+", "+entityId+", "+extraData+")");
}
try{
Thread.sleep(pauseBetweenRetries);
}catch(InterruptedException ie){
+ Thread.currentThread().interrupt();
break;
}
}
handleIncomingPdpChange(url, entityId, transaction);
break;
} catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPdpChange("+url+", "+entityId+", "+transaction+")");
}
try{
Thread.sleep(pauseBetweenRetries);
}catch(InterruptedException ie){
+ Thread.currentThread().interrupt();
break;
}
}
handleIncomingGroupChange(url, entityId, extraData, transaction, xacmlPapServlet);
break;
}catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingGroupChange("+url+", "+entityId+", "+extraData+", "+transaction+", "+xacmlPapServlet+")");
}
try{
Thread.sleep(pauseBetweenRetries);
}catch(InterruptedException ie){
+ Thread.currentThread().interrupt();
break;
}
}
//no changes should be being made in this function, we still need to close
transaction.rollbackTransaction();
}
- private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException{
+ private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException, PolicyDBException{
GroupEntity groupRecord = null;
long groupIdLong = -1;
try{
//call command that corresponds to the change that was made
}
//this will also handle removes, since incoming pdpGroup has no policies internally, we are just going to add them all in from the db
- private boolean updateGroupPoliciesInFileSystem(OnapPDPGroup pdpGroup,OnapPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException{
+ private boolean updateGroupPoliciesInFileSystem(OnapPDPGroup pdpGroup,OnapPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException, PolicyDBException{
if(!(pdpGroup instanceof StdPDPGroup)){
throw new PAPException("group is not a StdPDPGroup");
}
return didUpdate;
}
- private String removeExtensionAndVersionFromPolicyName(String originalPolicyName){
+ private String removeExtensionAndVersionFromPolicyName(String originalPolicyName) throws PolicyDBException{
return getPolicyNameAndVersionFromPolicyFileName(originalPolicyName)[0];
}
* @param originalPolicyName: a policy file name ex: Config_policy.2.xml
* @return An array [0]: The policy name, [1]: the policy version, as a string
*/
- private String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){
+ private String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName) throws PolicyDBException{
String policyName = originalPolicyName;
String[] nameAndVersion = new String[2];
try{
policyName = removeFileExtension(policyName);
nameAndVersion[0] = policyName.substring(0,policyName.lastIndexOf('.'));
if(isNullOrEmpty(nameAndVersion[0])){
- throw new Exception();
+ throw new PolicyDBException();
}
} catch(Exception e){
nameAndVersion[0] = originalPolicyName;
try{
nameAndVersion[1] = policyName.substring(policyName.lastIndexOf('.')+1);
if(isNullOrEmpty(nameAndVersion[1])){
- throw new Exception();
+ throw new PolicyDBException();
}
} catch(Exception e){
nameAndVersion[1] = "1";
try{
startTransactionSynced(this.em,transactionWaitTime);
} catch(Exception e){
+ logger.debug(e);
throw new PersistenceException("Could not lock transaction within "+transactionWaitTime+" milliseconds");
}
}
+ "\n TimeStamp = " + date.getTime()
+ "\n\n");
}
+ Thread.currentThread().interrupt();
return;
}
if(logger.isDebugEnabled()){
try{
em.close();
}catch(Exception e){
- logger.warn("Could not close already closed transaction");
+ logger.warn("Could not close already closed transaction", e);
}
}
try{
configPath = Paths.get(configPath).toString();
} catch(InvalidPathException e){
- logger.error("Invalid config path: "+configPath);
+ logger.error("Invalid config path: "+configPath, e);
throw new IllegalArgumentException("Invalid config path: "+configPath);
}
return configPath;
@Override
- public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username) throws PAPException {
+ public void deleteGroup(OnapPDPGroup group, OnapPDPGroup moveToGroup, String username) throws PolicyDBException {
logger.debug("deleteGroup(PDPGroup group, PDPGroup moveToGroup, String username) as deleteGroup("+group+", "+moveToGroup+","+username+") called");
if(group == null){
throw new IllegalArgumentException("PDPGroup group cannot be null");
if(group.isDefaultGroup()){
PolicyLogger.error("The default group "+group.getId()+" was attempted to be deleted. It cannot be.");
- throw new PAPException("You cannot delete the default group.");
+ throw new PolicyDBException("You cannot delete the default group.");
}
synchronized(emLock){
checkBeforeOperationRun();
}
} else {
PolicyLogger.error("Group "+group.getId()+" is trying to be delted with PDPs. No group was provided to move them to");
- throw new PAPException("Group has PDPs. Must provide a group for them to move to");
+ throw new PolicyDBException("Group has PDPs. Must provide a group for them to move to");
}
}
}
@Override
- public void addPolicyToGroup(String groupID, String policyID, String username) {
+ public void addPolicyToGroup(String groupID, String policyID, String username) throws PolicyDBException {
logger.debug("addPolicyToGroup(String groupID, String policyID, String username) as addPolicyToGroup("+groupID+", "+policyID+","+username+") called");
if(isNullOrEmpty(groupID, policyID, username)){
throw new IllegalArgumentException("groupID, policyID, and username must not be null or empty");
try{
policyQueryList = policyQuery.getResultList();
} catch(Exception e){
+ logger.debug(e);
PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if policy exists policyQuery.getResultList()");
throw new PersistenceException("Query failed trying to check if policy "+policyNameScopeAndVersion[0]+" exists");
}
}
}
}catch(Exception e){
+ logger.debug(e);
PolicyLogger.error("Could not delete old versions for policy "+policy.getPolicyName()+", ID: "+policy.getPolicyId());
}
group.addPolicyToGroup(policy);
String computeScope(String fullPath, String pathToExclude){
return PolicyDBDao.computeScope(fullPath, pathToExclude);
}
- String encryptPassword(String password) throws Exception{
+ String encryptPassword(String password) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{
return PolicyDBDao.encryptPassword(password);
}
- String decryptPassword(String password) throws Exception{
+ String decryptPassword(String password) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
return PolicyDBDao.decryptPassword(password);
}
String getDescriptionFromXacml(String xacmlData){
return PolicyDBDao.getDescriptionFromXacml(xacmlData);
}
- String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){
+ String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName) throws PolicyDBException{
return PolicyDBDao.this.getPolicyNameAndVersionFromPolicyFileName(originalPolicyName);
}
}