Skip to content

Commit

Permalink
IGNITE-18312 [IEP-81] Use IgniteClient instead of GridClient in contr…
Browse files Browse the repository at this point in the history
…ol-utility (#11648)
  • Loading branch information
NSAmelchev authored Dec 16, 2024
1 parent 12cc804 commit 789ef9f
Show file tree
Hide file tree
Showing 58 changed files with 904 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
import org.apache.ignite.ssl.SslContextFactory;

import static org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND;
import static org.apache.ignite.configuration.ConnectorConfiguration.DFLT_TCP_PORT;
import static org.apache.ignite.internal.client.GridClientConfiguration.DFLT_PING_INTERVAL;
import static org.apache.ignite.internal.client.GridClientConfiguration.DFLT_PING_TIMEOUT;
import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_HOST;
import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_PORT;
import static org.apache.ignite.internal.commandline.CommandHandler.UTILITY_NAME;
import static org.apache.ignite.internal.commandline.CommandHandler.useConnectorConnection;
import static org.apache.ignite.internal.commandline.argument.parser.CLIArgument.optionalArg;
import static org.apache.ignite.internal.management.api.CommandUtils.CMD_WORDS_DELIM;
import static org.apache.ignite.internal.management.api.CommandUtils.NAME_PREFIX;
Expand Down Expand Up @@ -184,7 +186,7 @@ public ArgumentParser(IgniteLogger log, IgniteCommandRegistry registry) {
"Whenever possible, use interactive prompt for password (just discard %s option).", name, name));

arg(CMD_HOST, "HOST_OR_IP", String.class, DFLT_HOST);
arg(CMD_PORT, "PORT", Integer.class, DFLT_PORT, PORT_VALIDATOR);
arg(CMD_PORT, "PORT", Integer.class, useConnectorConnection() ? DFLT_TCP_PORT : DFLT_PORT, PORT_VALIDATOR);
arg(CMD_USER, "USER", String.class, null);
arg(CMD_PASSWORD, "PASSWORD", String.class, null, (BiConsumer<String, String>)securityWarn);
arg(CMD_PING_INTERVAL, "PING_INTERVAL", Long.class, DFLT_PING_INTERVAL, POSITIVE_LONG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@

/**
* Adapter of new management API command for legacy {@code control.sh} execution flow.
*
* @deprecated Use {@link CliIgniteClientInvoker} instead. Will be removed in the next releases.
*/
public class CliCommandInvoker<A extends IgniteDataTransferObject> extends CommandInvoker<A> implements AutoCloseable {
@Deprecated
public class CliCommandInvoker<A extends IgniteDataTransferObject> extends CommandInvoker<A> implements CloseableCliCommandInvoker {
/** Client configuration. */
private final GridClientConfiguration clientCfg;

Expand All @@ -58,17 +61,15 @@ public CliCommandInvoker(Command<A, ?> cmd, A arg, GridClientConfiguration clien
this.clientCfg = clientCfg;
}

/**
* @return Message text to show user for. {@code null} means that confirmantion is not required.
*/
public String confirmationPrompt() {
/** {@inheritDoc} */
@Override public String confirmationPrompt() {
return cmd.confirmationPrompt(arg);
}

/** */
public <R> R invokeBeforeNodeStart(Consumer<String> printer) throws Exception {
/** {@inheritDoc} */
@Override public <R> R invokeBeforeNodeStart(Consumer<String> printer) throws Exception {
try (GridClientBeforeNodeStart client = startClientBeforeNodeStart(clientCfg)) {
return ((BeforeNodeStartCommand<A, R>)cmd).execute(client, arg, printer);
return ((BeforeNodeStartCommand<A, R>)cmd).execute(client.beforeStartState(), arg, printer);
}
catch (GridClientDisconnectedException e) {
throw new GridClientException(e.getCause());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal.commandline;

import java.util.function.Consumer;
import org.apache.ignite.Ignition;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;
import org.apache.ignite.internal.client.GridClientNode;
import org.apache.ignite.internal.client.GridClientNodeStateBeforeStart;
import org.apache.ignite.internal.client.thin.TcpIgniteClient;
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.BeforeNodeStartCommand;
import org.apache.ignite.internal.management.api.Command;
import org.apache.ignite.internal.management.api.CommandInvoker;
import org.apache.ignite.internal.management.api.CommandUtils;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.internal.processors.odbc.ClientListenerNioListener.MANAGEMENT_CLIENT_ATTR;

/**
* Adapter of new management API command for {@code control.sh} execution flow.
*/
public class CliIgniteClientInvoker<A extends IgniteDataTransferObject> extends CommandInvoker<A> implements CloseableCliCommandInvoker {
/** Client configuration. */
private final ClientConfiguration cfg;

/** Client. */
private IgniteClient client;

/** @param cmd Command to execute. */
public CliIgniteClientInvoker(Command<A, ?> cmd, A arg, ClientConfiguration cfg) {
super(cmd, arg, null);

this.cfg = cfg;
}

/** {@inheritDoc} */
@Override protected GridClientNode defaultNode() {
return CommandUtils.clusterToClientNode(igniteClient().cluster().forOldest().node());
}

/** {@inheritDoc} */
@Override protected @Nullable IgniteClient igniteClient() {
if (client == null) {
if (cmd instanceof BeforeNodeStartCommand) {
cfg.setUserAttributes(F.asMap(MANAGEMENT_CLIENT_ATTR, Boolean.TRUE.toString()));
cfg.setAutoBinaryConfigurationEnabled(false);
}

client = Ignition.startClient(cfg);
}

return client;
}

/** {@inheritDoc} */
@Override public String confirmationPrompt() {
return cmd.confirmationPrompt(arg);
}

/** {@inheritDoc} */
@Override public <R> R invokeBeforeNodeStart(Consumer<String> printer) throws Exception {
return ((BeforeNodeStartCommand<A, R>)cmd).execute(new GridClientNodeStateBeforeStart() {
@Override public void stopWarmUp() {
((TcpIgniteClient)igniteClient()).stopWarmUp();
}
}, arg, printer);
}

/** {@inheritDoc} */
@Override public void close() {
U.closeQuiet(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal.commandline;

import java.util.function.Consumer;
import org.apache.ignite.internal.management.api.CommandInvoker;

/**
* CLI command invoker.
*/
public interface CloseableCliCommandInvoker extends AutoCloseable {
/** @return Message text to show user for. {@code null} means that confirmantion is not required. */
String confirmationPrompt();

/** @see CommandInvoker#prepare(Consumer) */
boolean prepare(Consumer<String> printer) throws Exception;

/** @see CommandInvoker#invoke(Consumer, boolean) */
<R> R invoke(Consumer<String> printer, boolean verbose) throws Exception;

/** */
<R> R invokeBeforeNodeStart(Consumer<String> printer) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.client.ClientAuthenticationException;
import org.apache.ignite.client.ClientConnectionException;
import org.apache.ignite.client.SslMode;
import org.apache.ignite.configuration.ClientConfiguration;
import org.apache.ignite.configuration.ClientConnectorConfiguration;
import org.apache.ignite.configuration.ConnectorConfiguration;
import org.apache.ignite.internal.client.GridClientAuthenticationException;
import org.apache.ignite.internal.client.GridClientClosedException;
import org.apache.ignite.internal.client.GridClientConfiguration;
Expand Down Expand Up @@ -75,6 +80,7 @@
import static java.lang.System.lineSeparator;
import static java.util.Objects.nonNull;
import static org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND;
import static org.apache.ignite.IgniteSystemProperties.getBoolean;
import static org.apache.ignite.internal.IgniteVersionUtils.ACK_VER_STR;
import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_AUTO_CONFIRMATION;
Expand Down Expand Up @@ -131,7 +137,13 @@ public class CommandHandler {
public static final String DFLT_HOST = "127.0.0.1";

/** */
public static final int DFLT_PORT = 11211;
public static final int DFLT_PORT = 10800;

/**
* System property for backward compatibility. Will be removed in future releases.
* Enables connection to {@link ConnectorConfiguration REST connector} and forcefully sets default port to 11211.
*/
public static final String IGNITE_CONTROL_UTILITY_USE_CONNECTOR_CONNECTION = "IGNITE_CONTROL_UTILITY_USE_CONNECTOR_CONNECTION";

/** */
private final Scanner in = new Scanner(System.in);
Expand Down Expand Up @@ -255,6 +267,13 @@ public <A extends IgniteDataTransferObject> int execute(List<String> rawArgs) {

cmdName = toFormattedCommandName(args.cmdPath().peekLast().getClass()).toUpperCase();

if (useConnectorConnection() && !(args.command() instanceof HelpCommand)) {
logger.warning("WARNING: Deprecated protocol (ConnectorConfiguration) used to connect to cluster. " +
"It will be removed in the next releases. Please update the control utility connection arguments " +
"to use a thin client protocol: set up a port and/or SSL configuration " +
"releated to the ClientConnectorConfiguration on nodes.");
}

int tryConnectMaxCnt = 3;

boolean suppliedAuth = !F.isEmpty(args.userName()) && !F.isEmpty(args.password());
Expand All @@ -263,8 +282,9 @@ public <A extends IgniteDataTransferObject> int execute(List<String> rawArgs) {

while (true) {
try (
CliCommandInvoker<A> invoker =
new CliCommandInvoker<>(args.command(), args.commandArg(), getClientConfiguration(args))
CloseableCliCommandInvoker invoker = useConnectorConnection()
? new CliCommandInvoker<>(args.command(), args.commandArg(), getClientConfiguration(args))
: new CliIgniteClientInvoker<>(args.command(), args.commandArg(), clientConfiguration(args))
) {
if (!invoker.prepare(logger::info))
return EXIT_CODE_OK;
Expand Down Expand Up @@ -349,9 +369,15 @@ else if (args.command() instanceof BeforeNodeStartCommand)
e = cause;

logger.error("Connection to cluster failed. " + errorMessage(e));

}

logger.error("Make sure you are connecting to the client connector (configured on a node via '" +
ClientConnectorConfiguration.class.getName() + "'). Connection to the REST connector was " +
"deprecated and will be removed for the control utility in future releases. Set up the '" +
IGNITE_CONTROL_UTILITY_USE_CONNECTOR_CONNECTION + "' system property to the 'true' to " +
"forcefully connect to the REST connector (configured on a node via '" +
ConnectorConfiguration.class.getName() + "'). ");

logger.info("Command [" + cmdName + "] finished with code: " + EXIT_CODE_CONNECTION_FAILED);

if (verbose)
Expand Down Expand Up @@ -446,6 +472,9 @@ private boolean isSSLMisconfigurationError(Throwable e) {
* to secured cluster.
*/
private boolean isConnectionClosedSilentlyException(Throwable e) {
if (e instanceof ClientConnectionException && e.getMessage().startsWith("Channel is closed"))
return true;

if (!(e instanceof GridClientDisconnectedException))
return false;

Expand Down Expand Up @@ -495,6 +524,36 @@ private String argumentsToString(List<String> rawArgs) {
return sb.toString();
}

/**
* @param args Common arguments.
* @return Thin client configuration to connect to cluster.
* @throws IgniteCheckedException If error occur.
*/
private ClientConfiguration clientConfiguration(ConnectionAndSslParameters args) throws IgniteCheckedException {
ClientConfiguration clientCfg = new ClientConfiguration();

clientCfg.setAddresses(args.host() + ":" + args.port());

if (!F.isEmpty(args.userName())) {
clientCfg.setUserName(args.userName());
clientCfg.setUserPassword(args.password());
}

if (!F.isEmpty(args.sslKeyStorePath()) || !F.isEmpty(args.sslFactoryConfigPath())) {
if (!F.isEmpty(args.sslKeyStorePath()) && !F.isEmpty(args.sslFactoryConfigPath())) {
throw new IllegalArgumentException("Incorrect SSL configuration. SSL factory config path should " +
"not be specified simultaneously with other SSL options like keystore path.");
}

clientCfg.setSslContextFactory(createSslSupportFactory(args));
clientCfg.setSslMode(SslMode.REQUIRED);
}

clientCfg.setClusterDiscoveryEnabled(false);

return clientCfg;
}

/**
* @param args Common arguments.
* @return Thin client configuration to connect to cluster.
Expand Down Expand Up @@ -531,7 +590,7 @@ private String argumentsToString(List<String> rawArgs) {

if (!F.isEmpty(args.sslKeyStorePath()) || !F.isEmpty(args.sslFactoryConfigPath())) {
if (!F.isEmpty(args.sslKeyStorePath()) && !F.isEmpty(args.sslFactoryConfigPath()))
throw new IgniteCheckedException("Incorrect SSL configuration. " +
throw new IllegalArgumentException("Incorrect SSL configuration. " +
"SSL factory config path should not be specified simultaneously with other SSL options like keystore path.");

clientCfg.setSslContextFactory(createSslSupportFactory(args));
Expand Down Expand Up @@ -656,10 +715,11 @@ private boolean confirm(String str) {

/**
* @param e Exception to check.
* @return {@code true} if specified exception is {@link GridClientAuthenticationException}.
* @return {@code true} if specified exception is an authentication exception.
*/
public static boolean isAuthError(Throwable e) {
return X.hasCause(e, GridClientAuthenticationException.class);
return X.hasCause(e, GridClientAuthenticationException.class)
|| X.hasCause(e, ClientAuthenticationException.class);
}

/**
Expand All @@ -671,7 +731,8 @@ private static boolean isConnectionError(Throwable e) {
e instanceof GridClientConnectionResetException ||
e instanceof GridClientDisconnectedException ||
e instanceof GridClientHandshakeException ||
e instanceof GridServerUnreachableException;
e instanceof GridServerUnreachableException ||
X.hasCause(e, ClientConnectionException.class);
}

/**
Expand Down Expand Up @@ -708,7 +769,7 @@ private String requestDataFromConsole(String msg) {
/** @param rawArgs Arguments. */
private void printHelp(List<String> rawArgs) {
boolean experimentalEnabled = rawArgs.stream().anyMatch(CMD_ENABLE_EXPERIMENTAL::equalsIgnoreCase) ||
IgniteSystemProperties.getBoolean(IGNITE_ENABLE_EXPERIMENTAL_COMMAND);
getBoolean(IGNITE_ENABLE_EXPERIMENTAL_COMMAND);

logger.info("Control utility script is used to execute admin commands on cluster or get common cluster info. " +
"The command has the following syntax:");
Expand Down Expand Up @@ -949,6 +1010,11 @@ private static class LengthCalculator implements Consumer<Field> {
}
}

/** */
public static boolean useConnectorConnection() {
return getBoolean(IGNITE_CONTROL_UTILITY_USE_CONNECTOR_CONNECTION);
}

/** */
public static class CommandName implements Consumer<Object> {
/** */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.processors.security.impl.TestSecurityData;
import org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.plugin.security.SecurityPermission;
import org.apache.ignite.plugin.security.SecurityPermissionSet;
import org.apache.ignite.plugin.security.SecurityPermissionSetBuilder;
Expand Down Expand Up @@ -64,7 +65,7 @@ public class SecurityCommandHandlerPermissionsTest extends GridCommandHandlerAbs
/** */
@Parameterized.Parameters(name = "cmdHnd={0}")
public static List<String> commandHandlers() {
return Collections.singletonList(CLI_CMD_HND);
return F.asList(CLI_CMD_HND, CLI_GRID_CLIENT_CMD_HND);
}

/** {@inheritDoc} */
Expand Down
Loading

0 comments on commit 789ef9f

Please sign in to comment.