Skip to content

Commit e28b848

Browse files
author
epriestley
committed
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
1 parent caa6fdf commit e28b848

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

scripts/repository/commit_hook.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
// Figure out which user is writing the commit.
3131

3232
if ($repository->isGit() || $repository->isHg()) {
33-
$username = getenv('PHABRICATOR_USER');
33+
$username = getenv(DiffusionCommitHookEngine::ENV_USER);
3434
if (!strlen($username)) {
35-
throw new Exception(pht('usage: PHABRICATOR_USER should be defined!'));
35+
throw new Exception(
36+
pht('usage: %s should be defined!', DiffusionCommitHookEngine::ENV_USER));
3637
}
3738

3839
// TODO: If this is a Mercurial repository, the hook we're responding to
@@ -41,8 +42,8 @@
4142

4243
} else if ($repository->isSVN()) {
4344
// NOTE: In Subversion, the entire environment gets wiped so we can't read
44-
// PHABRICATOR_USER. Instead, we've set "--tunnel-user" to specify the
45-
// correct user; read this user out of the commit log.
45+
// DiffusionCommitHookEngine::ENV_USER. Instead, we've set "--tunnel-user" to
46+
// specify the correct user; read this user out of the commit log.
4647

4748
if ($argc < 4) {
4849
throw new Exception(pht('usage: commit-hook <callsign> <repo> <txn>'));
@@ -86,6 +87,16 @@
8687

8788
$engine->setStdin($stdin);
8889

90+
$remote_address = getenv(DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS);
91+
if (strlen($remote_address)) {
92+
$engine->setRemoteAddress($remote_address);
93+
}
94+
95+
$remote_protocol = getenv(DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL);
96+
if (strlen($remote_protocol)) {
97+
$engine->setRemoteProtocol($remote_protocol);
98+
}
99+
89100
try {
90101
$err = $engine->execute();
91102
} catch (DiffusionCommitHookRejectException $ex) {

src/applications/diffusion/controller/DiffusionPushLogListController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function renderResultsList(
4141
),
4242
$callsign),
4343
$this->getHandle($log->getPusherPHID())->renderLink(),
44+
$log->getRemoteAddress()
45+
? long2ip($log->getRemoteAddress())
46+
: null,
47+
$log->getRemoteProtocol(),
4448
$log->getRefType(),
4549
$log->getRefName(),
4650
$log->getRefOldShort(),
@@ -54,6 +58,8 @@ public function renderResultsList(
5458
array(
5559
pht('Repository'),
5660
pht('Pusher'),
61+
pht('From'),
62+
pht('Via'),
5763
pht('Type'),
5864
pht('Name'),
5965
pht('Old'),
@@ -62,6 +68,8 @@ public function renderResultsList(
6268
))
6369
->setColumnClasses(
6470
array(
71+
'',
72+
'',
6573
'',
6674
'',
6775
'',

src/applications/diffusion/controller/DiffusionServeController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,11 @@ private function serveGitRequest(
318318
'PATH_INFO' => $request_path,
319319

320320
'REMOTE_USER' => $viewer->getUsername(),
321-
'PHABRICATOR_USER' => $viewer->getUsername(),
322321

323322
// TODO: Set these correctly.
324323
// GIT_COMMITTER_NAME
325324
// GIT_COMMITTER_EMAIL
326-
);
325+
) + $this->getCommonEnvironment($viewer);
327326

328327
$input = PhabricatorStartup::getRawInput();
329328

@@ -416,9 +415,7 @@ private function serveMercurialRequest(
416415
throw new Exception("Unable to find `hg` in PATH!");
417416
}
418417

419-
$env = array(
420-
'PHABRICATOR_USER' => $viewer->getUsername(),
421-
);
418+
$env = $this->getCommonEnvironment($viewer);
422419
$input = PhabricatorStartup::getRawInput();
423420

424421
$cmd = $request->getStr('cmd');
@@ -557,5 +554,15 @@ private function isValidGitShallowCloneResponse($stdout, $stderr) {
557554
return $has_pack && $is_hangup;
558555
}
559556

557+
private function getCommonEnvironment(PhabricatorUser $viewer) {
558+
$remote_addr = $this->getRequest()->getRemoteAddr();
559+
560+
return array(
561+
DiffusionCommitHookEngine::ENV_USER => $viewer->getUsername(),
562+
DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS => $remote_addr,
563+
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'http',
564+
);
565+
}
566+
560567
}
561568

src/applications/diffusion/engine/DiffusionCommitHookEngine.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,35 @@
77
*/
88
final class DiffusionCommitHookEngine extends Phobject {
99

10+
const ENV_USER = 'PHABRICATOR_USER';
11+
const ENV_REMOTE_ADDRESS = 'PHABRICATOR_REMOTE_ADDRESS';
12+
const ENV_REMOTE_PROTOCOL = 'PHABRICATOR_REMOTE_PROTOCOL';
13+
1014
private $viewer;
1115
private $repository;
1216
private $stdin;
1317
private $subversionTransaction;
1418
private $subversionRepository;
19+
private $remoteAddress;
20+
private $remoteProtocol;
21+
22+
public function setRemoteProtocol($remote_protocol) {
23+
$this->remoteProtocol = $remote_protocol;
24+
return $this;
25+
}
26+
27+
public function getRemoteProtocol() {
28+
return $this->remoteProtocol;
29+
}
1530

31+
public function setRemoteAddress($remote_address) {
32+
$this->remoteAddress = $remote_address;
33+
return $this;
34+
}
35+
36+
public function getRemoteAddress() {
37+
return $this->remoteAddress;
38+
}
1639

1740
public function setSubversionTransactionInfo($transaction, $repository) {
1841
$this->subversionTransaction = $transaction;
@@ -86,13 +109,21 @@ private function executeGitHook() {
86109
$transaction_key = PhabricatorHash::digestForIndex(
87110
Filesystem::readRandomBytes(64));
88111

112+
// If whatever we have here isn't a valid IPv4 address, just store `null`.
113+
// Older versions of PHP return `-1` on failure instead of `false`.
114+
$remote_address = $this->getRemoteAddress();
115+
$remote_address = max(0, ip2long($remote_address));
116+
$remote_address = nonempty($remote_address, null);
117+
118+
$remote_protocol = $this->getRemoteProtocol();
119+
89120
$logs = array();
90121
foreach ($updates as $update) {
91122
$log = PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer())
92123
->setRepositoryPHID($this->getRepository()->getPHID())
93124
->setEpoch(time())
94-
->setRemoteAddress(null) // TODO: Populate this where possible.
95-
->setRemoteProtocol(null) // TODO: Populate this where possible.
125+
->setRemoteAddress($remote_address)
126+
->setRemoteProtocol($remote_protocol)
96127
->setTransactionKey($transaction_key)
97128
->setRefType($update['type'])
98129
->setRefNameHash(PhabricatorHash::digestForIndex($update['ref']))

src/applications/diffusion/ssh/DiffusionSSHWorkflow.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ public function getArgs() {
1818
}
1919

2020
public function getEnvironment() {
21-
return array(
22-
'PHABRICATOR_USER' => $this->getUser()->getUsername(),
21+
$env = array(
22+
DiffusionCommitHookEngine::ENV_USER => $this->getUser()->getUsername(),
23+
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh',
2324
);
25+
26+
$ssh_client = getenv('SSH_CLIENT');
27+
if ($ssh_client) {
28+
// This has the format "<ip> <remote-port> <local-port>". Grab the IP.
29+
$remote_address = head(explode(' ', $ssh_client));
30+
$env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address;
31+
}
32+
33+
return $env;
2434
}
2535

2636
abstract protected function executeRepositoryOperations();

0 commit comments

Comments
 (0)