You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/06/30 10:20:06 UTC
directory-kerby git commit: DIRKRB-308 Introduced JAAS conf and
facilities for krb login in tests
Repository: directory-kerby
Updated Branches:
refs/heads/master d66905229 -> fdc3a9ec6
DIRKRB-308 Introduced JAAS conf and facilities for krb login in tests
Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/fdc3a9ec
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/fdc3a9ec
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/fdc3a9ec
Branch: refs/heads/master
Commit: fdc3a9ec692c60aae7656179892048d07e9582e2
Parents: d669052
Author: drankye <ka...@intel.com>
Authored: Wed Jul 1 00:18:00 2015 +0800
Committer: Drankye <dr...@gmail.com>
Committed: Wed Jul 1 00:18:00 2015 +0800
----------------------------------------------------------------------
.../kerby/kerberos/kdc/GssInteropTestBase.java | 20 +-
kerby-kdc-test/src/test/resources/kerberos.jaas | 8 -
.../kerberos/kerb/server/GssInteropTest.java | 30 +--
.../src/test/resources/kerberos.jaas | 8 -
.../kerby/kerberos/kerb/client/JaasKrbUtil.java | 219 +++++++++++++++++++
.../kerby/kerberos/kerb/client/Krb5Conf.java | 53 +++++
.../kerby/kerberos/kerb/server/Krb5Conf.java | 51 -----
.../kerberos/kerb/server/SimpleKdcServer.java | 1 +
8 files changed, 285 insertions(+), 105 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/GssInteropTestBase.java
----------------------------------------------------------------------
diff --git a/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/GssInteropTestBase.java b/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/GssInteropTestBase.java
index bb0fb48..73691d6 100644
--- a/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/GssInteropTestBase.java
+++ b/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/GssInteropTestBase.java
@@ -20,6 +20,7 @@
package org.apache.kerby.kerberos.kdc;
import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil;
import org.apache.kerby.kerberos.kerb.server.KdcTestBase;
import org.ietf.jgss.*;
import org.junit.Assert;
@@ -68,11 +69,9 @@ public abstract class GssInteropTestBase extends KdcTestBase {
@Test
public void testKdc() throws Exception {
- LoginContext loginContext = new LoginContext(getClientPrincipalName(),
- new KerberosCallbackHandler());
- loginContext.login();
-
- Subject clientSubject = loginContext.getSubject();
+ Subject clientSubject = JaasKrbUtil.loginUsingPassword(
+ getClientPrincipal(), getClientPassword());
+
Set<Principal> clientPrincipals = clientSubject.getPrincipals();
Assert.assertFalse(clientPrincipals.isEmpty());
@@ -91,18 +90,13 @@ public abstract class GssInteropTestBase extends KdcTestBase {
byte[] kerberosToken = (byte[]) Subject.doAs(clientSubject, action);
Assert.assertNotNull(kerberosToken);
- loginContext.logout();
-
validateServiceTicket(kerberosToken);
}
private void validateServiceTicket(byte[] ticket) throws Exception {
- // Get the TGT for the service
- LoginContext loginContext = new LoginContext(getServerPrincipalName(),
- new KerberosCallbackHandler());
- loginContext.login();
-
- Subject serviceSubject = loginContext.getSubject();
+ Subject serviceSubject = JaasKrbUtil.loginUsingPassword(
+ getServerPrincipal(), getClientPassword());
+
Set<Principal> servicePrincipals = serviceSubject.getPrincipals();
Assert.assertFalse(servicePrincipals.isEmpty());
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kdc-test/src/test/resources/kerberos.jaas
----------------------------------------------------------------------
diff --git a/kerby-kdc-test/src/test/resources/kerberos.jaas b/kerby-kdc-test/src/test/resources/kerberos.jaas
deleted file mode 100644
index 9bcdd0c..0000000
--- a/kerby-kdc-test/src/test/resources/kerberos.jaas
+++ /dev/null
@@ -1,8 +0,0 @@
-
-drankye {
- com.sun.security.auth.module.Krb5LoginModule required refreshKrb5Config=true useKeyTab=false principal="drankye";
-};
-
-test-service {
- com.sun.security.auth.module.Krb5LoginModule required refreshKrb5Config=true useKeyTab=false storeKey=true principal="test-service/localhost";
-};
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/GssInteropTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/GssInteropTest.java b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/GssInteropTest.java
index b6d1d8e..32b8ca8 100644
--- a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/GssInteropTest.java
+++ b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/GssInteropTest.java
@@ -20,6 +20,7 @@
package org.apache.kerby.kerberos.kerb.server;
import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil;
import org.ietf.jgss.*;
import org.junit.Assert;
import org.junit.Before;
@@ -47,20 +48,6 @@ public class GssInteropTest extends KdcTestBase {
return false;
}
- @Before
- @Override
- public void setUp() throws Exception {
- super.setUp();
-
- File file1 = new File(this.getClass().getResource(
- "/kerberos.jaas").getPath());
- String content1 = getFileContent(file1.getPath());
- String path1 = writeToTestDir(content1, file1.getName());
-
- // System.setProperty("sun.security.krb5.debug", "true");
- System.setProperty("java.security.auth.login.config", path1);
- }
-
@Override
protected void createPrincipals() throws KrbException {
getKdcServer().createPrincipal(getClientPrincipal(), getClientPassword());
@@ -73,11 +60,9 @@ public class GssInteropTest extends KdcTestBase {
@Test
public void testKdc() throws Exception {
- LoginContext loginContext = new LoginContext(getClientPrincipalName(),
- new KerberosCallbackHandler());
- loginContext.login();
+ Subject clientSubject = JaasKrbUtil.loginUsingPassword(
+ getClientPrincipal(), getClientPassword());
- Subject clientSubject = loginContext.getSubject();
Set<Principal> clientPrincipals = clientSubject.getPrincipals();
Assert.assertFalse(clientPrincipals.isEmpty());
@@ -96,18 +81,13 @@ public class GssInteropTest extends KdcTestBase {
byte[] kerberosToken = (byte[]) Subject.doAs(clientSubject, action);
Assert.assertNotNull(kerberosToken);
- loginContext.logout();
-
validateServiceTicket(kerberosToken);
}
private void validateServiceTicket(byte[] ticket) throws Exception {
- // Get the TGT for the service
- LoginContext loginContext = new LoginContext("test-service",
- new KerberosCallbackHandler());
- loginContext.login();
+ Subject serviceSubject = JaasKrbUtil.loginUsingPassword(
+ getServerPrincipal(), getClientPassword());
- Subject serviceSubject = loginContext.getSubject();
Set<Principal> servicePrincipals = serviceSubject.getPrincipals();
Assert.assertFalse(servicePrincipals.isEmpty());
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-kdc-test/src/test/resources/kerberos.jaas
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/test/resources/kerberos.jaas b/kerby-kerb/kerb-kdc-test/src/test/resources/kerberos.jaas
deleted file mode 100644
index 9bcdd0c..0000000
--- a/kerby-kerb/kerb-kdc-test/src/test/resources/kerberos.jaas
+++ /dev/null
@@ -1,8 +0,0 @@
-
-drankye {
- com.sun.security.auth.module.Krb5LoginModule required refreshKrb5Config=true useKeyTab=false principal="drankye";
-};
-
-test-service {
- com.sun.security.auth.module.Krb5LoginModule required refreshKrb5Config=true useKeyTab=false storeKey=true principal="test-service/localhost";
-};
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/JaasKrbUtil.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/JaasKrbUtil.java b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/JaasKrbUtil.java
new file mode 100644
index 0000000..2573103
--- /dev/null
+++ b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/JaasKrbUtil.java
@@ -0,0 +1,219 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.kerberos.kerb.client;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.kerberos.KerberosPrincipal;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import java.io.File;
+import java.io.IOException;
+import java.security.Principal;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * JAAS utilities for Kerberos login.
+ */
+public final class JaasKrbUtil {
+
+ private JaasKrbUtil() {}
+
+ public static Subject loginUsingPassword(
+ String principal, String password) throws LoginException {
+ Set<Principal> principals = new HashSet<Principal>();
+ principals.add(new KerberosPrincipal(principal));
+
+ Subject subject = new Subject(false, principals,
+ new HashSet<Object>(), new HashSet<Object>());
+
+ Configuration conf = usePassword(principal);
+ String confName = "PasswordConf";
+ CallbackHandler callback = new KrbCallbackHandler(principal, password);
+ LoginContext loginContext = new LoginContext(confName, subject, callback, conf);
+ loginContext.login();
+ return loginContext.getSubject();
+ }
+
+ public static Subject loginUsingTicketCache(
+ String principal, File cacheFile) throws LoginException {
+ Set<Principal> principals = new HashSet<Principal>();
+ principals.add(new KerberosPrincipal(principal));
+
+ Subject subject = new Subject(false, principals,
+ new HashSet<Object>(), new HashSet<Object>());
+
+ Configuration conf = useTicketCache(principal, cacheFile);
+ String confName = "TicketCacheConf";
+ LoginContext loginContext = new LoginContext(confName, subject, null, conf);
+ loginContext.login();
+ return loginContext.getSubject();
+ }
+
+ public static Subject loginUsingKeytab(
+ String principal, File keytabFile) throws LoginException {
+ Set<Principal> principals = new HashSet<Principal>();
+ principals.add(new KerberosPrincipal(principal));
+
+ Subject subject = new Subject(false, principals,
+ new HashSet<Object>(), new HashSet<Object>());
+
+ Configuration conf = useKeytab(principal, keytabFile);
+ String confName = "KeytabConf";
+ LoginContext loginContext = new LoginContext(confName, subject, null, conf);
+ loginContext.login();
+ return loginContext.getSubject();
+ }
+
+ public static Configuration usePassword(String principal) {
+ return new PasswordJaasConf(principal);
+ }
+
+ public static Configuration useTicketCache(String principal,
+ File credentialFile) {
+ return new TicketCacheJaasConf(principal, credentialFile);
+ }
+
+ public static Configuration useKeytab(String principal, File keytabFile) {
+ return new KeytabJaasConf(principal, keytabFile);
+ }
+
+ private static String getKrb5LoginModuleName() {
+ return System.getProperty("java.vendor").contains("IBM")
+ ? "com.ibm.security.auth.module.Krb5LoginModule"
+ : "com.sun.security.auth.module.Krb5LoginModule";
+ }
+
+ static class KeytabJaasConf extends Configuration {
+ private String principal;
+ private File keytabFile;
+
+ public KeytabJaasConf(String principal, File keytab) {
+ this.principal = principal;
+ this.keytabFile = keytab;
+ }
+
+ @Override
+ public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
+ Map<String, String> options = new HashMap<String, String>();
+ options.put("keyTab", keytabFile.getAbsolutePath());
+ options.put("principal", principal);
+ options.put("useKeyTab", "true");
+ options.put("storeKey", "true");
+ options.put("doNotPrompt", "true");
+ options.put("renewTGT", "true");
+ options.put("refreshKrb5Config", "true");
+ options.put("isInitiator", "false");
+ options.put("debug", "false");
+
+ return new AppConfigurationEntry[]{
+ new AppConfigurationEntry(getKrb5LoginModuleName(),
+ AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
+ options)};
+ }
+ }
+
+ static class TicketCacheJaasConf extends Configuration {
+ private String principal;
+ private File clientCredentialFile;
+
+ public TicketCacheJaasConf(String principal, File clientCredentialFile) {
+ this.principal = principal;
+ this.clientCredentialFile = clientCredentialFile;
+ }
+
+ @Override
+ public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
+ Map<String, String> options = new HashMap<String, String>();
+ options.put("principal", principal);
+ options.put("storeKey", "true");
+ options.put("doNotPrompt", "true");
+ options.put("useTicketCache", "true");
+ options.put("renewTGT", "true");
+ options.put("refreshKrb5Config", "true");
+ options.put("isInitiator", "true");
+ options.put("ticketCache", clientCredentialFile.getAbsolutePath());
+ options.put("debug", "false");
+
+ return new AppConfigurationEntry[]{
+ new AppConfigurationEntry(getKrb5LoginModuleName(),
+ AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
+ options)};
+ }
+ }
+
+ static class PasswordJaasConf extends Configuration {
+ private String principal;
+
+ public PasswordJaasConf(String principal) {
+ this.principal = principal;
+ }
+
+ @Override
+ public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
+ Map<String, String> options = new HashMap<>();
+ options.put("principal", principal);
+ options.put("storeKey", "true");
+ options.put("useTicketCache", "true");
+ options.put("useKeyTab", "false");
+ options.put("renewTGT", "true");
+ options.put("refreshKrb5Config", "true");
+ options.put("isInitiator", "true");
+ options.put("debug", "false");
+
+ return new AppConfigurationEntry[]{
+ new AppConfigurationEntry(getKrb5LoginModuleName(),
+ AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
+ options)};
+ }
+ }
+
+ public static class KrbCallbackHandler implements CallbackHandler {
+ private String principal;
+ private String password;
+
+ public KrbCallbackHandler(String principal, String password) {
+ this.principal = principal;
+ this.password = password;
+ }
+
+ public void handle(Callback[] callbacks)
+ throws IOException, UnsupportedCallbackException {
+ for (int i = 0; i < callbacks.length; i++) {
+ if (callbacks[i] instanceof PasswordCallback) {
+ PasswordCallback pc = (PasswordCallback) callbacks[i];
+ if (pc.getPrompt().contains(principal)) {
+ pc.setPassword(password.toCharArray());
+ break;
+ }
+ }
+ }
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/Krb5Conf.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/Krb5Conf.java b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/Krb5Conf.java
new file mode 100644
index 0000000..780574d
--- /dev/null
+++ b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/client/Krb5Conf.java
@@ -0,0 +1,53 @@
+package org.apache.kerby.kerberos.kerb.client;
+
+import org.apache.kerby.kerberos.kerb.server.KdcSetting;
+import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer;
+import org.apache.kerby.util.IOUtil;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Generate krb5 file using given kdc server settings.
+ */
+public class Krb5Conf {
+ private static final String KRB5_CONF = "java.security.krb5.conf";
+ private static final String KRB5_CONF_FILE = "krb5.conf";
+ private SimpleKdcServer kdcServer;
+
+ public Krb5Conf(SimpleKdcServer kdcServer) {
+ this.kdcServer = kdcServer;
+ }
+
+ public void initKrb5conf() throws IOException {
+ File confFile = generateConfFile();
+ System.setProperty(KRB5_CONF, confFile.getAbsolutePath());
+ }
+
+ // Read in krb5.conf and substitute in the correct port
+ private File generateConfFile() throws IOException {
+ KdcSetting setting = kdcServer.getKdcSetting();
+
+ String resourcePath = setting.allowUdp() ? "/krb5_udp.conf" : "/krb5.conf";
+ InputStream templateResource = getClass().getResourceAsStream(resourcePath);
+ String templateContent = IOUtil.readInput(templateResource);
+
+ String content = templateContent;
+
+ content = content.replaceAll("_REALM_", "" + setting.getKdcRealm());
+
+ int kdcPort = setting.allowUdp() ? setting.getKdcUdpPort() :
+ setting.getKdcTcpPort();
+ content = content.replaceAll("_PORT_",
+ String.valueOf(kdcPort));
+
+ int udpLimit = setting.allowUdp() ? 4096 : 1;
+ content = content.replaceAll("_UDP_LIMIT_", String.valueOf(udpLimit));
+
+ File confFile = new File(kdcServer.getWorkDir(), KRB5_CONF_FILE);
+ IOUtil.writeFile(content, confFile);
+
+ return confFile;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/Krb5Conf.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/Krb5Conf.java b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/Krb5Conf.java
deleted file mode 100644
index d13b3e6..0000000
--- a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/Krb5Conf.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package org.apache.kerby.kerberos.kerb.server;
-
-import org.apache.kerby.util.IOUtil;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * Generate krb5 file using given kdc server settings.
- */
-public class Krb5Conf {
- private static final String KRB5_CONF = "java.security.krb5.conf";
- private static final String KRB5_CONF_FILE = "krb5.conf";
- private SimpleKdcServer kdcServer;
-
- public Krb5Conf(SimpleKdcServer kdcServer) {
- this.kdcServer = kdcServer;
- }
-
- public void initKrb5conf() throws IOException {
- File confFile = generateConfFile();
- System.setProperty(KRB5_CONF, confFile.getAbsolutePath());
- }
-
- // Read in krb5.conf and substitute in the correct port
- private File generateConfFile() throws IOException {
- KdcSetting setting = kdcServer.getKdcSetting();
-
- String resourcePath = setting.allowUdp() ? "/krb5_udp.conf" : "/krb5.conf";
- InputStream templateResource = getClass().getResourceAsStream(resourcePath);
- String templateContent = IOUtil.readInput(templateResource);
-
- String content = templateContent;
-
- content = content.replaceAll("_REALM_", "" + setting.getKdcRealm());
-
- int kdcPort = setting.allowUdp() ? setting.getKdcUdpPort() :
- setting.getKdcTcpPort();
- content = content.replaceAll("_PORT_",
- String.valueOf(kdcPort));
-
- int udpLimit = setting.allowUdp() ? 4096 : 1;
- content = content.replaceAll("_UDP_LIMIT_", String.valueOf(udpLimit));
-
- File confFile = new File(kdcServer.getWorkDir(), KRB5_CONF_FILE);
- IOUtil.writeFile(content, confFile);
-
- return confFile;
- }
-}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/fdc3a9ec/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/SimpleKdcServer.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/SimpleKdcServer.java b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/SimpleKdcServer.java
index 6acf37f..d5ed8ff 100644
--- a/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/SimpleKdcServer.java
+++ b/kerby-kerb/kerb-simplekdc/src/main/java/org/apache/kerby/kerberos/kerb/server/SimpleKdcServer.java
@@ -21,6 +21,7 @@ package org.apache.kerby.kerberos.kerb.server;
import org.apache.kerby.kerberos.kerb.KrbException;
import org.apache.kerby.kerberos.kerb.admin.Kadmin;
+import org.apache.kerby.kerberos.kerb.client.Krb5Conf;
import org.apache.kerby.kerberos.kerb.client.KrbClient;
import org.apache.kerby.util.NetworkUtil;