def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = ['/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py'] for file_path in file_paths: expected_msg = 'Invalid file path: .*%s' % (file_path) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name='dummy_pack_1', resource_type='action', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_name='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def _write_data_files_to_disk(self, pack_ref, data_files): """ Write files to disk. """ written_file_paths = [] for data_file in data_files: file_path = data_file["file_path"] content = data_file["content"] file_path = get_pack_resource_file_abs_path(pack_ref=pack_ref, resource_type="action", file_path=file_path) LOG.debug('Writing data file "%s" to "%s"' % (str(data_file), file_path)) try: self._write_data_file(pack_ref=pack_ref, file_path=file_path, content=content) except (OSError, IOError) as e: # Throw a more user-friendly exception on Permission denied error if e.errno == errno.EACCES: msg = ( 'Unable to write data to "%s" (permission denied). Make sure ' "permissions for that pack directory are configured correctly so " "st2api can write to it." % (file_path)) raise ValueError(msg) raise e written_file_paths.append(file_path) return written_file_paths
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = [ '/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py' ] for file_path in file_paths: expected_msg = 'Invalid file path: .*%s' % (file_path) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def _write_data_files_to_disk(self, pack_ref, data_files): """ Write files to disk. """ written_file_paths = [] for data_file in data_files: file_path = data_file['file_path'] content = data_file['content'] file_path = get_pack_resource_file_abs_path(pack_ref=pack_ref, resource_type='action', file_path=file_path) LOG.debug('Writing data file "%s" to "%s"' % (str(data_file), file_path)) try: self._write_data_file(pack_ref=pack_ref, file_path=file_path, content=content) except (OSError, IOError) as e: # Throw a more user-friendly exception on Permission denied error if e.errno == errno.EACCES: msg = ('Unable to write data to "%s" (permission denied). Make sure ' 'permissions for that pack directory are configured correctly so ' 'st2api can write to it.' % (file_path)) raise ValueError(msg) raise e written_file_paths.append(file_path) return written_file_paths
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = ['/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py', '/opt/stackstorm/packs/invalid_pack/actions/my_action.py', '../../foo.py'] for file_path in file_paths: # action resource_type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack actions directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # sensor resource_type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack sensors directory (.*). For example "my_sensor.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='sensor', file_path=file_path) # no resource type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_file_abs_path, pack_ref='dummy_pack_1', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def _write_data_files_to_disk(self, pack_name, data_files): """ Write files to disk. """ written_file_paths = [] for data_file in data_files: file_path = data_file['file_path'] content = data_file['content'] file_path = get_pack_resource_file_abs_path(pack_name=pack_name, resource_type='action', file_path=file_path) LOG.debug('Writing data file "%s" to "%s"' % (str(data_file), file_path)) self._write_data_file(pack_name=pack_name, file_path=file_path, content=content) written_file_paths.append(file_path) return written_file_paths
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = "Invalid resource type: fooo" self.assertRaisesRegexp( ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name="dummy_pack_1", resource_type="fooo", file_path="test.py", ) # Invalid paths (directory traversal and absolute paths) file_paths = ["/tmp/foo.py", "../foo.py", "/etc/passwd", "../../foo.py"] for file_path in file_paths: expected_msg = "Invalid file path: .*%s" % (file_path) self.assertRaisesRegexp( ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name="dummy_pack_1", resource_type="action", file_path=file_path, ) # Valid paths file_paths = ["foo.py", "a/foo.py", "a/b/foo.py"] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1/actions", file_path) result = get_pack_resource_file_abs_path( pack_name="dummy_pack_1", resource_type="action", file_path=file_path ) self.assertEqual(result, expected)
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = [ '/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py', '/opt/stackstorm/packs/invalid_pack/actions/my_action.py', '../../foo.py' ] for file_path in file_paths: # action resource_type expected_msg = ( 'Invalid file path: ".*%s"\. File path needs to be relative to the ' 'pack actions directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # sensor resource_type expected_msg = ( 'Invalid file path: ".*%s"\. File path needs to be relative to the ' 'pack sensors directory (.*). For example "my_sensor.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='sensor', file_path=file_path) # no resource type expected_msg = ( 'Invalid file path: ".*%s"\. File path needs to be relative to the ' 'pack directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_file_abs_path, pack_ref='dummy_pack_1', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)