DefaultAccessControlService implements AccessControlService { public Optional<AccessControlViolation> checkAccessControl(final String component, final JsonEnvelope jsonEnvelope) { if (accessControlDisabled()) { logger.trace("Skipping access control due to configuration"); return empty(); } logger.trace("Performing access control for action: {}", jsonEnvelope.metadata().name()); return policyEvaluator.checkAccessPolicyFor(component, jsonEnvelope); }  Optional<AccessControlViolation> checkAccessControl(final String component, final JsonEnvelope jsonEnvelope);  }
@Test public void shouldDelegateTheAccessControlLogicToTheAccessController() throws Exception { assertThat(System.getProperty(ACCESS_CONTROL_DISABLED_PROPERTY), is(nullValue())); final Optional<AccessControlViolation> accessControlViolation = of(mock(AccessControlViolation.class)); when(policyEvaluator.checkAccessPolicyFor("command", jsonEnvelope)).thenReturn(accessControlViolation); assertThat(accessControlService.checkAccessControl("command", jsonEnvelope), is(sameInstance(accessControlViolation))); assertLogStatement(); }
@Test public void shouldIgnoreAccessControlIfTheAccessControlDisabledPropertyIsTrue() throws Exception { System.setProperty(ACCESS_CONTROL_DISABLED_PROPERTY, "true"); final Optional<AccessControlViolation> accessControlViolation = accessControlService.checkAccessControl("command", jsonEnvelope); assertThat(accessControlViolation.isPresent(), is(false)); verifyZeroInteractions(policyEvaluator); verify(logger).trace("Skipping access control due to configuration"); }
@Test public void shouldUseAccessControlIfTheAccessControlDisabledPropertyIsFalse() throws Exception { System.setProperty(ACCESS_CONTROL_DISABLED_PROPERTY, "false"); final Optional<AccessControlViolation> accessControlViolation = of(mock(AccessControlViolation.class)); when(policyEvaluator.checkAccessPolicyFor("command", jsonEnvelope)).thenReturn(accessControlViolation); assertThat(accessControlService.checkAccessControl("command", jsonEnvelope), is(sameInstance(accessControlViolation))); assertLogStatement(); }