use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testGetValidatedX509CertRecord.
@Test
public void testGetValidatedX509CertRecord() 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);
InstanceCertManager instanceCertManager = Mockito.mock(InstanceCertManager.class);
Mockito.when(instanceCertManager.getX509CertRecord("athenz.provider", "1001", "athenz.production")).thenReturn(null);
Mockito.when(instanceCertManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
ztsImpl.instanceCertManager = instanceCertManager;
Path path = Paths.get("src/test/resources/athenz.instanceid.pem");
String pem = new String(Files.readAllBytes(path));
X509Certificate cert = Crypto.loadX509Certificate(pem);
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
ResourceContext context = createResourceContext(principal);
ztsImpl.x509CertRefreshResetTime = new DynamicConfigLong(cert.getNotBefore().getTime() + 1);
X509CertRecord certRecord = ztsImpl.getValidatedX509CertRecord(context, "athenz.provider", "1001", "athenz.production", cert, "caller", "athenz", "athenz", "localhost");
assertNotNull(certRecord);
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRegisterInformationInvalidIP.
@Test
public void testPostInstanceRegisterInformationInvalidIP() 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));
InstanceCertManager instanceManager = Mockito.mock(InstanceCertManager.class);
Mockito.when(instanceManager.verifyInstanceCertIPAddress(Mockito.any(), Mockito.any())).thenReturn(false);
ztsImpl.instanceCertManager = instanceManager;
InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider").setToken(true);
ResourceContext context = createResourceContext(null);
try {
ztsImpl.postInstanceRegisterInformation(context, info);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 403);
assertTrue(ex.getMessage().contains("Unknown IP"));
}
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationSshHostCert.
@Test
public void testPostInstanceRefreshInformationSshHostCert() 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));
path = Paths.get("src/test/resources/sshhost_valid_sample.csr");
String sshCsr = new String(Files.readAllBytes(path));
Map<String, String> attrs = new HashMap<>();
attrs.put("certSSH", "true");
InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
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.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", "athenz.production")).thenReturn(certRecord);
Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
Mockito.when(instanceManager.generateSSHIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), 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).setSshCertificate("test ssh host certificate");
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.enableWorkloadStore = true;
InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr).setToken(true).setHostname("host1.athenz.cloud").setSsh(sshCsr);
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);
// setup the hostname resolver for our request
List<String> cnames = new ArrayList<>();
cnames.add("cname.athenz.info");
cnames.add("vip.athenz.info");
HostnameResolver resolver = Mockito.mock(HostnameResolver.class);
Mockito.when(resolver.isValidHostCnameList(principal.getFullName(), "host1.athenz.cloud", cnames, CertType.SSH_HOST)).thenReturn(true);
Mockito.when(resolver.isValidHostname("host1.athenz.cloud")).thenReturn(true);
ztsImpl.hostnameResolver = resolver;
InstanceIdentity instanceIdentity = ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
assertNotNull(instanceIdentity);
assertNotNull(instanceIdentity.getServiceToken());
assertNotNull(instanceIdentity.getSshCertificate());
ztsImpl.enableWorkloadStore = false;
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationSSHIdentityFailure.
@Test
public void testPostInstanceRefreshInformationSSHIdentityFailure() 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);
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
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", "athenz.production")).thenReturn(certRecord);
Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
assertNotNull(principal);
InstanceIdentity identity = new InstanceIdentity().setName("athenz.production");
Mockito.when(instanceManager.generateSSHIdentity(principal, identity, null, "ssh-csr", null, null, "user")).thenReturn(false);
ztsImpl.instanceCertManager = instanceManager;
InstanceRefreshInformation info = new InstanceRefreshInformation().setSsh("ssh-csr");
Path path = Paths.get("src/test/resources/athenz.instanceid.pem");
String pem = new String(Files.readAllBytes(path));
X509Certificate cert = Crypto.loadX509Certificate(pem);
principal.setX509Certificate(cert);
ResourceContext context = createResourceContext(principal);
try {
ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 500);
}
}
use of com.yahoo.athenz.zts.cert.InstanceCertManager in project athenz by yahoo.
the class ZTSImplTest method insertWorkloadRecordTest.
@Test
public void insertWorkloadRecordTest() {
Date certExpiryTime = new Date();
InstanceCertManager mockICM = Mockito.mock(InstanceCertManager.class);
InstanceCertManager origICM = zts.instanceCertManager;
zts.instanceCertManager = mockICM;
Mockito.when(mockICM.insertWorkloadRecord(any())).thenReturn(true, false);
zts.insertWorkloadRecord("athenz.api", "openstack", "123", "", "test1.host.yahoo.cloud", certExpiryTime);
Mockito.verify(mockICM, Mockito.times(0)).insertWorkloadRecord(any(WorkloadRecord.class));
zts.insertWorkloadRecord("athenz.api", "openstack", "123", "10.0.0.1", "test1.host.yahoo.cloud", certExpiryTime);
Mockito.verify(mockICM, Mockito.times(1)).insertWorkloadRecord(any(WorkloadRecord.class));
zts.insertWorkloadRecord("athenz.api", "openstack", "123", "10.0.0.1, 10.0.0.2", null, certExpiryTime);
Mockito.verify(mockICM, Mockito.times(3)).insertWorkloadRecord(any(WorkloadRecord.class));
zts.instanceCertManager = origICM;
}
Aggregations