Path: blob/master/test/jdk/sun/security/krb5/auto/KrbTicket.java
41152 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.Files;24import java.nio.file.Paths;25import java.time.Instant;26import java.util.Arrays;27import java.util.HashMap;28import java.util.Map;29import java.util.Set;30import javax.security.auth.RefreshFailedException;31import javax.security.auth.Subject;32import javax.security.auth.kerberos.KerberosTicket;33import javax.security.auth.login.LoginContext;3435/*36* @test37* @bug 6857795 8075299 819448638* @summary Checks Kerberos ticket properties39* @library /test/lib40* @run main jdk.test.lib.FileInstaller TestHosts TestHosts41* @run main/othervm -Djdk.net.hosts.file=TestHosts KrbTicket42*/43public class KrbTicket {4445private static final String REALM = "TEST.REALM";46private static final String HOST = "localhost";47private static final String USER = "TESTER";48private static final String USER_PRINCIPAL = USER + "@" + REALM;49private static final String PASSWORD = "password";50private static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;51private static final String KRB5_CONF_FILENAME = "krb5.conf";52private static final String JAAS_CONF = "jaas.conf";53private static final long TICKET_LIFTETIME = 5 * 60 * 1000; // 5 mins5455public static void main(String[] args) throws Exception {56// define principals57Map<String, String> principals = new HashMap<>();58principals.put(USER_PRINCIPAL, PASSWORD);59principals.put(KRBTGT_PRINCIPAL, null);6061System.setProperty("java.security.krb5.conf", KRB5_CONF_FILENAME);6263// start a local KDC instance64KDC kdc = KDC.startKDC(HOST, null, REALM, principals, null, null);65KDC.saveConfig(KRB5_CONF_FILENAME, kdc,66"forwardable = true", "proxiable = true");6768// create JAAS config69Files.write(Paths.get(JAAS_CONF), Arrays.asList(70"Client {",71" com.sun.security.auth.module.Krb5LoginModule required;",72"};"73));74System.setProperty("java.security.auth.login.config", JAAS_CONF);75System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");7677long startTime = Instant.now().getEpochSecond() * 1000;7879LoginContext lc = new LoginContext("Client",80new Helper.UserPasswordHandler(USER, PASSWORD));81lc.login();8283Subject subject = lc.getSubject();84System.out.println("subject: " + subject);8586Set creds = subject.getPrivateCredentials(87KerberosTicket.class);8889if (creds.size() > 1) {90throw new RuntimeException("Multiple credintials found");91}9293Object o = creds.iterator().next();94if (!(o instanceof KerberosTicket)) {95throw new RuntimeException("Instance of KerberosTicket expected");96}97KerberosTicket krbTkt = (KerberosTicket) o;9899System.out.println("forwardable = " + krbTkt.isForwardable());100System.out.println("proxiable = " + krbTkt.isProxiable());101System.out.println("renewable = " + krbTkt.isRenewable());102System.out.println("current = " + krbTkt.isCurrent());103104if (!krbTkt.isForwardable()) {105throw new RuntimeException("Forwardable ticket expected");106}107108if (!krbTkt.isProxiable()) {109throw new RuntimeException("Proxiable ticket expected");110}111112if (!krbTkt.isCurrent()) {113throw new RuntimeException("Ticket is not current");114}115116if (krbTkt.isRenewable()) {117throw new RuntimeException("Not renewable ticket expected");118}119try {120krbTkt.refresh();121throw new RuntimeException(122"Expected RefreshFailedException not thrown");123} catch(RefreshFailedException e) {124System.out.println("Expected exception: " + e);125}126127if (!checkTime(krbTkt, startTime)) {128throw new RuntimeException("Wrong ticket life time");129}130131krbTkt.destroy();132if (!krbTkt.isDestroyed()) {133throw new RuntimeException("Ticket not destroyed");134}135136System.out.println("Test passed");137}138139private static boolean checkTime(KerberosTicket krbTkt, long startTime) {140long ticketEndTime = krbTkt.getEndTime().getTime();141long roughLifeTime = ticketEndTime - startTime;142System.out.println("start time = " + startTime);143System.out.println("end time = " + ticketEndTime);144System.out.println("rough life time = " + roughLifeTime);145return roughLifeTime >= TICKET_LIFTETIME;146}147}148149150