Skip to content
Closed
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
4 changes: 2 additions & 2 deletions contrib/linux/actions/checks/check_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def process(self, criteria):
cmdfh = open(self.procDir + "/" + p + "/cmdline")
cmd = cmdfh.readline()
pInfo[1] = cmd
except:
continue
except IOError:
print("Error: can't find file or read data.")
finally:
cmdfh.close()
fh.close()
Expand Down
4 changes: 2 additions & 2 deletions contrib/linux/sensors/file_watch_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def cleanup(self):

try:
self._tail.notifier.stop()
except Exception:
pass
except Exception as e:
print("Error: tail notifier is still running.", e)

def add_trigger(self, trigger):
file_path = trigger['parameters'].get('file_path', None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,10 @@ def _format_action_exec_result(self, action_node, liveaction_db, created_at, upd

:rtype: ``dict``
"""
assert isinstance(created_at, datetime.datetime)
assert isinstance(updated_at, datetime.datetime)
if not isinstance(created_at, datetime.datetime):
raise TypeError('The created_at is not a datetime object.')
if not isinstance(updated_at, datetime.datetime):
raise TypeError('The updated_at is not a datetime object.')

result = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ def _get_action_instance(self):

LOG.debug('Received parameters: %s', parameters)

assert isinstance(parent_args, list)
if not isinstance(parent_args, list):
raise ValueError('The parent_args needs to be a list.')
obj = PythonActionWrapper(pack=args.pack,
file_path=args.file_path,
config=config,
Expand Down
3 changes: 2 additions & 1 deletion contrib/runners/python_runner/python_runner/python_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ def _get_output_values(self, exit_code, stdout, stderr, timed_out):

if ACTION_OUTPUT_RESULT_DELIMITER in stdout:
split = stdout.split(ACTION_OUTPUT_RESULT_DELIMITER)
assert len(split) == 3
if not len(split) == 3:
raise ValueError('The result length should be 3.')
action_result = split[1].strip()
stdout = split[0] + split[2]
else:
Expand Down
2 changes: 1 addition & 1 deletion st2actions/st2actions/cmd/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _run_scheduler():
handler.shutdown()
entrypoint.shutdown()
except:
pass
LOG.exception('Unable to shutdown scheduler.')

return 1

Expand Down
18 changes: 12 additions & 6 deletions st2api/st2api/controllers/v1/actionexecutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,16 @@ def validate(self):
're-running task(s) for a workflow.')

if self.parameters:
assert isinstance(self.parameters, dict)
if not isinstance(self.parameters, dict):
raise ValueError('The parameters needs to be a dictionary.')

if self.tasks:
assert isinstance(self.tasks, list)
if not isinstance(self.tasks, list):
raise ValueError('The tasks needs to be a list.')

if self.reset:
assert isinstance(self.reset, list)
if not isinstance(self.reset, list):
raise ValueError('The reset needs to be a list.')

if list(set(self.reset) - set(self.tasks)):
raise ValueError('List of tasks to reset does not match the tasks to rerun.')
Expand All @@ -405,13 +408,16 @@ def post(self, spec_api, id, requester_user, no_merge=False, show_secrets=False)
're-running task(s) for a workflow.')

if spec_api.parameters:
assert isinstance(spec_api.parameters, dict)
if not isinstance(spec_api.parameters, dict):
raise ValueError('The parameters needs to be a dictionary.')

if spec_api.tasks:
assert isinstance(spec_api.tasks, list)
if not isinstance(spec_api.tasks, list):
raise ValueError('The tasks needs to be a list.')

if spec_api.reset:
assert isinstance(spec_api.reset, list)
if not isinstance(spec_api.reset, list):
raise ValueError('The reset needs to be a list.')

if list(set(spec_api.reset) - set(spec_api.tasks)):
raise ValueError('List of tasks to reset does not match the tasks to rerun.')
Expand Down
18 changes: 12 additions & 6 deletions st2api/st2api/controllers/v1/keyvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def get_all(self, requester_user, prefix=None, scope=FULL_SYSTEM_SCOPE, user=Non
raw_filters['scope'] = FULL_SYSTEM_SCOPE
raw_filters['prefix'] = prefix

assert 'scope' in raw_filters
if 'scope'not in raw_filters:
raise KeyError('The scope is not found in raw filters.')
kvp_apis_system = super(KeyValuePairController, self)._get_all(
from_model_kwargs=from_model_kwargs,
sort=sort,
Expand All @@ -212,8 +213,10 @@ def get_all(self, requester_user, prefix=None, scope=FULL_SYSTEM_SCOPE, user=Non
else:
raw_filters['prefix'] = user_scope_prefix

assert 'scope' in raw_filters
assert 'prefix' in raw_filters
if 'scope'not in raw_filters:
raise KeyError('The scope is not found in raw filters.')
if 'prefix'not in raw_filters:
raise KeyError('The prefix is not found in raw filters.')
kvp_apis_user = super(KeyValuePairController, self)._get_all(
from_model_kwargs=from_model_kwargs,
sort=sort,
Expand All @@ -231,8 +234,10 @@ def get_all(self, requester_user, prefix=None, scope=FULL_SYSTEM_SCOPE, user=Non
prefix = get_key_reference(name=prefix or '', scope=scope, user=user)
raw_filters['prefix'] = user_scope_prefix

assert 'scope' in raw_filters
assert 'prefix' in raw_filters
if 'scope' not in raw_filters:
raise KeyError('The scope is not found in raw filters.')
if 'prefix' not in raw_filters:
raise KeyError('The prefix is not found in raw filters.')
kvp_apis = super(KeyValuePairController, self)._get_all(
from_model_kwargs=from_model_kwargs,
sort=sort,
Expand All @@ -243,7 +248,8 @@ def get_all(self, requester_user, prefix=None, scope=FULL_SYSTEM_SCOPE, user=Non
elif scope in [SYSTEM_SCOPE, FULL_SYSTEM_SCOPE]:
raw_filters['prefix'] = prefix

assert 'scope' in raw_filters
if 'scope' not in raw_filters:
raise KeyError('The scope is not found in raw filters.')
kvp_apis = super(KeyValuePairController, self)._get_all(
from_model_kwargs=from_model_kwargs,
sort=sort,
Expand Down
3 changes: 2 additions & 1 deletion st2api/st2api/controllers/v1/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ def __init__(self, payload=None):

def validate(self):
if self.payload:
assert isinstance(self.payload, dict)
if not isinstance(self.payload, dict):
raise TypeError('The payload has a value that is not a dictionary.')

return True

Expand Down
7 changes: 5 additions & 2 deletions st2client/st2client/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,11 @@ def delete_by_id(self, instance_id, **kwargs):
resp_json = response.json()
if resp_json:
return resp_json
except:
pass
except Exception as e:
response.reason += (
'\nUnable to retrieve detailed message '
'from the HTTP response. %s\n' % six.text_type(e)
)
return True


Expand Down
12 changes: 6 additions & 6 deletions st2client/st2client/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def ioctl_GWINSZ(fd):
for fd in (0, 1, 2):
try:
return ioctl_GWINSZ(fd)[1]
except Exception:
pass
except Exception as e:
print(e)

# 3. try os.ctermid()
try:
Expand All @@ -73,8 +73,8 @@ def ioctl_GWINSZ(fd):
return ioctl_GWINSZ(fd)[1]
finally:
os.close(fd)
except Exception:
pass
except Exception as e:
print(e)

# 4. try `stty size`
try:
Expand All @@ -85,8 +85,8 @@ def ioctl_GWINSZ(fd):
result = process.communicate()
if process.returncode == 0:
return tuple(int(x) for x in result[0].split())[1]
except Exception:
pass
except Exception as e:
print(e)

# 5. return default fallback value
return default
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/actionsregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def _register_actions_from_pack(self, pack, actions):
def register_actions(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):
if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise ValueError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/aliasesregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ def register_aliases(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):

if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise TypeError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/configsregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ def register_configs(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False, validate_configs=True):

if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise ValueError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/policiesregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def register_policy_types(module):
def register_policies(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):
if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise TypeError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/rulesregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def _register_rules_from_pack(self, pack, rules):
def register_rules(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):
if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise ValueError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/sensorsregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def _register_sensor_from_pack(self, pack, sensor):
def register_sensors(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):
if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise TypeError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/bootstrap/triggersregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def _register_trigger_from_pack(self, pack_base_path, pack, trigger):
def register_triggers(packs_base_paths=None, pack_dir=None, use_pack_cache=True,
fail_on_failure=False):
if packs_base_paths:
assert isinstance(packs_base_paths, list)
if not isinstance(packs_base_paths, list):
raise ValueError('The pack base paths has a value that is not a list.')

if not packs_base_paths:
packs_base_paths = content_utils.get_packs_base_paths()
Expand Down
6 changes: 4 additions & 2 deletions st2common/st2common/content/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def get_packs(self, base_dirs):
directory.
:rtype: ``dict``
"""
assert isinstance(base_dirs, list)
if not isinstance(base_dirs, list):
raise TypeError('The base dirs has a value that is not a list.')

result = {}
for base_dir in base_dirs:
Expand All @@ -88,7 +89,8 @@ def get_content(self, base_dirs, content_type):

:rtype: ``dict``
"""
assert isinstance(base_dirs, list)
if not isinstance(base_dirs, list):
raise TypeError('The base dirs has a value that is not a list.')

if content_type not in self.ALLOWED_CONTENT_TYPES:
raise ValueError('Unsupported content_type: %s' % (content_type))
Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/content/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def get_pack_file_abs_path(pack_ref, file_path, resource_type=None, use_pack_cac
path_components.append(normalized_file_path)
result = os.path.join(*path_components) # pylint: disable=E1120

assert normalized_file_path in result
if normalized_file_path not in result:
raise ValueError('This is not a normalized path to prevent directory traversal.')

# Final safety check for common prefix to avoid traversal attack
common_prefix = os.path.commonprefix([pack_base_path, result])
Expand Down
4 changes: 2 additions & 2 deletions st2common/st2common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def find_caller(stack_info=False, stacklevel=1):
sio.close()
rv = (filename, f.f_lineno, co.co_name, sinfo)
break
except Exception:
pass
except Exception as e:
print('Unable to find caller.', e)

return rv

Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/models/api/keyvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def to_model(cls, kvp):
raise ValueError(msg)

# Additional safety check to ensure that the value hasn't been decrypted
assert value == original_value
if not value == original_value:
raise ValueError("The value doesn't match with original value.")
elif secret:
cls._verif_key_is_set_up(name=name)

Expand Down
3 changes: 2 additions & 1 deletion st2common/st2common/models/system/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def _get_command_string(self, cmd, args):

:rtype: ``str``
"""
assert isinstance(args, (list, tuple))
if not isinstance(args, (list, tuple)):
raise ValueError('The args has a value that is not a list and tuple.')

args = [quote_unix(arg) for arg in args]
args = ' '.join(args)
Expand Down
6 changes: 4 additions & 2 deletions st2common/st2common/rbac/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def get_resource_type(cls, permission_type):
return ResourceType.EXECUTION

split = permission_type.split('_')
assert len(split) >= 2
if not len(split) >= 2:
raise ValueError('The length should be greater than or equal to 2.')

return '_'.join(split[:-1])

Expand All @@ -196,7 +197,8 @@ def get_permission_name(cls, permission_type):
:rtype: ``str``
"""
split = permission_type.split('_')
assert len(split) >= 2
if not len(split) >= 2:
raise ValueError('The length should be greater than or equal to 2.')

# Special case for PACK_VIEWS_INDEX_HEALTH
if permission_type == PermissionType.PACK_VIEWS_INDEX_HEALTH:
Expand Down
15 changes: 10 additions & 5 deletions st2common/st2common/runners/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def pre_run(self):
entry_point=self.entry_point,
worktree_path=self.git_worktree_path)

assert(entry_point.startswith(self.git_worktree_path))
if not (entry_point.startswith(self.git_worktree_path)):
raise ValueError("The entry point value does not match with git worktree path.")

self.entry_point = entry_point

Expand Down Expand Up @@ -375,8 +376,11 @@ def cleanup_git_worktree(self, worktree_path, pack_name, content_version):
:rtype: ``bool``
"""
# Safety check to make sure we don't remove something outside /tmp
assert(worktree_path.startswith('/tmp'))
assert(worktree_path.startswith('/tmp/%s' % (self.WORKTREE_DIRECTORY_PREFIX)))
if not (worktree_path.startswith('/tmp')):
raise ValueError('The worktree path does not match with /tmp.')
if not (worktree_path.startswith('/tmp/%s' % (self.WORKTREE_DIRECTORY_PREFIX))):
raise ValueError('The worktree path does not match with /tmp/"%s".' %
(self.WORKTREE_DIRECTORY_PREFIX))

if self._debug:
LOG.debug('Not removing git worktree "%s" because debug mode is enabled' %
Expand All @@ -387,8 +391,9 @@ def cleanup_git_worktree(self, worktree_path, pack_name, content_version):

try:
shutil.rmtree(worktree_path, ignore_errors=True)
except:
pass
except Exception:
msg = ('Unable to remove / cleanup the provided git worktree directory.')
LOG.exception(msg)

return True

Expand Down
Loading