1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.management.support; |
8 | |
|
9 | |
import org.mule.module.management.agent.ConfigurableJMXAuthenticator; |
10 | |
import org.mule.module.management.agent.JmxAgent; |
11 | |
import org.mule.util.StringUtils; |
12 | |
|
13 | |
import java.security.Principal; |
14 | |
import java.util.Collections; |
15 | |
import java.util.HashMap; |
16 | |
import java.util.HashSet; |
17 | |
import java.util.Map; |
18 | |
import java.util.Set; |
19 | |
|
20 | |
import javax.management.remote.JMXAuthenticator; |
21 | |
import javax.management.remote.JMXPrincipal; |
22 | |
import javax.security.auth.Subject; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class SimplePasswordJmxAuthenticator implements JMXAuthenticator, ConfigurableJMXAuthenticator |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | 0 | protected static final Log logger = LogFactory.getLog(JmxAgent.class); |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | private Map<String, Object> credentials = new HashMap<String, Object>(); |
42 | |
|
43 | |
public Subject authenticate (Object authToken) |
44 | |
{ |
45 | 0 | if (authToken == null) |
46 | |
{ |
47 | 0 | throw new SecurityException("No authentication token available"); |
48 | |
} |
49 | 0 | if (!(authToken instanceof String[]) || ((String[]) authToken).length != 2) |
50 | |
{ |
51 | 0 | throw new SecurityException("Unsupported credentials format"); |
52 | |
} |
53 | |
|
54 | 0 | String[] authentication = (String[]) authToken; |
55 | |
|
56 | 0 | String username = StringUtils.defaultString(authentication[0]); |
57 | 0 | String password = StringUtils.defaultString(authentication[1]); |
58 | |
|
59 | 0 | if (!credentials.containsKey(username)) |
60 | |
{ |
61 | 0 | throw new SecurityException("Unauthenticated user: " + username); |
62 | |
} |
63 | |
|
64 | 0 | Object pass = credentials.get(username); |
65 | 0 | if (!password.equals(pass == null ? "" : pass.toString())) |
66 | |
{ |
67 | 0 | throw new SecurityException("Invalid password"); |
68 | |
} |
69 | |
|
70 | 0 | Set<Principal> principals = new HashSet<Principal>(); |
71 | 0 | principals.add(new JMXPrincipal(username)); |
72 | 0 | return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET); |
73 | |
} |
74 | |
|
75 | |
public void setCredentials (Map<String, String> newCredentials) |
76 | |
{ |
77 | 0 | this.credentials.clear(); |
78 | 0 | if (newCredentials == null || newCredentials.isEmpty()) |
79 | |
{ |
80 | 0 | logger.warn("Credentials cache has been purged, remote access will no longer be available"); |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 0 | this.credentials.putAll(newCredentials); |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
public void configure(Map newCredentials) |
89 | |
{ |
90 | 0 | this.setCredentials(newCredentials); |
91 | 0 | } |
92 | |
} |