Search in sources :

Example 11 with PublicKeyEntry

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

the class JDBCConnectionTest method testInsertPublicKeyEntryInvalidService.

@Test
public void testInsertPublicKeyEntryInvalidService() 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);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for service id
    false);
    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)

Example 12 with PublicKeyEntry

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

the class JDBCConnectionTest method testUpdatePublicKeyEntryInvalidService.

@Test
public void testUpdatePublicKeyEntryInvalidService() 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);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for service id
    false);
    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 13 with PublicKeyEntry

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

the class JDBCConnectionTest method testInsertPublicKeyEntry.

@Test
public void testInsertPublicKeyEntry() 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.insertPublicKeyEntry("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)).setInt(1, 7);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "zms1");
    Mockito.verify(mockPrepStmt, times(1)).setString(3, "Value1");
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 14 with PublicKeyEntry

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

the class JDBCConnection method listPublicKeys.

@Override
public List<PublicKeyEntry> listPublicKeys(String domainName, String serviceName) {
    final String caller = "listPublicKeys";
    int domainId = getDomainId(domainName);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    int serviceId = getServiceId(domainId, serviceName);
    if (serviceId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_SERVICE, ZMSUtils.serviceResourceName(domainName, serviceName));
    }
    List<PublicKeyEntry> publicKeys = new ArrayList<>();
    try (PreparedStatement ps = con.prepareStatement(SQL_LIST_PUBLIC_KEY)) {
        ps.setInt(1, serviceId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            while (rs.next()) {
                PublicKeyEntry publicKey = new PublicKeyEntry().setId(rs.getString(ZMSConsts.DB_COLUMN_KEY_ID)).setKey(rs.getString(ZMSConsts.DB_COLUMN_KEY_VALUE));
                publicKeys.add(publicKey);
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return publicKeys;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 15 with PublicKeyEntry

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

the class JDBCConnection method getPublicKeyEntry.

@Override
public PublicKeyEntry getPublicKeyEntry(String domainName, String serviceName, String keyId, boolean domainStateCheck) {
    final String caller = "getPublicKeyEntry";
    int domainId = getDomainId(domainName, domainStateCheck);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    int serviceId = getServiceId(domainId, serviceName);
    if (serviceId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_SERVICE, ZMSUtils.serviceResourceName(domainName, serviceName));
    }
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_PUBLIC_KEY)) {
        ps.setInt(1, serviceId);
        ps.setString(2, keyId);
        try (ResultSet rs = executeQuery(ps, caller)) {
            if (rs.next()) {
                PublicKeyEntry publicKey = new PublicKeyEntry().setId(keyId).setKey(rs.getString(ZMSConsts.DB_COLUMN_KEY_VALUE));
                return publicKey;
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return null;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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