Search in sources :

Example 91 with ObjectStoreConnection

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;
}
Also used : ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) Authority(com.yahoo.athenz.auth.Authority) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 92 with ObjectStoreConnection

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;
}
Also used : ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) Authority(com.yahoo.athenz.auth.Authority) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 93 with ObjectStoreConnection

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());
    }
}
Also used : ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 94 with ObjectStoreConnection

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")));
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) EmbeddedMysql(com.wix.mysql.EmbeddedMysql) Mock(org.mockito.Mock) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) LocalDateTime(java.time.LocalDateTime) ZMSUtils(com.yahoo.athenz.zms.utils.ZMSUtils) Test(org.testng.annotations.Test) StringUtils(org.apache.commons.lang3.StringUtils) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) MockitoAnnotations(org.mockito.MockitoAnnotations) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert(org.testng.Assert) JDBCConnection(com.yahoo.athenz.zms.store.impl.jdbc.JDBCConnection) Struct(com.yahoo.rdl.Struct) AuditReferenceValidator(com.yahoo.athenz.common.server.audit.AuditReferenceValidator) Path(java.nio.file.Path) DataCache(com.yahoo.athenz.zms.DBService.DataCache) AfterClass(org.testng.annotations.AfterClass) Crypto(com.yahoo.athenz.auth.util.Crypto) FilePrivateKeyStore(com.yahoo.athenz.auth.impl.FilePrivateKeyStore) Files(java.nio.file.Files) ResourceUtils(com.yahoo.athenz.common.server.util.ResourceUtils) BeforeClass(org.testng.annotations.BeforeClass) IOException(java.io.IOException) Authority(com.yahoo.athenz.auth.Authority) MemberDueDays(com.yahoo.athenz.zms.config.MemberDueDays) Collectors(java.util.stream.Collectors) NotificationManager(com.yahoo.athenz.common.server.notification.NotificationManager) ZoneId(java.time.ZoneId) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Timestamp(com.yahoo.rdl.Timestamp) Principal(com.yahoo.athenz.auth.Principal) Paths(java.nio.file.Paths) MockAuditReferenceValidatorImpl(com.yahoo.athenz.zms.audit.MockAuditReferenceValidatorImpl) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Example 95 with ObjectStoreConnection

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")));
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) EmbeddedMysql(com.wix.mysql.EmbeddedMysql) Mock(org.mockito.Mock) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) LocalDateTime(java.time.LocalDateTime) ZMSUtils(com.yahoo.athenz.zms.utils.ZMSUtils) Test(org.testng.annotations.Test) StringUtils(org.apache.commons.lang3.StringUtils) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) MockitoAnnotations(org.mockito.MockitoAnnotations) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert(org.testng.Assert) JDBCConnection(com.yahoo.athenz.zms.store.impl.jdbc.JDBCConnection) Struct(com.yahoo.rdl.Struct) AuditReferenceValidator(com.yahoo.athenz.common.server.audit.AuditReferenceValidator) Path(java.nio.file.Path) DataCache(com.yahoo.athenz.zms.DBService.DataCache) AfterClass(org.testng.annotations.AfterClass) Crypto(com.yahoo.athenz.auth.util.Crypto) FilePrivateKeyStore(com.yahoo.athenz.auth.impl.FilePrivateKeyStore) Files(java.nio.file.Files) ResourceUtils(com.yahoo.athenz.common.server.util.ResourceUtils) BeforeClass(org.testng.annotations.BeforeClass) IOException(java.io.IOException) Authority(com.yahoo.athenz.auth.Authority) MemberDueDays(com.yahoo.athenz.zms.config.MemberDueDays) Collectors(java.util.stream.Collectors) NotificationManager(com.yahoo.athenz.common.server.notification.NotificationManager) ZoneId(java.time.ZoneId) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Timestamp(com.yahoo.rdl.Timestamp) Principal(com.yahoo.athenz.auth.Principal) Paths(java.nio.file.Paths) MockAuditReferenceValidatorImpl(com.yahoo.athenz.zms.audit.MockAuditReferenceValidatorImpl) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Test(org.testng.annotations.Test)

Aggregations

ObjectStoreConnection (com.yahoo.athenz.zms.store.ObjectStoreConnection)173 Test (org.testng.annotations.Test)96 ObjectStore (com.yahoo.athenz.zms.store.ObjectStore)38 AthenzDomain (com.yahoo.athenz.zms.store.AthenzDomain)34 Authority (com.yahoo.athenz.auth.Authority)23 Timestamp (com.yahoo.rdl.Timestamp)17 ArrayList (java.util.ArrayList)16 MemberDueDays (com.yahoo.athenz.zms.config.MemberDueDays)11 Principal (com.yahoo.athenz.auth.Principal)7 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)7 EmbeddedMysql (com.wix.mysql.EmbeddedMysql)5 FilePrivateKeyStore (com.yahoo.athenz.auth.impl.FilePrivateKeyStore)5 Crypto (com.yahoo.athenz.auth.util.Crypto)5 AuditReferenceValidator (com.yahoo.athenz.common.server.audit.AuditReferenceValidator)5 NotificationManager (com.yahoo.athenz.common.server.notification.NotificationManager)5 ResourceUtils (com.yahoo.athenz.common.server.util.ResourceUtils)5 DataCache (com.yahoo.athenz.zms.DBService.DataCache)5 MockAuditReferenceValidatorImpl (com.yahoo.athenz.zms.audit.MockAuditReferenceValidatorImpl)5 JDBCConnection (com.yahoo.athenz.zms.store.impl.jdbc.JDBCConnection)5 ZMSUtils (com.yahoo.athenz.zms.utils.ZMSUtils)5