Enhance push command with username/password authentication (#1734)

This commit is contained in:
Eduard Heimbuch
2021-07-23 13:42:39 +02:00
committed by GitHub
parent 624605daaa
commit f52c0b07bf
8 changed files with 160 additions and 304 deletions

View File

@@ -0,0 +1,2 @@
- type: changed
description: Add username/password authentication to push command ([#1734](https://github.com/scm-manager/scm-manager/pull/1734))

View File

@@ -24,8 +24,6 @@
package sonia.scm.repository.api;
//~--- non-JDK imports --------------------------------------------------------
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
@@ -38,44 +36,42 @@ import sonia.scm.repository.spi.PushCommandRequest;
import java.io.IOException;
import java.net.URL;
//~--- JDK imports ------------------------------------------------------------
/**
* The push command push changes to a other repository.
*
* @author Sebastian Sdorra
* @since 1.31
*/
public final class PushCommandBuilder
{
public final class PushCommandBuilder {
/**
* the logger for PushCommandBuilder
*/
private static final Logger logger =
LoggerFactory.getLogger(PushCommandBuilder.class);
private static final Logger logger = LoggerFactory.getLogger(PushCommandBuilder.class);
//~--- constructors ---------------------------------------------------------
private final PushCommand command;
private final PushCommandRequest request = new PushCommandRequest();
/**
* Constructs a new PushCommandBuilder.
*
* @param command implementation of the {@link PushCommand}
*/
PushCommandBuilder(PushCommand command)
{
PushCommandBuilder(PushCommand command) {
this.command = command;
}
//~--- methods --------------------------------------------------------------
/**
* Set username and password for request
*
* @param username username
* @param password password
* @return this builder instance.
* @since 2.22.0
*/
public PushCommandBuilder withUsernamePassword(String username, String password) {
request.setUsername(username);
request.setPassword(password);
return this;
}
/**
* Push all changes to the given remote repository.
*
* @param remoteRepository remote repository
*
* @return informations of the executed push command
*
* @throws IOException
*/
public PushResponse push(Repository remoteRepository) throws IOException {
@@ -97,11 +93,8 @@ public final class PushCommandBuilder
* Push all changes to the given remote url.
*
* @param url url of a remote repository
*
* @return informations of the executed push command
*
* @throws IOException
*
* @since 1.43
*/
public PushResponse push(String url) throws IOException {
@@ -110,17 +103,9 @@ public final class PushCommandBuilder
logger.info("push changes to url {}", url);
request.reset();
request.setRemoteUrl(remoteUrl);
return command.push(request);
}
//~--- fields ---------------------------------------------------------------
/** push command implementation */
private PushCommand command;
/** push command request */
private PushCommandRequest request = new PushCommandRequest();
}

View File

@@ -24,9 +24,7 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
@@ -35,6 +33,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.ScmTransportProtocol;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitRepositoryHandler;
@@ -44,230 +43,113 @@ import sonia.scm.repository.InternalRepositoryException;
import java.io.File;
import java.util.Collection;
//~--- JDK imports ------------------------------------------------------------
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand
{
/** Field description */
private static final String SCHEME = ScmTransportProtocol.NAME + "://";
private static final Logger LOG = LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
/**
* the logger for AbstractGitPushOrPullCommand
*/
private static final Logger logger =
LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
protected GitRepositoryHandler handler;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param handler
* @param context
*/
protected AbstractGitPushOrPullCommand(GitRepositoryHandler handler, GitContext context)
{
protected AbstractGitPushOrPullCommand(GitRepositoryHandler handler, GitContext context) {
super(context);
this.handler = handler;
}
//~--- methods --------------------------------------------------------------
protected long push(Repository source, String remoteUrl) {
protected long push(Repository source, String remoteUrl, String username, String password) {
Git git = Git.wrap(source);
org.eclipse.jgit.api.PushCommand push = git.push();
if (!Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password)) {
push.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password.toCharArray()));
}
push.setPushAll().setPushTags();
push.setRemote(remoteUrl);
long counter = -1;
try
{
try {
Iterable<PushResult> results = push.call();
if (results != null)
{
if (results != null) {
counter = 0;
for (PushResult result : results)
{
for (PushResult result : results) {
counter += count(git, result);
}
}
}
catch (Exception ex)
{
} catch (Exception ex) {
throw new InternalRepositoryException(repository, "could not execute push/pull command", ex);
}
return counter;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
protected sonia.scm.repository.Repository getRemoteRepository(
RemoteCommandRequest request)
{
Preconditions.checkNotNull(request, "request is required");
sonia.scm.repository.Repository remoteRepository =
request.getRemoteRepository();
Preconditions.checkNotNull(remoteRepository,
"remote repository is required");
return remoteRepository;
}
/**
* Method description
*
*
* @param request
*
* @return
*/
protected String getRemoteUrl(RemoteCommandRequest request)
{
protected String getRemoteUrl(RemoteCommandRequest request) {
String url;
sonia.scm.repository.Repository remRepo = request.getRemoteRepository();
if (remRepo != null)
{
if (remRepo != null) {
url = getRemoteUrl(remRepo);
}
else if (request.getRemoteUrl() != null)
{
} else if (request.getRemoteUrl() != null) {
url = request.getRemoteUrl().toExternalForm();
}
else
{
} else {
throw new IllegalArgumentException("repository or url is required");
}
return url;
}
/**
* Method description
*
*
* @param directory
*
* @return
*/
protected String getRemoteUrl(File directory)
{
protected String getRemoteUrl(File directory) {
return SCHEME.concat(directory.getAbsolutePath());
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
protected String getRemoteUrl(sonia.scm.repository.Repository repository)
{
protected String getRemoteUrl(sonia.scm.repository.Repository repository) {
return getRemoteUrl(handler.getDirectory(repository.getId()));
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param git
* @param result
*
* @return
*/
private long count(Git git, PushResult result)
{
private long count(Git git, PushResult result) {
long counter = 0;
Collection<RemoteRefUpdate> updates = result.getRemoteUpdates();
for (RemoteRefUpdate update : updates)
{
for (RemoteRefUpdate update : updates) {
counter += count(git, update);
}
return counter;
}
/**
* Method description
*
*
* @param git
* @param update
*
* @return
*/
private long count(Git git, RemoteRefUpdate update)
{
private long count(Git git, RemoteRefUpdate update) {
long counter = 0;
if (GitUtil.isHead(update.getRemoteName()))
{
try
{
if (GitUtil.isHead(update.getRemoteName())) {
try {
org.eclipse.jgit.api.LogCommand log = git.log();
ObjectId oldId = update.getExpectedOldObjectId();
if (GitUtil.isValidObjectId(oldId))
{
if (GitUtil.isValidObjectId(oldId)) {
log.not(oldId);
}
ObjectId newId = update.getNewObjectId();
if (GitUtil.isValidObjectId(newId))
{
if (GitUtil.isValidObjectId(newId)) {
log.add(newId);
}
Iterable<RevCommit> commits = log.call();
if (commits != null)
{
if (commits != null) {
counter += Iterables.size(commits);
}
logger.trace("counting {} commits for ref update {}", counter, update);
LOG.trace("counting {} commits for ref update {}", counter, update);
} catch (Exception ex) {
LOG.error("could not count pushed/pulled changesets", ex);
}
catch (Exception ex)
{
logger.error("could not count pushed/pulled changesets", ex);
}
}
else
{
logger.debug("do not count non branch ref update {}", update);
} else {
LOG.debug("do not count non branch ref update {}", update);
}
return counter;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
protected GitRepositoryHandler handler;
}

View File

@@ -71,7 +71,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
Repository sourceRepository = request.getRemoteRepository();
if (sourceRepository != null) {
response = pullFromScmRepository(sourceRepository);
response = pullFromScmRepository(sourceRepository, request.getUsername(), request.getPassword());
} else if (request.getRemoteUrl() != null) {
response = pullFromUrl(request);
} else {
@@ -129,7 +129,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
return counter;
}
private PullResponse pullFromScmRepository(Repository sourceRepository)
private PullResponse pullFromScmRepository(Repository sourceRepository, String username, String password)
throws IOException {
File sourceDirectory = handler.getDirectory(sourceRepository.getId());
@@ -150,7 +150,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
try (Git git = Git.open(sourceDirectory)) {
source = git.getRepository();
response = new PullResponse(push(source, getRemoteUrl(targetDirectory)));
response = new PullResponse(push(source, getRemoteUrl(targetDirectory), username, password));
} finally {
GitUtil.close(source);
}

View File

@@ -24,8 +24,6 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitRepositoryHandler;
@@ -34,54 +32,23 @@ import sonia.scm.repository.api.PushResponse;
import javax.inject.Inject;
import java.io.IOException;
//~--- JDK imports ------------------------------------------------------------
public class GitPushCommand extends AbstractGitPushOrPullCommand implements PushCommand {
/**
*
* @author Sebastian Sdorra
*/
public class GitPushCommand extends AbstractGitPushOrPullCommand
implements PushCommand
{
private static final Logger LOG = LoggerFactory.getLogger(GitPushCommand.class);
/** Field description */
private static final Logger logger =
LoggerFactory.getLogger(GitPushCommand.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param handler
* @param context
*/
@Inject
public GitPushCommand(GitRepositoryHandler handler, GitContext context) {
super(handler, context);
this.handler = handler;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*
* @throws IOException
*/
@Override
public PushResponse push(PushCommandRequest request)
throws IOException
{
throws IOException {
String remoteUrl = getRemoteUrl(request);
logger.debug("push changes from {} to {}", repository, remoteUrl);
LOG.debug("push changes from {} to {}", repository, remoteUrl);
return new PushResponse(push(open(), remoteUrl));
return new PushResponse(push(open(), remoteUrl, request.getUsername(), request.getPassword()));
}
}

View File

@@ -0,0 +1,79 @@
/*
* 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.spi;
import sonia.scm.io.INIConfiguration;
import sonia.scm.io.INIConfigurationReader;
import sonia.scm.io.INIConfigurationWriter;
import sonia.scm.io.INISection;
import sonia.scm.repository.HgRepositoryHandler;
import java.io.File;
import java.io.IOException;
import java.net.URI;
public class HgIniConfigurator {
private final HgCommandContext context;
private static final String AUTH_SECTION = "auth";
public HgIniConfigurator(HgCommandContext context) {
this.context = context;
}
public void addAuthenticationConfig(RemoteCommandRequest request, String url) throws IOException {
INIConfiguration ini = readIniConfiguration();
INISection authSection = ini.getSection(AUTH_SECTION);
if (authSection == null) {
authSection = new INISection(AUTH_SECTION);
ini.addSection(authSection);
}
URI parsedUrl = URI.create(url);
authSection.setParameter("import.prefix", parsedUrl.getHost());
authSection.setParameter("import.schemes", parsedUrl.getScheme());
authSection.setParameter("import.username", request.getUsername());
authSection.setParameter("import.password", request.getPassword());
writeIniConfiguration(ini);
}
public void removeAuthenticationConfig() throws IOException {
INIConfiguration ini = readIniConfiguration();
ini.removeSection(AUTH_SECTION);
writeIniConfiguration(ini);
}
public INIConfiguration readIniConfiguration() throws IOException {
return new INIConfigurationReader().read(getHgrcFile());
}
public void writeIniConfiguration(INIConfiguration ini) throws IOException {
new INIConfigurationWriter().write(ini, getHgrcFile());
}
public File getHgrcFile() {
return new File(context.getDirectory(), HgRepositoryHandler.PATH_HGRC);
}
}

View File

@@ -31,23 +31,16 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.ContextEntry;
import sonia.scm.event.ScmEventBus;
import sonia.scm.io.INIConfiguration;
import sonia.scm.io.INIConfigurationReader;
import sonia.scm.io.INIConfigurationWriter;
import sonia.scm.io.INISection;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.PullResponse;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
public class HgPullCommand extends AbstractHgPushOrPullCommand implements PullCommand {
private static final Logger LOG = LoggerFactory.getLogger(HgPullCommand.class);
private static final String AUTH_SECTION = "auth";
private final ScmEventBus eventBus;
private final HgLazyChangesetResolver changesetResolver;
private final HgRepositoryHookEventFactory eventFactory;
@@ -69,13 +62,14 @@ public class HgPullCommand extends AbstractHgPushOrPullCommand implements PullCo
public PullResponse pull(PullCommandRequest request)
throws IOException {
String url = getRemoteUrl(request);
HgIniConfigurator iniConfigurator = new HgIniConfigurator(getContext());
LOG.debug("pull changes from {} to {}", url, getContext().getScmRepository());
List<Changeset> result;
if (!Strings.isNullOrEmpty(request.getUsername()) && !Strings.isNullOrEmpty(request.getPassword())) {
addAuthenticationConfig(request, url);
iniConfigurator.addAuthenticationConfig(request, url);
}
try {
@@ -83,7 +77,7 @@ public class HgPullCommand extends AbstractHgPushOrPullCommand implements PullCo
} catch (ExecutionException ex) {
throw new ImportFailedException(ContextEntry.ContextBuilder.entity(getRepository()).build(), "could not execute pull command", ex);
} finally {
removeAuthenticationConfig();
iniConfigurator.removeAuthenticationConfig();
}
firePostReceiveRepositoryHookEvent();
@@ -94,37 +88,4 @@ public class HgPullCommand extends AbstractHgPushOrPullCommand implements PullCo
private void firePostReceiveRepositoryHookEvent() {
eventBus.post(eventFactory.createEvent(context, changesetResolver));
}
public void addAuthenticationConfig(PullCommandRequest request, String url) throws IOException {
INIConfiguration ini = readIniConfiguration();
INISection authSection = ini.getSection(AUTH_SECTION);
if (authSection == null) {
authSection = new INISection(AUTH_SECTION);
ini.addSection(authSection);
}
URI parsedUrl = URI.create(url);
authSection.setParameter("import.prefix", parsedUrl.getHost());
authSection.setParameter("import.schemes", parsedUrl.getScheme());
authSection.setParameter("import.username", request.getUsername());
authSection.setParameter("import.password", request.getPassword());
writeIniConfiguration(ini);
}
public void removeAuthenticationConfig() throws IOException {
INIConfiguration ini = readIniConfiguration();
ini.removeSection(AUTH_SECTION);
writeIniConfiguration(ini);
}
public INIConfiguration readIniConfiguration() throws IOException {
return new INIConfigurationReader().read(getHgrcFile());
}
public void writeIniConfiguration(INIConfiguration ini) throws IOException {
new INIConfigurationWriter().write(ini, getHgrcFile());
}
public File getHgrcFile() {
return new File(getContext().getDirectory(), HgRepositoryHandler.PATH_HGRC);
}
}

View File

@@ -24,10 +24,9 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.aragost.javahg.Changeset;
import com.aragost.javahg.commands.ExecutionException;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.HgRepositoryHandler;
@@ -35,56 +34,37 @@ import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.api.PushResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
//~--- JDK imports ------------------------------------------------------------
import static com.aragost.javahg.commands.flags.PushCommandFlags.on;
/**
*
* @author Sebastian Sdorra
*/
public class HgPushCommand extends AbstractHgPushOrPullCommand
implements PushCommand
{
public class HgPushCommand extends AbstractHgPushOrPullCommand implements PushCommand {
/** Field description */
private static final Logger logger =
LoggerFactory.getLogger(HgPushCommand.class);
private static final Logger LOG = LoggerFactory.getLogger(HgPushCommand.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param handler
* @param context
*/
public HgPushCommand(HgRepositoryHandler handler, HgCommandContext context)
{
public HgPushCommand(HgRepositoryHandler handler, HgCommandContext context) {
super(handler, context);
}
//~--- methods --------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
public PushResponse push(PushCommandRequest request)
throws IOException
{
throws IOException {
String url = getRemoteUrl(request);
logger.debug("push changes from {} to {}", getRepository(), url);
LOG.debug("push changes from {} to {}", getRepository(), url);
List<Changeset> result = Collections.EMPTY_LIST;
List<Changeset> result;
HgIniConfigurator iniConfigurator = new HgIniConfigurator(getContext());
try {
if (!Strings.isNullOrEmpty(request.getUsername()) && !Strings.isNullOrEmpty(request.getPassword())) {
iniConfigurator.addAuthenticationConfig(request, url);
}
try
{
result = com.aragost.javahg.commands.PushCommand.on(open()).execute(url);
}
catch (ExecutionException ex)
{
result = on(open()).execute(url);
} catch (ExecutionException ex) {
throw new InternalRepositoryException(getRepository(), "could not execute push command", ex);
} finally {
iniConfigurator.removeAuthenticationConfig();
}
return new PushResponse(result.size());