Generalize SSH passthru for repository hosting
Summary:
Ref T2230. In Git, we can determine if a command is read-only or read/write from the command itself, but this isn't the case in Mercurial or SVN.
For Mercurial and SVN, we need to proxy the protocol that's coming over the wire, look at each request from the client, and then check if it's a read or a write. To support this, provide a more flexible version of `passthruIO`.
The way this will work is:
- The SSH IO channel is wrapped in a `ProtocolChannel` which can parse the the incoming stream into message objects.
- The `willWriteCallback` will look at those messages and determine if they're reads or writes.
- If they're writes, it will check for write permission.
- If we're good to go, the message object is converted back into a byte stream and handed to the underlying command.
Test Plan: Executed `git clone`, `git clone --depth 3`, `git push` (against no-write repo, got error), `git push` (against valid repo).
Reviewers: btrahan
Reviewed By: btrahan
CC: hach-que, asherkin, aran
Maniphest Tasks: T2230
Differential Revision: https://secure.phabricator.com/D7551
This commit is contained in:
@@ -14,10 +14,6 @@ final class DiffusionSSHGitReceivePackWorkflow
|
||||
));
|
||||
}
|
||||
|
||||
public function isReadOnly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getRequestPath() {
|
||||
$args = $this->getArgs();
|
||||
return head($args->getArg('dir'));
|
||||
@@ -25,10 +21,17 @@ final class DiffusionSSHGitReceivePackWorkflow
|
||||
|
||||
protected function executeRepositoryOperations(
|
||||
PhabricatorRepository $repository) {
|
||||
|
||||
// This is a write, and must have write access.
|
||||
$this->requireWriteAccess();
|
||||
|
||||
$future = new ExecFuture(
|
||||
'git-receive-pack %s',
|
||||
$repository->getLocalPath());
|
||||
$err = $this->passthruIO($future);
|
||||
$err = $this->newPassthruCommand()
|
||||
->setIOChannel($this->getIOChannel())
|
||||
->setCommandChannelFromExecFuture($future)
|
||||
->execute();
|
||||
|
||||
if (!$err) {
|
||||
$repository->writeStatusMessage(
|
||||
|
||||
@@ -14,10 +14,6 @@ final class DiffusionSSHGitUploadPackWorkflow
|
||||
));
|
||||
}
|
||||
|
||||
public function isReadOnly() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getRequestPath() {
|
||||
$args = $this->getArgs();
|
||||
return head($args->getArg('dir'));
|
||||
@@ -28,7 +24,10 @@ final class DiffusionSSHGitUploadPackWorkflow
|
||||
|
||||
$future = new ExecFuture('git-upload-pack %s', $repository->getLocalPath());
|
||||
|
||||
return $this->passthruIO($future);
|
||||
return $this->newPassthruCommand()
|
||||
->setIOChannel($this->getIOChannel())
|
||||
->setCommandChannelFromExecFuture($future)
|
||||
->execute();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,12 +3,17 @@
|
||||
abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow {
|
||||
|
||||
private $args;
|
||||
private $repository;
|
||||
private $hasWriteAccess;
|
||||
|
||||
public function getRepository() {
|
||||
return $this->repository;
|
||||
}
|
||||
|
||||
public function getArgs() {
|
||||
return $this->args;
|
||||
}
|
||||
|
||||
abstract protected function isReadOnly();
|
||||
abstract protected function getRequestPath();
|
||||
abstract protected function executeRepositoryOperations(
|
||||
PhabricatorRepository $repository);
|
||||
@@ -23,6 +28,7 @@ abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow {
|
||||
|
||||
try {
|
||||
$repository = $this->loadRepository();
|
||||
$this->repository = $repository;
|
||||
return $this->executeRepositoryOperations($repository);
|
||||
} catch (Exception $ex) {
|
||||
$this->writeError(get_class($ex).': '.$ex->getMessage());
|
||||
@@ -56,26 +62,11 @@ abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow {
|
||||
pht('No repository "%s" exists!', $callsign));
|
||||
}
|
||||
|
||||
$is_push = !$this->isReadOnly();
|
||||
|
||||
switch ($repository->getServeOverSSH()) {
|
||||
case PhabricatorRepository::SERVE_READONLY:
|
||||
if ($is_push) {
|
||||
throw new Exception(
|
||||
pht('This repository is read-only over SSH.'));
|
||||
}
|
||||
break;
|
||||
case PhabricatorRepository::SERVE_READWRITE:
|
||||
if ($is_push) {
|
||||
$can_push = PhabricatorPolicyFilter::hasCapability(
|
||||
$viewer,
|
||||
$repository,
|
||||
DiffusionCapabilityPush::CAPABILITY);
|
||||
if (!$can_push) {
|
||||
throw new Exception(
|
||||
pht('You do not have permission to push to this repository.'));
|
||||
}
|
||||
}
|
||||
// If we have read or read/write access, proceed for now. We will
|
||||
// check write access when the user actually issues a write command.
|
||||
break;
|
||||
case PhabricatorRepository::SERVE_OFF:
|
||||
default:
|
||||
@@ -86,4 +77,40 @@ abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow {
|
||||
return $repository;
|
||||
}
|
||||
|
||||
protected function requireWriteAccess() {
|
||||
if ($this->hasWriteAccess === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
$repository = $this->getRepository();
|
||||
$viewer = $this->getUser();
|
||||
|
||||
switch ($repository->getServeOverSSH()) {
|
||||
case PhabricatorRepository::SERVE_READONLY:
|
||||
throw new Exception(
|
||||
pht('This repository is read-only over SSH.'));
|
||||
break;
|
||||
case PhabricatorRepository::SERVE_READWRITE:
|
||||
$can_push = PhabricatorPolicyFilter::hasCapability(
|
||||
$viewer,
|
||||
$repository,
|
||||
DiffusionCapabilityPush::CAPABILITY);
|
||||
if (!$can_push) {
|
||||
throw new Exception(
|
||||
pht('You do not have permission to push to this repository.'));
|
||||
}
|
||||
break;
|
||||
case PhabricatorRepository::SERVE_OFF:
|
||||
default:
|
||||
// This shouldn't be reachable because we don't get this far if the
|
||||
// repository isn't enabled, but kick them out anyway.
|
||||
throw new Exception(
|
||||
pht('This repository is not available over SSH.'));
|
||||
}
|
||||
|
||||
$this->hasWriteAccess = true;
|
||||
return $this->hasWriteAccess;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user