use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationRefreshRequired.
@Test
public void testPostInstanceRefreshInformationRefreshRequired() 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("16503746516960996918");
certRecord.setPrevSerial("16503746516960996918");
certRecord.setClientCert(true);
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;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr).setToken(true);
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 instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
assertNotNull(instanceIdentity.getServiceToken());
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method getTransportRulesEdgeCasesTest.
@Test
public void getTransportRulesEdgeCasesTest() {
InstanceCertManager mockICM = Mockito.mock(InstanceCertManager.class);
InstanceCertManager origICM = zts.instanceCertManager;
zts.instanceCertManager = mockICM;
mockICM.setWorkloadStore(null);
final String domainName = "transportrulesedge";
addDomainToDataStore(domainName, "api");
Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null);
ResourceContext context = createResourceContext(principal);
TransportRules transportRules = zts.getTransportRules(context, domainName, "api");
assertTrue(transportRules.getEgressRules().isEmpty());
assertTrue(transportRules.getIngressRules().isEmpty());
DataCache domain = new DataCache();
DomainData domainData = new DomainData();
domainData.setName(domainName);
domain.setDomainData(domainData);
domainData.setRoles(new ArrayList<>());
Role role1 = ZTSTestUtils.createRoleObject(domainName, "ACL.api.inbound-4443", "dom1.svc1");
domainData.getRoles().add(role1);
Policy policy1 = ZTSTestUtils.createPolicyObject(domainName, "ACL.api.inbound", domainName + ":role.ACL.api.inbound-4443", false, "TCP-IN:1024-65535:4443", domainName + ":api", AssertionEffect.ALLOW);
domainData.setPolicies(new com.yahoo.athenz.zms.SignedPolicies());
domainData.getPolicies().setContents(new com.yahoo.athenz.zms.DomainPolicies());
domainData.getPolicies().getContents().setPolicies(new ArrayList<>());
domainData.getPolicies().getContents().getPolicies().add(policy1);
store.getCacheStore().put(domainName, domain);
addDomainToDataStore("dom1", "svc1");
Map<String, Role> rolesMap = new HashMap<>();
rolesMap.put(domainName + ":role.ACL.api.inbound-4443", role1);
domain.processPolicy(domainName, policy1, rolesMap);
zts.dataStore.getDataCache(domainName).getTransportRulesInfoForService("api").put("TCP-XYZ:1024-65535:4443", Collections.singletonList("dom1.svc1"));
transportRules = zts.getTransportRules(context, domainName, "api");
assertTrue(transportRules.getEgressRules().isEmpty());
assertTrue(transportRules.getIngressRules().isEmpty());
zts.instanceCertManager = origICM;
store.getCacheStore().invalidate("athenz");
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformation.
private void testPostInstanceRefreshInformation(final String csrPath, final String hostname) 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(csrPath);
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("169894876839995310717517774217024528903");
certRecord.setPrevSerial("169894876839995310717517774217024528903");
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.hostname.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;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr).setToken(true);
if (hostname != null) {
info.setHostname(hostname);
}
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 instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
assertNotNull(instanceIdentity.getServiceToken());
ArgumentCaptor<X509CertRecord> captor = ArgumentCaptor.forClass(X509CertRecord.class);
Mockito.verify(instanceManager, atLeastOnce()).updateX509CertRecord(captor.capture());
X509CertRecord actualCert = captor.getValue();
assertEquals(actualCert.getHostName(), hostname);
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformation.
@Test
public void testPostInstanceRefreshInformation() throws IOException {
ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
DataStore store = new DataStore(structStore, null);
ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
SignedDomain providerDomain = signedAuthorizedProviderDomain();
store.processDomain(providerDomain, false);
SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
store.processDomain(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("athenz.provider")).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("16503746516960996918");
certRecord.setPrevSerial("16503746516960996918");
Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001")).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.anyInt());
ztsImpl.instanceProviderManager = instanceProviderManager;
ztsImpl.instanceCertManager = instanceManager;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr).setToken(true);
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
X509Certificate cert = Crypto.loadX509Certificate(pem);
principal.setX509Certificate(cert);
ResourceContext context = createResourceContext(principal);
InstanceIdentity instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
assertNotNull(instanceIdentity.getServiceToken());
}
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;
}
Aggregations