1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-28 17:52:43 +01:00

Store pusher remote address and push protocol in PushLog

Summary: Ref T4195. Stores remote address and protocol in the logs, where possible.

Test Plan: Pushed some stuff, looked at the log, saw data.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4195

Differential Revision: https://secure.phabricator.com/D7711
This commit is contained in:
epriestley 2013-12-05 11:59:22 -08:00
parent caa6fdf56d
commit e28b848ab2
5 changed files with 80 additions and 13 deletions

View file

@ -30,9 +30,10 @@ $engine->setRepository($repository);
// Figure out which user is writing the commit. // Figure out which user is writing the commit.
if ($repository->isGit() || $repository->isHg()) { if ($repository->isGit() || $repository->isHg()) {
$username = getenv('PHABRICATOR_USER'); $username = getenv(DiffusionCommitHookEngine::ENV_USER);
if (!strlen($username)) { if (!strlen($username)) {
throw new Exception(pht('usage: PHABRICATOR_USER should be defined!')); throw new Exception(
pht('usage: %s should be defined!', DiffusionCommitHookEngine::ENV_USER));
} }
// TODO: If this is a Mercurial repository, the hook we're responding to // TODO: If this is a Mercurial repository, the hook we're responding to
@ -41,8 +42,8 @@ if ($repository->isGit() || $repository->isHg()) {
} else if ($repository->isSVN()) { } else if ($repository->isSVN()) {
// NOTE: In Subversion, the entire environment gets wiped so we can't read // NOTE: In Subversion, the entire environment gets wiped so we can't read
// PHABRICATOR_USER. Instead, we've set "--tunnel-user" to specify the // DiffusionCommitHookEngine::ENV_USER. Instead, we've set "--tunnel-user" to
// correct user; read this user out of the commit log. // specify the correct user; read this user out of the commit log.
if ($argc < 4) { if ($argc < 4) {
throw new Exception(pht('usage: commit-hook <callsign> <repo> <txn>')); throw new Exception(pht('usage: commit-hook <callsign> <repo> <txn>'));
@ -86,6 +87,16 @@ if ($repository->isHg()) {
$engine->setStdin($stdin); $engine->setStdin($stdin);
$remote_address = getenv(DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS);
if (strlen($remote_address)) {
$engine->setRemoteAddress($remote_address);
}
$remote_protocol = getenv(DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL);
if (strlen($remote_protocol)) {
$engine->setRemoteProtocol($remote_protocol);
}
try { try {
$err = $engine->execute(); $err = $engine->execute();
} catch (DiffusionCommitHookRejectException $ex) { } catch (DiffusionCommitHookRejectException $ex) {

View file

@ -41,6 +41,10 @@ final class DiffusionPushLogListController extends DiffusionController
), ),
$callsign), $callsign),
$this->getHandle($log->getPusherPHID())->renderLink(), $this->getHandle($log->getPusherPHID())->renderLink(),
$log->getRemoteAddress()
? long2ip($log->getRemoteAddress())
: null,
$log->getRemoteProtocol(),
$log->getRefType(), $log->getRefType(),
$log->getRefName(), $log->getRefName(),
$log->getRefOldShort(), $log->getRefOldShort(),
@ -54,6 +58,8 @@ final class DiffusionPushLogListController extends DiffusionController
array( array(
pht('Repository'), pht('Repository'),
pht('Pusher'), pht('Pusher'),
pht('From'),
pht('Via'),
pht('Type'), pht('Type'),
pht('Name'), pht('Name'),
pht('Old'), pht('Old'),
@ -62,6 +68,8 @@ final class DiffusionPushLogListController extends DiffusionController
)) ))
->setColumnClasses( ->setColumnClasses(
array( array(
'',
'',
'', '',
'', '',
'', '',

View file

@ -318,12 +318,11 @@ final class DiffusionServeController extends DiffusionController {
'PATH_INFO' => $request_path, 'PATH_INFO' => $request_path,
'REMOTE_USER' => $viewer->getUsername(), 'REMOTE_USER' => $viewer->getUsername(),
'PHABRICATOR_USER' => $viewer->getUsername(),
// TODO: Set these correctly. // TODO: Set these correctly.
// GIT_COMMITTER_NAME // GIT_COMMITTER_NAME
// GIT_COMMITTER_EMAIL // GIT_COMMITTER_EMAIL
); ) + $this->getCommonEnvironment($viewer);
$input = PhabricatorStartup::getRawInput(); $input = PhabricatorStartup::getRawInput();
@ -416,9 +415,7 @@ final class DiffusionServeController extends DiffusionController {
throw new Exception("Unable to find `hg` in PATH!"); throw new Exception("Unable to find `hg` in PATH!");
} }
$env = array( $env = $this->getCommonEnvironment($viewer);
'PHABRICATOR_USER' => $viewer->getUsername(),
);
$input = PhabricatorStartup::getRawInput(); $input = PhabricatorStartup::getRawInput();
$cmd = $request->getStr('cmd'); $cmd = $request->getStr('cmd');
@ -557,5 +554,15 @@ final class DiffusionServeController extends DiffusionController {
return $has_pack && $is_hangup; return $has_pack && $is_hangup;
} }
private function getCommonEnvironment(PhabricatorUser $viewer) {
$remote_addr = $this->getRequest()->getRemoteAddr();
return array(
DiffusionCommitHookEngine::ENV_USER => $viewer->getUsername(),
DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS => $remote_addr,
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'http',
);
}
} }

View file

@ -7,12 +7,35 @@
*/ */
final class DiffusionCommitHookEngine extends Phobject { final class DiffusionCommitHookEngine extends Phobject {
const ENV_USER = 'PHABRICATOR_USER';
const ENV_REMOTE_ADDRESS = 'PHABRICATOR_REMOTE_ADDRESS';
const ENV_REMOTE_PROTOCOL = 'PHABRICATOR_REMOTE_PROTOCOL';
private $viewer; private $viewer;
private $repository; private $repository;
private $stdin; private $stdin;
private $subversionTransaction; private $subversionTransaction;
private $subversionRepository; private $subversionRepository;
private $remoteAddress;
private $remoteProtocol;
public function setRemoteProtocol($remote_protocol) {
$this->remoteProtocol = $remote_protocol;
return $this;
}
public function getRemoteProtocol() {
return $this->remoteProtocol;
}
public function setRemoteAddress($remote_address) {
$this->remoteAddress = $remote_address;
return $this;
}
public function getRemoteAddress() {
return $this->remoteAddress;
}
public function setSubversionTransactionInfo($transaction, $repository) { public function setSubversionTransactionInfo($transaction, $repository) {
$this->subversionTransaction = $transaction; $this->subversionTransaction = $transaction;
@ -86,13 +109,21 @@ final class DiffusionCommitHookEngine extends Phobject {
$transaction_key = PhabricatorHash::digestForIndex( $transaction_key = PhabricatorHash::digestForIndex(
Filesystem::readRandomBytes(64)); Filesystem::readRandomBytes(64));
// If whatever we have here isn't a valid IPv4 address, just store `null`.
// Older versions of PHP return `-1` on failure instead of `false`.
$remote_address = $this->getRemoteAddress();
$remote_address = max(0, ip2long($remote_address));
$remote_address = nonempty($remote_address, null);
$remote_protocol = $this->getRemoteProtocol();
$logs = array(); $logs = array();
foreach ($updates as $update) { foreach ($updates as $update) {
$log = PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer()) $log = PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer())
->setRepositoryPHID($this->getRepository()->getPHID()) ->setRepositoryPHID($this->getRepository()->getPHID())
->setEpoch(time()) ->setEpoch(time())
->setRemoteAddress(null) // TODO: Populate this where possible. ->setRemoteAddress($remote_address)
->setRemoteProtocol(null) // TODO: Populate this where possible. ->setRemoteProtocol($remote_protocol)
->setTransactionKey($transaction_key) ->setTransactionKey($transaction_key)
->setRefType($update['type']) ->setRefType($update['type'])
->setRefNameHash(PhabricatorHash::digestForIndex($update['ref'])) ->setRefNameHash(PhabricatorHash::digestForIndex($update['ref']))

View file

@ -18,9 +18,19 @@ abstract class DiffusionSSHWorkflow extends PhabricatorSSHWorkflow {
} }
public function getEnvironment() { public function getEnvironment() {
return array( $env = array(
'PHABRICATOR_USER' => $this->getUser()->getUsername(), DiffusionCommitHookEngine::ENV_USER => $this->getUser()->getUsername(),
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh',
); );
$ssh_client = getenv('SSH_CLIENT');
if ($ssh_client) {
// This has the format "<ip> <remote-port> <local-port>". Grab the IP.
$remote_address = head(explode(' ', $ssh_client));
$env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address;
}
return $env;
} }
abstract protected function executeRepositoryOperations(); abstract protected function executeRepositoryOperations();