use of com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRegisterInformationNetworkFailure.
@Test
public void testPostInstanceRegisterInformationNetworkFailure() 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(instanceProviderManager.getProvider(eq("athenz.provider"), Mockito.any())).thenReturn(providerClient);
Mockito.when(providerClient.confirmInstance(Mockito.any())).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(504, "Connect Timeout")).thenThrow(new com.yahoo.athenz.instance.provider.ResourceException(403, "Instance Revoked"));
ztsImpl.instanceProviderManager = instanceProviderManager;
InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider");
ResourceContext context = createResourceContext(null);
try {
ztsImpl.postInstanceRegisterInformation(context, info);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 504);
assertTrue(ex.getMessage().contains("Connect Timeout"));
}
try {
ztsImpl.postInstanceRegisterInformation(context, info);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 403);
assertTrue(ex.getMessage().contains("Instance Revoked"));
}
}
use of com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore in project athenz by yahoo.
the class ZTSImplTest method testGetRoleTokenAddrLoopbackNoXFF.
@Test
public void testGetRoleTokenAddrLoopbackNoXFF() {
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
Mockito.when(servletRequest.getRemoteAddr()).thenReturn("127.0.0.1");
Mockito.when(servletRequest.isSecure()).thenReturn(true);
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);
ZTSImpl.serverHostName = "localhost";
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", true);
store.processSignedDomain(signedDomain, false);
Principal principal = SimplePrincipal.create("user_domain", "user", "v=U1;d=user_domain;n=user;s=signature", 0, null);
ResourceContext context = createResourceContext(principal, servletRequest);
RoleToken roleToken = ztsImpl.getRoleToken(context, "coretech", null, 600, 1200, null);
com.yahoo.athenz.auth.token.RoleToken token = new com.yahoo.athenz.auth.token.RoleToken(roleToken.getToken());
assertNotNull(token);
assertNotNull(token.getUnsignedToken());
}
use of com.yahoo.athenz.common.server.store.impl.ZMSFileChangeLogStore 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.common.server.store.impl.ZMSFileChangeLogStore 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.common.server.store.impl.ZMSFileChangeLogStore 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;
}
Aggregations