Path: blob/master/test/jdk/sun/security/krb5/auto/BogusKDC.java
41152 views
/*1* Copyright (c) 2015, 2021, 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.io.File;24import java.io.FileWriter;25import java.io.IOException;26import java.io.PrintWriter;27import java.util.HashMap;28import java.util.Map;29import javax.security.auth.callback.CallbackHandler;30import javax.security.auth.login.LoginContext;31import javax.security.auth.login.LoginException;3233/*34* @test35* @bug 4515853 8075297 819448636* @summary Checks that Kerberos client tries replica KDC37* if primary KDC is not responding38* @library /test/lib39* @run main jdk.test.lib.FileInstaller TestHosts TestHosts40* @run main/othervm -Djdk.net.hosts.file=TestHosts BogusKDC41*/42public class BogusKDC {4344static final String TEST_SRC = System.getProperty("test.src", ".");45static final String HOST = "localhost";46static final String NOT_EXISTING_HOST = "not.existing.host";47static final String REALM = "TEST.REALM";48static final String USER = "USER";49static final String USER_PRINCIPAL = USER + "@" + REALM;50static final String USER_PASSWORD = "password";51static final String KRBTGT_PRINCIPAL = "krbtgt/" + REALM;52static final String KRB5_CONF = "krb5.conf";53static final int WRONG_KDC_PORT = 21;5455static final String KRB5_CONF_TEMPLATE = ""56+ "[libdefaults]\n"57+ "default_realm = TEST.REALM\n"58+ "max_retries = 1\n"59+ "\n"60+ "[realms]\n"61+ "TEST.REALM = {\n"62+ " kdc = %s\n"63+ " kdc = localhost:%d\n"64+ "}";6566public static void main(String[] args) throws LoginException, IOException {67Map<String, String> principals = new HashMap<>();68principals.put(USER_PRINCIPAL, USER_PASSWORD);69principals.put(KRBTGT_PRINCIPAL, null);7071System.setProperty("java.security.krb5.conf", KRB5_CONF);7273// start a local KDC74KDC kdc = KDC.startKDC(HOST, KRB5_CONF, REALM, principals, null, null);7576System.setProperty("java.security.auth.login.config",77TEST_SRC + File.separator + "refreshKrb5Config.jaas");7879CallbackHandler handler = new Helper.UserPasswordHandler(80USER, USER_PASSWORD);8182// create a krb5 config with non-existing host for primary KDC,83// and wrong port for replica KDC84try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {85w.write(String.format(KRB5_CONF_TEMPLATE,86KDC.NOT_EXISTING_HOST, WRONG_KDC_PORT));87w.flush();88}8990// login with not-refreshable config91try {92new LoginContext("NotRefreshable", handler).login();93throw new RuntimeException("Expected exception not thrown");94} catch (LoginException le) {95System.out.println("Expected login failure: " + le);96}9798// create a krb5 config with non-existing host for primary KDC,99// but correct port for replica KDC100try (PrintWriter w = new PrintWriter(new FileWriter(KRB5_CONF))) {101w.write(String.format(KRB5_CONF_TEMPLATE,102KDC.NOT_EXISTING_HOST, kdc.getPort()));103w.flush();104}105106// login with not-refreshable config107try {108new LoginContext("NotRefreshable", handler).login();109throw new RuntimeException("Expected exception not thrown");110} catch (LoginException le) {111System.out.println("Expected login failure: " + le);112}113114// login with refreshable config115new LoginContext("Refreshable", handler).login();116117System.out.println("Test passed");118}119}120121122