UpgradeCatalog242 extends AbstractUpgradeCatalog { @Transactional void convertRolePrincipals() { LOG.info("Converting pseudo principle types to role principals"); PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class); PrivilegeDAO privilegeDAO = injector.getInstance(PrivilegeDAO.class); PrincipalDAO principalDAO = injector.getInstance(PrincipalDAO.class); PrincipalTypeDAO principalTypeDAO = injector.getInstance(PrincipalTypeDAO.class); Map<String, String> principalTypeToRole = new HashMap<String, String>(); principalTypeToRole.put("ALL.CLUSTER.ADMINISTRATOR", "CLUSTER.ADMINISTRATOR"); principalTypeToRole.put("ALL.CLUSTER.OPERATOR", "CLUSTER.OPERATOR"); principalTypeToRole.put("ALL.CLUSTER.USER", "CLUSTER.USER"); principalTypeToRole.put("ALL.SERVICE.ADMINISTRATOR", "SERVICE.ADMINISTRATOR"); principalTypeToRole.put("ALL.SERVICE.OPERATOR", "SERVICE.OPERATOR"); principalTypeToRole.put("ALL.SERVICE.OPERATIOR", "SERVICE.OPERATOR"); for (Map.Entry<String, String> entry : principalTypeToRole.entrySet()) { String principalTypeName = entry.getKey(); String roleName = entry.getValue(); PermissionEntity role = permissionDAO.findByName(roleName); PrincipalEntity rolePrincipalEntity = (role == null) ? null : role.getPrincipal(); PrincipalTypeEntity principalTypeEntity = principalTypeDAO.findByName(principalTypeName); if (principalTypeEntity != null) { List<PrincipalEntity> principalEntities = principalDAO.findByPrincipalType(principalTypeName); for (PrincipalEntity principalEntity : principalEntities) { Set<PrivilegeEntity> privilegeEntities = principalEntity.getPrivileges(); for (PrivilegeEntity privilegeEntity : privilegeEntities) { if (rolePrincipalEntity == null) { LOG.info("Removing privilege (id={}) since no role principle was found for {}:\n{}", privilegeEntity.getId(), roleName, formatPrivilegeEntityDetails(privilegeEntity)); privilegeDAO.remove(privilegeEntity); } else { LOG.info("Updating privilege (id={}) to use role principle for {}:\n{}", privilegeEntity.getId(), roleName, formatPrivilegeEntityDetails(privilegeEntity)); privilegeEntity.setPrincipal(rolePrincipalEntity); privilegeDAO.merge(privilegeEntity); } } principalDAO.remove(principalEntity); } principalTypeDAO.remove(principalTypeEntity); } } LOG.info("Converting pseudo principle types to role principals - complete."); } @Inject  UpgradeCatalog242(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion();  }
@Test public void testConvertRolePrincipals() throws AmbariException, SQLException { EasyMockSupport easyMockSupport = new EasyMockSupport(); PrincipalEntity clusterAdministratorPrincipalEntity = easyMockSupport.createMock(PrincipalEntity.class); PermissionEntity clusterAdministratorPermissionEntity = easyMockSupport.createMock(PermissionEntity.class); expect(clusterAdministratorPermissionEntity.getPrincipal()) .andReturn(clusterAdministratorPrincipalEntity) .once(); PrincipalTypeEntity allClusterAdministratorPrincipalTypeEntity = easyMockSupport.createMock(PrincipalTypeEntity.class); PermissionDAO permissionDAO = easyMockSupport.createMock(PermissionDAO.class); expect(permissionDAO.findByName("CLUSTER.ADMINISTRATOR")) .andReturn(clusterAdministratorPermissionEntity) .once(); expect(permissionDAO.findByName(anyString())) .andReturn(null) .anyTimes(); PrincipalTypeDAO principalTypeDAO = easyMockSupport.createMock(PrincipalTypeDAO.class); expect(principalTypeDAO.findByName("ALL.CLUSTER.ADMINISTRATOR")) .andReturn(allClusterAdministratorPrincipalTypeEntity) .once(); expect(principalTypeDAO.findByName(anyString())) .andReturn(null) .anyTimes(); principalTypeDAO.remove(allClusterAdministratorPrincipalTypeEntity); expectLastCall().once(); ResourceEntity allClusterAdministratorPrivilege1Resource = easyMockSupport.createMock(ResourceEntity.class); expect(allClusterAdministratorPrivilege1Resource.getId()).andReturn(1L).once(); PrincipalEntity allClusterAdministratorPrivilege1Principal = easyMockSupport.createMock(PrincipalEntity.class); expect(allClusterAdministratorPrivilege1Principal.getId()).andReturn(1L).once(); PermissionEntity allClusterAdministratorPrivilege1Permission = easyMockSupport.createMock(PermissionEntity.class); expect(allClusterAdministratorPrivilege1Permission.getId()).andReturn(1).once(); PrivilegeEntity allClusterAdministratorPrivilege1 = easyMockSupport.createMock(PrivilegeEntity.class); expect(allClusterAdministratorPrivilege1.getId()).andReturn(1).atLeastOnce(); expect(allClusterAdministratorPrivilege1.getResource()).andReturn(allClusterAdministratorPrivilege1Resource).once(); expect(allClusterAdministratorPrivilege1.getPrincipal()).andReturn(allClusterAdministratorPrivilege1Principal).once(); expect(allClusterAdministratorPrivilege1.getPermission()).andReturn(allClusterAdministratorPrivilege1Permission).once(); allClusterAdministratorPrivilege1.setPrincipal(clusterAdministratorPrincipalEntity); expectLastCall().once(); ResourceEntity allClusterAdministratorPrivilege2Resource = easyMockSupport.createMock(ResourceEntity.class); expect(allClusterAdministratorPrivilege2Resource.getId()).andReturn(2L).once(); PrincipalEntity allClusterAdministratorPrivilege2Principal = easyMockSupport.createMock(PrincipalEntity.class); expect(allClusterAdministratorPrivilege2Principal.getId()).andReturn(2L).once(); PermissionEntity allClusterAdministratorPrivilege2Permission = easyMockSupport.createMock(PermissionEntity.class); expect(allClusterAdministratorPrivilege2Permission.getId()).andReturn(2).once(); PrivilegeEntity allClusterAdministratorPrivilege2 = easyMockSupport.createMock(PrivilegeEntity.class); expect(allClusterAdministratorPrivilege2.getId()).andReturn(2).atLeastOnce(); expect(allClusterAdministratorPrivilege2.getResource()).andReturn(allClusterAdministratorPrivilege2Resource).once(); expect(allClusterAdministratorPrivilege2.getPrincipal()).andReturn(allClusterAdministratorPrivilege2Principal).once(); expect(allClusterAdministratorPrivilege2.getPermission()).andReturn(allClusterAdministratorPrivilege2Permission).once(); allClusterAdministratorPrivilege2.setPrincipal(clusterAdministratorPrincipalEntity); expectLastCall().once(); Set<PrivilegeEntity> allClusterAdministratorPrivileges = new HashSet<PrivilegeEntity>(); allClusterAdministratorPrivileges.add(allClusterAdministratorPrivilege1); allClusterAdministratorPrivileges.add(allClusterAdministratorPrivilege2); PrincipalEntity allClusterAdministratorPrincipalEntity = easyMockSupport.createMock(PrincipalEntity.class); expect(allClusterAdministratorPrincipalEntity.getPrivileges()) .andReturn(allClusterAdministratorPrivileges) .once(); List<PrincipalEntity> allClusterAdministratorPrincipals = new ArrayList<PrincipalEntity>(); allClusterAdministratorPrincipals.add(allClusterAdministratorPrincipalEntity); PrincipalDAO principalDAO = easyMockSupport.createMock(PrincipalDAO.class); expect(principalDAO.findByPrincipalType("ALL.CLUSTER.ADMINISTRATOR")) .andReturn(allClusterAdministratorPrincipals) .once(); principalDAO.remove(allClusterAdministratorPrincipalEntity); expectLastCall().once(); PrivilegeDAO privilegeDAO = easyMockSupport.createMock(PrivilegeDAO.class); expect(privilegeDAO.merge(allClusterAdministratorPrivilege1)) .andReturn(allClusterAdministratorPrivilege1) .once(); expect(privilegeDAO.merge(allClusterAdministratorPrivilege2)) .andReturn(allClusterAdministratorPrivilege2) .once(); Injector injector = easyMockSupport.createNiceMock(Injector.class); expect(injector.getInstance(PrincipalTypeDAO.class)).andReturn(principalTypeDAO).atLeastOnce(); expect(injector.getInstance(PrincipalDAO.class)).andReturn(principalDAO).atLeastOnce(); expect(injector.getInstance(PermissionDAO.class)).andReturn(permissionDAO).atLeastOnce(); expect(injector.getInstance(PrivilegeDAO.class)).andReturn(privilegeDAO).atLeastOnce(); easyMockSupport.replayAll(); UpgradeCatalog242 upgradeCatalog = new UpgradeCatalog242(injector); injector.injectMembers(upgradeCatalog); upgradeCatalog.convertRolePrincipals(); easyMockSupport.verifyAll(); }