Search in sources :

Example 6 with ObjectStoreConnection

use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.

the class DBServiceTest method testProcessDomainWithUpdateNullTags.

@Test
public void testProcessDomainWithUpdateNullTags() {
    ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
    ObjectStore savedStore = zms.dbService.store;
    Map<String, TagValueList> domainTags = new HashMap<>();
    domainTags.put("tagToBeRemoved", new TagValueList().setList(Collections.singletonList("val0")));
    domainTags.put("tagKey", new TagValueList().setList(Arrays.asList("val1", "val2")));
    Domain domain = new Domain().setName("newDomain").setTags(domainTags);
    Mockito.when(conn.insertDomain(domain)).thenReturn(true);
    Mockito.when(conn.insertDomainTags("newDomain", domainTags)).thenReturn(true);
    Mockito.when(conn.insertRole(anyString(), any(Role.class))).thenReturn(true);
    Mockito.when(conn.insertRoleMember(any(), any(), any(), any(), any())).thenReturn(true);
    Mockito.when(conn.insertPolicy(any(), any())).thenReturn(true);
    Mockito.when(conn.insertAssertion(any(), any(), any(), any())).thenReturn(true);
    Mockito.when(mockObjStore.getConnection(false, true)).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn);
    zms.dbService.store = mockObjStore;
    Domain createdDomain = zms.dbService.makeDomain(mockDomRsrcCtx, domain, Collections.singletonList(adminUser), null, auditRef);
    assertEquals(createdDomain.getTags(), domainTags);
    Mockito.when(conn.updateDomain(any(Domain.class))).thenReturn(true);
    Mockito.when(conn.deleteDomainTags(anyString(), anySet())).thenReturn(true);
    Mockito.when(conn.insertDomainTags(anyString(), anyMap())).thenReturn(true);
    Mockito.when(conn.getDomain("newDomain")).thenReturn(domain);
    Mockito.when(mockObjStore.getConnection(false, true)).thenReturn(conn).thenReturn(conn).thenReturn(conn).thenReturn(conn);
    // update domain meta
    DomainMeta meta = new DomainMeta().setTags(null);
    zms.dbService.executePutDomainMeta(mockDomRsrcCtx, domain, meta, null, false, auditRef, "putDomainMeta");
    assertEquals(createdDomain.getTags(), domainTags);
    zms.dbService.store = savedStore;
}
Also used : ObjectStore(com.yahoo.athenz.zms.store.ObjectStore) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) Test(org.testng.annotations.Test)

Example 7 with ObjectStoreConnection

use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.

the class DBServiceTest method testRoleSameTagKeyValues.

@Test
public void testRoleSameTagKeyValues() {
    ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
    Map<String, TagValueList> roleTags = Collections.singletonMap("tagKey", new TagValueList().setList(Collections.singletonList("tagVal")));
    Role role = new Role().setName("role").setTags(roleTags);
    Mockito.when(conn.insertRole(anyString(), any())).thenReturn(true);
    Mockito.when(conn.insertRoleTags(anyString(), anyString(), any())).thenReturn(true);
    StringBuilder auditDetails = new StringBuilder("testAudit");
    boolean success = zms.dbService.processRole(conn, null, "sys.auth", "newRole", role, adminUser, auditRef, false, auditDetails);
    assertTrue(success);
    // process the same role again with the same tags
    Role newRole = new Role().setName("role").setTags(roleTags);
    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 should be empty
    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().isEmpty());
    // assert tags to add should be empty
    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.isEmpty());
    // asert first tag insertion
    Map<String, TagValueList> resultFirstInsertTags = tagInsertCapture.getAllValues().get(0);
    assertTrue(resultFirstInsertTags.containsKey("tagKey"));
    assertTrue(resultFirstInsertTags.values().stream().flatMap(l -> l.getList().stream()).collect(Collectors.toList()).contains("tagVal"));
}
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 8 with ObjectStoreConnection

use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.

the class DBServiceTest method testRemovePrincipalFromAllRolesExceptions.

@Test
public void testRemovePrincipalFromAllRolesExceptions() {
    ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
    Mockito.when(conn.getPrincipalRoles("user.joe", null)).thenThrow(new ResourceException(404)).thenThrow(new ResourceException(501));
    // no exception if store returns 404
    zms.dbService.removePrincipalFromAllRoles(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
    try {
        zms.dbService.removePrincipalFromAllRoles(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 9 with ObjectStoreConnection

use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.

the class DBServiceTest method testUpdateDomainMembersExpirationFailure.

@Test
public void testUpdateDomainMembersExpirationFailure() {
    final String domainName = "expiration-failure";
    Domain domain = new Domain().setName(domainName).setMemberExpiryDays(100).setModified(Timestamp.fromCurrentTime());
    Domain updateDomain = new Domain().setName(domainName).setMemberExpiryDays(50);
    ObjectStoreConnection mockConn = Mockito.mock(ObjectStoreConnection.class);
    // we're going to make sure to throw an exception here
    // since this should never be called
    Mockito.when(mockConn.getAthenzDomain(domainName)).thenThrow(new ResourceException(400));
    Authority savedAuthority = zms.dbService.zmsConfig.getUserAuthority();
    Authority authority = Mockito.mock(Authority.class);
    zms.dbService.zmsConfig.setUserAuthority(authority);
    zms.dbService.updateDomainMembersExpiration(mockDomRsrcCtx, mockConn, domain, updateDomain, auditRef, "testUpdateMdomainMembersExpirationFailure");
    zms.dbService.zmsConfig.setUserAuthority(savedAuthority);
}
Also used : Authority(com.yahoo.athenz.auth.Authority) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) Test(org.testng.annotations.Test)

Example 10 with ObjectStoreConnection

use of com.yahoo.athenz.zms.store.ObjectStoreConnection in project athenz by yahoo.

the class DBServiceTest method testRemovePrincipalFromAllGroupsDeleteUserException.

@Test
public void testRemovePrincipalFromAllGroupsDeleteUserException() {
    DomainGroupMember roles = new DomainGroupMember();
    roles.setMemberGroups(new ArrayList<>());
    GroupMember group1 = new GroupMember();
    group1.setDomainName("dom1");
    group1.setGroupName("group1");
    roles.getMemberGroups().add(group1);
    GroupMember group2 = new GroupMember();
    group2.setDomainName("dom1");
    group2.setGroupName("group2");
    roles.getMemberGroups().add(group2);
    ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class);
    Mockito.when(conn.getPrincipalGroups("user.joe", null)).thenReturn(roles);
    Mockito.when(conn.deleteGroupMember("dom1", "group1", "user.joe", adminUser, "unittest")).thenReturn(true);
    Mockito.when(conn.deleteGroupMember("dom1", "group2", "user.joe", adminUser, "unittest")).thenThrow(new ResourceException(501));
    // we should handle the exception without any errors
    zms.dbService.removePrincipalFromAllGroups(mockDomRsrcCtx, conn, "user.joe", adminUser, "unittest");
}
Also used : 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