Merge branch 'stable-2.14' into stable-2.15

* stable-2.14:
  Propagate access path for SSH commands
  Revert "Fix access path propagation on git/ssh protocol"
  Set version to 2.14.16-SNAPSHOT

Change-Id: I0e724fb029e41d4cc677fab0e5eaef415e35e20c
This commit is contained in:
Luca Milanesio 2018-10-12 15:07:06 +01:00
commit f1cfa9fa77
4 changed files with 43 additions and 54 deletions

View File

@ -33,6 +33,8 @@ public abstract class AbstractGitCommand extends BaseCommand {
@Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name") @Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name")
protected ProjectControl projectControl; protected ProjectControl projectControl;
@Inject private SshScope sshScope;
@Inject private GitRepositoryManager repoManager; @Inject private GitRepositoryManager repoManager;
@Inject private SshSession session; @Inject private SshSession session;
@ -51,25 +53,30 @@ public abstract class AbstractGitCommand extends BaseCommand {
@Override @Override
public void start(Environment env) { public void start(Environment env) {
Context ctx = context.subContext(newSession(), context.getCommandLine()); Context ctx = context.subContext(newSession(), context.getCommandLine());
startThreadWithContext( final Context old = sshScope.set(ctx);
ctx, try {
new ProjectCommandRunnable() { startThread(
@Override new ProjectCommandRunnable() {
public void executeParseCommand() throws Exception { @Override
parseCommandLine(); public void executeParseCommand() throws Exception {
} parseCommandLine();
}
@Override @Override
public void run() throws Exception { public void run() throws Exception {
AbstractGitCommand.this.service(); AbstractGitCommand.this.service();
} }
@Override @Override
public Project.NameKey getProjectName() { public Project.NameKey getProjectName() {
Project project = projectControl.getProjectState().getProject(); Project project = projectControl.getProjectState().getProject();
return project.getNameKey(); return project.getNameKey();
} }
}); },
AccessPath.GIT);
} finally {
sshScope.set(old);
}
} }
private SshSession newSession() { private SshSession newSession() {
@ -78,7 +85,6 @@ public abstract class AbstractGitCommand extends BaseCommand {
session, session,
session.getRemoteAddress(), session.getRemoteAddress(),
userFactory.create(session.getRemoteAddress(), user.getAccountId())); userFactory.create(session.getRemoteAddress(), user.getAccountId()));
n.setAccessPath(AccessPath.GIT);
return n; return n;
} }

View File

@ -24,6 +24,7 @@ import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.registration.DynamicMap; import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.AccessPath;
import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.DynamicOptions; import com.google.gerrit.server.DynamicOptions;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
@ -49,7 +50,6 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Optional;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -261,25 +261,27 @@ public abstract class BaseCommand implements Command {
} }
/** /**
* Spawn a function into its own thread with the provided context. * Spawn a function into its own thread.
* *
* <p>Typically this should be invoked within {@link Command#start(Environment)}, such as: * <p>Typically this should be invoked within {@link Command#start(Environment)}, such as:
* *
* <pre> * <pre>
* startThreadWithContext(SshScope.Context context, new CommandRunnable() { * startThread(new CommandRunnable() {
* public void run() throws Exception { * public void run() throws Exception {
* runImp(); * runImp();
* } * }
* }); * },
* accessPath);
* </pre> * </pre>
* *
* <p>If the function throws an exception, it is translated to a simple message for the client, a * <p>If the function throws an exception, it is translated to a simple message for the client, a
* non-zero exit code, and the stack trace is logged. * non-zero exit code, and the stack trace is logged.
* *
* @param thunk the runnable to execute on the thread, performing the command's logic. * @param thunk the runnable to execute on the thread, performing the command's logic.
* @param accessPath the path used by the end user for running the SSH command
*/ */
protected void startThreadWithContext(SshScope.Context context, CommandRunnable thunk) { protected void startThread(final CommandRunnable thunk, AccessPath accessPath) {
final TaskThunk tt = new TaskThunk(thunk, Optional.ofNullable(context)); final TaskThunk tt = new TaskThunk(thunk, accessPath);
if (isAdminHighPriorityCommand()) { if (isAdminHighPriorityCommand()) {
// Admin commands should not block the main work threads (there // Admin commands should not block the main work threads (there
@ -292,28 +294,6 @@ public abstract class BaseCommand implements Command {
} }
} }
/**
* Spawn a function into its own thread.
*
* <p>Typically this should be invoked within {@link Command#start(Environment)}, such as:
*
* <pre>
* startThread(new CommandRunnable() {
* public void run() throws Exception {
* runImp();
* }
* });
* </pre>
*
* <p>If the function throws an exception, it is translated to a simple message for the client, a
* non-zero exit code, and the stack trace is logged.
*
* @param thunk the runnable to execute on the thread, performing the command's logic.
*/
protected void startThread(final CommandRunnable thunk) {
startThreadWithContext(null, thunk);
}
private boolean isAdminHighPriorityCommand() { private boolean isAdminHighPriorityCommand() {
if (getClass().getAnnotation(AdminHighPriorityCommand.class) != null) { if (getClass().getAnnotation(AdminHighPriorityCommand.class) != null) {
try { try {
@ -436,21 +416,20 @@ public abstract class BaseCommand implements Command {
private final class TaskThunk implements CancelableRunnable, ProjectRunnable { private final class TaskThunk implements CancelableRunnable, ProjectRunnable {
private final CommandRunnable thunk; private final CommandRunnable thunk;
private final Context taskContext;
private final String taskName; private final String taskName;
private final AccessPath accessPath;
private Project.NameKey projectName; private Project.NameKey projectName;
private TaskThunk(CommandRunnable thunk, Optional<Context> oneOffContext) { private TaskThunk(final CommandRunnable thunk, AccessPath accessPath) {
this.thunk = thunk; this.thunk = thunk;
this.taskName = getTaskName(); this.taskName = getTaskName();
this.taskContext = oneOffContext.orElse(context); this.accessPath = accessPath;
} }
@Override @Override
public void cancel() { public void cancel() {
synchronized (this) { synchronized (this) {
final Context old = sshScope.set(taskContext); final Context old = sshScope.set(context);
try { try {
onExit(STATUS_CANCEL); onExit(STATUS_CANCEL);
} finally { } finally {
@ -465,7 +444,8 @@ public abstract class BaseCommand implements Command {
final Thread thisThread = Thread.currentThread(); final Thread thisThread = Thread.currentThread();
final String thisName = thisThread.getName(); final String thisName = thisThread.getName();
int rc = 0; int rc = 0;
final Context old = sshScope.set(taskContext); context.getSession().setAccessPath(accessPath);
final Context old = sshScope.set(context);
try { try {
context.started = TimeUtil.nowMs(); context.started = TimeUtil.nowMs();
thisThread.setName("SSH " + taskName); thisThread.setName("SSH " + taskName);

View File

@ -14,6 +14,7 @@
package com.google.gerrit.sshd; package com.google.gerrit.sshd;
import com.google.gerrit.server.AccessPath;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.apache.sshd.server.Environment; import org.apache.sshd.server.Environment;
@ -38,7 +39,8 @@ public abstract class SshCommand extends BaseCommand {
stderr.flush(); stderr.flush();
} }
} }
}); },
AccessPath.SSH_COMMAND);
} }
protected abstract void run() throws UnloggedFailure, Failure, Exception; protected abstract void run() throws UnloggedFailure, Failure, Exception;

View File

@ -24,6 +24,7 @@ package com.google.gerrit.sshd.commands;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.server.AccessPath;
import com.google.gerrit.server.tools.ToolsCatalog; import com.google.gerrit.server.tools.ToolsCatalog;
import com.google.gerrit.server.tools.ToolsCatalog.Entry; import com.google.gerrit.server.tools.ToolsCatalog.Entry;
import com.google.gerrit.sshd.BaseCommand; import com.google.gerrit.sshd.BaseCommand;
@ -82,7 +83,7 @@ final class ScpCommand extends BaseCommand {
@Override @Override
public void start(Environment env) { public void start(Environment env) {
startThread(this::runImp); startThread(this::runImp, AccessPath.SSH_COMMAND);
} }
private void runImp() { private void runImp() {