Search in sources :

Example 6 with PublicKeyEntry

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

the class JDBCConnectionTest method testInsertPublicKeyEntryException.

@Test
public void testInsertPublicKeyEntryException() 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.insertPublicKeyEntry("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 7 with PublicKeyEntry

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

the class SignUtilsTest method testAsStructRoleService.

@Test
public void testAsStructRoleService() {
    List<Role> roles = new ArrayList<Role>();
    Role mRole = Mockito.mock(Role.class);
    roles.add(mRole);
    List<String> items = new ArrayList<String>();
    String item = "check_item";
    items.add(item);
    List<ServiceIdentity> services = new ArrayList<ServiceIdentity>();
    ServiceIdentity mService = Mockito.mock(ServiceIdentity.class);
    services.add(mService);
    List<PublicKeyEntry> publicKeys = new ArrayList<PublicKeyEntry>();
    PublicKeyEntry mPublicKey = Mockito.mock(PublicKeyEntry.class);
    publicKeys.add(mPublicKey);
    SignedPolicies signedPolicies = Mockito.mock(SignedPolicies.class);
    Mockito.when(mockDomain.getEnabled()).thenReturn(null);
    Mockito.when(mockDomain.getAccount()).thenReturn("chk_string");
    Mockito.when(mockDomain.getRoles()).thenReturn(roles);
    Mockito.when(mRole.getMembers()).thenReturn(items);
    Mockito.when(mockDomain.getServices()).thenReturn(services);
    Mockito.when(mService.getHosts()).thenReturn(null);
    Mockito.when(mService.getPublicKeys()).thenReturn(publicKeys);
    Mockito.when(mockDomain.getPolicies()).thenReturn(signedPolicies);
    Mockito.when(signedPolicies.getContents()).thenReturn(mockPolicies);
    String check = SignUtils.asCanonicalString(mockDomain);
    assertNotNull(check);
    assertEquals(check, "{\"account\":\"chk_string\",\"policies\":{\"contents\":{\"policies\":[]}},\"roles\":[{\"members\":[\"check_item\"],\"roleMembers\":[]}],\"services\":[{\"publicKeys\":[{}]}],\"ypmId\":0}");
    Mockito.when(mService.getPublicKeys()).thenReturn(null);
    check = SignUtils.asCanonicalString(mockDomain);
    assertNotNull(check);
    assertEquals(check, "{\"account\":\"chk_string\",\"policies\":{\"contents\":{\"policies\":[]}},\"roles\":[{\"members\":[\"check_item\"],\"roleMembers\":[]}],\"services\":[{\"publicKeys\":[]}],\"ypmId\":0}");
}
Also used : Role(com.yahoo.athenz.zms.Role) PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) SignedPolicies(com.yahoo.athenz.zms.SignedPolicies) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 8 with PublicKeyEntry

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

the class FileConnection method getPublicKeyEntry.

@Override
public PublicKeyEntry getPublicKeyEntry(String domainName, String serviceName, String keyId, boolean domainStateCheck) {
    DomainStruct domainStruct = getDomainStruct(domainName);
    if (domainStruct == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "getPublicKeyEntry");
    }
    if (domainStateCheck && domainStruct.getMeta().getEnabled() == Boolean.FALSE) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain disabled", "getPublicKeyEntry");
    }
    ServiceIdentity service = getServiceObject(domainStruct, serviceName);
    if (service == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "service not found", "getPublicKeyEntry");
    }
    List<PublicKeyEntry> publicKeys = service.getPublicKeys();
    if (publicKeys == null) {
        return null;
    }
    for (PublicKeyEntry keyEntry : publicKeys) {
        if (keyId.equals(keyEntry.getId())) {
            return keyEntry;
        }
    }
    return null;
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity)

Example 9 with PublicKeyEntry

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

the class FilePublicKeyStore method loadPublicKeys.

void loadPublicKeys(ArrayList<PublicKeyEntry> publicKeys, Map<String, PublicKey> keyMap) {
    if (publicKeys == null) {
        return;
    }
    for (PublicKeyEntry publicKey : publicKeys) {
        String id = publicKey.getId();
        String key = publicKey.getKey();
        if (key == null || id == null) {
            continue;
        }
        PublicKey pubKey = null;
        try {
            pubKey = Crypto.loadPublicKey(Crypto.ybase64DecodeString(key));
        } catch (Exception e) {
            LOG.error("Invalid ZTS public key for id: " + id + " - " + e.getMessage());
            continue;
        }
        keyMap.put(id, pubKey);
    }
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) PublicKey(java.security.PublicKey)

Example 10 with PublicKeyEntry

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

the class FileConnectionTest method testUpdatePublicKeyEntry.

@Test
public void testUpdatePublicKeyEntry() {
    File fileDir = new File("/home/athenz/zms_store");
    File quotaDir = new File("/home/athenz/zms_quota");
    try (FileConnection fileconnection = new FileConnection(fileDir, quotaDir)) {
        PublicKeyEntry keyEntry = new PublicKeyEntry();
        try {
            fileconnection.updatePublicKeyEntry("domain1", "service1", keyEntry);
        } catch (Exception ex) {
            assertTrue(true);
        }
    }
}
Also used : PublicKeyEntry(com.yahoo.athenz.zms.PublicKeyEntry) File(java.io.File) 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