Skip to content

Commit

Permalink
ROX-16728: On validation show concrete error (#259)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Rukletsov <rukletsov@gmail.com>
  • Loading branch information
janisz and rukletsov authored Jul 26, 2023
1 parent 4e1bd1a commit 591e05e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

Expand Down Expand Up @@ -47,7 +48,10 @@
import org.kohsuke.stapler.verb.POST;

import javax.annotation.Nonnull;
import javax.net.ssl.SSLException;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

import static com.stackrox.jenkins.plugins.services.ApiClientFactory.StackRoxTlsValidationMode.INSECURE_ACCEPT_ANY;
Expand Down Expand Up @@ -252,9 +256,19 @@ public FormValidation doTestConnection(@QueryParameter("portalAddress") final St
if (checkRoxAuthStatus(portalAddress, apiToken, tlsVerify, caCertPEM)) {
return FormValidation.ok("Success");
}
return FormValidation.error(Messages.StackroxBuilder_TestConnectionError());
} catch (Exception e) {
return FormValidation.error(e, Messages.StackroxBuilder_TestConnectionError());
return FormValidation.error("Invalid credentials, user not authenticated");
} catch (Exception ex) {
Throwable e = Throwables.getRootCause(ex);
if (e instanceof ServiceException) {
return FormValidation.error(e, "Invalid response from StackRox portal");
} else if (e instanceof UnknownHostException) {
return FormValidation.error(e, "Unknown host: " + portalAddress);
} else if (e instanceof SSLException) {
return FormValidation.error(e, "Could not validate TLS");
} else if (e instanceof SocketException) {
return FormValidation.error(e, "Connection error");
}
return FormValidation.error(ex, "Failed to connect to StackRox portal, please provide a valid portal address and API token");
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
StackroxBuilder.DescriptorImpl.DisplayName=StackRox Container Image Scanner
StackroxBuilder.InvalidPortalAddressError=Please enter a valid StackRox portal address
StackroxBuilder.EmptyAPITokenError=Please enter a valid StackRox API token
StackroxBuilder.TestConnectionError=Failed to connect to StackRox portal, please provide a valid portal address and API token

0 comments on commit 591e05e

Please sign in to comment.