33import com .google .common .base .CharMatcher ;
44import com .google .common .base .Splitter ;
55import com .google .common .base .Strings ;
6+ import com .google .common .base .Throwables ;
67import com .google .common .collect .ImmutableList ;
78import com .google .common .collect .Lists ;
89
4748import org .kohsuke .stapler .verb .POST ;
4849
4950import javax .annotation .Nonnull ;
51+ import javax .net .ssl .SSLException ;
5052import java .io .IOException ;
53+ import java .net .SocketException ;
54+ import java .net .UnknownHostException ;
5155import java .util .List ;
5256
5357import 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
0 commit comments