Search in sources :

Example 16 with PublicKeyEntry

use of com.yahoo.athenz.zms.PublicKeyEntry 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)

Example 17 with PublicKeyEntry

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

the class SignUtils method asStruct.

private static Struct asStruct(ServiceIdentity service) {
    // all of our fields are in canonical order based
    // on their attribute name
    Struct struct = new Struct();
    appendObject(struct, ATTR_DESCRIPTION, service.getDescription());
    appendObject(struct, ATTR_EXECUTABLE, service.getExecutable());
    appendObject(struct, ATTR_GROUP, service.getGroup());
    appendList(struct, ATTR_HOSTS, service.getHosts());
    appendObject(struct, ATTR_MODIFIED, service.getModified());
    appendObject(struct, ATTR_NAME, service.getName());
    appendObject(struct, ATTR_PROVIDER_ENDPOINT, service.getProviderEndpoint());
    List<PublicKeyEntry> publicKeys = service.getPublicKeys();
    Array publicKeysArray = new Array();
    if (publicKeys != null) {
        for (PublicKeyEntry publicKey : publicKeys) {
            Struct structPublicKey = new Struct();
            appendObject(structPublicKey, ATTR_ID, publicKey.getId());
            appendObject(structPublicKey, ATTR_KEY, publicKey.getKey());
            publicKeysArray.add(structPublicKey);
        }
    }
    appendArray(struct, ATTR_PUBLIC_KEYS, publicKeysArray);
    appendObject(struct, ATTR_USER, service.getUser());
    return struct;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) Array(com.yahoo.rdl.Array) Struct(com.yahoo.rdl.Struct)

Example 18 with PublicKeyEntry

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

the class JDBCConnectionTest method testUpdatePublicKeyEntry.

@Test
public void testUpdatePublicKeyEntry() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// service id
    7);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for service id
    true);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    boolean requestSuccess = jdbcConn.updatePublicKeyEntry("my-domain", "service1", publicKey);
    assertTrue(requestSuccess);
    // getting domain and service ids
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "service1");
    // public key entry statement
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "Value1");
    Mockito.verify(mockPrepStmt, times(1)).setInt(2, 7);
    Mockito.verify(mockPrepStmt, times(1)).setString(3, "zms1");
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 19 with PublicKeyEntry

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

the class JDBCConnectionTest method testUpdatePublicKeyEntryInvalidDomain.

@Test
public void testUpdatePublicKeyEntryInvalidDomain() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    false);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    try {
        jdbcConn.updatePublicKeyEntry("my-domain", "service1", publicKey);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 20 with PublicKeyEntry

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

the class JDBCConnectionTest method testInsertPublicKeyEntryInvalidDomain.

@Test
public void testInsertPublicKeyEntryInvalidDomain() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    PublicKeyEntry publicKey = new PublicKeyEntry().setId("zms1").setKey("Value1");
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    false);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    try {
        jdbcConn.insertPublicKeyEntry("my-domain", "service1", publicKey);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Aggregations

PublicKeyEntry (com.yahoo.athenz.zms.PublicKeyEntry)22 Test (org.testng.annotations.Test)13 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)11 SQLException (java.sql.SQLException)9 ResourceException (com.yahoo.athenz.zms.ResourceException)6 ServiceIdentity (com.yahoo.athenz.zms.ServiceIdentity)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 Struct (com.yahoo.rdl.Struct)2 AthenzConfig (com.yahoo.athenz.common.config.AthenzConfig)1 Role (com.yahoo.athenz.zms.Role)1 SignedPolicies (com.yahoo.athenz.zms.SignedPolicies)1 Array (com.yahoo.rdl.Array)1 File (java.io.File)1 PublicKey (java.security.PublicKey)1 HashMap (java.util.HashMap)1