ZTSAuthorizer implements Authorizer { @Override public boolean access(String op, String resource, Principal principal, String trustDomain) { resource = resource.toLowerCase(); if (trustDomain != null) { trustDomain = trustDomain.toLowerCase(); } op = op.toLowerCase(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("access:({}, {}, {}, {})", op, resource, principal, trustDomain); } if (!AuthzHelper.authorityAuthorizationAllowed(principal)) { LOGGER.error("Authority is not allowed to support authorization checks"); return false; } String domainName = AuthzHelper.retrieveResourceDomain(resource, op, trustDomain); if (domainName == null) { throw new ResourceException(ResourceException.NOT_FOUND, new ResourceError().code(ResourceException.NOT_FOUND).message("Domain not found")); } DataCache domain = dataStore.getDataCache(domainName); if (domain == null) { throw new ResourceException(ResourceException.NOT_FOUND, new ResourceError().code(ResourceException.NOT_FOUND).message("Domain not found")); } AccessStatus accessStatus = evaluateAccess(domain, principal.getFullName(), op, resource, trustDomain); return accessStatus == AccessStatus.ALLOWED; } ZTSAuthorizer(final DataStore dataStore); @Override boolean access(String op, String resource, Principal principal, String trustDomain);  }
@Test public void testAccessAuthoritySupport() { ZTSAuthorizer authz = new ZTSAuthorizer(null); Authority authority = Mockito.mock(Authority.class); Mockito.when(authority.allowAuthorization()).thenReturn(false); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getAuthority()).thenReturn(authority); assertFalse(authz.access("op", "resource", principal, null)); }
@Test public void testAccessInvalidResourceDomain() { ZTSAuthorizer authz = new ZTSAuthorizer(null); Authority authority = Mockito.mock(Authority.class); Mockito.when(authority.allowAuthorization()).thenReturn(true); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getAuthority()).thenReturn(authority); try { authz.access("op", "invalid-resource", principal, null); fail(); } catch (ResourceException ex) { assertEquals(ex.getCode(), 404); } }