@Override public void changeEvent(CoreEvent updated) { requireNonNull(event); event = updated; securityContext = event.getSecurityContext(); }
@Override public SecurityContext authenticate(CoreEvent event) throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException, EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException { called = true; if (!accept) { throw new StaticMessageUnauthorisedException(); } return event.getSecurityContext(); }
@Override protected boolean matchesSafely(CoreEvent item) { return (messageMatcher != null ? messageMatcher.matches(item.getMessage()) : true) && (variablesMatcher != null ? variablesMatcher.matches(item.getVariables()) : true) && (securityContextMatcher != null ? securityContextMatcher.matches(item.getSecurityContext()) : true) && (errorTypeMatcher != null ? errorTypeMatcher.matches(item.getError().get().getErrorType()) : true); }
this.configuration = configuration; this.event = event; this.securityContext = event.getSecurityContext(); this.componentModel = componentModel; this.parameters = parameters;
@Test public void changedSecurityContext() throws Exception { SecurityContext context = flowRunner("setSecureCage").run().getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Test public void filteredProviders() throws Exception { SecurityContext context = flowRunner("setSecureCageFilterProviders") .withVariable("providers", asList(MOCK_PROVIDER)) .run() .getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Override protected void describeMismatchSafely(CoreEvent item, Description mismatchDescription) { mismatchDescription.appendText("was "); if (messageMatcher != null) { messageMatcher.describeMismatch(item.getMessage(), mismatchDescription); } if (variablesMatcher != null) { variablesMatcher.describeMismatch(item.getVariables(), mismatchDescription); } if (errorTypeMatcher != null) { errorTypeMatcher.describeMismatch(item.getError().get().getErrorType(), mismatchDescription); } if (securityContextMatcher != null) { securityContextMatcher.describeMismatch(item.getSecurityContext(), mismatchDescription); } }
@Test public void filteredProvidersExpectedException() throws Exception { expectedException.expectCause(instanceOf(SecurityProviderNotFoundException.class)); SecurityContext context = flowRunner("setSecureCageFilterProviders") .withVariable("providers", asList("Invalid")) .run() .getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Test public void securityContextSerialization() throws Exception { Transformer transformer = createSerializableToByteArrayTransformer(); transformer.setMuleContext(muleContext); CoreEvent event = CoreEvent.builder(testEvent()).securityContext(createTestAuthentication()).build(); Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(event); assertNotNull(serialized); ByteArrayToObject trans = new ByteArrayToObject(); trans.setMuleContext(muleContext); CoreEvent deserialized = (CoreEvent) trans.transform(serialized); assertThat(deserialized.getSecurityContext().getAuthentication().getPrincipal(), is(event.getSecurityContext().getAuthentication().getPrincipal())); assertThat(deserialized.getSecurityContext().getAuthentication().getProperties().get("key1"), is(event.getSecurityContext().getAuthentication().getProperties().get("key1"))); assertThat(deserialized.getSecurityContext().getAuthentication().getCredentials(), is(event.getSecurityContext().getAuthentication().getCredentials())); }
@Override public void changeEvent(CoreEvent updated) { requireNonNull(event); event = updated; securityContext = event.getSecurityContext(); }
@Test public void securityContextCopy() throws Exception { SecurityContext securityContext = mock(SecurityContext.class); CoreEvent event = CoreEvent.builder(testEvent()).securityContext(securityContext).build(); CoreEvent eventCopy = CoreEvent.builder(event).message(Message.of("copy")).build(); assertThat(securityContext, sameInstance(eventCopy.getSecurityContext())); }
@Override public SecurityContext authenticate(CoreEvent event) throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException, EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException { called = true; if (!accept) { throw new StaticMessageUnauthorisedException(); } return event.getSecurityContext(); }
@Override protected boolean matchesSafely(CoreEvent item) { return (messageMatcher != null ? messageMatcher.matches(item.getMessage()) : true) && (variablesMatcher != null ? variablesMatcher.matches(item.getVariables()) : true) && (securityContextMatcher != null ? securityContextMatcher.matches(item.getSecurityContext()) : true) && (errorTypeMatcher != null ? errorTypeMatcher.matches(item.getError().get().getErrorType()) : true); }
@Test public void changedSecurityContext() throws Exception { SecurityContext context = flowRunner("setSecureCage").run().getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Test public void filteredProviders() throws Exception { SecurityContext context = flowRunner("setSecureCageFilterProviders") .withVariable("providers", asList(MOCK_PROVIDER)) .run() .getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Override protected void describeMismatchSafely(CoreEvent item, Description mismatchDescription) { mismatchDescription.appendText("was "); if (messageMatcher != null) { messageMatcher.describeMismatch(item.getMessage(), mismatchDescription); } if (variablesMatcher != null) { variablesMatcher.describeMismatch(item.getVariables(), mismatchDescription); } if (errorTypeMatcher != null) { errorTypeMatcher.describeMismatch(item.getError().get().getErrorType(), mismatchDescription); } if (securityContextMatcher != null) { securityContextMatcher.describeMismatch(item.getSecurityContext(), mismatchDescription); } }
@Test public void filteredProvidersExpectedException() throws Exception { expectedException.expectCause(instanceOf(SecurityProviderNotFoundException.class)); SecurityContext context = flowRunner("setSecureCageFilterProviders") .withVariable("providers", asList("Invalid")) .run() .getSecurityContext(); assertThat(context, is(notNullValue())); assertThat(context, is(securityContext)); }
@Override protected SecurityContext authenticateInbound(CoreEvent event) throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException, EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException { String userHeader = (String) credentialsAccessor.getCredentials(event); if (userHeader == null) { throw new CredentialsNotSetException(event, event.getSecurityContext(), this); } Credentials user = new DefaultMuleCredentials(userHeader, getSecurityManager()); Authentication authentication; try { authentication = getSecurityManager().authenticate(new DefaultMuleAuthentication(user)); } catch (Exception e) { // Authentication failed if (logger.isDebugEnabled()) { logger.debug("Authentication request for user: " + user.getUsername() + " failed: " + e.toString()); } throw new UnauthorisedException(authFailedForUser(user.getUsername()), e); } // Authentication success if (logger.isDebugEnabled()) { logger.debug("Authentication success: " + authentication.toString()); } SecurityContext context = getSecurityManager().createSecurityContext(authentication); context.setAuthentication(authentication); return context; }
@Test public void securityContextSerialization() throws Exception { Transformer transformer = createSerializableToByteArrayTransformer(); transformer.setMuleContext(muleContext); CoreEvent event = CoreEvent.builder(testEvent()).securityContext(createTestAuthentication()).build(); Serializable serialized = (Serializable) createSerializableToByteArrayTransformer().transform(event); assertNotNull(serialized); ByteArrayToObject trans = new ByteArrayToObject(); trans.setMuleContext(muleContext); CoreEvent deserialized = (CoreEvent) trans.transform(serialized); assertThat(deserialized.getSecurityContext().getAuthentication().getPrincipal(), is(event.getSecurityContext().getAuthentication().getPrincipal())); assertThat(deserialized.getSecurityContext().getAuthentication().getProperties().get("key1"), is(event.getSecurityContext().getAuthentication().getProperties().get("key1"))); assertThat(deserialized.getSecurityContext().getAuthentication().getCredentials(), is(event.getSecurityContext().getAuthentication().getCredentials())); }
@Test public void securityContextCopy() throws Exception { SecurityContext securityContext = mock(SecurityContext.class); CoreEvent event = CoreEvent.builder(testEvent()).securityContext(securityContext).build(); CoreEvent eventCopy = CoreEvent.builder(event).message(Message.of("copy")).build(); assertThat(securityContext, sameInstance(eventCopy.getSecurityContext())); }