use of com.yahoo.athenz.common.server.ssh.SSHCertRecord in project athenz by yahoo.
the class InstanceCertManagerTest method testUpdateSSHHostPrincipalWithCertRequest.
@Test
public void testUpdateSSHHostPrincipalWithCertRequest() {
InstanceCertManager instance = new InstanceCertManager(null, null, null, true, null);
SSHCertRecord record = new SSHCertRecord();
SSHCertRequest sshCertRequest = new SSHCertRequest();
instance.updateSSHHostPrincipals(sshCertRequest, record);
assertEquals(record.getPrincipals(), "127.0.0.1");
// reset and test with meta set to null
record.setPrincipals(null);
sshCertRequest.setCertRequestData(new SSHCertRequestData());
sshCertRequest.setCertRequestMeta(null);
instance.updateSSHHostPrincipals(sshCertRequest, record);
assertEquals(record.getPrincipals(), "127.0.0.1");
// reset and test with data set to null
record.setPrincipals(null);
sshCertRequest.setCertRequestData(null);
sshCertRequest.setCertRequestMeta(new SSHCertRequestMeta());
instance.updateSSHHostPrincipals(sshCertRequest, record);
assertEquals(record.getPrincipals(), "127.0.0.1");
// reset and test csr with principals and xprincipals
record.setPrincipals(null);
sshCertRequest.setCertRequestData(new SSHCertRequestData().setPrincipals(Arrays.asList("principal1", "principal2")));
sshCertRequest.setCertRequestMeta(new SSHCertRequestMeta().setKeyIdPrincipals(Arrays.asList("principal2", "principal3")));
instance.updateSSHHostPrincipals(sshCertRequest, record);
Set<String> result = new HashSet<>(Arrays.asList(record.getPrincipals().split(",")));
assertEquals(result.size(), 3);
assertTrue(result.contains("principal1"));
assertTrue(result.contains("principal2"));
assertTrue(result.contains("principal3"));
}
use of com.yahoo.athenz.common.server.ssh.SSHCertRecord in project athenz by yahoo.
the class InstanceCertManagerTest method testUpdateSSHCertRecordException.
@Test
public void testUpdateSSHCertRecordException() {
InstanceCertManager instance = new InstanceCertManager(null, null, null, true, null);
SSHRecordStore certStore = Mockito.mock(SSHRecordStore.class);
SSHRecordStoreConnection certConnection = Mockito.mock(SSHRecordStoreConnection.class);
when(certStore.getConnection()).thenReturn(certConnection);
when(certConnection.updateSSHCertRecord(any())).thenThrow(new RuntimeException("Fail to update"));
instance.setSSHStore(certStore);
SSHCertRecord sshCertRecord = new SSHCertRecord();
assertFalse(instance.updateSSHCertRecord(sshCertRecord, true));
instance.shutdown();
}
use of com.yahoo.athenz.common.server.ssh.SSHCertRecord in project athenz by yahoo.
the class InstanceCertManagerTest method getGenerateSSHCertificate.
@Test
public void getGenerateSSHCertificate() {
SSHSigner sshSigner = Mockito.mock(SSHSigner.class);
Principal principal = Mockito.mock(Principal.class);
SSHCertRequest certRequest = new SSHCertRequest();
SSHCertRequestMeta meta = new SSHCertRequestMeta();
meta.setInstanceId("id");
meta.setAthenzService("athenz.api");
meta.setCertType("host");
certRequest.setCertRequestMeta(meta);
SSHCertificates sshCertificates = new SSHCertificates();
InstanceCertManager instanceManager = new InstanceCertManager(null, null, null, false, null);
// let's insert our ssh record first
SSHCertRecord certRecord = new SSHCertRecord();
certRecord.setInstanceId("id");
certRecord.setService("athenz.api");
certRecord.setPrincipals("127.0.0.1");
instanceManager.updateSSHCertRecord(certRecord, false);
// during the function call we'll add the principals
// field so for mock we're going to remove that
when(sshSigner.generateCertificate(any(), any(), any(), any())).thenReturn(sshCertificates);
instanceManager.setSSHSigner(sshSigner);
assertEquals(instanceManager.generateSSHCertificates(principal, certRequest), sshCertificates);
instanceManager.shutdown();
}
use of com.yahoo.athenz.common.server.ssh.SSHCertRecord in project athenz by yahoo.
the class InstanceCertManagerTest method testGenerateSshIdentityHostException.
@Test
public void testGenerateSshIdentityHostException() {
String sshCsr = "{\"pubkey\":\"key\",\"certtype\":\"host\"}";
SSHSigner sshSigner = Mockito.mock(SSHSigner.class);
SSHCertRequest sshRequest = new SSHCertRequest();
sshRequest.setCsr(sshCsr);
SSHCertificate cert = new SSHCertificate();
cert.setCertificate("ssh-cert");
InstanceIdentity identity = new InstanceIdentity().setName("athenz.service");
SSHCertRecord sshCertRecord = new SSHCertRecord();
sshCertRecord.setPrincipals("127.0.0.1");
when(sshSigner.generateCertificate(null, sshRequest, sshCertRecord, "host")).thenThrow(new ResourceException(400, "invalid request"));
when(sshSigner.getSignerCertificate(ZTSConsts.ZTS_SSH_HOST)).thenReturn("ssh-host");
when(sshSigner.getSignerCertificate(ZTSConsts.ZTS_SSH_USER)).thenReturn("ssh-user");
InstanceCertManager instanceManager = new InstanceCertManager(null, null, null, true, null);
instanceManager.setSSHSigner(sshSigner);
assertFalse(instanceManager.generateSSHIdentity(null, identity, null, sshCsr, null, sshCertRecord, "host"));
instanceManager.shutdown();
}
use of com.yahoo.athenz.common.server.ssh.SSHCertRecord in project athenz by yahoo.
the class InstanceCertManagerTest method testGenerateSshIdentityExceptions.
@Test
public void testGenerateSshIdentityExceptions() {
String sshCsr = "{\"csr\":\"csr\",\"certtype\":\"host\"}";
SSHSigner sshSigner = Mockito.mock(com.yahoo.athenz.common.server.ssh.SSHSigner.class);
SSHCertRequest sshRequest = new SSHCertRequest();
sshRequest.setCsr(sshCsr);
when(sshSigner.generateCertificate(null, sshRequest, null, "host")).thenThrow(new com.yahoo.athenz.common.server.rest.ResourceException(403, "Forbidden")).thenThrow(new RuntimeException("IO error"));
InstanceCertManager instanceManager = new InstanceCertManager(null, null, null, true, null);
instanceManager.setSSHSigner(sshSigner);
InstanceIdentity identity = new InstanceIdentity().setName("athenz.service");
// first we should get the resource exception
boolean result = instanceManager.generateSSHIdentity(null, identity, "", sshCsr, null, new SSHCertRecord(), "host");
assertFalse(result);
// next we should get the io exception
result = instanceManager.generateSSHIdentity(null, identity, "", sshCsr, null, new SSHCertRecord(), "host");
assertFalse(result);
instanceManager.shutdown();
}
Aggregations