- private void createGroupsFromDatabase(){
- //get list of groups
- boolean foundDefault = false;
- //need to avoid infinite loop, just in case
- boolean alreadyRunAdd = false;
- while(!foundDefault){
-
- EntityManager em = emf.createEntityManager();
- Query getGroups = em.createQuery("SELECT g FROM GroupEntity g WHERE g.deleted=:deleted");
- getGroups.setParameter("deleted", false);
- List<?> groups = getGroups.getResultList();
- em.close();
- //make a folder for each group in pdps folders
- Path pdpsPath = Paths.get(XACMLProperties.getProperty("xacml.pap.pdps"));
- if(!pdpsPath.toFile().exists()){
- try {
- FileUtils.forceMkdir(pdpsPath.toFile());
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not make the new pdps folder; one does not exist");
- }
- }
- Properties propertyFileProperties = new Properties();
- String groupList = "";
- String defaultGroup = "";
- HashSet<String> currentGroupPaths = new HashSet<>();
- for(Object o : groups){
- GroupEntity group = (GroupEntity)o;
- Path groupPath = Paths.get(pdpsPath.toString(), group.getGroupId());
- currentGroupPaths.add(groupPath.getFileName().toString());
- if(groupPath.toFile().exists()){
- try {
- FileUtils.forceDelete(Paths.get(groupPath.toString(), "xacml.policy.properties").toFile());
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete old xacml.policy.properties file");
- }
- File[] xmlFiles = groupPath.toFile().listFiles(new FileFilter(){
-
- @Override
- public boolean accept(File pathname) {
- return pathname.toString().endsWith(".xml");
- }
-
- });
- for(File deleteMe : xmlFiles){
- try {
- FileUtils.forceDelete(deleteMe);
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete one of the policy files that we are going to replace: "+deleteMe.toString());
- }
- }
- } else {
- try {
- FileUtils.forceMkdir(groupPath.toFile());
- } catch (IOException e) {
- logger.error("Exception Occured"+e);
- }
- }
- Properties policyProperties = new Properties();
- String rootPolicies = "";
- for(PolicyEntity policy : group.getPolicies()){
- Path newPolicyPath = Paths.get(groupPath.toString(),getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
- File newPolicyFile = newPolicyPath.toFile();
- try {
- newPolicyFile.createNewFile();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
- try {
- FileOutputStream policyFileStream = new FileOutputStream(newPolicyFile);
- policyFileStream.write(policy.getPolicyData().getBytes("UTF-8"));
- policyFileStream.close();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
- policyProperties.setProperty(getPdpPolicyName(policy.getPolicyName(),policy.getScope())+".name",removeExtensionAndVersionFromPolicyName(policy.getPolicyName()));
- rootPolicies += ",".concat(getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
- }
- Path xacmlPolicyPropertiesPath = Paths.get(groupPath.toString(),"xacml.policy.properties");
- File xacmlPolicyPropertiesFile = xacmlPolicyPropertiesPath.toFile();
- if(rootPolicies.length() > 0){
- rootPolicies = rootPolicies.substring(1);
- }
- policyProperties.setProperty("xacml.referencedPolicies", "");
- policyProperties.setProperty("xacml.rootPolicies", rootPolicies);
-
- try {
- xacmlPolicyPropertiesFile.createNewFile();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
- try {
- FileOutputStream xacmlPolicyPropertiesFileStream = new FileOutputStream(xacmlPolicyPropertiesFile);
- policyProperties.store(xacmlPolicyPropertiesFileStream, "");
- xacmlPolicyPropertiesFileStream.close();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
-
- em = emf.createEntityManager();
- Query getPdpsQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group AND p.deleted=:deleted");
- getPdpsQuery.setParameter("group", group);
- getPdpsQuery.setParameter("deleted", false);
- List<?> pdps = getPdpsQuery.getResultList();
- em.close();
- String pdpLine = "";
- for(Object o2 : pdps){
- PdpEntity pdp = (PdpEntity)o2;
- pdpLine += ",".concat(pdp.getPdpId());
- propertyFileProperties.setProperty(pdp.getPdpId()+".description",pdp.getDescription());
- propertyFileProperties.setProperty(pdp.getPdpId()+".jmxport",String.valueOf(pdp.getJmxPort()));
- propertyFileProperties.setProperty(pdp.getPdpId()+".name",pdp.getPdpName());
- }
- if(pdpLine.length() > 0){
- pdpLine = pdpLine.substring(1);
- }
- propertyFileProperties.setProperty(group.getGroupId()+".description", group.getDescription());
- propertyFileProperties.setProperty(group.getGroupId()+".name", group.getgroupName());
- propertyFileProperties.setProperty(group.getGroupId()+".pdps",pdpLine);
- groupList += ",".concat(group.getGroupId());
- if(group.isDefaultGroup()){
- defaultGroup = group.getGroupId();
- foundDefault = true;
- }
- }
- //check the list of directories in the pdps folder and make sure none should be deleted
- File[] filesInPdpsFolder = pdpsPath.toFile().listFiles(new FileFilter(){
- @Override
- public boolean accept(File pathname) {
- return pathname.isDirectory();
- }
- });
- for(File f : filesInPdpsFolder){
- if(f.isDirectory()){
- if(!currentGroupPaths.contains(f.toPath().getFileName().toString())){
- try {
- FileUtils.forceDelete(f);
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete pdp group folder, which has been deleted from the database");
- }
- }
- }
- }
- if(!foundDefault && !alreadyRunAdd){
- alreadyRunAdd = true;
- //add default group to db
- try{
- em = emf.createEntityManager();
- em.getTransaction().begin();
- GroupEntity newDefaultGroup = new GroupEntity();
- em.persist(newDefaultGroup);
- newDefaultGroup.setDescription("The default group where new PDP's are put.");
- newDefaultGroup.setGroupId("default");
- newDefaultGroup.setGroupName("default");
- newDefaultGroup.setDefaultGroup(true);
- newDefaultGroup.setCreatedBy("automaticallyAdded");
- newDefaultGroup.setModifiedBy("automaticallyAdded");
- em.flush();
- em.getTransaction().commit();
- continue;
- } catch(Exception e){
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not add a new default group to the database");
- }
- }
-
- Path xacmlPropertiesPath = Paths.get(pdpsPath.toString(),"xacml.properties");
- File xacmlPropertiesFile = xacmlPropertiesPath.toFile();
- if(groupList.length()>0){
- groupList = groupList.substring(1);
- }
- propertyFileProperties.setProperty("xacml.pap.groups",groupList);
- propertyFileProperties.setProperty("xacml.pap.groups.default",defaultGroup);
- try {
- FileUtils.forceDelete(xacmlPropertiesFile);
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old xacml.properties file");
- }
- try {
- xacmlPropertiesFile.createNewFile();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
- try {
- FileOutputStream xacmlPropertiesFileStream = new FileOutputStream(xacmlPropertiesFile);
- propertyFileProperties.store(xacmlPropertiesFileStream, "");
- xacmlPropertiesFileStream.close();
- } catch (IOException e) {
- PolicyLogger.error(e.getMessage());
- }
- //if we get this far down, something went wrong and we don't want to get stuck in the loop
- foundDefault = true;
- }
- //put policies in group folder
- //create xacml.policy.properties in each folder with list of policies in that folder
- //get list of pdps
- //create xacml.properties with list of groups and pdps and other info
- }
-