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

View File

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

View File

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

View File

@@ -46,6 +46,7 @@ import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermissions; import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType; import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command; import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.PullCommandBuilder; import sonia.scm.repository.api.PullCommandBuilder;
import sonia.scm.repository.api.RepositoryService; import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory; import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -216,9 +217,12 @@ public class RepositoryImportResource {
} }
pullCommand.pull(request.getUrl()); pullCommand.pull(request.getUrl());
} catch (ImportFailedException ex) {
handleImportFailure(ex, repository);
throw ex;
} catch (Exception ex) { } catch (Exception ex) {
handleImportFailure(ex, repository); 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(); return Response.created(URI.create(resourceLinks.repository().self(repository.getNamespace(), repository.getName()))).build();

View File

@@ -286,12 +286,16 @@
"FVS9JY1T21": { "FVS9JY1T21": {
"displayName": "Fehler bei der Anfrage", "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." "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": { "namespaceStrategies": {
"UsernameNamespaceStrategy": "Benutzername", "UsernameNamespaceStrategy": "Benutzername",
"CustomNamespaceStrategy": "Benutzerdefiniert", "CustomNamespaceStrategy": "Benutzerdefiniert",
"CurrentYearNamespaceStrategy": "Aktuelles Jahr", "CurrentYearNamespaceStrategy": "Aktuelles Jahr",
"RepositoryTypeNamespaceStrategy": "Repository Typ" "RepositoryTypeNamespaceStrategy": "Repository Typ"
} }
} }

View File

@@ -286,6 +286,10 @@
"FVS9JY1T21": { "FVS9JY1T21": {
"displayName": "Error in the request", "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." "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": { "namespaceStrategies": {