| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.auth; |
| 17 | |
| 18 | import com.datastax.driver.core.AuthProvider; |
| 19 | import com.datastax.driver.core.Authenticator; |
| 20 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 21 | import com.google.common.base.Charsets; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | |
| 24 | import javax.security.auth.Subject; |
| 25 | import javax.security.auth.login.LoginContext; |
| 26 | import javax.security.auth.login.LoginException; |
| 27 | import javax.security.sasl.Sasl; |
| 28 | import javax.security.sasl.SaslClient; |
| 29 | import javax.security.sasl.SaslException; |
| 30 | import java.net.InetSocketAddress; |
| 31 | import java.security.PrivilegedActionException; |
| 32 | import java.security.PrivilegedExceptionAction; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Map; |
| 35 | |
| 36 | /** |
| 37 | * AuthProvider that provides GSSAPI authenticator instances for clients to connect |
| 38 | * to DSE clusters secured with the DseAuthenticator. |
| 39 | * <p/> |
| 40 | * <h1>Kerberos Authentication</h1> |
| 41 | * The SASL protocol name defaults to "dse"; should you need to change that |
| 42 | * it can be overridden using the <code>dse.sasl.protocol</code> system property. |
| 43 | * <p/> |
| 44 | * Keytab and ticket cache settings are specified using a standard JAAS |
| 45 | * configuration file. The location of the file can be set using the |
| 46 | * <code>java.security.auth.login.config</code> system property or by adding a |
| 47 | * <code>login.config.url.n</code> entry in the <code>java.security</code> properties |
| 48 | * file. |
| 49 | * <p/> |
| 50 | * See the following documents for further details on the |
| 51 | * <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/tutorials/LoginConfigFile.html">JAAS Login Configuration File</a> and the |
| 52 | * <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html">JAAS Authentication Tutorial</a> |
| 53 | * for more on JAAS in general. |
| 54 | * <p/> |
| 55 | * <h1>Authentication using ticket cache</h1> |
| 56 | * Run <code>kinit</code> to obtain a ticket and populate the cache before |
| 57 | * connecting. JAAS config: |
| 58 | * <p/> |
| 59 | * <pre> |
| 60 | * DseClient { |
| 61 | * com.sun.security.auth.module.Krb5LoginModule required |
| 62 | * useTicketCache=true |
| 63 | * renewTGT=true; |
| 64 | * }; |
| 65 | * </pre> |
| 66 | * <p/> |
| 67 | * <p/> |
| 68 | * <h1>Authentication using a keytab file</h1> |
| 69 | * To enable authentication using a keytab file, specify its location on disk. |
| 70 | * If your keytab contains more than one principal key, you should also specify |
| 71 | * which one to select. |
| 72 | * <p/> |
| 73 | * <pre> |
| 74 | * DseClient { |
| 75 | * com.sun.security.auth.module.Krb5LoginModule required |
| 76 | * useKeyTab=true |
| 77 | * keyTab="/path/to/file.keytab" |
| 78 | * principal="user@MYDOMAIN.COM"; |
| 79 | * }; |
| 80 | * </pre> |
| 81 | * To create a cluster using this auth provider: |
| 82 | * <pre> |
| 83 | * Cluster cluster = Cluster.builder() |
| 84 | * .addContactPoint(hostname) |
| 85 | * .withAuthProvider(new DseGSSAPIAuthProvider()) |
| 86 | * .build(); |
| 87 | * </pre> |
| 88 | */ |
| 89 | public class DseGSSAPIAuthProvider implements AuthProvider { |
| 90 | public Authenticator newAuthenticator(InetSocketAddress host, String authenticator) throws AuthenticationException { |
| 91 | return new GSSAPIAuthenticator(authenticator, host); |
| 92 | } |
| 93 | |
| 94 | private static class GSSAPIAuthenticator extends BaseDseAuthenticator { |
| 95 | private static final String JAAS_CONFIG_ENTRY = "DseClient"; |
| 96 | private static final String[] SUPPORTED_MECHANISMS = new String[]{"GSSAPI"}; |
| 97 | private static final String SASL_PROTOCOL_NAME = "dse"; |
| 98 | private static final String SASL_PROTOCOL_NAME_PROPERTY = "dse.sasl.protocol"; |
| 99 | private static final Map<String, String> DEFAULT_PROPERTIES = |
| 100 | ImmutableMap.<String, String>builder().put(Sasl.SERVER_AUTH, "true") |
| 101 | .put(Sasl.QOP, "auth") |
| 102 | .build(); |
| 103 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 104 | private static final byte[] MECHANISM = "GSSAPI".getBytes(Charsets.UTF_8); |
| 105 | private static final byte[] SERVER_INITIAL_CHALLENGE = "GSSAPI-START".getBytes(Charsets.UTF_8); |
| 106 | |
| 107 | private final Subject subject; |
| 108 | private final SaslClient saslClient; |
| 109 | |
| 110 | public GSSAPIAuthenticator(String authenticator, InetSocketAddress host) { |
| 111 | super(authenticator); |
| 112 | try { |
| 113 | LoginContext login = new LoginContext(JAAS_CONFIG_ENTRY); |
| 114 | login.login(); |
| 115 | subject = login.getSubject(); |
| 116 | saslClient = Sasl.createSaslClient(SUPPORTED_MECHANISMS, |
| 117 | null, |
| 118 | System.getProperty(SASL_PROTOCOL_NAME_PROPERTY, SASL_PROTOCOL_NAME), |
| 119 | host.getAddress().getCanonicalHostName(), |
| 120 | DEFAULT_PROPERTIES, |
| 121 | null); |
| 122 | } catch (LoginException e) { |
| 123 | throw new RuntimeException(e); |
| 124 | } catch (SaslException e) { |
| 125 | throw new RuntimeException(e); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public byte[] getMechanism() { |
| 130 | return MECHANISM.clone(); |
| 131 | } |
| 132 | |
| 133 | public byte[] getInitialServerChallenge() { |
| 134 | return SERVER_INITIAL_CHALLENGE.clone(); |
| 135 | } |
| 136 | |
| 137 | public byte[] evaluateChallenge(byte[] challenge) { |
| 138 | if (Arrays.equals(SERVER_INITIAL_CHALLENGE, challenge)) { |
| 139 | if (!saslClient.hasInitialResponse()) { |
| 140 | return EMPTY_BYTE_ARRAY; |
| 141 | } |
| 142 | challenge = EMPTY_BYTE_ARRAY; |
| 143 | } |
| 144 | final byte[] internalChallenge = challenge; |
| 145 | try { |
| 146 | return Subject.doAs(subject, new PrivilegedExceptionAction<byte[]>() { |
| 147 | public byte[] run() throws SaslException { |
| 148 | return saslClient.evaluateChallenge(internalChallenge); |
| 149 | } |
| 150 | }); |
| 151 | } catch (PrivilegedActionException e) { |
| 152 | throw new RuntimeException(e.getException()); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |