author | ssahoo |
Thu, 25 Jan 2018 05:57:22 -0800 | |
changeset 48668 | 2da4a52715d8 |
parent 47216 | 71c04702a3d5 |
child 49276 | 3acb379b8672 |
permissions | -rw-r--r-- |
40540 | 1 |
/* |
48668 | 2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
40540 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.*; |
|
25 |
import java.net.DatagramSocket; |
|
26 |
import java.net.ServerSocket; |
|
27 |
import java.nio.file.Files; |
|
28 |
import java.nio.file.Paths; |
|
29 |
import java.security.Security; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Random; |
|
33 |
import java.util.regex.Matcher; |
|
34 |
import java.util.regex.Pattern; |
|
35 |
import javax.security.auth.login.LoginException; |
|
36 |
import sun.security.krb5.Asn1Exception; |
|
37 |
import sun.security.krb5.Config; |
|
38 |
||
39 |
/* |
|
40 |
* @test |
|
48668 | 41 |
* @bug 8164656 8181461 8194486 |
40540 | 42 |
* @summary krb5.kdc.bad.policy test |
48668 | 43 |
* @library /test/lib |
44 |
* @run main jdk.test.lib.FileInstaller TestHosts TestHosts |
|
45 |
* @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy udp |
|
46 |
* @run main/othervm -Djdk.net.hosts.file=TestHosts KdcPolicy tcp |
|
40540 | 47 |
*/ |
48 |
public class KdcPolicy { |
|
49 |
||
50 |
// Is this test on UDP? |
|
51 |
static boolean udp; |
|
52 |
||
53 |
public static void main(String[] args) throws Exception { |
|
54 |
||
55 |
udp = args[0].equals("udp"); |
|
56 |
||
57 |
try { |
|
58 |
main0(); |
|
59 |
} catch (LoginException le) { |
|
60 |
Throwable cause = le.getCause(); |
|
61 |
if (cause instanceof Asn1Exception) { |
|
62 |
System.out.println("Another process sends a packet to " + |
|
63 |
"this server. Ignored."); |
|
64 |
return; |
|
65 |
} |
|
66 |
throw le; |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
static DebugMatcher cm = new DebugMatcher(); |
|
71 |
||
72 |
static void main0() throws Exception { |
|
73 |
||
74 |
System.setProperty("sun.security.krb5.debug", "true"); |
|
75 |
||
76 |
// One real KDC. Must be created before fake KDCs |
|
77 |
// to read the TestHosts file. |
|
78 |
OneKDC kdc = new OneKDC(null); |
|
79 |
||
80 |
// Two fake KDCs, d1 and d2 only listen but do not respond. |
|
81 |
||
82 |
if (udp) { |
|
83 |
try (DatagramSocket d1 = new DatagramSocket(); |
|
84 |
DatagramSocket d2 = new DatagramSocket()) { |
|
85 |
run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort()); |
|
86 |
} |
|
87 |
} else { |
|
88 |
try (ServerSocket d1 = new ServerSocket(0); |
|
89 |
ServerSocket d2 = new ServerSocket(0)) { |
|
90 |
run(d1.getLocalPort(), d2.getLocalPort(), kdc.getPort()); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
static void run(int p1, int p2, int p3) throws Exception { |
|
96 |
||
97 |
// cm.kdc() will return a and b for fake KDCs, and c for real KDC. |
|
98 |
cm.addPort(-1).addPort(p1).addPort(p2).addPort(p3); |
|
99 |
||
100 |
System.setProperty("java.security.krb5.conf", "alternative-krb5.conf"); |
|
101 |
||
102 |
// Check default timeout is 30s. Use real KDC only, otherwise too |
|
103 |
// slow to wait for timeout. |
|
104 |
writeConf(-1, -1, p3); |
|
105 |
test("c30000c30000"); |
|
106 |
||
107 |
// 1. Default policy is tryLast |
|
108 |
//Security.setProperty("krb5.kdc.bad.policy", "tryLast"); |
|
109 |
||
110 |
// Need a real KDC, otherwise there is no last good. |
|
111 |
// This test waste 3 seconds waiting for d1 to timeout. |
|
112 |
// It is possible the real KDC cannot fulfil the request |
|
113 |
// in 3s, so it might fail (either 1st time or 2nd time). |
|
114 |
writeConf(1, 3000, p1, p3); |
|
45519
9409e9e9ce09
8181461: sun/security/krb5/auto/KdcPolicy.java fails with java.lang.Exception: Does not match
weijun
parents:
40540
diff
changeset
|
115 |
test("a3000c3000c3000|a3000c3000-|a3000c3000c3000a3000-"); |
40540 | 116 |
|
117 |
// If a test case won't use a real KDC, it can be sped up. |
|
118 |
writeConf(3, 5, p1, p2); |
|
119 |
test("a5a5a5b5b5b5-"); // default max_retries == 3 |
|
120 |
test("a5a5a5b5b5b5-"); // all bad means no bad |
|
121 |
||
122 |
// 2. No policy. |
|
123 |
Security.setProperty("krb5.kdc.bad.policy", ""); |
|
124 |
Config.refresh(); |
|
125 |
||
126 |
// This case needs a real KDC, otherwise, all bad means no |
|
127 |
// bad and we cannot tell the difference. This case waste 3 |
|
128 |
// seconds on d1 to timeout twice. It is possible the real KDC |
|
129 |
// cannot fulfil the request within 3s, so it might fail |
|
130 |
// (either 1st time or 2nd time). |
|
131 |
writeConf(1, 3000, p1, p3); |
|
132 |
test("a3000c3000a3000c3000|a3000c3000-|a3000c3000a3000c3000-"); |
|
133 |
||
134 |
// 3. tryLess with no argument means tryLess:1,5000 |
|
135 |
Security.setProperty("krb5.kdc.bad.policy", "tryLess"); |
|
136 |
||
137 |
// This case will waste 11s. We are checking that the default |
|
138 |
// value of 5000 in tryLess is only used if it's less than timeout |
|
139 |
// in krb5.conf |
|
140 |
writeConf(1, 6000, p1); |
|
141 |
test("a6000-"); // timeout in krb5.conf is 6s |
|
142 |
test("a5000-"); // tryLess to 5s. This line can be made faster if |
|
143 |
// d1 is a read KDC, but we have no existing method |
|
144 |
// to start KDC on an existing ServerSocket (port). |
|
145 |
||
146 |
writeConf(-1, 4, p1, p2); |
|
147 |
test("a4a4a4b4b4b4-"); // default max_retries == 3 |
|
148 |
test("a4b4-"); // tryLess to 1. And since 4 < 5000, use 4. |
|
149 |
Config.refresh(); |
|
150 |
test("a4a4a4b4b4b4-"); |
|
151 |
||
152 |
writeConf(5, 4, p1, p2); |
|
153 |
test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5 |
|
154 |
test("a4b4-"); |
|
155 |
Config.refresh(); |
|
156 |
test("a4a4a4a4a4b4b4b4b4b4-"); |
|
157 |
||
158 |
// 3. tryLess with arguments |
|
159 |
Security.setProperty("krb5.kdc.bad.policy", |
|
160 |
"tryLess:2,5"); |
|
161 |
||
162 |
writeConf(-1, 6, p1, p2); |
|
163 |
test("a6a6a6b6b6b6-"); // default max_retries == 3 |
|
164 |
test("a5a5b5b5-"); // tryLess to 2 |
|
165 |
Config.refresh(); |
|
166 |
test("a6a6a6b6b6b6-"); |
|
167 |
||
168 |
writeConf(5, 4, p1, p2); |
|
169 |
test("a4a4a4a4a4b4b4b4b4b4-"); // user-provided max_retries == 5 |
|
170 |
test("a4a4b4b4-"); // tryLess to 2 |
|
171 |
Config.refresh(); |
|
172 |
test("a4a4a4a4a4b4b4b4b4b4-"); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Writes a krb5.conf file. |
|
177 |
* @param max max_retries, -1 if not set |
|
178 |
* @param to kdc_timeout, -1 if not set |
|
179 |
* @param ports where KDCs listen on |
|
180 |
*/ |
|
181 |
static void writeConf(int max, int to, int... ports) throws Exception { |
|
182 |
||
183 |
// content of krb5.conf |
|
184 |
String conf = ""; |
|
185 |
||
186 |
// Extra settings in [libdefaults] |
|
187 |
String inDefaults = ""; |
|
188 |
||
189 |
// Extra settings in [realms] |
|
190 |
String inRealm = ""; |
|
191 |
||
192 |
// We will randomly put extra settings only in [libdefaults], |
|
193 |
// or in [realms] but with different values in [libdefaults], |
|
194 |
// to prove that settings in [realms] override those in [libdefaults]. |
|
195 |
Random r = new Random(); |
|
196 |
||
197 |
if (max > 0) { |
|
198 |
if (r.nextBoolean()) { |
|
199 |
inDefaults += "max_retries = " + max + "\n"; |
|
200 |
} else { |
|
201 |
inRealm += " max_retries = " + max + "\n"; |
|
202 |
inDefaults += "max_retries = " + (max + 1) + "\n"; |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
if (to > 0) { |
|
207 |
if (r.nextBoolean()) { |
|
208 |
inDefaults += "kdc_timeout = " + to + "\n"; |
|
209 |
} else { |
|
210 |
inRealm += " kdc_timeout = " + to + "\n"; |
|
211 |
inDefaults += "kdc_timeout = " + (to + 1) + "\n"; |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
if (udp) { |
|
216 |
if (r.nextBoolean()) { |
|
217 |
inDefaults += "udp_preference_limit = 10000\n"; |
|
218 |
} else if (r.nextBoolean()) { |
|
219 |
inRealm += " udp_preference_limit = 10000\n"; |
|
220 |
inDefaults += "udp_preference_limit = 1\n"; |
|
221 |
} // else no settings means UDP |
|
222 |
} else { |
|
223 |
if (r.nextBoolean()) { |
|
224 |
inDefaults += "udp_preference_limit = 1\n"; |
|
225 |
} else { |
|
226 |
inRealm += " udp_preference_limit = 1\n"; |
|
227 |
inDefaults += "udp_preference_limit = 10000\n"; |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
conf = "[libdefaults]\n" + |
|
232 |
"default_realm = " + OneKDC.REALM + "\n" + |
|
233 |
inDefaults + |
|
234 |
"\n" + |
|
235 |
"[realms]\n" + |
|
236 |
OneKDC.REALM + " = {\n"; |
|
237 |
||
238 |
for (int port : ports) { |
|
239 |
conf += " kdc = " + OneKDC.KDCHOST + ":" + port + "\n" + |
|
240 |
inRealm; |
|
241 |
} |
|
242 |
||
243 |
conf += "}\n"; |
|
244 |
||
245 |
Files.write(Paths.get("alternative-krb5.conf"), conf.getBytes()); |
|
246 |
Config.refresh(); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* One call of krb5 login. As long as the result matches one of expected, |
|
251 |
* the test is considered as success. The grammar of expected is |
|
252 |
* |
|
253 |
* kdc#, timeout, kdc#, timeout, ..., optional "-" for failure |
|
254 |
*/ |
|
255 |
static void test(String... expected) throws Exception { |
|
256 |
||
257 |
System.out.println("------------------TEST----------------------"); |
|
258 |
PrintStream oldOut = System.out; |
|
259 |
boolean failed = false; |
|
260 |
ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
|
261 |
System.setOut(new PrintStream(bo)); |
|
262 |
try { |
|
263 |
Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false); |
|
264 |
} catch (Exception e) { |
|
265 |
failed = true; |
|
266 |
} finally { |
|
267 |
System.setOut(oldOut); |
|
268 |
} |
|
269 |
||
270 |
String[] lines = new String(bo.toByteArray()).split("\n"); |
|
271 |
StringBuilder sb = new StringBuilder(); |
|
272 |
for (String line: lines) { |
|
273 |
if (cm.match(line)) { |
|
274 |
if (udp != cm.isUDP()) { |
|
275 |
sb.append("x"); |
|
276 |
} |
|
277 |
sb.append(cm.kdc()).append(cm.timeout()); |
|
278 |
} |
|
279 |
} |
|
280 |
if (failed) sb.append('-'); |
|
281 |
||
282 |
String output = sb.toString(); |
|
283 |
||
284 |
boolean found = false; |
|
285 |
for (String ex : expected) { |
|
286 |
if (output.matches(ex)) { |
|
287 |
System.out.println("Expected: " + ex + ", actual " + output); |
|
288 |
found = true; |
|
289 |
break; |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
if (!found) { |
|
294 |
System.out.println("--------------- ERROR START -------------"); |
|
295 |
System.out.println(new String(bo.toByteArray())); |
|
296 |
System.out.println("--------------- ERROR END ---------------"); |
|
297 |
throw new Exception("Does not match. Output is " + output); |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* A helper class to match the krb5 debug output: |
|
303 |
* >>> KDCCommunication: kdc=host UDP:11555, timeout=200,Attempt =1, #bytes=138 |
|
304 |
* |
|
305 |
* Example: |
|
306 |
* DebugMatcher cm = new DebugMatcher(); |
|
307 |
* cm.addPort(12345).addPort(11555); |
|
308 |
* for (String line : debugOutput) { |
|
309 |
* if (cm.match(line)) { |
|
310 |
* System.out.printf("%c%d\n", cm.kdc(), cm.timeout()); |
|
311 |
* // shows b200 for the example above |
|
312 |
* } |
|
313 |
* } |
|
314 |
*/ |
|
315 |
static class DebugMatcher { |
|
316 |
||
317 |
static final Pattern re = Pattern.compile( |
|
318 |
">>> KDCCommunication: kdc=\\S+ (TCP|UDP):(\\d+), " + |
|
319 |
"timeout=(\\d+),Attempt\\s*=(\\d+)"); |
|
320 |
||
321 |
List<Integer> kdcPorts = new ArrayList<>(); |
|
322 |
Matcher matcher; |
|
323 |
||
324 |
/** |
|
325 |
* Add KDC ports one by one. See {@link #kdc()}. |
|
326 |
*/ |
|
327 |
DebugMatcher addPort(int port) { |
|
328 |
if (port > 0) { |
|
329 |
kdcPorts.add(port); |
|
330 |
} else { |
|
331 |
kdcPorts.clear(); |
|
332 |
} |
|
333 |
return this; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* When a line matches the ">>> KDCCommunication:" pattern. After a |
|
338 |
* match, the getters below can be called on this match. |
|
339 |
*/ |
|
340 |
boolean match(String line) { |
|
341 |
matcher = re.matcher(line); |
|
342 |
return matcher.find(); |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Protocol of this match, "UDP" or "TCP". |
|
347 |
*/ |
|
348 |
boolean isUDP() { |
|
349 |
return matcher.group(1).equals("UDP"); |
|
350 |
} |
|
351 |
||
352 |
/** |
|
353 |
* KDC for this match, "a" for the one 1st added bt addPort(), "b" |
|
354 |
* for second, etc. Undefined for not added. |
|
355 |
*/ |
|
356 |
char kdc() { |
|
357 |
int port = Integer.parseInt(matcher.group(2)); |
|
358 |
return (char) (kdcPorts.indexOf(port) + 'a'); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* Timeout value for this match. |
|
363 |
*/ |
|
364 |
int timeout() { |
|
365 |
return Integer.parseInt(matcher.group(3)); |
|
366 |
} |
|
367 |
} |
|
368 |
} |