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

Add some nuance to the on-off logic for RESOURCE_SERVER #603

Merged
merged 3 commits into from
Sep 4, 2024
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
5 changes: 3 additions & 2 deletions ansible_base/resource_registry/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import ansible_base.lib.checks # noqa: F401 - register checks
from ansible_base.lib.utils.db import ensure_transaction, migrations_are_complete
from ansible_base.resource_registry.utils.settings import resource_server_defined

logger = logging.getLogger('ansible_base.resource_registry.apps')

Expand Down Expand Up @@ -101,10 +102,10 @@ def proxies_of_model(cls):

def _should_reverse_sync():
enabled = getattr(settings, 'RESOURCE_SERVER_SYNC_ENABLED', False)
if not getattr(settings, 'RESOURCE_SERVER', False):
if enabled and (not resource_server_defined()):
logger.error("RESOURCE_SERVER is not configured. Reverse sync will not be enabled.")
enabled = False
if hasattr(settings, 'RESOURCE_SERVER') and ('SECRET_KEY' not in settings.RESOURCE_SERVER or not settings.RESOURCE_SERVER['SECRET_KEY']):
if enabled and resource_server_defined() and ('SECRET_KEY' not in settings.RESOURCE_SERVER or not settings.RESOURCE_SERVER['SECRET_KEY']):
logger.error("RESOURCE_SERVER['SECRET_KEY'] is not configured. Reverse sync will not be enabled.")
enabled = False
return enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ansible_base.resource_registry.resource_server import get_resource_server_config
from ansible_base.resource_registry.utils.auth_code import get_user_auth_code
from ansible_base.resource_registry.utils.settings import resource_server_defined


def redirect_to_resource_server(*args, social=None, user=None, **kwargs):
Expand All @@ -11,7 +12,9 @@ def redirect_to_resource_server(*args, social=None, user=None, **kwargs):
"""

# Allow for disabling this pipeline without removing it from the settings.
if not getattr(settings, 'ENABLE_SERVICE_BACKED_SSO', False):
# If resource server is defined, also silently quit
# for ease of connected vs disconnected configs
if (not getattr(settings, 'ENABLE_SERVICE_BACKED_SSO', False)) or (not resource_server_defined()):
return None

oidc_alt_key = None
Expand Down
5 changes: 5 additions & 0 deletions ansible_base/resource_registry/utils/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.conf import settings


def resource_server_defined() -> bool:
return bool(getattr(settings, 'RESOURCE_SERVER', {}).get('URL', ''))
4 changes: 2 additions & 2 deletions test_app/tests/resource_registry/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_sync_to_resource_server_create_update_and_ansible_id_given(self, organi
(
{
'RESOURCE_SERVER_SYNC_ENABLED': True,
'RESOURCE_SERVER': {'url': 'http://localhost:8000', 'SECRET_KEY': 'foo'},
'RESOURCE_SERVER': {'URL': 'http://localhost:8000', 'SECRET_KEY': 'foo'},
'RESOURCE_SERVICE_PATH': "/foo",
},
True,
Expand All @@ -223,7 +223,7 @@ def test_sync_to_resource_server_create_update_and_ansible_id_given(self, organi
(
{
'RESOURCE_SERVER_SYNC_ENABLED': False,
'RESOURCE_SERVER': {'url': 'http://localhost:8000', 'SECRET_KEY': 'foo'},
'RESOURCE_SERVER': {'URL': 'http://localhost:8000', 'SECRET_KEY': 'foo'},
'RESOURCE_SERVICE_PATH': "/foo",
},
False,
Expand Down
Loading