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,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());