jdk/test/sun/security/krb5/auto/TwoPrinces.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 31474 7b79555b8073
permissions -rw-r--r--
8152538: UL doesn't inline the LogTagSet::is_level check Reviewed-by: mlarsson, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9499
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     1
/*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     4
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     8
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    13
 * accompanied this code).
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    14
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    18
 *
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    21
 * questions.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    22
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    23
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    24
/*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    25
 * @test
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    26
 * @bug 6894072
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    27
 * @compile -XDignore.symbol.file TwoPrinces.java
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    28
 * @run main/othervm TwoPrinces
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    29
 * @summary always refresh keytab
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    30
 */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    31
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    32
import java.io.File;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    33
import java.io.FileOutputStream;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    34
import sun.security.jgss.GSSUtil;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    35
import sun.security.krb5.Config;
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    36
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    37
public class TwoPrinces {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    38
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    39
    public static void main(String[] args)
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    40
            throws Exception {
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    41
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    42
        KDC k1 = KDC.create("R1");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    43
        k1.addPrincipal("u1", "hello".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    44
        k1.addPrincipalRandKey("krbtgt/R1");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    45
        k1.addPrincipalRandKey("host/same.host");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    46
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    47
        KDC k2 = KDC.create("R2");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    48
        k2.addPrincipal("u2", "hello".toCharArray());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    49
        k2.addPrincipalRandKey("krbtgt/R2");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    50
        k2.addPrincipalRandKey("host/same.host");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    51
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    52
        System.setProperty("java.security.krb5.conf", "krb5.conf");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    53
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    54
        // R1 is the default realm now
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    55
        KDC.saveConfig("krb5.conf", k1, k2);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    56
        Config.refresh();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    57
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    58
        k1.writeKtab("ktab1");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    59
        k2.writeKtab("ktab2");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    60
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    61
        // A JAAS config file with 2 Krb5LoginModules, after commit, the
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    62
        // subject with have principals and keytabs from both sides
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    63
        System.setProperty("java.security.auth.login.config", "jaas.conf");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    64
        File f = new File("jaas.conf");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    65
        FileOutputStream fos = new FileOutputStream(f);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    66
        fos.write((
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    67
                "me {\n"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    68
                + "  com.sun.security.auth.module.Krb5LoginModule required"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    69
                + "    isInitiator=true principal=\"host/same.host@R1\""
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    70
                + "    useKeyTab=true keyTab=ktab1 storeKey=true;\n"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    71
                + "  com.sun.security.auth.module.Krb5LoginModule required"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    72
                + "    isInitiator=true principal=\"host/same.host@R2\""
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    73
                + "    useKeyTab=true keyTab=ktab2 storeKey=true;\n"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    74
                + "};\n"
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    75
                ).getBytes());
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    76
        fos.close();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    77
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    78
        /*
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    79
         * This server side context will be able to act as services in both
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    80
         * realms. Please note that we still don't support a single instance
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    81
         * of server to accept connections from two realms at the same time.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    82
         * Therefore, we must call startAsServer in a given realm to start
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    83
         * working there. The same Subject never changes anyway.
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    84
         */
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    85
        Context s = Context.fromJAAS("me");
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    86
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    87
        // Default realm still R1
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    88
        s.startAsServer("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    89
        Context c1 = Context.fromUserPass("u1", "hello".toCharArray(), false);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    90
        c1.startAsClient("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    91
        Context.handshake(c1, s);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    92
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    93
        KDC.saveConfig("krb5.conf", k2, k1);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    94
        Config.refresh();
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    95
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    96
        // Default realm now R2
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    97
        s.startAsServer("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    98
        Context c2 = Context.fromUserPass("u2", "hello".toCharArray(), false);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
    99
        c2.startAsClient("host@same.host", GSSUtil.GSS_KRB5_MECH_OID);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   100
        Context.handshake(c2, s);
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   101
    }
f3115698a012 6894072: always refresh keytab
weijun
parents:
diff changeset
   102
}