Search in sources :

Example 46 with InstanceCertManager

use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRegisterInformationWithHostname.

@Test
public void testPostInstanceRegisterInformationWithHostname() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null, ztsMetric);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processSignedDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processSignedDomain(tenantDomain, false);
    Path path = Paths.get("src/test/resources/athenz.instanceid.hostname.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
    InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
    Mockito.when(providerClient.getProviderScheme()).thenReturn(InstanceProvider.Scheme.CLASS);
    Map<String, String> attrs = new HashMap<>();
    attrs.put("certSSH", "true");
    InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider").setAttributes(attrs);
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    Mockito.when(instanceProviderManager.getProvider(eq("athenz.provider"), Mockito.any())).thenReturn(providerClient);
    Mockito.when(providerClient.confirmInstance(Mockito.any())).thenReturn(confirmation);
    Mockito.when(instanceManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
    path = Paths.get("src/test/resources/athenz.instanceid.pem");
    String pem = new String(Files.readAllBytes(path));
    InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
    Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any());
    Mockito.doReturn(true).when(instanceManager).insertWorkloadRecord(any(WorkloadRecord.class));
    ztsImpl.instanceProviderManager = instanceProviderManager;
    ztsImpl.instanceCertManager = instanceManager;
    ztsImpl.enableWorkloadStore = true;
    InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider").setToken(true).setHostname("host1.athenz.cloud");
    ResourceContext context = createResourceContext(null);
    Response response = ztsImpl.postInstanceRegisterInformation(context, info);
    assertEquals(response.getStatus(), 201);
    InstanceIdentity resIdentity = (InstanceIdentity) response.getEntity();
    assertNotNull(resIdentity.getX509Certificate());
    ztsImpl.enableWorkloadStore = false;
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) ChangeLogStore(com.yahoo.athenz.common.server.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) InstanceProvider(com.yahoo.athenz.instance.provider.InstanceProvider) Test(org.testng.annotations.Test)

Example 47 with InstanceCertManager

use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRefreshInformationSerialMismatchRevokeMigration.

@Test
public void testPostInstanceRefreshInformationSerialMismatchRevokeMigration() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null, ztsMetric);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processSignedDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processSignedDomain(tenantDomain, false);
    Path path = Paths.get("src/test/resources/athenz.instanceid.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
    InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
    InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider");
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    Mockito.when(instanceProviderManager.getProvider(eq("athenz.provider"), Mockito.any())).thenReturn(providerClient);
    Mockito.when(providerClient.refreshInstance(Mockito.any())).thenReturn(confirmation);
    X509CertRecord certRecord = new X509CertRecord();
    certRecord.setInstanceId("1001");
    certRecord.setProvider("athenz.provider");
    certRecord.setService("athenz.production");
    certRecord.setCurrentSerial("101");
    certRecord.setPrevSerial("101");
    Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001", "athenz.production")).thenReturn(certRecord);
    Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
    path = Paths.get("src/test/resources/athenz.instanceid.pem");
    String pem = new String(Files.readAllBytes(path));
    InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
    Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any());
    ztsImpl.instanceProviderManager = instanceProviderManager;
    ztsImpl.instanceCertManager = instanceManager;
    ztsImpl.x509CertRefreshResetTime = new DynamicConfigLong(System.currentTimeMillis());
    InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr);
    CertificateAuthority certAuthority = new CertificateAuthority();
    SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
    assertNotNull(principal);
    X509Certificate cert = Crypto.loadX509Certificate(pem);
    principal.setX509Certificate(cert);
    ResourceContext context = createResourceContext(principal);
    InstanceIdentity refreshIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
    assertNotNull(refreshIdentity);
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) DynamicConfigLong(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigLong) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) X509Certificate(java.security.cert.X509Certificate) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) ChangeLogStore(com.yahoo.athenz.common.server.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) InstanceProvider(com.yahoo.athenz.instance.provider.InstanceProvider) Test(org.testng.annotations.Test)

Example 48 with InstanceCertManager

use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRegisterInformationInvalidHostname.

@Test
public void testPostInstanceRegisterInformationInvalidHostname() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null, ztsMetric);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processSignedDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processSignedDomain(tenantDomain, false);
    Path path = Paths.get("src/test/resources/athenz.instanceid.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
    InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
    Mockito.when(providerClient.getProviderScheme()).thenReturn(InstanceProvider.Scheme.CLASS);
    Map<String, String> attrs = new HashMap<>();
    attrs.put("certSSH", "true");
    InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider").setAttributes(attrs);
    HostnameResolver resolver = Mockito.mock(HostnameResolver.class);
    Mockito.when(resolver.isValidHostname("host1.athenz.cloud")).thenReturn(false);
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    Mockito.when(instanceProviderManager.getProvider("athenz.provider", resolver)).thenReturn(providerClient);
    Mockito.when(providerClient.confirmInstance(Mockito.any())).thenReturn(confirmation);
    Mockito.when(instanceManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
    path = Paths.get("src/test/resources/athenz.instanceid.pem");
    String pem = new String(Files.readAllBytes(path));
    InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
    Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any());
    ztsImpl.instanceProviderManager = instanceProviderManager;
    ztsImpl.instanceCertManager = instanceManager;
    ztsImpl.hostnameResolver = resolver;
    InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider").setToken(true).setHostname("unknown.host.athenz.cloud");
    ResourceContext context = createResourceContext(null);
    try {
        ztsImpl.postInstanceRegisterInformation(context, info);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), ResourceException.BAD_REQUEST);
    }
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) ChangeLogStore(com.yahoo.athenz.common.server.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore) HostnameResolver(com.yahoo.athenz.common.server.dns.HostnameResolver) DataStore(com.yahoo.athenz.zts.store.DataStore) InstanceProvider(com.yahoo.athenz.instance.provider.InstanceProvider) Test(org.testng.annotations.Test)

Example 49 with InstanceCertManager

use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.

the class ZTSImplTest method updateWorkloadRecordTest.

@Test
public void updateWorkloadRecordTest() {
    Date certExpiryTime = new Date();
    InstanceCertManager mockICM = Mockito.mock(InstanceCertManager.class);
    InstanceCertManager origICM = zts.instanceCertManager;
    zts.instanceCertManager = mockICM;
    Mockito.when(mockICM.updateWorkloadRecord(any())).thenReturn(true, false);
    zts.updateWorkloadRecord("athenz.api", "openstack", "123", "", "test.host-1.yahoo.cloud", certExpiryTime);
    Mockito.verify(mockICM, Mockito.times(0)).updateWorkloadRecord(any(WorkloadRecord.class));
    zts.updateWorkloadRecord("athenz.api", "openstack", "123", "10.0.0.1", "test.host-1.yahoo.cloud", certExpiryTime);
    Mockito.verify(mockICM, Mockito.times(1)).updateWorkloadRecord(any(WorkloadRecord.class));
    zts.updateWorkloadRecord("athenz.api", "openstack", "123", "10.0.0.1, 10.0.0.2", null, certExpiryTime);
    Mockito.verify(mockICM, Mockito.times(3)).updateWorkloadRecord(any(WorkloadRecord.class));
    zts.instanceCertManager = origICM;
}
Also used : InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Test(org.testng.annotations.Test)

Example 50 with InstanceCertManager

use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.

the class ZTSImplTest method getWorkloadsByServiceTest.

@Test
public void getWorkloadsByServiceTest() {
    final String domainName = "workloadsbyservice";
    final String serviceName = "api";
    Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null);
    ResourceContext context = createResourceContext(principal);
    // try with invalid domain first ( domain not in data cache )
    try {
        zts.getWorkloadsByService(context, domainName, serviceName);
        fail();
    } catch (ResourceException re) {
        assertEquals(ResourceException.BAD_REQUEST, re.getCode());
    }
    DataCache domain = new DataCache();
    DomainData domainData = new DomainData();
    domainData.setName(domainName);
    domain.setDomainData(domainData);
    domainData.setServices(new ArrayList<>());
    ServiceIdentity service = new ServiceIdentity();
    service.setName(generateServiceIdentityName(domainName, serviceName));
    setServicePublicKey(service, "0", ZTS_Y64_CERT0);
    List<String> hosts = new ArrayList<>();
    hosts.add("host1");
    hosts.add("host2");
    service.setHosts(hosts);
    domainData.getServices().add(service);
    store.getCacheStore().put(domainName, domain);
    domain.processServiceIdentity(service);
    InstanceCertManager mockICM = Mockito.mock(InstanceCertManager.class);
    InstanceCertManager origICM = zts.instanceCertManager;
    zts.instanceCertManager = mockICM;
    List<String> svcIps = Arrays.asList("10.1.1.1", "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
    List<Workload> dynamicWls = new ArrayList<>();
    Workload wl1 = new Workload().setProvider("openstack").setIpAddresses(svcIps).setUuid("instance-id-os-1").setUpdateTime(Timestamp.fromCurrentTime()).setHostname("test1.host.yahoo.cloud");
    Workload wl2 = new Workload().setProvider("openstack").setIpAddresses(Collections.singletonList("10.1.1.2")).setUuid("instance-id-os-2").setUpdateTime(Timestamp.fromCurrentTime()).setHostname("test2.host.yahoo.cloud");
    Workload wl3 = new Workload().setProvider("kubernetes").setIpAddresses(Collections.singletonList("10.2.1.1")).setUuid("instance-id-k8s-1").setUpdateTime(Timestamp.fromCurrentTime()).setHostname("test3.host.yahoo.cloud");
    Workload wl4 = new Workload().setProvider("kubernetes").setIpAddresses(Collections.singletonList("10.2.1.2")).setUuid("instance-id-k8s-2").setUpdateTime(Timestamp.fromCurrentTime()).setHostname("test4.host.yahoo.cloud");
    Workload wl5 = new Workload().setProvider("aws").setIpAddresses(Collections.singletonList("10.3.1.1")).setUuid("instance-id-aws-1").setUpdateTime(Timestamp.fromCurrentTime()).setHostname("test5.host.yahoo.cloud");
    dynamicWls.add(wl1);
    dynamicWls.add(wl2);
    dynamicWls.add(wl3);
    dynamicWls.add(wl4);
    dynamicWls.add(wl5);
    Mockito.when(mockICM.getWorkloadsByService(domainName, serviceName)).thenReturn(dynamicWls);
    Workloads workloads = zts.getWorkloadsByService(context, domainName, serviceName);
    assertNotNull(workloads);
    assertThat(workloads.getWorkloadList(), hasItems(wl1, wl2, wl3, wl4, wl5));
    zts.instanceCertManager = origICM;
}
Also used : InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) DataCache(com.yahoo.athenz.zts.cache.DataCache) Principal(com.yahoo.athenz.auth.Principal) Test(org.testng.annotations.Test)

Aggregations

InstanceCertManager (com.yahoo.athenz.zts.cert.InstanceCertManager)71 Test (org.testng.annotations.Test)69 DataStore (com.yahoo.athenz.zts.store.DataStore)61 ChangeLogStore (com.yahoo.athenz.common.server.store.ChangeLogStore)56 ZMSFileChangeLogStore (com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore)56 MockZMSFileChangeLogStore (com.yahoo.athenz.zts.store.MockZMSFileChangeLogStore)56 Path (java.nio.file.Path)49 InstanceProvider (com.yahoo.athenz.instance.provider.InstanceProvider)39 InstanceConfirmation (com.yahoo.athenz.instance.provider.InstanceConfirmation)34 X509Certificate (java.security.cert.X509Certificate)30 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)23 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Response (javax.ws.rs.core.Response)11 Principal (com.yahoo.athenz.auth.Principal)10 HostnameResolver (com.yahoo.athenz.common.server.dns.HostnameResolver)7 JOSEException (com.nimbusds.jose.JOSEException)3 CertificateAuthority (com.yahoo.athenz.auth.impl.CertificateAuthority)3 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)3 DynamicConfigLong (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigLong)3 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)3