| 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.Authenticator; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import javax.security.auth.Subject; |
| 23 | import javax.security.auth.login.LoginContext; |
| 24 | import javax.security.auth.login.LoginException; |
| 25 | import java.net.InetSocketAddress; |
| 26 | |
| 27 | /** |
| 28 | * Responsible for authenticating with secured DSE services using Kerberos |
| 29 | * over GSSAPI & SASL. The actual SASL negotiation is delegated to a |
| 30 | * PrivilegedSaslClient which performs the priviledged actions on behalf |
| 31 | * of the logged in user. |
| 32 | */ |
| 33 | public class KerberosAuthenticator implements Authenticator |
| 34 | { |
| 35 | private static final Logger logger = LoggerFactory.getLogger(KerberosAuthenticator.class); |
| 36 | |
| 37 | public static final String JAAS_CONFIG_ENTRY = "DseClient"; |
| 38 | public static final String[] SUPPORTED_MECHANISMS = new String[]{"GSSAPI"}; |
| 39 | public static final String SASL_PROTOCOL_NAME = "dse"; |
| 40 | public static final String SASL_PROTOCOL_NAME_PROPERTY = "dse.sasl.protocol"; |
| 41 | |
| 42 | private final PrivilegedSaslClient saslClient; |
| 43 | |
| 44 | public KerberosAuthenticator(InetSocketAddress host) |
| 45 | { |
| 46 | saslClient = new PrivilegedSaslClient(loginSubject(), |
| 47 | SUPPORTED_MECHANISMS, |
| 48 | null, |
| 49 | System.getProperty(SASL_PROTOCOL_NAME_PROPERTY, SASL_PROTOCOL_NAME), |
| 50 | host.getAddress().getCanonicalHostName(), |
| 51 | PrivilegedSaslClient.DEFAULT_PROPERTIES, |
| 52 | null); |
| 53 | } |
| 54 | |
| 55 | private Subject loginSubject() |
| 56 | { |
| 57 | Subject subject = new Subject(); |
| 58 | try |
| 59 | { |
| 60 | LoginContext login = new LoginContext(JAAS_CONFIG_ENTRY, subject); |
| 61 | login.login(); |
| 62 | return subject; |
| 63 | } catch (LoginException e) |
| 64 | { |
| 65 | throw new RuntimeException(e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public byte[] initialResponse() |
| 71 | { |
| 72 | return saslClient.getInitialResponse(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public byte[] evaluateChallenge(byte[] challenge) |
| 77 | { |
| 78 | byte[] response = saslClient.evaluateChallenge(challenge); |
| 79 | if (response == null) |
| 80 | { |
| 81 | // If we generate a null response, then authentication has completed (if |
| 82 | // not, warn), and return without sending a response back to the server. |
| 83 | logger.trace("Response to server is null: authentication should now be complete."); |
| 84 | if (!saslClient.isComplete()) { |
| 85 | String error = "Client generated a null sasl response, but authentication is not complete."; |
| 86 | logger.error(error); |
| 87 | throw new RuntimeException(error); |
| 88 | } |
| 89 | } |
| 90 | return response; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onAuthenticationSuccess(byte[] token) |
| 95 | { |
| 96 | // no-op |
| 97 | } |
| 98 | } |