Skip to content

Commit 591e05e

Browse files
janiszrukletsov
andauthored
ROX-16728: On validation show concrete error (#259)
Co-authored-by: Alex Rukletsov <rukletsov@gmail.com>
1 parent 4e1bd1a commit 591e05e

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

stackrox-container-image-scanner/src/main/java/com/stackrox/jenkins/plugins/StackroxBuilder.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.CharMatcher;
44
import com.google.common.base.Splitter;
55
import com.google.common.base.Strings;
6+
import com.google.common.base.Throwables;
67
import com.google.common.collect.ImmutableList;
78
import com.google.common.collect.Lists;
89

@@ -47,7 +48,10 @@
4748
import org.kohsuke.stapler.verb.POST;
4849

4950
import javax.annotation.Nonnull;
51+
import javax.net.ssl.SSLException;
5052
import java.io.IOException;
53+
import java.net.SocketException;
54+
import java.net.UnknownHostException;
5155
import java.util.List;
5256

5357
import static com.stackrox.jenkins.plugins.services.ApiClientFactory.StackRoxTlsValidationMode.INSECURE_ACCEPT_ANY;
@@ -252,9 +256,19 @@ public FormValidation doTestConnection(@QueryParameter("portalAddress") final St
252256
if (checkRoxAuthStatus(portalAddress, apiToken, tlsVerify, caCertPEM)) {
253257
return FormValidation.ok("Success");
254258
}
255-
return FormValidation.error(Messages.StackroxBuilder_TestConnectionError());
256-
} catch (Exception e) {
257-
return FormValidation.error(e, Messages.StackroxBuilder_TestConnectionError());
259+
return FormValidation.error("Invalid credentials, user not authenticated");
260+
} catch (Exception ex) {
261+
Throwable e = Throwables.getRootCause(ex);
262+
if (e instanceof ServiceException) {
263+
return FormValidation.error(e, "Invalid response from StackRox portal");
264+
} else if (e instanceof UnknownHostException) {
265+
return FormValidation.error(e, "Unknown host: " + portalAddress);
266+
} else if (e instanceof SSLException) {
267+
return FormValidation.error(e, "Could not validate TLS");
268+
} else if (e instanceof SocketException) {
269+
return FormValidation.error(e, "Connection error");
270+
}
271+
return FormValidation.error(ex, "Failed to connect to StackRox portal, please provide a valid portal address and API token");
258272
}
259273
}
260274

@@ -264,7 +278,7 @@ private boolean checkRoxAuthStatus(final String portalAddress, final String apiT
264278
V1AuthStatus status = new AuthServiceApi(apiClient).authServiceGetAuthStatus();
265279
return !Strings.isNullOrEmpty(status.getUserId());
266280
} catch (ApiException e) {
267-
throw ServiceException.fromApiException("Could not get auth status.", e);
281+
throw ServiceException.fromApiException("Could not get auth status", e);
268282
}
269283
}
270284

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
StackroxBuilder.DescriptorImpl.DisplayName=StackRox Container Image Scanner
22
StackroxBuilder.InvalidPortalAddressError=Please enter a valid StackRox portal address
33
StackroxBuilder.EmptyAPITokenError=Please enter a valid StackRox API token
4-
StackroxBuilder.TestConnectionError=Failed to connect to StackRox portal, please provide a valid portal address and API token
5-

0 commit comments

Comments
 (0)