def get_one(self, name_or_id, *file_path_components): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/files/<pack_name>/<file path> """ pack_db = self._get_by_name_or_id(name_or_id=name_or_id) if not pack_db: msg = 'Pack with name_or_id "%s" does not exist' % (name_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_name = pack_db.name normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) content_type = mimetypes.guess_type(normalized_file_path)[0] or 'application/octet-stream' response.headers['Cache-Control'] = 'public, max-age=86400' response.headers['Content-Type'] = content_type response.body = self._get_file_content(file_path=normalized_file_path) return response
def get_one(self, name_or_id, *file_path_components): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_name>/<file path> """ pack_db = self._get_by_name_or_id(name_or_id=name_or_id) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_name = pack_db.name normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile( normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) content = self._get_file_content(file_path=normalized_file_path) return content
def get_one(self, name_or_id): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_name> """ pack_db = self._get_by_name_or_id(name_or_id=name_or_id) pack_name = pack_db.name pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk continue content = self._get_file_content(file_path=normalized_file_path) item = { 'file_path': file_path, 'content': content } result.append(item) return result
def get_one(self, name_or_id): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_name> """ pack_db = self._get_by_name_or_id(name_or_id=name_or_id) pack_name = pack_db.name pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile( normalized_file_path): # Ignore references to files which don't exist on disk continue content = self._get_file_content(file_path=normalized_file_path) item = {'file_path': file_path, 'content': content} result.append(item) return result
def get_one(self, ref_or_id, file_path, requester_user, **kwargs): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/file/<pack_ref_or_id>/<file path> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path: raise ValueError('Missing file path') pack_ref = pack_db.ref # Note: Until list filtering is in place we don't require RBAC check for icon file permission_type = PermissionType.PACK_VIEW if file_path not in WHITELISTED_FILE_PATHS: rbac_utils.assert_user_has_resource_db_permission( user_db=requester_user, resource_db=pack_db, permission_type=permission_type) normalized_file_path = get_pack_file_abs_path(pack_ref=pack_ref, file_path=file_path) if not normalized_file_path or not os.path.isfile( normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) file_size, file_mtime = self._get_file_stats( file_path=normalized_file_path) response = Response() if not self._is_file_changed(file_mtime, **kwargs): response.status = http_client.NOT_MODIFIED else: if file_size is not None and file_size > MAX_FILE_SIZE: msg = ('File %s exceeds maximum allowed file size (%s bytes)' % (file_path, MAX_FILE_SIZE)) raise ValueError(msg) content_type = mimetypes.guess_type(normalized_file_path)[0] or \ 'application/octet-stream' response.headers['Content-Type'] = content_type response.body = self._get_file_content( file_path=normalized_file_path) response.headers['Last-Modified'] = format_date_time(file_mtime) response.headers['ETag'] = repr(file_mtime) return response
def get_one(self, ref_or_id, file_path, requester_user, if_none_match=None, if_modified_since=None): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/file/<pack_ref_or_id>/<file path> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path: raise ValueError('Missing file path') pack_ref = pack_db.ref # Note: Until list filtering is in place we don't require RBAC check for icon file permission_type = PermissionType.PACK_VIEW if file_path not in WHITELISTED_FILE_PATHS: rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user, resource_db=pack_db, permission_type=permission_type) normalized_file_path = get_pack_file_abs_path(pack_ref=pack_ref, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) file_size, file_mtime = self._get_file_stats(file_path=normalized_file_path) response = Response() if not self._is_file_changed(file_mtime, if_none_match=if_none_match, if_modified_since=if_modified_since): response.status = http_client.NOT_MODIFIED else: if file_size is not None and file_size > MAX_FILE_SIZE: msg = ('File %s exceeds maximum allowed file size (%s bytes)' % (file_path, MAX_FILE_SIZE)) raise ValueError(msg) content_type = mimetypes.guess_type(normalized_file_path)[0] or \ 'application/octet-stream' response.headers['Content-Type'] = content_type response.body = self._get_file_content(file_path=normalized_file_path) response.headers['Last-Modified'] = format_date_time(file_mtime) response.headers['ETag'] = repr(file_mtime) return response
def get_one(self, ref_or_id, requester_user): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_ref_or_id> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) rbac_utils = get_rbac_backend().get_utils_class() rbac_utils.assert_user_has_resource_db_permission( user_db=requester_user, resource_db=pack_db, permission_type=PermissionType.PACK_VIEW, ) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) pack_ref = pack_db.ref pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path( pack_ref=pack_ref, file_path=file_path ) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk continue file_size = self._get_file_size(file_path=normalized_file_path) if file_size is not None and file_size > MAX_FILE_SIZE: LOG.debug( 'Skipping file "%s" which size exceeds max file size (%s bytes)' % (normalized_file_path, MAX_FILE_SIZE) ) continue content = self._get_file_content(file_path=normalized_file_path) include_file = self._include_file(file_path=file_path, content=content) if not include_file: LOG.debug('Skipping binary file "%s"' % (normalized_file_path)) continue item = {"file_path": file_path, "content": content} result.append(item) return result
def get_one(self, ref_or_id, *file_path_components): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/file/<pack_ref_or_id>/<file path> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_ref = pack_db.ref # Note: Until list filtering is in place we don't require RBAC check for icon file if file_path not in WHITELISTED_FILE_PATHS: assert_request_user_has_resource_db_permission(request=pecan.request, resource_db=pack_db, permission_type=PermissionType.PACK_VIEW) normalized_file_path = get_pack_file_abs_path(pack_ref=pack_ref, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) file_size, file_mtime = self._get_file_stats(file_path=normalized_file_path) if not self._is_file_changed(file_mtime): self._add_cache_headers(file_mtime) response.status = http_client.NOT_MODIFIED return response if file_size is not None and file_size > MAX_FILE_SIZE: msg = ('File %s exceeds maximum allowed file size (%s bytes)' % (file_path, MAX_FILE_SIZE)) raise ValueError(msg) content_type = mimetypes.guess_type(normalized_file_path)[0] or 'application/octet-stream' self._add_cache_headers(file_mtime) response.headers['Content-Type'] = content_type response.body = self._get_file_content(file_path=normalized_file_path) return response
def get_one(self, ref_or_id, requester_user): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_ref_or_id> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user, resource_db=pack_db, permission_type=PermissionType.PACK_VIEW) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) pack_ref = pack_db.ref pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path(pack_ref=pack_ref, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk continue file_size = self._get_file_size(file_path=normalized_file_path) if file_size is not None and file_size > MAX_FILE_SIZE: LOG.debug('Skipping file "%s" which size exceeds max file size (%s bytes)' % (normalized_file_path, MAX_FILE_SIZE)) continue content = self._get_file_content(file_path=normalized_file_path) include_file = self._include_file(file_path=file_path, content=content) if not include_file: LOG.debug('Skipping binary file "%s"' % (normalized_file_path)) continue item = { 'file_path': file_path, 'content': content } result.append(item) return result
def get_one(self, ref_or_id): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_ref_or_id> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) pack_name = pack_db.name pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile( normalized_file_path): # Ignore references to files which don't exist on disk continue file_size = self._get_file_size(file_path=normalized_file_path) if file_size is not None and file_size > MAX_FILE_SIZE: LOG.debug( 'Skipping file "%s" which size exceeds max file size (%s bytes)' % (normalized_file_path, MAX_FILE_SIZE)) continue content = self._get_file_content(file_path=normalized_file_path) include_file = self._include_file(file_path=file_path, content=content) if not include_file: LOG.debug('Skipping binary file "%s"' % (normalized_file_path)) continue item = {'file_path': file_path, 'content': content} result.append(item) return result
def get_one(self, ref_or_id, *file_path_components): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/files/<pack_ref_or_id>/<file path> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_name = pack_db.name normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) file_size, file_mtime = self._get_file_stats(file_path=normalized_file_path) if not self._is_file_changed(file_mtime): self._add_cache_headers(file_mtime) response.status = http_client.NOT_MODIFIED return response if file_size is not None and file_size > MAX_FILE_SIZE: msg = ('File %s exceeds maximum allowed file size (%s bytes)' % (file_path, MAX_FILE_SIZE)) raise ValueError(msg) content_type = mimetypes.guess_type(normalized_file_path)[0] or 'application/octet-stream' self._add_cache_headers(file_mtime) response.headers['Content-Type'] = content_type response.body = self._get_file_content(file_path=normalized_file_path) return response
def get_one(self, ref_or_id): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_ref_or_id> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) pack_name = pack_db.name pack_files = pack_db.files result = [] for file_path in pack_files: normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk continue file_size = self._get_file_size(file_path=normalized_file_path) if file_size is not None and file_size > MAX_FILE_SIZE: LOG.debug( 'Skipping file "%s" which size exceeds max file size (%s bytes)' % (normalized_file_path, MAX_FILE_SIZE) ) continue content = self._get_file_content(file_path=normalized_file_path) include_file = self._include_file(file_path=file_path, content=content) if not include_file: LOG.debug('Skipping binary file "%s"' % (normalized_file_path)) continue item = {"file_path": file_path, "content": content} result.append(item) return result
def get_one(self, ref_or_id, *file_path_components): """ Outputs the content of a specific file in a pack. Handles requests: GET /packs/views/files/<pack_ref_or_id>/<file path> """ pack_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) if not pack_db: msg = 'Pack with ref_or_id "%s" does not exist' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_name = pack_db.name normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile( normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) file_size = self._get_file_size(file_path=normalized_file_path) if file_size is not None and file_size > MAX_FILE_SIZE: msg = ('File %s exceeds maximum allowed file size (%s bytes)' % (file_path, MAX_FILE_SIZE)) raise ValueError(msg) content_type = mimetypes.guess_type( normalized_file_path)[0] or 'application/octet-stream' response.headers['Cache-Control'] = 'public, max-age=86400' response.headers['Content-Type'] = content_type response.body = self._get_file_content(file_path=normalized_file_path) return response
def get_one(self, name_or_id, *file_path_components): """ Outputs the content of all the files inside the pack. Handles requests: GET /packs/views/files/<pack_name>/<file path> """ pack_db = self._get_by_name_or_id(name_or_id=name_or_id) if not file_path_components: raise ValueError('Missing file path') file_path = os.path.join(*file_path_components) pack_name = pack_db.name normalized_file_path = get_pack_file_abs_path(pack_name=pack_name, file_path=file_path) if not normalized_file_path or not os.path.isfile(normalized_file_path): # Ignore references to files which don't exist on disk raise StackStormDBObjectNotFoundError('File "%s" not found' % (file_path)) content = self._get_file_content(file_path=normalized_file_path) return content