Skip to content

Commit

Permalink
tagging: switch to admin setting for tagging environment
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Oct 1, 2024
1 parent 93c2d88 commit 8dd940a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions TAGGING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You must allow `s3:GetObjectTagging` and `s3:PutObjectTagging` permission to the
## Sources
The following sources are implemented currently:
### Environment
What environment the file was uploaded in. Configure the environment using `$CFG->objectfs_environment_name`
What environment the file was uploaded in. Configure the environment using `taggingenvironment` in the objectfs plugin settings.

This tag is also used by objectfs to determine if tags can be overwritten. See [Multiple environments setup](#multiple-environments-setup) for more information.

Expand All @@ -30,7 +30,7 @@ Either `orphan` if the file no longer exists in the `files` table in Moodle, oth
This feature is designed to work in situations where multiple environments (e.g. prod, staging) points to the same bucket, however, some setup is needed:

1. Turn off `overwriteobjecttags` in every environment except the production environment.
2. Configure `$CFG->objectfs_environment_name` to be unique for all environments.
2. Configure `taggingenvironment` to be unique for all environments.

By doing the above two steps, it will allow the production environment to always set its own tags, even if a file was first uploaded to staging and then to production.

Expand Down
8 changes: 4 additions & 4 deletions classes/local/tag/environment_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public static function get_description(): string {
* @return string|null string if set, else null
*/
private static function get_env(): ?string {
global $CFG;
$value = get_config('tool_objectfs', 'taggingenvironment');

if (empty($CFG->objectfs_environment_name)) {
if (empty($value)) {
return null;
}

// Must never be greater than 128, unlikely, but we must enforce this.
if (strlen($CFG->objectfs_environment_name) > 128) {
if (strlen($value) > 128) {
throw new moodle_exception('tagsource:environment:toolong', 'tool_objectfs');
}

return $CFG->objectfs_environment_name;
return $value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/tests/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function setUp(): void {
global $CFG;
$CFG->alternative_file_system_class = '\\tool_objectfs\\tests\\test_file_system';
$CFG->forced_plugin_settings['tool_objectfs']['deleteexternal'] = false;
$CFG->objectfs_environment_name = 'test';
set_config('taggingenvironment', 'test', 'tool_objectfs');
$this->filesystem = new test_file_system();
$this->logger = new \tool_objectfs\log\null_logger();

Expand Down
4 changes: 3 additions & 1 deletion lang/en/tool_objectfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@
$string['check:tokenexpiry:na'] = 'Token expired not implemented for filesystem, or no token is set';
$string['settings:tokenexpirywarnperiod'] = 'Token expiry warn period';

$string['settings:taggingenvironment'] = 'Tagging environment';
$string['settings:taggingenvironment:desc'] = 'The \'environment\' tag value. Used to distinguish the source of objects when multiple environments share a single bucket.';
$string['settings:taggingheader'] = 'Tagging settings';
$string['settings:taggingenabled'] = 'Tagging enabled';
$string['settings:maxtaggingperrun'] = 'Object tagging adhoc sync maximum objects per run';
Expand All @@ -302,7 +304,7 @@
$string['check:tagging:na'] = 'Tagging not enabled or is not supported by file system';
$string['check:tagging:error'] = 'Error trying to tag object';

$string['tagsource:environment'] = 'Environment defined by $CFG->objectfs_environment_name, currently: "{$a}".';
$string['tagsource:environment'] = 'Environment defined by the "taggingenvironment" setting, currently: "{$a}".';
$string['tagsource:environment:toolong'] = 'The value defined in objectfs_environment_name is too long. It must be < 128 chars';
$string['tagsource:location'] = 'Location of file, either "orphan" or "active".';

Expand Down
7 changes: 7 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@
$settings->add(new admin_setting_configcheckbox('tool_objectfs/taggingenabled',
new lang_string('settings:taggingenabled', 'tool_objectfs'), '', 0));

$settings->add(new admin_setting_configtext('tool_objectfs/taggingenvironment',
new lang_string('settings:taggingenvironment', 'tool_objectfs'),
get_string('settings:taggingenvironment:desc', 'tool_objectfs'),
'',
PARAM_TEXT
));

$settings->add(new admin_setting_description('tool_objectfs/tagsources',
new lang_string('settings:tagsources', 'tool_objectfs'),
tag_manager::get_tag_source_summary_html()
Expand Down
6 changes: 4 additions & 2 deletions tests/local/tagging_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,10 @@ public function test_get_sync_status_string_does_not_exist() {
*/
public function test_environment_source_too_long() {
global $CFG;
$CFG->objectfs_environment_name = 'This is a really long string.
It needs to be long because it needs to be more than 128 chars for the test to trigger an exception.';
set_config('taggingenvironment', 'This is a really long string.
It needs to be long because it needs to be more than 128 chars for the test to trigger an exception.',
'tool_objectfs');

$source = new environment_source();

$this->expectException(moodle_exception::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/object_file_system_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ public function test_push_object_tags_replicated(string $objectenv, string $push
bool $expectedoverride) {
global $CFG, $DB;
$CFG->phpunit_objectfs_supports_object_tagging = true;
$CFG->objectfs_environment_name = $objectenv;
set_config('taggingenvironment', $objectenv, 'tool_objectfs');

set_config('overwriteobjecttags', $canoverride, 'tool_objectfs');
$this->assertEquals($canoverride, tag_manager::can_overwrite_object_tags());
Expand All @@ -1129,7 +1129,7 @@ public function test_push_object_tags_replicated(string $objectenv, string $push
$localtags = $DB->get_records('tool_objectfs_object_tags', ['objectid' => $object->id]);
$this->assertCount(0, $localtags);

$CFG->objectfs_environment_name = $pushenv;
set_config('taggingenvironment', $pushenv, 'tool_objectfs');

// Sync the file.
$this->filesystem->push_object_tags($object->contenthash);
Expand Down

0 comments on commit 8dd940a

Please sign in to comment.