Search in sources :

Example 1 with PublicKeyEntry

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

the class FileConnection method updateServiceIdentity.

@Override
public boolean updateServiceIdentity(String domainName, ServiceIdentity service) {
    DomainStruct domainStruct = getDomainStruct(domainName);
    if (domainStruct == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "updateServiceIdentity");
    }
    if (domainStruct.getServices() == null) {
        domainStruct.setServices(new HashMap<String, ServiceIdentity>());
    }
    HashMap<String, ServiceIdentity> services = domainStruct.getServices();
    service.setModified(Timestamp.fromCurrentTime());
    String serviceName = extractServiceName(domainName, service.getName());
    // here we only need to update the main attrs and not
    // the public keys and hosts
    List<PublicKeyEntry> publicKeys = service.getPublicKeys();
    List<String> hosts = service.getHosts();
    ServiceIdentity originalService = getServiceObject(domainStruct, serviceName);
    if (originalService != null) {
        service.setPublicKeys(originalService.getPublicKeys());
        service.setHosts(originalService.getHosts());
    } else {
        service.setPublicKeys(null);
        service.setHosts(null);
    }
    service.setModified(Timestamp.fromCurrentTime());
    services.put(serviceName, service);
    putDomainStruct(domainName, domainStruct);
    service.setPublicKeys(publicKeys);
    service.setHosts(hosts);
    return true;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity)

Example 2 with PublicKeyEntry

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

the class FileConnection method deletePublicKeyEntry.

@Override
public boolean deletePublicKeyEntry(String domainName, String serviceName, String keyId) {
    DomainStruct domainStruct = getDomainStruct(domainName);
    if (domainStruct == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "deletePublicKeyEntry");
    }
    ServiceIdentity service = getServiceObject(domainStruct, serviceName);
    if (service == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "service not found", "deletePublicKeyEntry");
    }
    List<PublicKeyEntry> keyList = service.getPublicKeys();
    boolean keyRemoved = removePublicKeyEntry(keyList, keyId);
    if (!keyRemoved) {
        return false;
    }
    putDomainStruct(domainName, domainStruct);
    return true;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity)

Example 3 with PublicKeyEntry

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

the class JDBCConnectionTest method testGetPublicKeyEntry.

@Test
public void testGetPublicKeyEntry() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    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).thenReturn(// for key
    true);
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_KEY_VALUE)).thenReturn("Value1");
    PublicKeyEntry publicKey = jdbcConn.getPublicKeyEntry("my-domain", "service1", "zone1", false);
    assertNotNull(publicKey);
    assertEquals("Value1", publicKey.getKey());
    assertEquals("zone1", publicKey.getId());
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 4 with PublicKeyEntry

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

the class JDBCConnectionTest method testUpdatePublicKeyEntryException.

@Test
public void testUpdatePublicKeyEntryException() 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.when(mockPrepStmt.executeUpdate()).thenThrow(new SQLException("failed operation", "state", 1001));
    try {
        jdbcConn.updatePublicKeyEntry("my-domain", "service1", publicKey);
        fail();
    } catch (Exception ex) {
        assertTrue(true);
    }
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) SQLException(java.sql.SQLException) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) ResourceException(com.yahoo.athenz.zms.ResourceException) SQLException(java.sql.SQLException) Test(org.testng.annotations.Test)

Example 5 with PublicKeyEntry

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

the class JDBCConnectionTest method testListPublicKeys.

@Test
public void testListPublicKeys() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    // return domain/service id
    Mockito.when(mockResultSet.getInt(1)).thenReturn(5).thenReturn(7);
    Mockito.when(mockResultSet.next()).thenReturn(// this one is for domain id
    true).thenReturn(// this one is for service id
    true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_KEY_ID)).thenReturn("zms1.zone1").thenReturn("zms2.zone1").thenReturn("zms3.zone1");
    Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_KEY_VALUE)).thenReturn("Value1").thenReturn("Value2").thenReturn("Value3");
    List<PublicKeyEntry> publicKeys = jdbcConn.listPublicKeys("my-domain", "service1");
    // data back is sorted
    assertEquals(3, publicKeys.size());
    assertEquals("zms1.zone1", publicKeys.get(0).getId());
    assertEquals("Value1", publicKeys.get(0).getKey());
    assertEquals("zms2.zone1", publicKeys.get(1).getId());
    assertEquals("Value2", publicKeys.get(1).getKey());
    assertEquals("zms3.zone1", publicKeys.get(2).getId());
    assertEquals("Value3", publicKeys.get(2).getKey());
    jdbcConn.close();
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) 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