use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testEnforceRoleUserAuthorityRestrictionsNoUpdate.
@Test
public void testEnforceRoleUserAuthorityRestrictionsNoUpdate() {
Authority savedAuthority = zms.dbService.zmsConfig.getUserAuthority();
Authority authority = Mockito.mock(Authority.class);
Mockito.when(authority.getDateAttribute("user.joe", "elevated-clearance")).thenReturn(null);
zms.dbService.zmsConfig.setUserAuthority(authority);
final String domainName = "authority-test";
final String roleName = "auth-role";
ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(mockConn.insertRoleMember(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockConn.updateDomainModTimestamp(domainName)).thenReturn(true);
Mockito.when(mockObjStore.getConnection(true, true)).thenReturn(mockConn);
// first we're going to return a null role and then a role
// with no members - in both cases we return without processing
// any code
Role role = new Role().setUserAuthorityExpiration("elevated-clearance");
List<RoleMember> roleMembers = new ArrayList<>();
roleMembers.add(new RoleMember().setMemberName("user.joe").setExpiration(Timestamp.fromMillis(System.currentTimeMillis() - 10000)));
Mockito.when(mockConn.getRole(domainName, roleName)).thenReturn(role);
Mockito.when(mockConn.listRoleMembers(domainName, roleName, false)).thenReturn(roleMembers);
ObjectStore savedStore = zms.dbService.store;
zms.dbService.store = mockObjStore;
// the request should complete successfully
zms.dbService.enforceRoleUserAuthorityRestrictions(domainName, roleName, null);
zms.dbService.zmsConfig.setUserAuthority(savedAuthority);
zms.dbService.store = savedStore;
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testEnforceGroupUserAuthorityFilterRestrictionsUpdate.
@Test
public void testEnforceGroupUserAuthorityFilterRestrictionsUpdate() {
Authority savedAuthority = zms.dbService.zmsConfig.getUserAuthority();
Authority authority = Mockito.mock(Authority.class);
Mockito.when(authority.isAttributeSet("user.joe", "employee")).thenReturn(false);
Mockito.when(authority.isAttributeSet("user.jane", "employee")).thenReturn(true);
zms.dbService.zmsConfig.setUserAuthority(authority);
final String domainName = "authority-test";
final String groupName = "auth-group";
ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(mockConn.insertGroupMember(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(true);
Mockito.when(mockConn.updateDomainModTimestamp(domainName)).thenReturn(true);
Mockito.when(mockObjStore.getConnection(true, true)).thenReturn(mockConn);
// first we're going to return a null group and then a group
// with no members - in both cases we return without processing
// any code
Group group = new Group().setUserAuthorityFilter("employee");
List<GroupMember> groupMembers = new ArrayList<>();
groupMembers.add(new GroupMember().setMemberName("user.joe"));
groupMembers.add(new GroupMember().setMemberName("user.jane"));
Mockito.when(mockConn.getGroup(domainName, groupName)).thenReturn(group);
Mockito.when(mockConn.listGroupMembers(domainName, groupName, false)).thenReturn(groupMembers);
ObjectStore savedStore = zms.dbService.store;
zms.dbService.store = mockObjStore;
// the request should complete successfully
zms.dbService.enforceGroupUserAuthorityRestrictions(domainName, groupName, null);
zms.dbService.zmsConfig.setUserAuthority(savedAuthority);
zms.dbService.store = savedStore;
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testRemovePrincipalFromAllGroupExceptions.
@Test
public void testRemovePrincipalFromAllGroupExceptions() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Mockito.when(conn.getPrincipalGroups("user.joe", null)).thenThrow(new ResourceException(404)).thenThrow(new ResourceException(501));
// no exception if store returns 404
zms.dbService.removePrincipalFromAllGroups(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
try {
zms.dbService.removePrincipalFromAllGroups(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
fail();
} catch (ResourceException ex) {
assertEquals(501, ex.getCode());
}
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testProcessGroupWithTagsUpdate.
@Test
public void testProcessGroupWithTagsUpdate() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Map<String, TagValueList> groupTags = new HashMap<>();
groupTags.put("tagToBeRemoved", new TagValueList().setList(Collections.singletonList("val0")));
groupTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
Group group = new Group().setName("newGroup").setTags(groupTags);
Mockito.when(conn.insertGroup(anyString(), any())).thenReturn(true);
Mockito.when(conn.insertGroupTags("newGroup", "sys.auth", groupTags)).thenReturn(true);
StringBuilder auditDetails = new StringBuilder("testAudit");
boolean success = zms.dbService.processGroup(conn, null, "sys.auth", "newGroup", group, adminUser, auditRef, auditDetails);
assertTrue(success);
// new group
Map<String, TagValueList> newGroupTags = new HashMap<>();
newGroupTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
newGroupTags.put("newTagKey", new TagValueList().setList(Arrays.asList("val3", "val4")));
newGroupTags.put("newTagKey2", new TagValueList().setList(Arrays.asList("val5", "val6")));
Group newGroup = new Group().setName("newGroup").setTags(newGroupTags);
Mockito.when(conn.updateGroup("sys.auth", newGroup)).thenReturn(true);
Mockito.when(conn.deleteGroupTags(anyString(), anyString(), anySet())).thenReturn(true);
Mockito.when(conn.insertGroupTags(anyString(), anyString(), anyMap())).thenReturn(true);
success = zms.dbService.processGroup(conn, group, "sys.auth", "newGroup", newGroup, adminUser, auditRef, auditDetails);
assertTrue(success);
// assert tags to remove
Set<String> expectedTagsToBeRemoved = new HashSet<>(Collections.singletonList("tagToBeRemoved"));
ArgumentCaptor<Set<String>> tagCapture = ArgumentCaptor.forClass(Set.class);
ArgumentCaptor<String> groupCapture = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> domainCapture = ArgumentCaptor.forClass(String.class);
Mockito.verify(conn, times(1)).deleteGroupTags(groupCapture.capture(), domainCapture.capture(), tagCapture.capture());
assertEquals("newGroup", groupCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
assertTrue(tagCapture.getValue().containsAll(expectedTagsToBeRemoved));
// assert tags to add
ArgumentCaptor<Map<String, TagValueList>> tagInsertCapture = ArgumentCaptor.forClass(Map.class);
Mockito.verify(conn, times(2)).insertGroupTags(groupCapture.capture(), domainCapture.capture(), tagInsertCapture.capture());
assertEquals("newGroup", groupCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
Map<String, TagValueList> resultInsertTags = tagInsertCapture.getAllValues().get(1);
assertTrue(resultInsertTags.keySet().containsAll(Arrays.asList("newTagKey", "newTagKey2")));
assertTrue(resultInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).containsAll(Arrays.asList("val3", "val4", "val5", "val6")));
// assert first tag insertion
Map<String, TagValueList> resultFirstInsertTags = tagInsertCapture.getAllValues().get(0);
assertTrue(resultFirstInsertTags.keySet().containsAll(Arrays.asList("tagKey", "tagToBeRemoved")));
assertTrue(resultFirstInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).containsAll(Arrays.asList("val0", "val1", "val2")));
}
use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.
the class DBServiceTest method testProcessRoleWithTagsUpdate.
@Test
public void testProcessRoleWithTagsUpdate() {
ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
Map<String, TagValueList> roleTags = new HashMap<>();
roleTags.put("tagToBeRemoved", new TagValueList().setList(Collections.singletonList("val0")));
roleTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
Role role = new Role().setName("newRole").setTags(roleTags);
Mockito.when(conn.insertRole(anyString(), any())).thenReturn(true);
Mockito.when(conn.insertRoleTags("newRole", "sys.auth", roleTags)).thenReturn(true);
StringBuilder auditDetails = new StringBuilder("testAudit");
boolean success = zms.dbService.processRole(conn, null, "sys.auth", "newRole", role, adminUser, auditRef, false, auditDetails);
assertTrue(success);
// new role
Map<String, TagValueList> newRoleTags = new HashMap<>();
newRoleTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
newRoleTags.put("newTagKey", new TagValueList().setList(Arrays.asList("val3", "val4")));
newRoleTags.put("newTagKey2", new TagValueList().setList(Arrays.asList("val5", "val6")));
Role newRole = new Role().setName("newRole").setTags(newRoleTags);
Mockito.when(conn.updateRole("sys.auth", newRole)).thenReturn(true);
Mockito.when(conn.deleteRoleTags(anyString(), anyString(), anySet())).thenReturn(true);
Mockito.when(conn.insertRoleTags(anyString(), anyString(), anyMap())).thenReturn(true);
success = zms.dbService.processRole(conn, role, "sys.auth", "newRole", newRole, adminUser, auditRef, false, auditDetails);
assertTrue(success);
// assert tags to remove
Set<String> expectedTagsToBeRemoved = new HashSet<>(Collections.singletonList("tagToBeRemoved"));
ArgumentCaptor<Set<String>> tagCapture = ArgumentCaptor.forClass(Set.class);
ArgumentCaptor<String> roleCapture = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> domainCapture = ArgumentCaptor.forClass(String.class);
Mockito.verify(conn, times(1)).deleteRoleTags(roleCapture.capture(), domainCapture.capture(), tagCapture.capture());
assertEquals("newRole", roleCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
assertTrue(tagCapture.getValue().containsAll(expectedTagsToBeRemoved));
// assert tags to add
ArgumentCaptor<Map<String, TagValueList>> tagInsertCapture = ArgumentCaptor.forClass(Map.class);
Mockito.verify(conn, times(2)).insertRoleTags(roleCapture.capture(), domainCapture.capture(), tagInsertCapture.capture());
assertEquals("newRole", roleCapture.getValue());
assertEquals("sys.auth", domainCapture.getValue());
Map<String, TagValueList> resultInsertTags = tagInsertCapture.getAllValues().get(1);
assertTrue(resultInsertTags.keySet().containsAll(Arrays.asList("newTagKey", "newTagKey2")));
assertTrue(resultInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).containsAll(Arrays.asList("val3", "val4", "val5", "val6")));
// assert first tag insertion
Map<String, TagValueList> resultFirstInsertTags = tagInsertCapture.getAllValues().get(0);
assertTrue(resultFirstInsertTags.keySet().containsAll(Arrays.asList("tagKey", "tagToBeRemoved")));
assertTrue(resultFirstInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).containsAll(Arrays.asList("val0", "val1", "val2")));
}
Aggregations