valepakh commented on code in PR #2472:
URL: https://github.com/apache/ignite-3/pull/2472#discussion_r1301334739


##########
modules/cli/src/integrationTest/java/org/apache/ignite/internal/cli/commands/questions/ItConnectToBasicAuthClusterTest.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.cli.commands.questions;
+
+import static 
org.apache.ignite.internal.cli.commands.cliconfig.TestConfigManagerHelper.readClusterConfigurationWithEnabledAuth;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertAll;
+
+import java.io.IOException;
+import org.apache.ignite.InitParametersBuilder;
+import org.apache.ignite.internal.cli.commands.ItConnectToClusterTestBase;
+import org.apache.ignite.internal.cli.config.CliConfigKeys;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+class ItConnectToBasicAuthClusterTest extends ItConnectToClusterTestBase {
+
+    @Override
+    protected void configureInitParameters(InitParametersBuilder builder) {
+        
builder.clusterConfiguration(readClusterConfigurationWithEnabledAuth());
+    }
+
+    @Test
+    @DisplayName("Should ask for auth configuration connect to last connected 
cluster HTTPS url")
+    void connectOnStartAskAuth() throws IOException {
+        // Given prompt before connect
+        String promptBefore = getPrompt();
+        assertThat(promptBefore).isEqualTo("[disconnected]> ");

Review Comment:
   ```suggestion
           assertThat(getPrompt()).isEqualTo("[disconnected]> ");
   ```



##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/call/connect/ConnectWizardCall.java:
##########
@@ -28,29 +29,48 @@
 import org.apache.ignite.rest.client.invoker.ApiException;
 
 /**
- * Call which tries to connect to the Ignite 3 node and in case of SSL error 
asks the user for the SSL configuration and tries again.
+ * Call which tries to connect to the Ignite 3 node and in case of error (SSL 
error or auth error) asks the user for the
+ * SSL configuration or Auth configuration and tries again.
  */
 @Singleton
-public class ConnectSslCall implements Call<ConnectCallInput, String> {
+public class ConnectWizardCall implements Call<ConnectCallInput, String> {
     @Inject
     private ConnectCall connectCall;
 
     @Inject
     private ConnectSslConfigCall connectSslConfigCall;
 
+    @Inject
+    private ConnectAuthConfigCall connectAuthConfigCall;
+
     @Override
     public CallOutput<String> execute(ConnectCallInput input) {
         CallOutput<String> output = connectCall.execute(input);
         if (output.hasError()) {
             if (output.errorCause().getCause() instanceof ApiException) {
                 ApiException cause = (ApiException) 
output.errorCause().getCause();
                 Throwable apiCause = cause.getCause();
+
+                // Configure SSL
                 if (apiCause instanceof SSLException) {
                     FlowBuilder<Void, SslConfig> flowBuilder = 
ConnectToClusterQuestion.askQuestionOnSslError();
                     Flowable<SslConfig> result = 
flowBuilder.build().start(Flowable.empty());
                     if (result.hasResult()) {
                         return connectSslConfigCall.execute(new 
ConnectSslConfigCallInput(input.url(), result.value()));
                     }
+
+                // Configure rest basic auth
+                } else if (cause.getCode() == 
HttpStatus.UNAUTHORIZED.getCode()) {

Review Comment:
   How this will behave with the cluster which configures both SSL and basic 
auth and client doesn't have any/have some of it configured? Could you add the 
test?



##########
modules/cli/src/main/java/org/apache/ignite/internal/cli/call/connect/ConnectAuthConfigCall.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.cli.call.connect;
+
+import static 
org.apache.ignite.internal.cli.config.CliConfigKeys.BASIC_AUTHENTICATION_PASSWORD;
+import static 
org.apache.ignite.internal.cli.config.CliConfigKeys.BASIC_AUTHENTICATION_USERNAME;
+
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
+import org.apache.ignite.internal.cli.config.ConfigManager;
+import org.apache.ignite.internal.cli.config.ConfigManagerProvider;
+import org.apache.ignite.internal.cli.core.call.Call;
+import org.apache.ignite.internal.cli.core.call.CallOutput;
+import org.apache.ignite.internal.cli.core.call.DefaultCallOutput;
+import org.apache.ignite.internal.cli.core.exception.IgniteCliApiException;
+import org.apache.ignite.internal.cli.core.rest.ApiClientFactory;
+import org.apache.ignite.internal.cli.core.rest.ApiClientSettings;
+import org.apache.ignite.rest.client.api.NodeConfigurationApi;
+import org.apache.ignite.rest.client.invoker.ApiClient;
+import org.apache.ignite.rest.client.invoker.ApiException;
+
+/**
+ * Call which connects to the Ignite 3 node with provided basic auth 
configuration and saves the configuration to the cli config.
+ */
+@Singleton
+public class ConnectAuthConfigCall implements Call<ConnectCallInput, String> {
+
+    @Inject
+    private ConnectCall connectCall;
+
+    @Inject
+    private ConfigManagerProvider configManagerProvider;
+
+    @Override
+    public CallOutput<String> execute(ConnectCallInput input) {
+        try {
+            checkConnection(input);
+            saveCredentials(input);
+            return connectCall.execute(input);

Review Comment:
   `ConnectCall` will still try to connect without authentication first, then 
again with authentication, and only then will save the session. Maybe we could 
extract saving logic from it and use directly here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to