Elffuss BraidHub

kikocis/java-driver public

6697eb958eb9 · 3.3 KiBRawPermalink
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 */
16package com.datastax.driver.auth;
17
18import com.datastax.driver.core.Authenticator;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import javax.security.auth.Subject;
23import javax.security.auth.login.LoginContext;
24import javax.security.auth.login.LoginException;
25import 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 */
33public class KerberosAuthenticator implements Authenticator {
34 private static final Logger logger = LoggerFactory.getLogger(KerberosAuthenticator.class);
35
36 public static final String JAAS_CONFIG_ENTRY = "DseClient";
37 public static final String[] SUPPORTED_MECHANISMS = new String[]{"GSSAPI"};
38 public static final String SASL_PROTOCOL_NAME = "dse";
39 public static final String SASL_PROTOCOL_NAME_PROPERTY = "dse.sasl.protocol";
40
41 private final PrivilegedSaslClient saslClient;
42
43 public KerberosAuthenticator(InetSocketAddress host) {
44 saslClient = new PrivilegedSaslClient(loginSubject(),
45 SUPPORTED_MECHANISMS,
46 null,
47 System.getProperty(SASL_PROTOCOL_NAME_PROPERTY, SASL_PROTOCOL_NAME),
48 host.getAddress().getCanonicalHostName(),
49 PrivilegedSaslClient.DEFAULT_PROPERTIES,
50 null);
51 }
52
53 private Subject loginSubject() {
54 Subject subject = new Subject();
55 try {
56 LoginContext login = new LoginContext(JAAS_CONFIG_ENTRY, subject);
57 login.login();
58 return subject;
59 } catch (LoginException e) {
60 throw new RuntimeException(e);
61 }
62 }
63
64 @Override
65 public byte[] initialResponse() {
66 return saslClient.getInitialResponse();
67 }
68
69 @Override
70 public byte[] evaluateChallenge(byte[] challenge) {
71 byte[] response = saslClient.evaluateChallenge(challenge);
72 if (response == null) {
73 // If we generate a null response, then authentication has completed (if
74 // not, warn), and return without sending a response back to the server.
75 logger.trace("Response to server is null: authentication should now be complete.");
76 if (!saslClient.isComplete()) {
77 String error = "Client generated a null sasl response, but authentication is not complete.";
78 logger.error(error);
79 throw new RuntimeException(error);
80 }
81 }
82 return response;
83 }
84
85 @Override
86 public void onAuthenticationSuccess(byte[] token) {
87 // no-op
88 }
89}