Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/KrbTicket.java
38853 views
/*1* Copyright (c) 2015, 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 807529938* @summary Checks Kerberos ticket properties39* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock KrbTicket40*/41public class KrbTicket {4243private static final String REALM = "TEST.REALM";44private static final String HOST = "localhost";45private static final String USER = "TESTER";46private static final String USER_PRINCIPAL = USER + "@" + REALM;47private static final String PASSWORD = "password";48private static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;49private static final String KRB5_CONF_FILENAME = "krb5.conf";50private static final String JAAS_CONF = "jaas.conf";51private static final long TICKET_LIFTETIME = 5 * 60 * 1000; // 5 mins5253public static void main(String[] args) throws Exception {54// define principals55Map<String, String> principals = new HashMap<>();56principals.put(USER_PRINCIPAL, PASSWORD);57principals.put(KRBTGT_PRINCIPAL, null);5859System.setProperty("java.security.krb5.conf", KRB5_CONF_FILENAME);6061// start a local KDC instance62KDC kdc = KDC.startKDC(HOST, null, REALM, principals, null, null);63KDC.saveConfig(KRB5_CONF_FILENAME, kdc,64"forwardable = true", "proxiable = true");6566// create JAAS config67Files.write(Paths.get(JAAS_CONF), Arrays.asList(68"Client {",69" com.sun.security.auth.module.Krb5LoginModule required;",70"};"71));72System.setProperty("java.security.auth.login.config", JAAS_CONF);73System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");7475long startTime = Instant.now().getEpochSecond() * 1000;7677LoginContext lc = new LoginContext("Client",78new Helper.UserPasswordHandler(USER, PASSWORD));79lc.login();8081Subject subject = lc.getSubject();82System.out.println("subject: " + subject);8384Set creds = subject.getPrivateCredentials(85KerberosTicket.class);8687if (creds.size() > 1) {88throw new RuntimeException("Multiple credintials found");89}9091Object o = creds.iterator().next();92if (!(o instanceof KerberosTicket)) {93throw new RuntimeException("Instance of KerberosTicket expected");94}95KerberosTicket krbTkt = (KerberosTicket) o;9697System.out.println("forwardable = " + krbTkt.isForwardable());98System.out.println("proxiable = " + krbTkt.isProxiable());99System.out.println("renewable = " + krbTkt.isRenewable());100System.out.println("current = " + krbTkt.isCurrent());101102if (!krbTkt.isForwardable()) {103throw new RuntimeException("Forwardable ticket expected");104}105106if (!krbTkt.isProxiable()) {107throw new RuntimeException("Proxiable ticket expected");108}109110if (!krbTkt.isCurrent()) {111throw new RuntimeException("Ticket is not current");112}113114if (krbTkt.isRenewable()) {115throw new RuntimeException("Not renewable ticket expected");116}117try {118krbTkt.refresh();119throw new RuntimeException(120"Expected RefreshFailedException not thrown");121} catch(RefreshFailedException e) {122System.out.println("Expected exception: " + e);123}124125if (!checkTime(krbTkt, startTime)) {126throw new RuntimeException("Wrong ticket life time");127}128129krbTkt.destroy();130if (!krbTkt.isDestroyed()) {131throw new RuntimeException("Ticket not destroyed");132}133134System.out.println("Test passed");135}136137private static boolean checkTime(KerberosTicket krbTkt, long startTime) {138long ticketEndTime = krbTkt.getEndTime().getTime();139long roughLifeTime = ticketEndTime - startTime;140System.out.println("start time = " + startTime);141System.out.println("end time = " + ticketEndTime);142System.out.println("rough life time = " + roughLifeTime);143return roughLifeTime >= TICKET_LIFTETIME;144}145}146147148