14327
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 7184246
|
|
27 |
* @summary Simplify Config.get() of krb5
|
|
28 |
*/
|
|
29 |
import java.lang.reflect.Field;
|
|
30 |
import java.lang.reflect.Method;
|
|
31 |
import java.util.Hashtable;
|
|
32 |
import java.util.Vector;
|
|
33 |
import sun.security.krb5.Config;
|
|
34 |
import sun.security.krb5.SCDynamicStoreConfig;
|
|
35 |
|
|
36 |
public class SCDynamicConfigTest {
|
|
37 |
|
|
38 |
static Vector<Hashtable<String,String>>hosts() {
|
|
39 |
Vector <Hashtable<String,String>> result = new Vector<>();
|
|
40 |
Hashtable<String,String> pair = new Hashtable<>();
|
|
41 |
pair.put("host", "127.0.0.1");
|
|
42 |
result.add(pair);
|
|
43 |
pair = new Hashtable<>();
|
|
44 |
pair.put("host", "127.0.0.2");
|
|
45 |
result.add(pair);
|
|
46 |
return result;
|
|
47 |
}
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
// Reconstruct a typical SCDynamicConfig.getKerberosConfig() output
|
|
51 |
Hashtable<String, Object> conf = new Hashtable<>();
|
|
52 |
|
|
53 |
Hashtable<String, Object> libdefaults = new Hashtable<>();
|
|
54 |
libdefaults.put("default_realm", "REALM.COM");
|
|
55 |
conf.put("libdefaults", libdefaults);
|
|
56 |
|
|
57 |
Hashtable<String, Object> realms = new Hashtable<>();
|
|
58 |
Hashtable<String, Object> thisRealm = new Hashtable<>();
|
|
59 |
realms.put("REALM.COM", thisRealm);
|
|
60 |
thisRealm.put("kpasswd", hosts());
|
|
61 |
thisRealm.put("kadmin", hosts());
|
|
62 |
thisRealm.put("kdc", hosts());
|
|
63 |
conf.put("realms", realms);
|
|
64 |
|
|
65 |
Hashtable<String, Object> domain_realm = new Hashtable<>();
|
|
66 |
domain_realm.put(".realm.com", "REALM.COM");
|
|
67 |
domain_realm.put("realm.com", "REALM.COM");
|
|
68 |
conf.put("domain_realm", domain_realm);
|
|
69 |
|
|
70 |
System.out.println("SCDynamicConfig:\n");
|
|
71 |
System.out.println(conf);
|
|
72 |
|
|
73 |
// Simulate SCDynamicConfig.getConfig() output
|
|
74 |
Method m = SCDynamicStoreConfig.class.getDeclaredMethod(
|
|
75 |
"convertNativeConfig", Hashtable.class);
|
|
76 |
m.setAccessible(true);
|
|
77 |
conf = (Hashtable)m.invoke(null, conf);
|
|
78 |
|
|
79 |
System.out.println("\nkrb5.conf:\n");
|
|
80 |
System.out.println(conf);
|
|
81 |
|
|
82 |
// Feed it into a Config object
|
|
83 |
System.setProperty("java.security.krb5.conf", "not-a-file");
|
|
84 |
Config cf = Config.getInstance();
|
|
85 |
Field f = Config.class.getDeclaredField("stanzaTable");
|
|
86 |
f.setAccessible(true);
|
|
87 |
f.set(cf, conf);
|
|
88 |
|
|
89 |
System.out.println("\nConfig:\n");
|
|
90 |
System.out.println(cf);
|
|
91 |
|
|
92 |
if (!cf.getDefaultRealm().equals("REALM.COM")) {
|
|
93 |
throw new Exception();
|
|
94 |
}
|
|
95 |
if (!cf.getKDCList("REALM.COM").equals("127.0.0.1 127.0.0.2")) {
|
|
96 |
throw new Exception();
|
|
97 |
}
|
|
98 |
if (!cf.get("domain_realm", ".realm.com").equals("REALM.COM")) {
|
|
99 |
throw new Exception();
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|