improve error handling on repository import if the credentials were wrong or missing

This commit is contained in:
Eduard Heimbuch
2020-11-26 12:57:40 +01:00
parent 0682118879
commit a7c4d41e4e
7 changed files with 111 additions and 43 deletions

View File

@@ -0,0 +1,44 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.api;
import sonia.scm.ContextEntry;
import sonia.scm.ExceptionWithContext;
import java.util.List;
public class ImportFailedException extends ExceptionWithContext {
private static final String CODE = "D6SHRfqQw1";
public ImportFailedException(List<ContextEntry> context, String message, Exception cause) {
super(context, message, cause);
}
@Override
public String getCode() {
return CODE;
}
}

View File

@@ -29,7 +29,6 @@ package sonia.scm.repository.spi;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
@@ -41,10 +40,12 @@ import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ContextEntry;
import sonia.scm.repository.GitRepositoryHandler;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.PullResponse;
import javax.inject.Inject;
@@ -200,15 +201,13 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
try {
//J-
FetchCommand fetchCommand = git.fetch();
if (!Strings.isNullOrEmpty(request.getUsername()) && !Strings.isNullOrEmpty(request.getPassword())) {
fetchCommand.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(request.getUsername(), request.getPassword())
);
}
FetchResult result = fetchCommand
FetchResult result = git.fetch()
.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(
Strings.nullToEmpty(request.getUsername()),
Strings.nullToEmpty(request.getPassword())
)
)
.setRefSpecs(new RefSpec(REF_SPEC))
.setRemote(request.getRemoteUrl().toExternalForm())
.setTagOpt(TagOpt.FETCH_TAGS)
@@ -216,7 +215,16 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
//J+
response = convert(git, result);
} catch (GitAPIException ex) {
} catch
(GitAPIException ex) {
if (ex.getMessage().contains("not authorized")) {
throw new ImportFailedException(
ContextEntry.ContextBuilder.entity(repository).build(),
"Repository import failed. The credentials are wrong or missing.",
ex
);
}
throw new InternalRepositoryException(repository, "error during pull", ex);
}

View File

@@ -108,6 +108,7 @@ const RepositoryForm: FC<Props> = ({
const isCreateMode = () => creationMode === "CREATE";
const isEditMode = () => !!repository;
const isModifiable = () => !!repository && !!repository._links.update;
const disabled = (!isModifiable() && isEditMode()) || loading;
const isValid = () => {
return !(
@@ -176,6 +177,7 @@ const RepositoryForm: FC<Props> = ({
onChange={handleImportUrlChange}
value={importUrl}
helpText={t("help.importUrlHelpText")}
disabled={disabled}
/>
</Column>
<Column className="column is-half">
@@ -184,6 +186,7 @@ const RepositoryForm: FC<Props> = ({
onChange={setUsername}
value={username}
helpText={t("help.usernameHelpText")}
disabled={disabled}
/>
</Column>
<Column className="column is-half">
@@ -193,6 +196,7 @@ const RepositoryForm: FC<Props> = ({
value={password}
type="password"
helpText={t("help.passwordHelpText")}
disabled={disabled}
/>
</Column>
</Columns>
@@ -221,6 +225,7 @@ const RepositoryForm: FC<Props> = ({
validationError={nameValidationError}
errorMessage={t("validation.name-invalid")}
helpText={t("help.nameHelpText")}
disabled={disabled}
/>
<SpaceBetween>
<SelectWrapper>
@@ -230,6 +235,7 @@ const RepositoryForm: FC<Props> = ({
value={repo ? repo.type : ""}
options={createSelectOptions(repositoryTypes)}
helpText={t("help.typeHelpText")}
disabled={disabled}
/>
</SelectWrapper>
{!isImportMode() && (
@@ -239,6 +245,7 @@ const RepositoryForm: FC<Props> = ({
checked={initRepository}
onChange={toggleInitCheckbox}
helpText={t("help.initializeRepository")}
disabled={disabled}
/>
{initRepository && (
<ExtensionPoint name="repos.create.initialize" props={extensionProps} renderAll={true} />
@@ -287,16 +294,12 @@ const RepositoryForm: FC<Props> = ({
setImportUrl(url);
};
const disabled = !isModifiable() && isEditMode();
const submitButton = () => {
if (disabled) {
return null;
}
const translationKey = isImportMode() ? "repositoryForm.submitImport" : "repositoryForm.submitCreate";
return <Level
right={<SubmitButton disabled={!isValid()} loading={loading} label={t(translationKey)} />}
/>;
return <Level right={<SubmitButton disabled={!isValid()} loading={loading} label={t(translationKey)} />} />;
};
return (

View File

@@ -150,17 +150,19 @@ class AddRepository extends React.Component<Props> {
error={error}
showContentOnError={true}
>
{importLoading ? (
<>
{/*//TODO fix this CSS*/}
{!error && <RepositoryFormSwitcher creationMode={this.isImportPage() ? "IMPORT" : "CREATE"} />}
{importLoading && (
<>
<Notification type="info">{t("import.pending.infoText")}</Notification>
<Loading />
<hr/>
</>
) : (
<>
{!error && <RepositoryFormSwitcher creationMode={this.isImportPage() ? "IMPORT" : "CREATE"} />}
)}
<RepositoryForm
repositoryTypes={repositoryTypes}
loading={createLoading}
loading={createLoading || importLoading}
namespaceStrategy={namespaceStrategies.current}
createRepository={(repo, initRepository) => {
createRepo(repoLink, repo, initRepository, (repo: Repository) => this.repoCreated(repo));
@@ -172,7 +174,6 @@ class AddRepository extends React.Component<Props> {
creationMode={this.isImportPage() ? "IMPORT" : "CREATE"}
/>
</>
)}
</Page>
);
}

View File

@@ -46,6 +46,7 @@ import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.PullCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -216,9 +217,12 @@ public class RepositoryImportResource {
}
pullCommand.pull(request.getUrl());
} catch (ImportFailedException ex) {
handleImportFailure(ex, repository);
throw ex;
} catch (Exception ex) {
handleImportFailure(ex, repository);
throw new InternalRepositoryException(repository, "Import failed. Most likely the credentials are wrong or missing.", ex);
throw new InternalRepositoryException(repository, "Repository Import failed.", ex);
}
return Response.created(URI.create(resourceLinks.repository().self(repository.getNamespace(), repository.getName()))).build();

View File

@@ -286,6 +286,10 @@
"FVS9JY1T21": {
"displayName": "Fehler bei der Anfrage",
"description": "Bei der Anfrage trat ein Fehler auf. Prüfen Sie bitte den Status der HTTP Antwort und die konkrete Meldung."
},
"D6SHRfqQw1": {
"displayName": "Repository Import fehlgeschlagen",
"description": "Das Repository konnte nicht importiert werden. Entweder wurden die Zugangsdaten (Benutzername/Passwort) nicht gesetzt oder sind fehlerhaft. Bitte prüfen Sie Ihre Eingaben."
}
},
"namespaceStrategies": {

View File

@@ -286,6 +286,10 @@
"FVS9JY1T21": {
"displayName": "Error in the request",
"description": "While processing the request there was an error. Please check the http return status and the concrete error message."
},
"D6SHRfqQw1": {
"displayName": "Repository import failed",
"description": "The repository could not be imported. Either the credentials (username/password) are wrong or missing. Please check your inputs."
}
},
"namespaceStrategies": {