Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6449] improve(CLI): Refactor getAuth method in CLI #6451

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CatalogCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);
List<String> missingEntities = Lists.newArrayList();

if (CommandActions.LIST.equals(command)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ColumnCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public class CommandContext {
private final String url;
private final boolean quiet;
private final CommandLine line;
private final String auth;

private String ignoreEnv;
private boolean ignoreSet = false;
private String urlEnv;
private boolean urlSet = false;
private String authEnv;
private boolean authSet = false;
// Can add more "global" command flags here without any major changes e.g. a guiet flag

/**
Expand All @@ -55,6 +58,7 @@ public CommandContext(CommandLine line) {

this.url = getUrl();
this.ignoreVersions = getIgnore();
this.auth = this.getAuth();
}

/**
Expand Down Expand Up @@ -102,6 +106,15 @@ public boolean quiet() {
return quiet;
}

/**
* Returns the authentication type.
*
* @return The authentication type.
*/
public String auth() {
return auth;
}

/**
* Retrieves the Gravitino URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitino config file.
Expand Down Expand Up @@ -166,4 +179,34 @@ private boolean getIgnore() {

return ignore;
}

private String getAuth() {
// If specified on the command line use that
if (line.hasOption(GravitinoOptions.SIMPLE)) {
return GravitinoOptions.SIMPLE;
}

// Cache the Gravitino authentication type environment variable
if (authEnv == null && !authSet) {
authEnv = System.getenv("GRAVITINO_AUTH");
authSet = true;
}

// If set return the Gravitino authentication type environment variable
if (authEnv != null) {
return authEnv;
}

// Check if the authentication type is specified in the configuration file
GravitinoConfig config = new GravitinoConfig(null);
if (config.fileExists()) {
config.read();
String configAuthType = config.getGravitinoAuthType();
if (configAuthType != null) {
return configAuthType;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,9 @@

import com.google.common.base.Joiner;
import java.util.List;
import org.apache.commons.cli.CommandLine;

public abstract class CommandHandler {
public static final Joiner COMMA_JOINER = Joiner.on(", ").skipNulls();
private String authEnv;
private boolean authSet = false;

/**
* Retrieves the Gravitino authentication from the command line options or the GRAVITINO_AUTH
* environment variable or the Gravitino config file.
*
* @param line The command line instance.
* @return The Gravitino authentication, or null if not found.
*/
public String getAuth(CommandLine line) {
// If specified on the command line use that
if (line.hasOption(GravitinoOptions.SIMPLE)) {
return GravitinoOptions.SIMPLE;
}

// Cache the Gravitino authentication type environment variable
if (authEnv == null && !authSet) {
authEnv = System.getenv("GRAVITINO_AUTH");
authSet = true;
}

// If set return the Gravitino authentication type environment variable
if (authEnv != null) {
return authEnv;
}

// Check if the authentication type is specified in the configuration file
GravitinoConfig config = new GravitinoConfig(null);
if (config.fileExists()) {
config.read();
String configAuthType = config.getGravitinoAuthType();
if (configAuthType != null) {
return configAuthType;
}
}

return null;
}

protected abstract void handle();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public FilesetCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public GroupCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

if (CommandActions.LIST.equals(command)) {
handleListCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MetalakeCommandHandler(
public void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

if (CommandActions.LIST.equals(command)) {
handleListCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ModelCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public OwnerCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

if (entityName == null && !CommandEntities.METALAKE.equals(entity)) {
System.err.println(ErrorMessages.MISSING_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public RoleCommandHandler(

/** Handles the command execution logic based on the provided command. */
public void handle() {
String auth = getAuth(line);
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(auth, userName);
Command.setAuthenticationMode(context.auth(), userName);

metalake = new FullName(line).getMetalakeName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public SchemaCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (metalake == null) missingEntities.add(CommandEntities.METALAKE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public TableCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TagCommandHandler(
public void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

metalake = name.getMetalakeName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public TopicCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public UserCommandHandler(
@Override
protected void handle() {
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
Command.setAuthenticationMode(getAuth(line), userName);
Command.setAuthenticationMode(context.auth(), userName);

user = line.getOptionValue(GravitinoOptions.USER);

Expand Down