Skip to content

Commit

Permalink
IGNITE-18312 [IEP-94] Use IgniteClient instead of GridClient in contr…
Browse files Browse the repository at this point in the history
…ol-utility

wip
  • Loading branch information
NSAmelchev committed Oct 31, 2024
1 parent 9f4f738 commit e8acbce
Show file tree
Hide file tree
Showing 70 changed files with 1,076 additions and 637 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.net.URL;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.cache.configuration.Factory;
import javax.net.ssl.SSLContext;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.dto.IgniteDataTransferObject;
import org.apache.ignite.internal.management.api.Command;
import org.apache.ignite.internal.management.api.CommandInvoker;
import org.apache.ignite.internal.util.IgniteUtils;
import org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.ssl.SslContextFactory;
import org.springframework.context.ApplicationContext;

/**
* Adapter of new management API command for legacy {@code control.sh} execution flow.
*/
public abstract class AbstractCliCommandInvoker<A extends IgniteDataTransferObject> extends CommandInvoker<A>
implements AutoCloseable {
/** */
protected final ConnectionAndSslParameters<A> args;

/** */
protected final Function<String, char[]> pwdReader;

/** */
protected AbstractCliCommandInvoker(
Command<A, ?> cmd,
ConnectionAndSslParameters<A> args,
Function<String, char[]> pwdReader
) {
super(cmd, args.commandArg(), null);

this.args = args;
this.pwdReader = pwdReader;
}

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

/** */
public abstract <R> R invokeBeforeNodeStart(Consumer<String> printer) throws Exception;

/**
* @param args Commond args.
* @return Ssl support factory.
*/
protected Factory<SSLContext> createSslSupportFactory(ConnectionAndSslParameters args) throws IgniteCheckedException {
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.");
}

if (!F.isEmpty(args.sslFactoryConfigPath())) {
URL springCfg = IgniteUtils.resolveSpringUrl(args.sslFactoryConfigPath());

ApplicationContext ctx = IgniteSpringHelperImpl.applicationContext(springCfg);

return (Factory<SSLContext>)ctx.getBean(Factory.class);
}

SslContextFactory factory = new SslContextFactory();

if (args.sslProtocol().length > 1)
factory.setProtocols(args.sslProtocol());
else
factory.setProtocol(args.sslProtocol()[0]);

factory.setKeyAlgorithm(args.sslKeyAlgorithm());
factory.setCipherSuites(args.getSslCipherSuites());
factory.setKeyStoreFilePath(args.sslKeyStorePath());

if (args.sslKeyStorePassword() != null)
factory.setKeyStorePassword(args.sslKeyStorePassword());
else {
char[] keyStorePwd = pwdReader.apply("SSL keystore password: ");

args.sslKeyStorePassword(keyStorePwd);
factory.setKeyStorePassword(keyStorePwd);
}

factory.setKeyStoreType(args.sslKeyStoreType());

if (F.isEmpty(args.sslTrustStorePath()))
factory.setTrustManagers(SslContextFactory.getDisabledTrustManager());
else {
factory.setTrustStoreFilePath(args.sslTrustStorePath());

if (args.sslTrustStorePassword() != null)
factory.setTrustStorePassword(args.sslTrustStorePassword());
else {
char[] trustStorePwd = pwdReader.apply("SSL truststore password: ");

args.sslTrustStorePassword(trustStorePwd);
factory.setTrustStorePassword(trustStorePwd);
}

factory.setTrustStoreType(args.sslTrustStoreType());
}

return factory;
}
}
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
Loading

0 comments on commit e8acbce

Please sign in to comment.