Elffuss BraidHub

kikocis/java-driver public

c994d7eb1103 · 3.5 KiBRawPermalink
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package com.datastax.driver.auth;
19
20import com.datastax.driver.core.Authenticator;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import javax.security.auth.Subject;
25import javax.security.auth.login.LoginContext;
26import javax.security.auth.login.LoginException;
27import java.net.InetSocketAddress;
28
29/**
30 * Responsible for authenticating with secured DSE services using Kerberos
31 * over GSSAPI & SASL. The actual SASL negotiation is delegated to a
32 * PrivilegedSaslClient which performs the priviledged actions on behalf
33 * of the logged in user.
34 */
35public class KerberosAuthenticator implements Authenticator
36{
37 private static final Logger logger = LoggerFactory.getLogger(KerberosAuthenticator.class);
38
39 public static final String JAAS_CONFIG_ENTRY = "DseClient";
40 public static final String[] SUPPORTED_MECHANISMS = new String[]{"GSSAPI"};
41 public static final String SASL_PROTOCOL_NAME = "dse";
42 public static final String SASL_PROTOCOL_NAME_PROPERTY = "dse.sasl.protocol";
43
44 private final PrivilegedSaslClient saslClient;
45
46 public KerberosAuthenticator(InetSocketAddress host)
47 {
48 saslClient = new PrivilegedSaslClient(loginSubject(),
49 SUPPORTED_MECHANISMS,
50 null,
51 System.getProperty(SASL_PROTOCOL_NAME_PROPERTY, SASL_PROTOCOL_NAME),
52 host.getAddress().getCanonicalHostName(),
53 PrivilegedSaslClient.DEFAULT_PROPERTIES,
54 null);
55 }
56
57 private Subject loginSubject()
58 {
59 Subject subject = new Subject();
60 try
61 {
62 LoginContext login = new LoginContext(JAAS_CONFIG_ENTRY, subject);
63 login.login();
64 return subject;
65 } catch (LoginException e)
66 {
67 throw new RuntimeException(e);
68 }
69 }
70
71 @Override
72 public byte[] initialResponse()
73 {
74 return saslClient.getInitialResponse();
75 }
76
77 @Override
78 public byte[] evaluateChallenge(byte[] challenge)
79 {
80 byte[] response = saslClient.evaluateChallenge(challenge);
81 if (response == null)
82 {
83 // If we generate a null response, then authentication has completed (if
84 // not, warn), and return without sending a response back to the server.
85 logger.trace("Response to server is null: authentication should now be complete.");
86 if (!saslClient.isComplete()) {
87 String error = "Client generated a null sasl response, but authentication is not complete.";
88 logger.error(error);
89 throw new RuntimeException(error);
90 }
91 }
92 return response;
93 }
94
95 @Override
96 public void onAuthenticationSuccess(byte[] token)
97 {
98 // no-op
99 }
100}