diff options
author | Bjoern Schiessle <bjoern@schiessle.org> | 2017-07-28 14:50:40 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-08-01 10:07:53 +0200 |
commit | 06b9e580814cffdc2567686cd9aac80e7418bc26 (patch) | |
tree | 27ff4d05838b7fe9f5752c0ac16d916fea815013 | |
parent | 51d85eb2f84675f292279fc4d5977c958620e802 (diff) | |
download | nextcloud-server-06b9e580814cffdc2567686cd9aac80e7418bc26.tar.gz nextcloud-server-06b9e580814cffdc2567686cd9aac80e7418bc26.zip |
expire requestSharedSecret job after 30 days
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
-rw-r--r-- | apps/federation/lib/BackgroundJob/RequestSharedSecret.php | 41 | ||||
-rw-r--r-- | apps/federation/lib/TrustedServers.php | 7 | ||||
-rw-r--r-- | apps/federation/tests/TrustedServersTest.php | 5 |
3 files changed, 48 insertions, 5 deletions
diff --git a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php index 77d0234ef74..8a4bd8c4996 100644 --- a/apps/federation/lib/BackgroundJob/RequestSharedSecret.php +++ b/apps/federation/lib/BackgroundJob/RequestSharedSecret.php @@ -76,6 +76,9 @@ class RequestSharedSecret extends Job { private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; + /** @var int 30 day = 2592000sec */ + private $maxLifespan = 2592000; + /** * RequestSharedSecret constructor. * @@ -129,8 +132,10 @@ class RequestSharedSecret extends Job { $this->parentExecute($jobList, $logger); } - if (!$this->retainJob) { - $jobList->remove($this, $this->argument); + $jobList->remove($this, $this->argument); + + if ($this->retainJob) { + $this->reAddJob($jobList, $this->argument); } } @@ -147,10 +152,20 @@ class RequestSharedSecret extends Job { protected function run($argument) { $target = $argument['url']; + $created = isset($argument['created']) ? (int)$argument['created'] : time(); + $currentTime = time(); $source = $this->urlGenerator->getAbsoluteURL('/'); $source = rtrim($source, '/'); $token = $argument['token']; + // kill job after 30 days of trying + $deadline = $currentTime - $this->maxLifespan; + if ($created < $deadline) { + $this->retainJob = false; + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); + return; + } + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; @@ -198,4 +213,26 @@ class RequestSharedSecret extends Job { } } + + /** + * re-add background job + * + * @param IJobList $jobList + * @param array $argument + */ + protected function reAddJob(IJobList $jobList, array $argument) { + + $url = $argument['url']; + $created = isset($argument['created']) ? (int)$argument['created'] : time(); + $token = $argument['token']; + + $jobList->add( + RequestSharedSecret::class, + [ + 'url' => $url, + 'token' => $token, + 'created' => $created + ] + ); + } } diff --git a/apps/federation/lib/TrustedServers.php b/apps/federation/lib/TrustedServers.php index 9bf1452eab3..de7d2a1df9a 100644 --- a/apps/federation/lib/TrustedServers.php +++ b/apps/federation/lib/TrustedServers.php @@ -111,7 +111,8 @@ class TrustedServers { 'OCA\Federation\BackgroundJob\RequestSharedSecret', [ 'url' => $url, - 'token' => $token + 'token' => $token, + 'created' => $this->getTimestamp() ] ); } @@ -275,4 +276,8 @@ class TrustedServers { return 'https://' . $url; } + + protected function getTimestamp() { + return time(); + } } diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php index 598c2f01c90..ee63faa3ba5 100644 --- a/apps/federation/tests/TrustedServersTest.php +++ b/apps/federation/tests/TrustedServersTest.php @@ -117,10 +117,11 @@ class TrustedServersTest extends TestCase { $this->dispatcher ] ) - ->setMethods(['normalizeUrl', 'updateProtocol']) + ->setMethods(['normalizeUrl', 'updateProtocol', 'getTimestamp']) ->getMock(); $trustedServers->expects($this->once())->method('updateProtocol') ->with('url')->willReturn('https://url'); + $trustedServers->expects($this->any())->method('getTimestamp')->willReturn(1234567); $this->dbHandler->expects($this->once())->method('addServer')->with('https://url') ->willReturn($success); @@ -130,7 +131,7 @@ class TrustedServersTest extends TestCase { $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); $this->jobList->expects($this->once())->method('add') ->with('OCA\Federation\BackgroundJob\RequestSharedSecret', - ['url' => 'https://url', 'token' => 'token']); + ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]); } else { $this->jobList->expects($this->never())->method('add'); } |