Skip to content

Commit 3d2ce66

Browse files
authored
Merge pull request #635 from catalyst/add-token-expiry-check
Add token expiry check
2 parents 6388430 + d97914c commit 3d2ce66

29 files changed

Lines changed: 341 additions & 44 deletions

classes/check/token_expiry.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_objectfs\check;
18+
19+
use core\check\check;
20+
use core\check\result;
21+
22+
/**
23+
* Token expiry check.
24+
*
25+
* @package tool_objectfs
26+
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
27+
* @copyright Catalyst IT
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class token_expiry extends check {
31+
/**
32+
* Checks the token expiry time against thresholds
33+
* @return result
34+
*/
35+
public function get_result(): result {
36+
$config = \tool_objectfs\local\manager::get_objectfs_config();
37+
$client = \tool_objectfs\local\manager::get_client($config);
38+
39+
// No client set - n/a.
40+
if (empty($client)) {
41+
return new result(result::NA, get_string('check:tokenexpiry:na', 'tool_objectfs'));
42+
}
43+
44+
$expirytime = $client->get_token_expiry_time();
45+
$secondsleft = $expirytime - time();
46+
47+
$strparams = [
48+
'dayssince' => abs(round($secondsleft / DAYSECS)),
49+
'time' => userdate($expirytime),
50+
];
51+
52+
// Not implemented or token not set - n/a.
53+
if ($expirytime == -1) {
54+
return new result(result::NA, get_string('check:tokenexpiry:na', 'tool_objectfs'));
55+
}
56+
57+
// Is in past - token has expired.
58+
if ($secondsleft < 0) {
59+
return new result(result::CRITICAL, get_string('check:tokenexpiry:expired', 'tool_objectfs', $strparams));
60+
}
61+
62+
// Is in warning period - warn.
63+
$warnthreshold = (int) $config->tokenexpirywarnperiod;
64+
if ($secondsleft < $warnthreshold) {
65+
return new result(result::WARNING, get_string('check:tokenexpiry:expiresin', 'tool_objectfs', $strparams));
66+
}
67+
68+
// Else ok.
69+
return new result(result::OK, get_string('check:tokenexpiry:expiresin', 'tool_objectfs', $strparams));
70+
}
71+
}

classes/local/manager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ public static function get_available_fs_list() {
329329
* @return string
330330
*/
331331
public static function get_client_classname_from_fs($filesystem) {
332+
// Unit tests need to return the test client.
333+
if ($filesystem == '\tool_objectfs\tests\test_file_system') {
334+
return '\tool_objectfs\tests\test_client';
335+
}
332336
$clientclass = str_replace('_file_system', '', $filesystem);
333337
return str_replace('tool_objectfs\\', 'tool_objectfs\\local\\store\\', $clientclass.'\\client');
334338
}

classes/local/object_manipulator/candidates/manipulator_candidates.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17+
namespace tool_objectfs\local\object_manipulator\candidates;
18+
19+
use dml_exception;
20+
1721
/**
1822
* Interface manipulator_candidates
1923
* @package tool_objectfs
2024
* @author Gleimer Mora <gleimermora@catalyst-au.net>
2125
* @copyright Catalyst IT
2226
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2327
*/
24-
25-
namespace tool_objectfs\local\object_manipulator\candidates;
26-
27-
use dml_exception;
28-
2928
interface manipulator_candidates {
3029

3130
/**

classes/local/object_manipulator/object_manipulator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17+
namespace tool_objectfs\local\object_manipulator;
18+
19+
use stdClass;
20+
1721
/**
1822
* Object manipulator interface class.
1923
*
@@ -22,11 +26,6 @@
2226
* @copyright Catalyst IT
2327
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2428
*/
25-
26-
namespace tool_objectfs\local\object_manipulator;
27-
28-
use stdClass;
29-
3029
interface object_manipulator {
3130

3231

classes/local/store/azure/client.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
namespace tool_objectfs\local\store\azure;
2727

28+
use admin_setting_description;
2829
use SimpleXMLElement;
2930
use stdClass;
31+
use tool_objectfs\check\token_expiry;
3032
use tool_objectfs\local\store\azure\stream_wrapper;
3133
use tool_objectfs\local\store\object_client_base;
3234

@@ -360,9 +362,42 @@ public function define_client_section($settings, $config) {
360362
new \lang_string('settings:azure:sastoken', 'tool_objectfs'),
361363
new \lang_string('settings:azure:sastoken_help', 'tool_objectfs'), ''));
362364

365+
// Admin_setting_check only exists in 4.5+, in lower versions fallback to a basic description.
366+
if (class_exists('admin_setting_check')) {
367+
$settings->add(new admin_setting_check('tool_objectfs/check_tokenexpiry', new token_expiry(), true));
368+
} else {
369+
$summary = (new token_expiry())->get_result()->get_summary();
370+
$settings->add(new admin_setting_description('tool_objectfs/tokenexpirycheckresult',
371+
get_string('checktoken_expiry', 'tool_objectfs'), $summary));
372+
}
373+
363374
return $settings;
364375
}
365376

377+
/**
378+
* Returns token expiry time
379+
* @return int
380+
*/
381+
public function get_token_expiry_time(): int {
382+
if (empty($this->config->azure_sastoken)) {
383+
return -1;
384+
}
385+
386+
// Parse the sas token (it just uses url parameter encoding).
387+
$parts = [];
388+
parse_str($this->config->azure_sastoken, $parts);
389+
390+
// Get the 'se' part (signed expiry).
391+
if (!isset($parts['se'])) {
392+
// Assume expired (malformed).
393+
return 0;
394+
}
395+
396+
// Parse timestamp string into unix timestamp int.
397+
$expirystr = $parts['se'];
398+
return strtotime($expirystr);
399+
}
400+
366401
/**
367402
* Extract an error code from the XML response.
368403
*

classes/local/store/object_client.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17+
namespace tool_objectfs\local\store;
18+
1719
/**
1820
* Objectfs client interface.
1921
*
@@ -22,11 +24,7 @@
2224
* @copyright Catalyst IT
2325
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2426
*/
25-
26-
namespace tool_objectfs\local\store;
27-
2827
interface object_client {
29-
3028
/**
3129
* construct
3230
* @param \stdClass $config
@@ -137,6 +135,12 @@ public function proxy_range_request(\stored_file $file, $ranges);
137135
*/
138136
public function test_range_request($filesystem);
139137

138+
/**
139+
* Get the expiry time of the token used for this fs.
140+
* returns -1 if not implemented, or no token is set.
141+
* @return int unix timestamp the token set expires at
142+
*/
143+
public function get_token_expiry_time(): int;
140144
}
141145

142146

classes/local/store/object_client_base.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,13 @@ public function test_connection() {
187187
public function test_permissions($testdelete) {
188188
return (object)['success' => false, 'details' => ''];
189189
}
190+
191+
/**
192+
* Return expiry time of token, default is -1 meaning not implemented/enabled.
193+
* @return int
194+
*/
195+
public function get_token_expiry_time(): int {
196+
// Returning -1 = not implemented.
197+
return -1;
198+
}
190199
}

classes/privacy/provider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
1617
/**
1718
* Privacy provider.
1819
*

classes/task/objectfs_task.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17+
namespace tool_objectfs\task;
18+
1719
/**
1820
* Base abstract class for objectfs tasks.
1921
*
@@ -22,9 +24,6 @@
2224
* @copyright Catalyst IT
2325
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2426
*/
25-
26-
namespace tool_objectfs\task;
27-
2827
interface objectfs_task {
2928

3029
/**

classes/tests/test_azure_integration_client.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
use tool_objectfs\local\store\azure\client;
2020

2121
/**
22-
* [Description test_azure_integration_client]
23-
* @package tool_objectfs
22+
* Client used for integration testing azure client
23+
*
24+
* @package tool_objectfs
25+
* @copyright Catalyst IT
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2427
*/
2528
class test_azure_integration_client extends client {
2629

@@ -35,7 +38,10 @@ class test_azure_integration_client extends client {
3538
* @return void
3639
*/
3740
public function __construct($config) {
38-
parent::__construct($config);
41+
// Set config directly. Calling __construct will do nothing
42+
// since unit tests do not have the azure sdk installed.
43+
$this->config = $config;
44+
3945
$time = microtime();
4046
$this->runidentifier = md5($time);
4147
}

0 commit comments

Comments
 (0)