diff --git a/docs/configuration/system.md b/docs/configuration/system.md index 72dc35a2f..d1924f75b 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -186,8 +186,6 @@ Default: `$INSTALL_ROOT/netbox/scripts/` The file path to the location where [custom scripts](../customization/custom-scripts.md) will be kept. By default, this is the `netbox/scripts/` directory within the base NetBox installation path. -**Note:** If configuring STORAGES to use AWS S3 set SCRIPTS_ROOT to the prefix (or folder) you want scripts stored in, for example: `scripts` - --- ## SEARCH_BACKEND diff --git a/netbox/core/models/files.py b/netbox/core/models/files.py index e72011bed..79c79f191 100644 --- a/netbox/core/models/files.py +++ b/netbox/core/models/files.py @@ -1,5 +1,6 @@ import logging import os +from functools import cached_property from django.conf import settings from django.core.exceptions import ValidationError @@ -20,8 +21,6 @@ logger = logging.getLogger('netbox.core.files') class ManagedFile(SyncedDataMixin, models.Model): - storage = None - """ Database representation for a file on disk. This class is typically wrapped by a proxy class (e.g. ScriptModule) to provide additional functionality. @@ -101,11 +100,9 @@ class ManagedFile(SyncedDataMixin, models.Model): with storage.open(path, 'wb+') as new_file: new_file.write(self.data) + @cached_property def get_storage(self): - if self.storage is None: - self.storage = storages.create_storage(storages.backends["scripts"]) - - return self.storage + return storages.create_storage(storages.backends["scripts"]) def clean(self): super().clean() diff --git a/netbox/extras/models/mixins.py b/netbox/extras/models/mixins.py index 6a8482bc6..2fc9f3e47 100644 --- a/netbox/extras/models/mixins.py +++ b/netbox/extras/models/mixins.py @@ -10,6 +10,9 @@ __all__ = ( class CustomStoragesLoader(importlib.abc.Loader): + """ + Custom loader for exec_module to use django-storages instead of the file system. + """ def __init__(self, filename): self.filename = filename diff --git a/netbox/extras/storage.py b/netbox/extras/storage.py index 8f73b1090..ede4fac7f 100644 --- a/netbox/extras/storage.py +++ b/netbox/extras/storage.py @@ -4,7 +4,11 @@ from django.utils.functional import cached_property class ScriptFileSystemStorage(FileSystemStorage): - + """ + Custom storage for scripts - for django-storages as the default one will + go off media-root and raise security errors as the scripts can be outside + the media-root directory. + """ @cached_property def base_location(self): return settings.SCRIPTS_ROOT diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 47991cdd2..e41f52dbd 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -236,13 +236,18 @@ DATABASES = { # if STORAGE_BACKEND is not None: - warnings.warn( - "STORAGE_BACKEND is deprecated, use the new STORAGES setting instead." - ) + if STORAGES is not None: + raise ImproperlyConfigured( + "STORAGE_BACKEND and STORAGES are both set, remove the deprecated STORAGE_BACKEND setting." + ) + else: + warnings.warn( + "STORAGE_BACKEND is deprecated, use the new STORAGES setting instead." + ) -if STORAGE_BACKEND is not None and STORAGES is not None: - raise ImproperlyConfigured( - "STORAGE_BACKEND and STORAGES are both set, remove the deprecated STORAGE_BACKEND setting." +if STORAGE_CONFIG is not None: + warnings.warn( + "STORAGE_CONFIG is deprecated, use the new STORAGES setting instead." ) # Default STORAGES for Django @@ -287,7 +292,7 @@ if STORAGE_BACKEND == 'swift.storage.SwiftStorage': for param, value in STORAGE_CONFIG.items(): if param.startswith('SWIFT_'): globals()[param] = value - +# TODO: End of deprecated code # # Redis