001: /*
002: * $Id: AbstractEndpointSecurityFilter.java 10961 2008-02-22 19:01:02Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.security;
012:
013: import org.mule.api.MuleContext;
014: import org.mule.api.MuleEvent;
015: import org.mule.api.MuleMessage;
016: import org.mule.api.context.MuleContextAware;
017: import org.mule.api.endpoint.ImmutableEndpoint;
018: import org.mule.api.endpoint.InboundEndpoint;
019: import org.mule.api.endpoint.OutboundEndpoint;
020: import org.mule.api.lifecycle.InitialisationException;
021: import org.mule.api.lifecycle.LifecycleTransitionResult;
022: import org.mule.api.security.CredentialsAccessor;
023: import org.mule.api.security.CryptoFailureException;
024: import org.mule.api.security.EncryptionStrategyNotFoundException;
025: import org.mule.api.security.EndpointSecurityFilter;
026: import org.mule.api.security.SecurityException;
027: import org.mule.api.security.SecurityManager;
028: import org.mule.api.security.SecurityProvider;
029: import org.mule.api.security.SecurityProviderNotFoundException;
030: import org.mule.api.security.UnknownAuthenticationTypeException;
031: import org.mule.api.transformer.TransformerException;
032: import org.mule.config.i18n.CoreMessages;
033: import org.mule.transformer.TransformerTemplate;
034: import org.mule.util.StringUtils;
035:
036: import java.util.Arrays;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <code>AbstractEndpointSecurityFilter</code> provides basic initialisation for
043: * all security filters, namely configuring the SecurityManager for this instance
044: */
045:
046: public abstract class AbstractEndpointSecurityFilter implements
047: EndpointSecurityFilter, MuleContextAware {
048:
049: protected transient Log logger = LogFactory.getLog(getClass());
050:
051: protected SecurityManager securityManager;
052: private String securityProviders;
053: protected ImmutableEndpoint endpoint;
054: private boolean inbound = false;
055: private boolean authenticate;
056: private CredentialsAccessor credentialsAccessor;
057: private boolean isInitialised = false;
058:
059: protected MuleContext muleContext;
060:
061: public void setMuleContext(MuleContext context) {
062: this .muleContext = context;
063: }
064:
065: public final LifecycleTransitionResult initialise()
066: throws InitialisationException {
067: if (securityManager == null) {
068: securityManager = muleContext.getSecurityManager();
069: }
070: if (securityManager == null) {
071: throw new InitialisationException(CoreMessages
072: .authSecurityManagerNotSet(), this );
073: }
074:
075: // This filter may only allow authentication on a subset of registered
076: // security providers
077: if (securityProviders != null) {
078: SecurityManager localManager = new MuleSecurityManager();
079: String[] sp = StringUtils.splitAndTrim(securityProviders,
080: ",");
081: for (int i = 0; i < sp.length; i++) {
082: SecurityProvider provider = securityManager
083: .getProvider(sp[i]);
084: if (provider != null) {
085: localManager.addProvider(provider);
086: } else {
087: throw new InitialisationException(CoreMessages
088: .objectNotRegistered("Security Provider",
089: sp[i]), this );
090: }
091: }
092: securityManager = localManager;
093: }
094:
095: // further functionality moved to lazy initialisation
096: return LifecycleTransitionResult.OK;
097: }
098:
099: protected final synchronized void lazyInit()
100: throws InitialisationException {
101: if (!isInitialised) {
102: initialiseEndpoint();
103: isInitialised = true;
104: }
105: }
106:
107: protected final void initialiseEndpoint()
108: throws InitialisationException {
109: if (endpoint == null) {
110: throw new InitialisationException(CoreMessages
111: .objectIsNull("Endpoint"), this );
112: }
113:
114: if (endpoint instanceof InboundEndpoint) {
115: inbound = true;
116: } else if (endpoint instanceof OutboundEndpoint) {
117: inbound = false;
118: } else {
119: throw new InitialisationException(CoreMessages
120: .authEndpointMustSendOrReceive(), this );
121: }
122: doInitialise();
123: }
124:
125: public boolean isAuthenticate() {
126: return authenticate;
127: }
128:
129: public void setAuthenticate(boolean authenticate) {
130: this .authenticate = authenticate;
131: }
132:
133: /** @param manager */
134: public void setSecurityManager(SecurityManager manager) {
135: securityManager = manager;
136: }
137:
138: public SecurityManager getSecurityManager() {
139: return securityManager;
140: }
141:
142: public String getSecurityProviders() {
143: return securityProviders;
144: }
145:
146: public void setSecurityProviders(String providers) {
147: securityProviders = providers;
148: }
149:
150: public ImmutableEndpoint getEndpoint() {
151: return endpoint;
152: }
153:
154: public synchronized void setEndpoint(ImmutableEndpoint endpoint) {
155: this .endpoint = endpoint;
156: isInitialised = false;
157: }
158:
159: public void authenticate(MuleEvent event) throws SecurityException,
160: UnknownAuthenticationTypeException, CryptoFailureException,
161: SecurityProviderNotFoundException,
162: EncryptionStrategyNotFoundException,
163: InitialisationException {
164: lazyInit();
165: if (inbound) {
166: authenticateInbound(event);
167: } else {
168: authenticateOutbound(event);
169: }
170: }
171:
172: public CredentialsAccessor getCredentialsAccessor() {
173: return credentialsAccessor;
174: }
175:
176: public void setCredentialsAccessor(
177: CredentialsAccessor credentialsAccessor) {
178: this .credentialsAccessor = credentialsAccessor;
179: }
180:
181: protected void updatePayload(MuleMessage message,
182: final Object payload) throws TransformerException {
183: TransformerTemplate trans = new TransformerTemplate(
184: new TransformerTemplate.TransformerCallback() {
185: public Object doTransform(MuleMessage message)
186: throws Exception {
187: return payload;
188: }
189: });
190:
191: message
192: .applyTransformers(Arrays
193: .asList(new Object[] { trans }));
194: }
195:
196: protected abstract void authenticateInbound(MuleEvent event)
197: throws SecurityException, CryptoFailureException,
198: SecurityProviderNotFoundException,
199: EncryptionStrategyNotFoundException,
200: UnknownAuthenticationTypeException;
201:
202: protected abstract void authenticateOutbound(MuleEvent event)
203: throws SecurityException,
204: SecurityProviderNotFoundException, CryptoFailureException;
205:
206: protected abstract void doInitialise()
207: throws InitialisationException;
208:
209: }
|