Skip to content

Commit 8ab7798

Browse files
justinmcleanjerryshao
authored andcommitted
[#5384] Support Kerberos authentication in the Gravitino CLI. (#5766)
### What changes were proposed in this pull request? Support Kerberos authentication. ### Why are the changes needed? To add support for Kerberos authentication. Fix: #5384 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Tested locally.
1 parent fb949c1 commit 8ab7798

File tree

5 files changed

+113
-15
lines changed

5 files changed

+113
-15
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public String getUrl() {
781781
return urlEnv;
782782
}
783783

784-
// Check if the metalake name is specified in the configuration file
784+
// Check if the Gravitino URL is specified in the configuration file
785785
if (config.fileExists()) {
786786
config.read();
787787
String configURL = config.getGravitinoURL();
@@ -806,24 +806,24 @@ public String getAuth() {
806806
return GravitinoOptions.SIMPLE;
807807
}
808808

809-
// Cache the Gravitino URL environment variable
809+
// Cache the Gravitino authentication type environment variable
810810
if (authEnv == null && !authSet) {
811811
authEnv = System.getenv("GRAVITINO_AUTH");
812812
authSet = true;
813813
}
814814

815-
// If set return the Gravitino URL environment variable
815+
// If set return the Gravitino authentication type environment variable
816816
if (authEnv != null) {
817817
return authEnv;
818818
}
819819

820-
// Check if the metalake name is specified in the configuration file
820+
// Check if the authentication type is specified in the configuration file
821821
GravitinoConfig config = new GravitinoConfig(null);
822822
if (config.fileExists()) {
823823
config.read();
824-
String configAuth = config.getGravitinoAuth();
825-
if (configAuth != null) {
826-
return configAuth;
824+
String configAuthType = config.getGravitinoAuthType();
825+
if (configAuthType != null) {
826+
return configAuthType;
827827
}
828828
}
829829

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoConfig.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public class GravitinoConfig {
3636
private String metalake;
3737
private String url;
3838
private boolean ignore;
39-
private String authentication;
39+
private String authType;
4040
private OAuthData oauth;
41+
private KerberosData kerberos;
4142

4243
/**
4344
* Creates a GravitinoConfig object with a specified config file. If no file is provided, it
@@ -93,7 +94,18 @@ public void read() {
9394
ignore = prop.getProperty(ignoreKey).equals("true");
9495
}
9596
if (prop.containsKey(authKey)) {
96-
authentication = prop.getProperty(authKey);
97+
authType = prop.getProperty(authKey);
98+
}
99+
100+
if (authKey.equals("oauth")) {
101+
oauth =
102+
new OAuthData(
103+
prop.getProperty("serverURI"),
104+
prop.getProperty("credential"),
105+
prop.getProperty("token"),
106+
prop.getProperty("scope"));
107+
} else if (authKey.equals("kerberos")) {
108+
kerberos = new KerberosData(prop.getProperty("principal"), prop.getProperty("keytabFile"));
97109
}
98110

99111
if (authKey.equals("oauth")) {
@@ -143,20 +155,29 @@ public String getConfigFile() {
143155
}
144156

145157
/**
146-
* Retrieves the Gravitino authentication stored in the configuration.
158+
* Retrieves the Gravitino authentication type stored in the configuration.
147159
*
148-
* @return The Gravitino authentication or null if not set.
160+
* @return The Gravitino authentication type or null if not set.
149161
*/
150-
public String getGravitinoAuth() {
151-
return authentication;
162+
public String getGravitinoAuthType() {
163+
return authType;
152164
}
153165

154166
/**
155-
* Retrieves the Gravitino oAuth authentication configuration.
167+
* Retrieves the Gravitino OAuth configuration.
156168
*
157-
* @return The Gravitino authentication or null if not set.
169+
* @return The Gravitino OAuth data or null if not set.
158170
*/
159171
public OAuthData getOAuth() {
160172
return oauth;
161173
}
174+
175+
/**
176+
* Retrieves the Gravitino kerberos configuration.
177+
*
178+
* @return The Gravitino Kerberos data or null if not set.
179+
*/
180+
public KerberosData getKerberos() {
181+
return kerberos;
182+
}
162183
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.cli;
21+
22+
public class KerberosData {
23+
protected final String principal;
24+
protected final String keytabFile;
25+
26+
/**
27+
* Constructs an {@code KerberosData} instance with the specified principal and keytab file.
28+
*
29+
* @param principal the Kerberos principal (e.g. a user or service identity)
30+
* @param keytabFile the path to the keytab file
31+
*/
32+
public KerberosData(String principal, String keytabFile) {
33+
this.principal = principal;
34+
this.keytabFile = keytabFile;
35+
}
36+
37+
/**
38+
* Returns the Kerberos principal associated with this {@code KerberosData}.
39+
*
40+
* @return the principal
41+
*/
42+
public String getPrincipal() {
43+
return principal;
44+
}
45+
46+
/**
47+
* Returns the keytab file path associated with this {@code KerberosData}.
48+
*
49+
* @return the keytab file path
50+
*/
51+
public String getKeytabFile() {
52+
return keytabFile;
53+
}
54+
}

clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java

+14
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121

2222
import static org.apache.gravitino.client.GravitinoClientBase.Builder;
2323

24+
import java.io.File;
2425
import org.apache.gravitino.cli.GravitinoConfig;
26+
import org.apache.gravitino.cli.KerberosData;
2527
import org.apache.gravitino.cli.OAuthData;
2628
import org.apache.gravitino.cli.outputs.PlainFormat;
2729
import org.apache.gravitino.cli.outputs.TableFormat;
2830
import org.apache.gravitino.client.DefaultOAuth2TokenProvider;
2931
import org.apache.gravitino.client.GravitinoAdminClient;
3032
import org.apache.gravitino.client.GravitinoClient;
3133
import org.apache.gravitino.client.GravitinoClientBase;
34+
import org.apache.gravitino.client.KerberosTokenProvider;
3235
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
3336

3437
/* The base for all commands. */
@@ -41,6 +44,7 @@ public abstract class Command {
4144

4245
private static final String SIMPLE_AUTH = "simple";
4346
private static final String OAUTH_AUTH = "oauth";
47+
private static final String KERBEROS_AUTH = "kerberos";
4448

4549
private final String url;
4650
private final boolean ignoreVersions;
@@ -139,6 +143,16 @@ protected <T extends GravitinoClientBase> Builder<T> constructClient(Builder<T>
139143
.build();
140144

141145
builder = builder.withOAuth(tokenProvider);
146+
} else if (authentication.equals(KERBEROS_AUTH)) {
147+
GravitinoConfig config = new GravitinoConfig(null);
148+
KerberosData kerberos = config.getKerberos();
149+
KerberosTokenProvider tokenProvider =
150+
KerberosTokenProvider.builder()
151+
.withClientPrincipal(kerberos.getPrincipal())
152+
.withKeyTabFile(new File(kerberos.getKeytabFile()))
153+
.build();
154+
155+
builder = builder.withKerberosAuth(tokenProvider);
142156
} else {
143157
System.err.println("Unsupported authentication type " + authentication);
144158
}

docs/cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ token=test
141141
scope=token/test
142142
```
143143

144+
Kerberos authentication can also be configured via the configuration file.
145+
146+
```text
147+
# Authentication
148+
auth=kerberos
149+
principal=user/admin@foo.com
150+
keytabFile=file.keytab
151+
```
152+
144153
### Potentially unsafe operations
145154

146155
For operations that delete data or rename a metalake the user with be prompted to make sure they wish to run this command. The `--force` option can be specified to override this behaviour.

0 commit comments

Comments
 (0)