1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.security; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.context.MuleContextAware; |
18 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
19 | |
import org.mule.api.endpoint.InboundEndpoint; |
20 | |
import org.mule.api.endpoint.OutboundEndpoint; |
21 | |
import org.mule.api.lifecycle.InitialisationException; |
22 | |
import org.mule.api.security.CredentialsAccessor; |
23 | |
import org.mule.api.security.CryptoFailureException; |
24 | |
import org.mule.api.security.EncryptionStrategyNotFoundException; |
25 | |
import org.mule.api.security.EndpointSecurityFilter; |
26 | |
import org.mule.api.security.SecurityException; |
27 | |
import org.mule.api.security.SecurityManager; |
28 | |
import org.mule.api.security.SecurityProvider; |
29 | |
import org.mule.api.security.SecurityProviderNotFoundException; |
30 | |
import org.mule.api.security.UnknownAuthenticationTypeException; |
31 | |
import org.mule.config.i18n.CoreMessages; |
32 | |
import org.mule.transformer.TransformerTemplate; |
33 | |
import org.mule.util.StringUtils; |
34 | |
|
35 | |
import org.apache.commons.logging.Log; |
36 | |
import org.apache.commons.logging.LogFactory; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public abstract class AbstractEndpointSecurityFilter implements EndpointSecurityFilter, MuleContextAware |
44 | |
{ |
45 | |
|
46 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
47 | |
|
48 | |
protected SecurityManager securityManager; |
49 | |
private String securityProviders; |
50 | |
protected ImmutableEndpoint endpoint; |
51 | 0 | private boolean inbound = false; |
52 | |
private boolean authenticate; |
53 | |
private CredentialsAccessor credentialsAccessor; |
54 | 0 | private boolean isInitialised = false; |
55 | |
|
56 | |
protected MuleContext muleContext; |
57 | |
|
58 | |
public void setMuleContext(MuleContext context) |
59 | |
{ |
60 | 0 | this.muleContext = context; |
61 | 0 | } |
62 | |
|
63 | |
public final void initialise() throws InitialisationException |
64 | |
{ |
65 | 0 | if (securityManager == null) |
66 | |
{ |
67 | 0 | securityManager = muleContext.getSecurityManager(); |
68 | |
} |
69 | 0 | if (securityManager == null) |
70 | |
{ |
71 | 0 | throw new InitialisationException(CoreMessages.authSecurityManagerNotSet(), this); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | 0 | if (securityProviders != null) |
77 | |
{ |
78 | 0 | SecurityManager localManager = new MuleSecurityManager(); |
79 | 0 | String[] securityProviders = StringUtils.splitAndTrim(this.securityProviders, ","); |
80 | 0 | for (String sp : securityProviders) |
81 | |
{ |
82 | 0 | SecurityProvider provider = securityManager.getProvider(sp); |
83 | 0 | if (provider != null) |
84 | |
{ |
85 | 0 | localManager.addProvider(provider); |
86 | |
} |
87 | |
else |
88 | |
{ |
89 | 0 | throw new InitialisationException( |
90 | |
CoreMessages.objectNotRegistered( |
91 | |
"Security Provider", sp), this); |
92 | |
} |
93 | |
} |
94 | 0 | securityManager = localManager; |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
protected final synchronized void lazyInit(ImmutableEndpoint endpoint) throws InitialisationException |
99 | |
{ |
100 | 0 | if (!isInitialised) |
101 | |
{ |
102 | 0 | initialiseEndpoint(endpoint); |
103 | 0 | isInitialised = true; |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
protected final void initialiseEndpoint(ImmutableEndpoint endpoint) throws InitialisationException |
108 | |
{ |
109 | 0 | if (endpoint != null) |
110 | |
{ |
111 | 0 | this.endpoint = endpoint; |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("Endpoint"), this); |
116 | |
} |
117 | |
|
118 | 0 | if (endpoint instanceof InboundEndpoint) |
119 | |
{ |
120 | 0 | inbound = true; |
121 | |
} |
122 | 0 | else if (endpoint instanceof OutboundEndpoint) |
123 | |
{ |
124 | 0 | inbound = false; |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | 0 | throw new InitialisationException(CoreMessages.authEndpointMustSendOrReceive(), this); |
129 | |
} |
130 | 0 | doInitialise(); |
131 | 0 | } |
132 | |
|
133 | |
public boolean isAuthenticate() |
134 | |
{ |
135 | 0 | return authenticate; |
136 | |
} |
137 | |
|
138 | |
public void setAuthenticate(boolean authenticate) |
139 | |
{ |
140 | 0 | this.authenticate = authenticate; |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
public void setSecurityManager(SecurityManager manager) |
145 | |
{ |
146 | 0 | securityManager = manager; |
147 | 0 | } |
148 | |
|
149 | |
public SecurityManager getSecurityManager() |
150 | |
{ |
151 | 0 | return securityManager; |
152 | |
} |
153 | |
|
154 | |
public String getSecurityProviders() |
155 | |
{ |
156 | 0 | return securityProviders; |
157 | |
} |
158 | |
|
159 | |
public void setSecurityProviders(String providers) |
160 | |
{ |
161 | 0 | securityProviders = providers; |
162 | 0 | } |
163 | |
|
164 | |
public ImmutableEndpoint getEndpoint() |
165 | |
{ |
166 | 0 | return endpoint; |
167 | |
} |
168 | |
|
169 | |
public synchronized void setEndpoint(ImmutableEndpoint endpoint) |
170 | |
{ |
171 | 0 | this.endpoint = endpoint; |
172 | 0 | isInitialised = false; |
173 | 0 | } |
174 | |
|
175 | |
public void authenticate(MuleEvent event) |
176 | |
throws SecurityException, UnknownAuthenticationTypeException, CryptoFailureException, |
177 | |
SecurityProviderNotFoundException, EncryptionStrategyNotFoundException, |
178 | |
InitialisationException |
179 | |
{ |
180 | 0 | lazyInit(event.getEndpoint()); |
181 | 0 | if (inbound) |
182 | |
{ |
183 | 0 | authenticateInbound(event); |
184 | |
} |
185 | |
else |
186 | |
{ |
187 | 0 | authenticateOutbound(event); |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
public CredentialsAccessor getCredentialsAccessor() |
192 | |
{ |
193 | 0 | return credentialsAccessor; |
194 | |
} |
195 | |
|
196 | |
public void setCredentialsAccessor(CredentialsAccessor credentialsAccessor) |
197 | |
{ |
198 | 0 | this.credentialsAccessor = credentialsAccessor; |
199 | 0 | } |
200 | |
|
201 | |
protected void updatePayload(MuleMessage message, final Object payload, MuleEvent event) throws MuleException |
202 | |
{ |
203 | 0 | TransformerTemplate trans = new TransformerTemplate(new TransformerTemplate.TransformerCallback() |
204 | 0 | { |
205 | |
public Object doTransform(MuleMessage message) throws Exception |
206 | |
{ |
207 | 0 | return payload; |
208 | |
} |
209 | |
}); |
210 | |
|
211 | 0 | message.applyTransformers(event, trans); |
212 | 0 | } |
213 | |
|
214 | |
protected abstract void authenticateInbound(MuleEvent event) |
215 | |
throws SecurityException, CryptoFailureException, SecurityProviderNotFoundException, |
216 | |
EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException; |
217 | |
|
218 | |
protected abstract void authenticateOutbound(MuleEvent event) |
219 | |
throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException; |
220 | |
|
221 | |
protected abstract void doInitialise() throws InitialisationException; |
222 | |
|
223 | |
} |