use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testPostDomainMetrics.
@Test
public void testPostDomainMetrics() {
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", false);
store.processDomain(signedDomain, false);
Principal principal = SimplePrincipal.create("user_domain", "user", "v=U1;d=user_domain;n=user;s=signature", 0, null);
ResourceContext context = createResourceContext(principal);
// create zts with a metric we can verify
CloudStore cloudStore = new CloudStore(null);
cloudStore.setHttpClient(null);
ZtsMetricTester metric = new ZtsMetricTester();
ZTSImpl ztsImpl = new ZTSImpl(cloudStore, store);
ZTSImpl.serverHostName = "localhost";
ztsImpl.metric = metric;
String testDomain = "coretech";
// create some metrics
List<DomainMetric> metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(99));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(27));
DomainMetrics req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// send the metrics
ztsImpl.postDomainMetrics(context, testDomain, req);
// verify metrics were recorded
Map<String, Integer> metrixMap = metric.getMap();
String key = "dom_metric_" + DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH.toString().toLowerCase() + testDomain;
Integer val = metrixMap.get(key);
assertNotNull(val);
assertEquals(val.intValue(), 99);
key = "dom_metric_" + DomainMetricType.ACCESS_ALLOWED.toString().toLowerCase() + testDomain;
val = metrixMap.get(key);
assertNotNull(val);
assertEquals(val.intValue(), 27);
// test - failure case - invalid domain
testDomain = "not_coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// send the metrics
metrixMap.clear();
String errMsg = "No such domain";
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 404);
assertTrue(ex.getMessage().contains(errMsg));
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - missing domain name in metric data
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setMetricList(metricList);
errMsg = "Missing required field: domainName";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - mismatch domain in uri and metric data
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName("not_coretech").setMetricList(metricList);
errMsg = "mismatched domain names";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - empty metric list
testDomain = "coretech";
// create some metrics
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(999));
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED).setMetricVal(277));
req = new DomainMetrics().setDomainName(testDomain);
errMsg = "Missing required field: metricList";
// send the metrics
metrixMap.clear();
try {
ztsImpl.postDomainMetrics(context, testDomain, req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
}
// verify no metrics were recorded
assertEquals(metrixMap.size(), 0);
// test - failure case - metric count is missing
testDomain = "coretech";
// create a single metric without a count
metricList = new ArrayList<>();
metricList.add(new DomainMetric().setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).setMetricVal(-1));
req = new DomainMetrics().setDomainName(testDomain).setMetricList(metricList);
// verify no metrics were recorded
metrixMap.clear();
ztsImpl.postDomainMetrics(context, testDomain, req);
assertEquals(metrixMap.size(), 0);
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testRetrieveTenantDomainName4PlusCompsValidDomainWithResourceGroup.
@Test
public void testRetrieveTenantDomainName4PlusCompsValidDomainWithResourceGroup() {
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", true);
store.processDomain(signedDomain, false);
assertEquals("coretech", zts.retrieveTenantDomainName("storage.tenant.coretech.resource_group.admin", "storage"));
assertEquals("coretech", zts.retrieveTenantDomainName("storage.tenant.coretech.resource_group.admin", null));
signedDomain = createSignedDomain("coretech.office.burbank", "weather", "storage", true);
store.processDomain(signedDomain, false);
assertEquals("coretech.office.burbank", zts.retrieveTenantDomainName("storage.tenant.coretech.office.burbank.resource_group.admin", "storage"));
assertEquals("coretech.office.burbank", zts.retrieveTenantDomainName("storage.tenant.coretech.office.burbank.resource_group.admin", null));
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testGetPublicKeyEntry.
@Test
public void testGetPublicKeyEntry() {
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", true);
store.processDomain(signedDomain, false);
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("hockey", "kings", "v=S1,d=hockey;n=kings;s=sig", 0, new PrincipalAuthority());
ResourceContext context = createResourceContext(principal);
PublicKeyEntry entry = zts.getPublicKeyEntry(context, "coretech", "storage", "0");
assertEquals(entry.getId(), "0");
assertEquals(entry.getKey(), ZTS_Y64_CERT0);
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testGetRoleTokenCertMismatchDomain.
@Test
public void testGetRoleTokenCertMismatchDomain() throws Exception {
RoleCertificateRequest req = new RoleCertificateRequest().setCsr(ROLE_CERT_DB_REQUEST).setExpiryTime(Long.valueOf(3600));
SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", true);
store.processDomain(signedDomain, false);
Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null);
ResourceContext context = createResourceContext(principal);
try {
zts.postRoleCertificateRequest(context, "coretech", "readers", req);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 400);
}
}
use of com.yahoo.athenz.zms.SignedDomain in project athenz by yahoo.
the class ZTSImplTest method testPostInstanceRefreshInformationSSHMismatchSerial.
@Test
public void testPostInstanceRefreshInformationSSHMismatchSerial() 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);
InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
X509CertRecord certRecord = new X509CertRecord();
certRecord.setInstanceId("1001");
certRecord.setProvider("athenz.provider");
certRecord.setService("athenz.production");
certRecord.setCurrentSerial("123413");
certRecord.setPrevSerial("123413");
Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001")).thenReturn(certRecord);
Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
ztsImpl.instanceCertManager = instanceManager;
InstanceRefreshInformation info = new InstanceRefreshInformation().setSsh("ssh-csr");
CertificateAuthority certAuthority = new CertificateAuthority();
SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
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(), 403);
}
}
Aggregations