ZTSImpl implements KeyStore, ZTSHandler { @Override public DomainMetrics postDomainMetrics(ResourceContext ctx, String domainName, DomainMetrics domainMetrics) { final String caller = ctx.getApiName(); final String principalDomain = logPrincipalAndGetDomain(ctx); if (readOnlyMode) { throw requestError("Server in Maintenance Read-Only mode. Please try your request later", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); } validateRequest(ctx.request(), principalDomain, caller); validate(domainName, TYPE_DOMAIN_NAME, principalDomain, caller); validate(domainMetrics, TYPE_DOMAIN_METRICS, principalDomain, caller); domainName = domainName.toLowerCase(); setRequestDomain(ctx, domainName); AthenzObject.DOMAIN_METRICS.convertToLowerCase(domainMetrics); DataCache data = dataStore.getDataCache(domainName); if (data == null) { metric.increment(HTTP_REQUEST, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); metric.increment(caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); throw notFoundError("postDomainMetrics: No such domain: " + domainName, caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); } final String metricDomain = domainMetrics.getDomainName(); if (!metricDomain.equals(domainName)) { final String errMsg = "postDomainMetrics: mismatched domain names: uri domain: " + domainName + " : metric domain: " + metricDomain; throw requestError(errMsg, caller, domainName, principalDomain); } List<DomainMetric> dmList = domainMetrics.getMetricList(); if (dmList == null || dmList.size() == 0) { final String errMsg = "postDomainMetrics: received no metrics for domain: " + domainName; throw requestError(errMsg, caller, domainName, principalDomain); } for (DomainMetric dm: dmList) { final String dmt = dm.getMetricType().toString().toLowerCase(); int count = dm.getMetricVal(); if (count <= 0) { LOGGER.error("postDomainMetrics: ignore metric: {} invalid counter {} received for domain {}", dmt, count, domainName); continue; } String metricName = DOM_METRIX_PREFIX + dmt; metric.increment(metricName, domainName, principalDomain, count); } return domainMetrics; } ZTSImpl();  ZTSImpl(CloudStore implCloudStore, DataStore implDataStore); @Override String getPublicKey(String domain, String service, String keyId); ServiceIdentity getServiceIdentity(ResourceContext ctx, String domainName, String serviceName); PublicKeyEntry getPublicKeyEntry(ResourceContext ctx, String domainName,
            String serviceName, String keyId); ServiceIdentityList getServiceIdentityList(ResourceContext ctx, String domainName); HostServices getHostServices(ResourceContext ctx, String host); Response getDomainSignedPolicyData(ResourceContext ctx, String domainName, String matchingTag); TenantDomains getTenantDomains(ResourceContext ctx, String providerDomainName,
            String userName, String roleName, String serviceName); @Override RoleToken getRoleToken(ResourceContext ctx, String domainName, String roleNames,
            Integer minExpiryTime, Integer maxExpiryTime, String proxyForPrincipal); @Override AccessTokenResponse postAccessTokenRequest(ResourceContext ctx, String request); RoleAccess getRoleAccess(ResourceContext ctx, String domainName, String principal); @Override RoleToken postRoleCertificateRequest(ResourceContext ctx, String domainName,
            String roleName, RoleCertificateRequest req); @Override RoleCertificate postRoleCertificateRequestExt(ResourceContext ctx, RoleCertificateRequest req); AWSTemporaryCredentials getAWSTemporaryCredentials(ResourceContext ctx, String domainName,
            String roleName, Integer durationSeconds, String externalId); @Override Response postInstanceRegisterInformation(ResourceContext ctx, InstanceRegisterInformation info); @Override InstanceIdentity postInstanceRefreshInformation(ResourceContext ctx, String provider,
            String domain, String service, String instanceId, InstanceRefreshInformation info); @Override void deleteInstanceIdentity(ResourceContext ctx, String provider,
            String domain, String service, String instanceId); @Deprecated @Override Identity postInstanceRefreshRequest(ResourceContext ctx, String domain,
            String service, InstanceRefreshRequest req); @Override Response postSSHCertRequest(ResourceContext ctx, SSHCertRequest certRequest); @Override JWKList getJWKList(ResourceContext ctx, Boolean rfc); @Deprecated @Override Identity postOSTKInstanceRefreshRequest(ResourceContext ctx, String domain,
            String service, OSTKInstanceRefreshRequest req); @Override ResourceAccess getResourceAccessExt(ResourceContext ctx, String action, String resource,
            String trustDomain, String checkPrincipal); @Override ResourceAccess getResourceAccess(ResourceContext ctx, String action, String resource,
            String trustDomain, String checkPrincipal); @Override Access getAccess(ResourceContext ctx, String domainName, String roleName,
            String principal); CertificateAuthorityBundle getCertificateAuthorityBundle(ResourceContext ctx, String name); @Override DomainMetrics postDomainMetrics(ResourceContext ctx, String domainName,
            DomainMetrics domainMetrics); @Override Status getStatus(ResourceContext ctx); @Override Schema getRdlSchema(ResourceContext context); ResourceContext newResourceContext(HttpServletRequest request,
                                              HttpServletResponse response,
                                              String apiName); static String getRootDir(); void recordMetrics(ResourceContext ctx, int httpStatus);  }
@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); CloudStore cloudStore = new CloudStore(); cloudStore.setHttpClient(null); ZtsMetricTester metric = new ZtsMetricTester(); ZTSImpl ztsImpl = new ZTSImpl(cloudStore, store); ZTSImpl.serverHostName = "localhost"; ztsImpl.metric = metric; String testDomain = "coretech"; 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); ztsImpl.postDomainMetrics(context, testDomain, req); 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); testDomain = "not_coretech"; 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); 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)); } assertEquals(metrixMap.size(), 0); testDomain = "coretech"; 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"; metrixMap.clear(); try { ztsImpl.postDomainMetrics(context, testDomain, req); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 400); assertTrue(ex.getMessage().contains(errMsg), ex.getMessage()); } assertEquals(metrixMap.size(), 0); testDomain = "coretech"; 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"; metrixMap.clear(); try { ztsImpl.postDomainMetrics(context, testDomain, req); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 400); assertTrue(ex.getMessage().contains(errMsg), ex.getMessage()); } assertEquals(metrixMap.size(), 0); testDomain = "coretech"; 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"; metrixMap.clear(); try { ztsImpl.postDomainMetrics(context, testDomain, req); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 400); assertTrue(ex.getMessage().contains(errMsg), ex.getMessage()); } assertEquals(metrixMap.size(), 0); testDomain = "coretech"; metricList = new ArrayList<>(); metricList.add( new DomainMetric(). setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH). setMetricVal(-1)); req = new DomainMetrics(). setDomainName(testDomain). setMetricList(metricList); metrixMap.clear(); ztsImpl.postDomainMetrics(context, testDomain, req); assertEquals(metrixMap.size(), 0); }
@Test public void testPostDomainMetricsNoMetrics() { 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); CloudStore cloudStore = new CloudStore(); cloudStore.setHttpClient(null); ZtsMetricTester metric = new ZtsMetricTester(); ZTSImpl ztsImpl = new ZTSImpl(cloudStore, store); ZTSImpl.serverHostName = "localhost"; ztsImpl.metric = metric; String testDomain = "coretech"; List<DomainMetric> metricList = new ArrayList<>(); DomainMetrics req = new DomainMetrics(). setDomainName(testDomain). setMetricList(metricList); try { ztsImpl.postDomainMetrics(context, testDomain, req); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 400); } metricList.add(new DomainMetric().setMetricType(null).setMetricVal(-1)); try { ztsImpl.postDomainMetrics(context, testDomain, req); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 400); } }