Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared script instances #603

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
PARAM_TYPE_EDITABLE_LIST = 'editable_list'
FILE_TYPE_FILE = 'file'
FILE_TYPE_DIR = 'dir'
SHARED_ACCESS_TYPE_ALL = "ALL_USERS"
7 changes: 6 additions & 1 deletion src/execution/execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from auth.authorization import Authorizer, is_same_user
from auth.user import User
from config.constants import SHARED_ACCESS_TYPE_ALL
from execution.executor import ScriptExecutor
from model import script_config
from model.model_helper import is_empty, AccessProhibitedException
Expand Down Expand Up @@ -145,7 +146,11 @@ def validate_execution_id(self, execution_id, user, only_active=True, allow_when

@staticmethod
def _can_access_execution(execution_info: _ExecutionInfo, user_id):
return (execution_info is not None) and (is_same_user(execution_info.owner_user.user_id, user_id))
if execution_info is None:
return False
shared_access_type = execution_info.config.access.get('shared_access', {}).get('type')
return (shared_access_type == SHARED_ACCESS_TYPE_ALL or \
is_same_user(execution_info.owner_user.user_id, user_id))

def get_user_parameter_values(self, execution_id):
return self._get_for_executor(execution_id,
Expand Down
4 changes: 4 additions & 0 deletions src/model/script_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from auth.authorization import ANY_USER
from config.exceptions import InvalidConfigException
from config.constants import SHARED_ACCESS_TYPE_ALL
from model import parameter_config
from model.model_helper import is_empty, fill_parameter_values, read_bool_from_config, InvalidValueException, \
read_str_from_config, replace_auth_vars
Expand Down Expand Up @@ -180,6 +181,8 @@ def _reload_config(self):
required_terminal = read_bool_from_config('requires_terminal', config, default=self._pty_enabled_default)
self.requires_terminal = required_terminal

self.access = config.get('access', {})

self.output_format = read_output_format(config)

self.output_files = config.get('output_files', [])
Expand Down Expand Up @@ -386,6 +389,7 @@ def get_sorted_config(config):
'group',
'allowed_users',
'admin_users',
'access',
'schedulable',
'include',
'output_files',
Expand Down
7 changes: 7 additions & 0 deletions src/tests/execution_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def test_can_access_different_user_reversed(self):

self.assertFalse(execution_service.can_access(execution_id, DEFAULT_USER_ID))

def test_can_access_different_user_shared_access(self):
execution_service = self.create_execution_service()
execution_id = self._start(execution_service)
execution_service._execution_infos[execution_id].config.access = {'shared_access': {'type': 'ALL_USERS'}}

self.assertTrue(execution_service.can_access(execution_id, 'another_user'))

def test_get_audit_name(self):
execution_service = self.create_execution_service()
execution_id = self._start(execution_service)
Expand Down
22 changes: 21 additions & 1 deletion web-src/src/admin/components/scripts-config/ScriptConfigForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
class="col s3 checkbox-field"/>
<TextField v-model="includeScript" :config="includeScriptField" class="col s5 offset-s1"/>
</div>

<div class="row">
<CheckBox v-model="globalInstances" :config="globalInstancesField"
class="col s3 checkbox-field"/>
</div>
</form>
</template>

Expand All @@ -56,6 +61,7 @@ import {
nameField,
outputFormatField,
requiresTerminalField,
globalInstancesField,
scriptPathField,
workDirField
} from './script-fields';
Expand Down Expand Up @@ -86,6 +92,7 @@ export default {
description: null,
workingDirectory: null,
requiresTerminal: null,
globalInstances: null,
includeScript: null,
outputFormat: null,
allowedUsers: [],
Expand All @@ -101,6 +108,7 @@ export default {
allowAllAdminsField,
outputFormatField,
requiresTerminalField,
globalInstancesField,
includeScriptField
}
},
Expand All @@ -125,6 +133,15 @@ export default {
}
});
});

this.$watch('globalInstances', (globalInstances) => {
if (globalInstances) {
this.$set(this.value, 'access', {'shared_access': {'type': 'ALL_USERS'}});
} else {
this.$delete(this.value, 'access');
}
});

},

watch: {
Expand All @@ -136,13 +153,16 @@ export default {
this.description = config['description'];
this.workingDirectory = config['working_directory'];
this.requiresTerminal = get(config, 'requires_terminal', true);
this.globalInstances = false;
if (config?.access?.shared_access?.type == "ALL_USERS"){
this.globalInstances = true;
}
this.includeScript = config['include'];
this.outputFormat = config['output_format'];
this.updateAccessFieldInVm(config,
'allowedUsers',
'allowAllUsers',
'allowed_users')

this.updateAccessFieldInVm(config,
'adminUsers',
'allowAllAdmins',
Expand Down
4 changes: 4 additions & 0 deletions web-src/src/admin/components/scripts-config/script-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ export const requiresTerminalField = {
export const includeScriptField = {
name: 'Include config',
description: 'Allows to include another shared config'
};
export const globalInstancesField = {
name: 'Shared Script Instances',
description: 'Allows script instances to be shared by all users'
};
33 changes: 33 additions & 0 deletions web-src/tests/unit/admin/ScriptConfig_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,38 @@ describe('Test ScriptConfig', function () {
});
});

describe('Test show shared instances access', function () {
it('Test show shared instances access unchecked', async function () {
store.state.scriptConfig.scriptConfig = {};

await vueTicks();

expect(_findField('Shared Script Instances').value).toBe(false);
});

it('Test show shared instances access checked', async function () {
store.state.scriptConfig.scriptConfig = {
'access': {'shared_access': {'type': 'ALL_USERS'}}
};

await vueTicks();

expect(_findField('Shared Script Instances').value).toBe(true);
});
});

describe('Test edit global_instances', function () {
it('Test update global_instances manually unchecked', async function () {
await _setValueByUser('Shared Script Instances', false);

expect(typeof store.state.scriptConfig.scriptConfig.access).toEqual('undefined');
});

it('Test update global_instances manually checked', async function () {
await _setValueByUser('Shared Script Instances', true);

expect(store.state.scriptConfig.scriptConfig.access).toEqual({'shared_access': {'type': 'ALL_USERS'}});
});
});

});