Search in sources :

Example 6 with WorkloadRecordStoreConnection

use of com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection in project athenz by yahoo.

the class InstanceCertManagerTest method testGetWorkloadsByIp.

@Test
public void testGetWorkloadsByIp() {
    InstanceCertManager instance = new InstanceCertManager(null, null, null, true, null);
    WorkloadRecordStore store = Mockito.mock(WorkloadRecordStore.class);
    instance.setWorkloadStore(store);
    WorkloadRecordStoreConnection storeConn = Mockito.mock(WorkloadRecordStoreConnection.class);
    Mockito.when(store.getConnection()).thenReturn(storeConn);
    long currTime = System.currentTimeMillis();
    Date d = new Date(currTime);
    WorkloadRecord w1 = ZTSTestUtils.createWorkloadRecord(d, d, "aws", "i-123", "test-host1.corp.yahoo.com", "10.0.0.1", "athenz.api", d);
    WorkloadRecord w5 = ZTSTestUtils.createWorkloadRecord(d, d, "aws", "i-123", "test-host2.corp.yahoo.com", "10.0.0.1", "athenz.secondapi", d);
    List<WorkloadRecord> workloadRecordList = new ArrayList<>();
    workloadRecordList.add(w1);
    workloadRecordList.add(w5);
    Mockito.when(storeConn.getWorkloadRecordsByIp(any())).thenReturn(workloadRecordList);
    List<Workload> workloadList = instance.getWorkloadsByIp("10.0.0.1");
    assertNotNull(workloadList);
    assertEquals(workloadList.size(), 2);
    List<String> expectedServices = workloadList.stream().map(Workload::getServiceName).collect(Collectors.toList());
    assertTrue(expectedServices.contains("api"));
    assertTrue(expectedServices.contains("secondapi"));
    instance.shutdown();
}
Also used : WorkloadRecordStore(com.yahoo.athenz.common.server.workload.WorkloadRecordStore) WorkloadRecordStoreConnection(com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Test(org.testng.annotations.Test)

Example 7 with WorkloadRecordStoreConnection

use of com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection in project athenz by yahoo.

the class InstanceCertManager method getWorkloadsByService.

public List<Workload> getWorkloadsByService(String domain, String service) {
    if (workloadStore == null) {
        return Collections.emptyList();
    }
    try (WorkloadRecordStoreConnection storeConnection = workloadStore.getConnection()) {
        List<WorkloadRecord> workloadRecords = storeConnection.getWorkloadRecordsByService(domain, service);
        Map<String, List<String>> flattenedIpAddresses = new HashMap<>();
        String mapKey;
        for (WorkloadRecord workloadRecord : workloadRecords) {
            mapKey = workloadRecord.getInstanceId() + ":" + workloadRecord.getProvider() + ":" + workloadRecord.getUpdateTime().getTime() + ":" + workloadRecord.getCertExpiryTime().getTime() + ":" + workloadRecord.getHostname();
            if (flattenedIpAddresses.containsKey(mapKey)) {
                flattenedIpAddresses.get(mapKey).add(workloadRecord.getIp());
            } else {
                List<String> ipList = new ArrayList<>();
                ipList.add(workloadRecord.getIp());
                flattenedIpAddresses.put(mapKey, ipList);
            }
        }
        return flattenedIpAddresses.entrySet().stream().map(entry -> {
            Workload wl = new Workload();
            String[] tempArr = entry.getKey().split(":");
            wl.setUuid(tempArr[0]).setProvider(tempArr[1]).setUpdateTime(Timestamp.fromMillis(Long.parseLong(tempArr[2]))).setCertExpiryTime(Timestamp.fromMillis(Long.parseLong(tempArr[3]))).setHostname(tempArr[4]).setIpAddresses(entry.getValue());
            return wl;
        }).collect(Collectors.toList());
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) Authorizer(com.yahoo.athenz.auth.Authorizer) java.util(java.util) AthenzUtils(com.yahoo.athenz.auth.util.AthenzUtils) RolesProvider(com.yahoo.athenz.common.server.db.RolesProvider) LoggerFactory(org.slf4j.LoggerFactory) WorkloadRecordStoreConnection(com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection) com.yahoo.athenz.common.server.cert(com.yahoo.athenz.common.server.cert) PrivateKeyStore(com.yahoo.athenz.auth.PrivateKeyStore) Function(java.util.function.Function) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) WorkloadRecordStoreFactory(com.yahoo.athenz.common.server.workload.WorkloadRecordStoreFactory) StringUtil(org.eclipse.jetty.util.StringUtil) X509CertUtils(com.yahoo.athenz.common.utils.X509CertUtils) CryptoException(com.yahoo.athenz.auth.util.CryptoException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) com.yahoo.athenz.zts.utils(com.yahoo.athenz.zts.utils) com.yahoo.athenz.zts(com.yahoo.athenz.zts) Crypto(com.yahoo.athenz.auth.util.Crypto) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) com.yahoo.athenz.common.server.ssh(com.yahoo.athenz.common.server.ssh) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Authority(com.yahoo.athenz.auth.Authority) HostnameResolver(com.yahoo.athenz.common.server.dns.HostnameResolver) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Collectors(java.util.stream.Collectors) NotificationManager(com.yahoo.athenz.common.server.notification.NotificationManager) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Timestamp(com.yahoo.rdl.Timestamp) Principal(com.yahoo.athenz.auth.Principal) WorkloadRecordStore(com.yahoo.athenz.common.server.workload.WorkloadRecordStore) InetAddresses(com.google.common.net.InetAddresses) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WorkloadRecordStoreConnection(com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord)

Aggregations

WorkloadRecordStoreConnection (com.yahoo.athenz.common.server.workload.WorkloadRecordStoreConnection)7 WorkloadRecordStore (com.yahoo.athenz.common.server.workload.WorkloadRecordStore)5 Test (org.testng.annotations.Test)5 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)3 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InetAddresses (com.google.common.net.InetAddresses)1 Authority (com.yahoo.athenz.auth.Authority)1 Authorizer (com.yahoo.athenz.auth.Authorizer)1 Principal (com.yahoo.athenz.auth.Principal)1 PrivateKeyStore (com.yahoo.athenz.auth.PrivateKeyStore)1 AthenzUtils (com.yahoo.athenz.auth.util.AthenzUtils)1 Crypto (com.yahoo.athenz.auth.util.Crypto)1 CryptoException (com.yahoo.athenz.auth.util.CryptoException)1 com.yahoo.athenz.common.server.cert (com.yahoo.athenz.common.server.cert)1 RolesProvider (com.yahoo.athenz.common.server.db.RolesProvider)1 HostnameResolver (com.yahoo.athenz.common.server.dns.HostnameResolver)1 NotificationManager (com.yahoo.athenz.common.server.notification.NotificationManager)1 com.yahoo.athenz.common.server.ssh (com.yahoo.athenz.common.server.ssh)1 WorkloadRecordStoreFactory (com.yahoo.athenz.common.server.workload.WorkloadRecordStoreFactory)1