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;
}
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);
}
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);
}
}
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;
}
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