Skip to content

Commit

Permalink
Adds kerberos support for kudu connector
Browse files Browse the repository at this point in the history
Includes handling of kerberos ticket expiration and product tests
demonstrating ticket renewal
  • Loading branch information
grantatspothero authored and hashhar committed Feb 24, 2022
1 parent ab182c7 commit 5b3d68e
Show file tree
Hide file tree
Showing 23 changed files with 906 additions and 33 deletions.
27 changes: 26 additions & 1 deletion docs/src/main/sphinx/connector/kudu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ replacing the properties as appropriate:
connector.name=kudu
## Defaults to NONE
kudu.authentication.type = NONE
## List of Kudu master addresses, at least one is needed (comma separated)
## Supported formats: example.com, example.com:7051, 192.0.2.1, 192.0.2.1:7051,
## [2001:db8::1], [2001:db8::1]:7051, 2001:db8::1
Expand Down Expand Up @@ -54,6 +57,29 @@ replacing the properties as appropriate:
## Disable Kudu client's collection of statistics.
#kudu.client.disable-statistics = false
Kerberos support
----------------

In order to connect to a kudu cluster that uses ``kerberos``
authentication, you need to configure the following kudu properties:

.. code-block:: properties
kudu.authentication.type = KERBEROS
## The kerberos client principal name
kudu.authentication.client.principal = clientprincipalname
## The path to the kerberos keytab file
## The configured client principal must exist in this keytab file
kudu.authentication.client.keytab = /path/to/keytab/file.keytab
## The path to the krb5.conf kerberos config file
kudu.authentication.config = /path/to/kerberos/krb5.conf
## Optional and defaults to "kudu"
## If kudu is running with a custom SPN this needs to be configured
kudu.authentication.server.principal.primary = kudu
Querying data
-------------
Expand Down Expand Up @@ -588,4 +614,3 @@ Limitations
-----------

- Only lower case table and column names in Kudu are supported.
- Using a secured Kudu cluster has not been tested.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,26 @@ public CachingKerberosAuthentication(KerberosAuthentication kerberosAuthenticati

public synchronized Subject getSubject()
{
if (subject == null || nextRefreshTime < System.currentTimeMillis()) {
if (subject == null || ticketNeedsRefresh()) {
subject = requireNonNull(kerberosAuthentication.getSubject(), "kerberosAuthentication.getSubject() is null");
KerberosTicket tgtTicket = getTicketGrantingTicket(subject);
nextRefreshTime = KerberosTicketUtils.getRefreshTime(tgtTicket);
}
return subject;
}

public synchronized void reauthenticateIfSoonWillBeExpired()
{
requireNonNull(subject, "subject is null, getSubject() must be called before reauthenticate()");
if (ticketNeedsRefresh()) {
kerberosAuthentication.attemptLogin(subject);
KerberosTicket tgtTicket = getTicketGrantingTicket(subject);
nextRefreshTime = KerberosTicketUtils.getRefreshTime(tgtTicket);
}
}

private boolean ticketNeedsRefresh()
{
return nextRefreshTime < System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public Subject getSubject()
}
}

public void attemptLogin(Subject subject)
{
try {
LoginContext loginContext = new LoginContext("", subject, null, configuration);
loginContext.login();
}
catch (LoginException e) {
throw new RuntimeException(e);
}
}

private static KerberosPrincipal createKerberosPrincipal(String principal)
{
try {
Expand Down
7 changes: 7 additions & 0 deletions plugin/trino-kudu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-spi</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed 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 io.trino.plugin.kudu;

import io.trino.plugin.base.authentication.CachingKerberosAuthentication;
import org.apache.kudu.client.KuduClient;

import javax.security.auth.Subject;

import java.security.PrivilegedAction;

import static java.util.Objects.requireNonNull;
import static org.apache.kudu.client.KuduClient.KuduClientBuilder;

public class KerberizedKuduClient
extends ForwardingKuduClient
{
private final KuduClient kuduClient;
private final CachingKerberosAuthentication cachingKerberosAuthentication;

KerberizedKuduClient(KuduClientBuilder kuduClientBuilder, CachingKerberosAuthentication cachingKerberosAuthentication)
{
requireNonNull(kuduClientBuilder, "kuduClientBuilder is null");
this.cachingKerberosAuthentication = requireNonNull(cachingKerberosAuthentication, "cachingKerberosAuthentication is null");
kuduClient = Subject.doAs(cachingKerberosAuthentication.getSubject(), (PrivilegedAction<KuduClient>) (kuduClientBuilder::build));
}

@Override
protected KuduClient delegate()
{
cachingKerberosAuthentication.reauthenticateIfSoonWillBeExpired();
return kuduClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed 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 io.trino.plugin.kudu;

import io.airlift.configuration.Config;
import io.airlift.configuration.ConfigDescription;

public class KuduAuthenticationConfig
{
private KuduAuthenticationType authenticationType = KuduAuthenticationType.NONE;

public enum KuduAuthenticationType
{
NONE,
KERBEROS;
}

public KuduAuthenticationType getAuthenticationType()
{
return authenticationType;
}

@Config("kudu.authentication.type")
@ConfigDescription("Kudu authentication type")
public KuduAuthenticationConfig setAuthenticationType(KuduAuthenticationType authenticationType)
{
this.authenticationType = authenticationType;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ public interface KuduClientWrapper

KuduScanner deserializeIntoScanner(byte[] serializedScanToken) throws IOException;

@Override
void close() throws KuduException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed 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 io.trino.plugin.kudu;

import io.airlift.configuration.Config;
import io.airlift.configuration.ConfigDescription;
import io.airlift.configuration.validation.FileExists;

import javax.validation.constraints.NotNull;

import java.io.File;
import java.util.Optional;

public class KuduKerberosConfig
{
private String clientPrincipal;
private File clientKeytab;
private File config;
// The kudu client defaults to using "kudu" if this is undefined
private Optional<String> kuduPrincipalPrimary = Optional.empty();

@NotNull
public String getClientPrincipal()
{
return clientPrincipal;
}

@Config("kudu.authentication.client.principal")
@ConfigDescription("Kudu Kerberos client principal")
public KuduKerberosConfig setClientPrincipal(String clientPrincipal)
{
this.clientPrincipal = clientPrincipal;
return this;
}

@NotNull
@FileExists
public File getClientKeytab()
{
return clientKeytab;
}

@Config("kudu.authentication.client.keytab")
@ConfigDescription("Kudu Kerberos client keytab location")
public KuduKerberosConfig setClientKeytab(File clientKeytab)
{
this.clientKeytab = clientKeytab;
return this;
}

@NotNull
@FileExists
public File getConfig()
{
return config;
}

@Config("kudu.authentication.config")
@ConfigDescription("Kudu Kerberos service configuration file")
public KuduKerberosConfig setConfig(File config)
{
this.config = config;
return this;
}

public Optional<String> getKuduPrincipalPrimary()
{
return kuduPrincipalPrimary;
}

@Config("kudu.authentication.server.principal.primary")
@ConfigDescription("The 'primary' portion of the kudu service principal name")
public KuduKerberosConfig setKuduPrincipalPrimary(String kuduPrincipalPrimary)
{
this.kuduPrincipalPrimary = Optional.ofNullable(kuduPrincipalPrimary);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.plugin.kudu;

import com.google.inject.Binder;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.multibindings.ProvidesIntoSet;
Expand All @@ -23,18 +22,12 @@
import io.trino.plugin.base.classloader.ForClassLoaderSafe;
import io.trino.plugin.kudu.procedures.RangePartitionProcedures;
import io.trino.plugin.kudu.properties.KuduTableProperties;
import io.trino.plugin.kudu.schema.NoSchemaEmulation;
import io.trino.plugin.kudu.schema.SchemaEmulation;
import io.trino.plugin.kudu.schema.SchemaEmulationByTableNameConvention;
import io.trino.spi.connector.ConnectorNodePartitioningProvider;
import io.trino.spi.connector.ConnectorPageSinkProvider;
import io.trino.spi.connector.ConnectorPageSourceProvider;
import io.trino.spi.connector.ConnectorSplitManager;
import io.trino.spi.procedure.Procedure;
import io.trino.spi.type.TypeManager;
import org.apache.kudu.client.KuduClient;

import javax.inject.Singleton;

import static io.airlift.configuration.ConfigBinder.configBinder;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -69,6 +62,8 @@ protected void setup(Binder binder)

binder.bind(RangePartitionProcedures.class).in(Scopes.SINGLETON);
Multibinder.newSetBinder(binder, Procedure.class);

install(new KuduSecurityModule());
}

@ProvidesIntoSet
Expand All @@ -82,28 +77,4 @@ Procedure getDropRangePartitionProcedure(RangePartitionProcedures procedures)
{
return procedures.getDropPartitionProcedure();
}

@Singleton
@Provides
KuduClientSession createKuduClientSession(KuduClientConfig config)
{
requireNonNull(config, "config is null");

KuduClient.KuduClientBuilder builder = new KuduClient.KuduClientBuilder(config.getMasterAddresses());
builder.defaultAdminOperationTimeoutMs(config.getDefaultAdminOperationTimeout().toMillis());
builder.defaultOperationTimeoutMs(config.getDefaultOperationTimeout().toMillis());
if (config.isDisableStatistics()) {
builder.disableStatistics();
}
KuduClient client = builder.build();

SchemaEmulation strategy;
if (config.isSchemaEmulationEnabled()) {
strategy = new SchemaEmulationByTableNameConvention(config.getSchemaEmulationPrefix());
}
else {
strategy = new NoSchemaEmulation();
}
return new KuduClientSession(new PassthroughKuduClient(client), strategy);
}
}
Loading

0 comments on commit 5b3d68e

Please sign in to comment.