Search in sources :

Example 41 with ServiceIdentity

use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.

the class JDBCConnectionTest method testGetServiceIdentityAllFields.

@Test
public void testGetServiceIdentityAllFields() throws Exception {
    Mockito.when(mockResultSet.next()).thenReturn(true);
    Mockito.doReturn("test description").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_DESCRIPTION);
    Mockito.doReturn(new java.sql.Timestamp(1454358916)).when(mockResultSet).getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED);
    Mockito.doReturn("/usr/bin64/athenz").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_EXECTUABLE);
    Mockito.doReturn("users").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_SVC_GROUP);
    Mockito.doReturn("root").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_SVC_USER);
    Mockito.doReturn("http://server.athenzcompany.com").when(mockResultSet).getString(ZMSConsts.DB_COLUMN_PROVIDER_ENDPOINT);
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    ServiceIdentity service = jdbcConn.getServiceIdentity("my-domain", "service1");
    assertNotNull(service);
    assertEquals("my-domain.service1", service.getName());
    assertEquals("/usr/bin64/athenz", service.getExecutable());
    assertEquals("users", service.getGroup());
    assertEquals("root", service.getUser());
    assertEquals("http://server.athenzcompany.com", service.getProviderEndpoint().toString());
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "service1");
    jdbcConn.close();
}
Also used : ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 42 with ServiceIdentity

use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.

the class JDBCConnectionTest method testInsertServiceIdentity.

@Test
public void testInsertServiceIdentity() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    ServiceIdentity service = new ServiceIdentity().setName("my-domain.service1");
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.when(mockResultSet.getInt(1)).thenReturn(5);
    boolean requestSuccess = jdbcConn.insertServiceIdentity("my-domain", service);
    assertTrue(requestSuccess);
    // get domain id
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    // update service
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "service1");
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "");
    Mockito.verify(mockPrepStmt, times(1)).setString(3, "");
    Mockito.verify(mockPrepStmt, times(1)).setString(4, "");
    Mockito.verify(mockPrepStmt, times(1)).setString(5, "");
    jdbcConn.close();
}
Also used : ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 43 with ServiceIdentity

use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.

the class JDBCConnectionTest method testUpdateServiceIdentityException.

@Test
public void testUpdateServiceIdentityException() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    ServiceIdentity service = new ServiceIdentity().setName("my-domain.service1");
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(// service id
    4);
    Mockito.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    try {
        jdbcConn.updateServiceIdentity("my-domain", service);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : SQLException(java.sql.SQLException) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 44 with ServiceIdentity

use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.

the class JDBCConnectionTest method testInsertServiceIdentityInvalidName.

@Test
public void testInsertServiceIdentityInvalidName() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    ServiceIdentity service = new ServiceIdentity().setName("service1");
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    Mockito.when(mockResultSet.next()).thenReturn(true);
    // return domain id
    Mockito.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(// service id
    4);
    try {
        jdbcConn.insertServiceIdentity("my-domain", service);
        fail();
    } catch (ResourceException ex) {
        assertEquals(400, ex.getCode());
    }
    jdbcConn.close();
}
Also used : ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) ResourceException(com.yahoo.athenz.zms.ResourceException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 45 with ServiceIdentity

use of com.yahoo.athenz.zms.ServiceIdentity in project athenz by yahoo.

the class JDBCConnection method getAthenzDomainServices.

void getAthenzDomainServices(String domainName, int domainId, AthenzDomain athenzDomain, String caller) {
    Map<String, ServiceIdentity> serviceMap = new HashMap<>();
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                String serviceName = rs.getString(ZMSConsts.DB_COLUMN_NAME);
                ServiceIdentity service = new ServiceIdentity().setName(ZMSUtils.serviceResourceName(domainName, serviceName)).setProviderEndpoint(saveValue(rs.getString(ZMSConsts.DB_COLUMN_PROVIDER_ENDPOINT))).setExecutable(saveValue(rs.getString(ZMSConsts.DB_COLUMN_EXECTUABLE))).setUser(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_USER))).setGroup(saveValue(rs.getString(ZMSConsts.DB_COLUMN_SVC_GROUP))).setModified(Timestamp.fromMillis(rs.getTimestamp(ZMSConsts.DB_COLUMN_MODIFIED).getTime()));
                List<PublicKeyEntry> publicKeys = new ArrayList<>();
                service.setPublicKeys(publicKeys);
                serviceMap.put(serviceName, service);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_HOSTS)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                String serviceName = rs.getString(1);
                ServiceIdentity service = serviceMap.get(serviceName);
                if (service == null) {
                    continue;
                }
                List<String> hosts = service.getHosts();
                if (hosts == null) {
                    hosts = new ArrayList<>();
                    service.setHosts(hosts);
                }
                hosts.add(rs.getString(2));
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_DOMAIN_SERVICES_PUBLIC_KEYS)) {
        ps.setInt(1, domainId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                String serviceName = rs.getString(1);
                ServiceIdentity service = serviceMap.get(serviceName);
                if (service == null) {
                    continue;
                }
                PublicKeyEntry publicKey = new PublicKeyEntry().setId(rs.getString(ZMSConsts.DB_COLUMN_KEY_ID)).setKey(rs.getString(ZMSConsts.DB_COLUMN_KEY_VALUE));
                service.getPublicKeys().add(publicKey);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    athenzDomain.getServices().addAll(serviceMap.values());
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ServiceIdentity (com.yahoo.athenz.zms.ServiceIdentity)53 Test (org.testng.annotations.Test)32 ArrayList (java.util.ArrayList)29 DomainData (com.yahoo.athenz.zms.DomainData)21 DataCache (com.yahoo.athenz.zts.cache.DataCache)17 Role (com.yahoo.athenz.zms.Role)11 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)11 MockZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore)11 ZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore)11 RoleMember (com.yahoo.athenz.zms.RoleMember)8 SignedDomain (com.yahoo.athenz.zms.SignedDomain)8 Domain (com.yahoo.athenz.zms.Domain)6 PublicKeyEntry (com.yahoo.athenz.zms.PublicKeyEntry)5 HostServices (com.yahoo.athenz.zts.HostServices)5 Set (java.util.Set)5 Policy (com.yahoo.athenz.zms.Policy)4 ResourceException (com.yahoo.athenz.zms.ResourceException)4 SQLException (java.sql.SQLException)4 Assertion (com.yahoo.athenz.zms.Assertion)3 MemberRole (com.yahoo.athenz.zts.cache.MemberRole)3