ZTSImpl implements KeyStore, ZTSHandler { @Override public Status getStatus(ResourceContext ctx) { final String caller = ctx.getApiName(); final String principalDomain = logPrincipalAndGetDomain(ctx); validateRequest(ctx.request(), principalDomain, caller, true); if (statusCertSigner) { if (instanceCertManager.getCACertificate() == null) { throw notFoundError("Unable to communicate with cert signer", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); } } if (healthCheckFile != null && !healthCheckFile.exists()) { throw notFoundError("Error - no status available", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); } if (statusChecker != null) { try { statusChecker.check(); } catch (StatusCheckException e) { throw error(e.getCode(), e.getMsg(), caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN, principalDomain); } } return successServerStatus; } 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 testGetStatus() { 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); ztsImpl.statusPort = 0; Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null); ResourceContext context = createResourceContext(principal); Status status = ztsImpl.getStatus(context); assertEquals(status.getCode(), ResourceException.OK); }
@Test public void testGetStatusWithStatusFile() throws IOException { System.setProperty(ZTSConsts.ZTS_PROP_HEALTH_CHECK_PATH, "/tmp/zts-healthcheck"); 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); ztsImpl.statusPort = 0; Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null); ResourceContext context = createResourceContext(principal); File healthCheckFile = new File("/tmp/zts-healthcheck"); healthCheckFile.delete(); try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { assertEquals(ResourceException.NOT_FOUND, ex.getCode()); } new FileOutputStream(healthCheckFile).close(); Status status = ztsImpl.getStatus(context); assertEquals(ResourceException.OK, status.getCode()); healthCheckFile.delete(); try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { assertEquals(ResourceException.NOT_FOUND, ex.getCode()); } System.clearProperty(ZTSConsts.ZTS_PROP_HEALTH_CHECK_PATH); }
@Test public void testGetStatusWithStatusChecker() { ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0"); DataStore store = new DataStore(structStore, null); Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null); ResourceContext context = createResourceContext(principal); System.setProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS, MockStatusCheckerNoException.class.getName()); ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store); ztsImpl.statusPort = 0; Status status = ztsImpl.getStatus(context); assertEquals(ResourceException.OK, status.getCode()); System.setProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS, MockStatusCheckerThrowException.NoArguments.class.getName()); ztsImpl = new ZTSImpl(mockCloudStore, store); ztsImpl.statusPort = 0; try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { int code = com.yahoo.athenz.common.server.rest.ResourceException.INTERNAL_SERVER_ERROR; String msg = com.yahoo.athenz.common.server.rest.ResourceException.symbolForCode(com.yahoo.athenz.zms.ResourceException.INTERNAL_SERVER_ERROR); assertEquals(new ResourceError().code(code).message(msg).toString(), ex.getData().toString()); } System.setProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS, MockStatusCheckerThrowException.NotFound.class.getName()); ztsImpl = new ZTSImpl(mockCloudStore, store); ztsImpl.statusPort = 0; try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { int code = com.yahoo.athenz.common.server.rest.ResourceException.NOT_FOUND; String msg = com.yahoo.athenz.common.server.rest.ResourceException.symbolForCode(com.yahoo.athenz.zms.ResourceException.NOT_FOUND); assertEquals(new ResourceError().code(code).message(msg).toString(), ex.getData().toString()); } System.setProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS, MockStatusCheckerThrowException.InternalServerErrorWithMessage.class.getName()); ztsImpl = new ZTSImpl(mockCloudStore, store); ztsImpl.statusPort = 0; try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { int code = com.yahoo.athenz.common.server.rest.ResourceException.INTERNAL_SERVER_ERROR; String msg = "error message"; assertEquals(new ResourceError().code(code).message(msg).toString(), ex.getData().toString()); } System.setProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS, MockStatusCheckerThrowException.CauseRuntimeException.class.getName()); ztsImpl = new ZTSImpl(mockCloudStore, store); ztsImpl.statusPort = 0; try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { int code = com.yahoo.athenz.common.server.rest.ResourceException.INTERNAL_SERVER_ERROR; String msg = "runtime exception"; assertEquals(new ResourceError().code(code).message(msg).toString(), ex.getData().toString()); } System.clearProperty(ZTSConsts.ZTS_PROP_STATUS_CHECKER_FACTORY_CLASS); }
@Test public void testGetStatusWithSigner() { 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); ztsImpl.statusPort = 0; ztsImpl.statusCertSigner = true; InstanceCertManager certManager = Mockito.mock(InstanceCertManager.class); Mockito.when(certManager.getCACertificate()).thenReturn("ca-cert"); ztsImpl.instanceCertManager = certManager; Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null); ResourceContext context = createResourceContext(principal); Status status = ztsImpl.getStatus(context); assertEquals(status.getCode(), ResourceException.OK); }
@Test public void testGetStatusWithSignerFailure() { 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); ztsImpl.statusPort = 0; ztsImpl.statusCertSigner = true; InstanceCertManager certManager = Mockito.mock(InstanceCertManager.class); Mockito.when(certManager.getCACertificate()).thenReturn(null); ztsImpl.instanceCertManager = certManager; Principal principal = SimplePrincipal.create("user_domain", "user1", "v=U1;d=user_domain;n=user;s=signature", 0, null); ResourceContext context = createResourceContext(principal); try { ztsImpl.getStatus(context); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 404); } }