1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package com.allanbank.mongodb; |
21 | |
|
22 | |
import java.beans.PropertyEditorSupport; |
23 | |
import java.util.Collections; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Map; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import com.allanbank.mongodb.util.log.Log; |
29 | |
import com.allanbank.mongodb.util.log.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class CredentialEditor extends PropertyEditorSupport { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final Set<String> MONGODB_URI_FIELDS; |
54 | |
|
55 | |
|
56 | 1 | protected static final Log LOG = LogFactory.getLog(CredentialEditor.class); |
57 | |
|
58 | |
static { |
59 | 1 | final Set<String> fields = new HashSet<String>(); |
60 | 1 | fields.add("gssapiservicename"); |
61 | 1 | fields.add("authsource"); |
62 | 1 | fields.add("authmechanism"); |
63 | |
|
64 | 1 | MONGODB_URI_FIELDS = Collections.unmodifiableSet(fields); |
65 | 1 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public CredentialEditor() { |
71 | 58 | super(); |
72 | 58 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
@Override |
84 | |
public void setAsText(final String credentialString) |
85 | |
throws IllegalArgumentException { |
86 | |
|
87 | 58 | if (MongoDbUri.isUri(credentialString)) { |
88 | 57 | Credential.Builder builder = null; |
89 | |
|
90 | 57 | final MongoDbUri uri = new MongoDbUri(credentialString); |
91 | 57 | if (uri.getUserName() != null) { |
92 | 15 | builder = Credential.builder().userName(uri.getUserName()) |
93 | |
.password(uri.getPassword().toCharArray()); |
94 | 15 | final String database = uri.getDatabase(); |
95 | 15 | if (!database.isEmpty()) { |
96 | 11 | builder.database(database); |
97 | |
} |
98 | |
} |
99 | 57 | final Credential parsed = fromUriParameters(builder, |
100 | |
uri.getParsedOptions()); |
101 | 57 | if (parsed != null) { |
102 | 15 | setValue(parsed); |
103 | |
} |
104 | 57 | } |
105 | |
else { |
106 | 1 | throw new IllegalArgumentException( |
107 | |
"Could not determine the credentials for '" |
108 | |
+ credentialString + "'."); |
109 | |
} |
110 | 57 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
private Credential fromUriParameters(final Credential.Builder builder, |
123 | |
final Map<String, String> parameters) { |
124 | |
|
125 | 57 | final String gssapiServiceName = parameters.remove("gssapiservicename"); |
126 | 57 | if ((gssapiServiceName != null)) { |
127 | 2 | if (builder != null) { |
128 | 1 | builder.kerberos().addOption("kerberos.service.name", |
129 | |
gssapiServiceName); |
130 | |
} |
131 | |
else { |
132 | 1 | LOG.info("Must supply a user name " |
133 | |
+ "to set a gssapiServiceName: '{}'.", |
134 | |
gssapiServiceName); |
135 | |
} |
136 | |
} |
137 | |
|
138 | 57 | final String authSource = parameters.remove("authsource"); |
139 | 57 | if (authSource != null) { |
140 | 2 | if (builder != null) { |
141 | 1 | builder.setDatabase(authSource); |
142 | |
} |
143 | |
else { |
144 | 1 | LOG.info("Must supply a user name " |
145 | |
+ "to set a authSource: '{}'.", authSource); |
146 | |
} |
147 | |
} |
148 | |
|
149 | 57 | final String authMechanism = parameters.remove("authmechanism"); |
150 | 57 | if (authMechanism != null) { |
151 | 6 | if (builder != null) { |
152 | 5 | if ("MONGODB-CR".equalsIgnoreCase(authMechanism)) { |
153 | 1 | builder.mongodbCR(); |
154 | |
} |
155 | 4 | else if ("MONGODB-X509".equalsIgnoreCase(authMechanism)) { |
156 | 1 | builder.x509(); |
157 | |
} |
158 | 3 | else if ("GSSAPI".equalsIgnoreCase(authMechanism)) { |
159 | 1 | builder.kerberos(); |
160 | |
} |
161 | 2 | else if ("PLAIN".equalsIgnoreCase(authMechanism)) { |
162 | 1 | builder.plainSasl(); |
163 | |
} |
164 | |
else { |
165 | 1 | LOG.warn("Unknown authMechanism: '{}'. " |
166 | |
+ "Not authenticating.", authMechanism); |
167 | |
} |
168 | |
} |
169 | |
else { |
170 | 1 | LOG.info("Must supply a user name and password " |
171 | |
+ "to set a authMechanism: '{}'.", authMechanism); |
172 | |
} |
173 | |
} |
174 | |
|
175 | 57 | if (builder != null) { |
176 | 15 | return builder.build(); |
177 | |
} |
178 | 42 | return null; |
179 | |
} |
180 | |
|
181 | |
} |