Skip to content

Commit 272046a

Browse files
author
epriestley
committed
Write a basic SSH pull log for Git
Summary: Ref T11766. When users run `git pull` or similar, log the operation in the pull log. Test Plan: Performed SSH pulls, got a log in the database. Today, this event log is purely diagnostic and has no UI. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11766 Differential Revision: https://secure.phabricator.com/D16738
1 parent c364421 commit 272046a

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/applications/diffusion/ssh/DiffusionGitUploadPackSSHWorkflow.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ protected function executeRepositoryOperations() {
1919
$device = AlmanacKeys::getLiveDevice();
2020

2121
$skip_sync = $this->shouldSkipReadSynchronization();
22+
$is_proxy = $this->shouldProxy();
2223

23-
if ($this->shouldProxy()) {
24+
if ($is_proxy) {
2425
$command = $this->getProxyCommand();
2526

2627
if ($device) {
@@ -48,6 +49,8 @@ protected function executeRepositoryOperations() {
4849
}
4950
$command = PhabricatorDaemon::sudoCommandAsDaemonUser($command);
5051

52+
$pull_event = $this->newPullEvent();
53+
5154
$future = id(new ExecFuture('%C', $command))
5255
->setEnv($this->getEnvironment());
5356

@@ -56,6 +59,26 @@ protected function executeRepositoryOperations() {
5659
->setCommandChannelFromExecFuture($future)
5760
->execute();
5861

62+
if ($err) {
63+
$pull_event
64+
->setResultType('error')
65+
->setResultCode($err);
66+
} else {
67+
$pull_event
68+
->setResultType('pull')
69+
->setResultCode(0);
70+
}
71+
72+
// TODO: Currently, when proxying, we do not write a log on the proxy.
73+
// Perhaps we should write a "proxy log". This is not very useful for
74+
// statistics or auditing, but could be useful for diagnostics. Marking
75+
// the proxy logs as proxied (and recording devicePHID on all logs) would
76+
// make differentiating between these use cases easier.
77+
78+
if (!$is_proxy) {
79+
$pull_event->save();
80+
}
81+
5982
if (!$err) {
6083
$this->waitForGitClient();
6184
}

src/applications/diffusion/ssh/DiffusionSSHWorkflow.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public function getEnvironment() {
3030
DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh',
3131
);
3232

33-
$ssh_client = getenv('SSH_CLIENT');
34-
if ($ssh_client) {
35-
// This has the format "<ip> <remote-port> <local-port>". Grab the IP.
36-
$remote_address = head(explode(' ', $ssh_client));
33+
$remote_address = $this->getSSHRemoteAddress();
34+
if ($remote_address !== null) {
3735
$env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address;
3836
}
3937

@@ -259,5 +257,17 @@ protected function shouldSkipReadSynchronization() {
259257
return false;
260258
}
261259

260+
protected function newPullEvent() {
261+
$viewer = $this->getViewer();
262+
$repository = $this->getRepository();
263+
$remote_address = $this->getSSHRemoteAddress();
264+
265+
return id(new PhabricatorRepositoryPullEvent())
266+
->setEpoch(PhabricatorTime::getNow())
267+
->setRemoteAddress($remote_address)
268+
->setRemoteProtocol('ssh')
269+
->setPullerPHID($viewer->getPHID())
270+
->setRepositoryPHID($repository->getPHID());
271+
}
262272

263273
}

src/infrastructure/ssh/PhabricatorSSHWorkflow.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,19 @@ public function getOriginalArguments() {
8383
return $this->originalArguments;
8484
}
8585

86+
public function getSSHRemoteAddress() {
87+
$ssh_client = getenv('SSH_CLIENT');
88+
if (!strlen($ssh_client)) {
89+
return null;
90+
}
91+
92+
// TODO: When commands are proxied, the original remote address should
93+
// also be proxied.
94+
95+
// This has the format "<ip> <remote-port> <local-port>". Grab the IP.
96+
$remote_address = head(explode(' ', $ssh_client));
97+
98+
return $remote_address;
99+
}
100+
86101
}

0 commit comments

Comments
 (0)