Skip to content

Commit

Permalink
Merge pull request ansible#12832 from no-12/allow_metrics_for_anonymo…
Browse files Browse the repository at this point in the history
…us_users

Allow metrics collection for anonymous users via settings
  • Loading branch information
john-westcott-iv authored Jan 18, 2023
2 parents e9a1582 + 5775ff1 commit 884ab42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions awx/api/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
category=_('Authentication'),
category_slug='authentication',
)
register(
'ALLOW_METRICS_FOR_ANONYMOUS_USERS',
field_class=fields.BooleanField,
default=False,
label=_('Allow anonymous users to poll metrics'),
help_text=_('If true, anonymous users are allowed to poll metrics.'),
category=_('Authentication'),
category_slug='authentication',
)


def authentication_validate(serializer, attrs):
Expand Down
9 changes: 8 additions & 1 deletion awx/api/views/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import logging

# Django
from django.conf import settings
from django.utils.translation import gettext_lazy as _

# Django REST Framework
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.exceptions import PermissionDenied

Expand All @@ -31,9 +33,14 @@ class MetricsView(APIView):

renderer_classes = [renderers.PlainTextRenderer, renderers.PrometheusJSONRenderer, renderers.BrowsableAPIRenderer]

def initialize_request(self, request, *args, **kwargs):
if settings.ALLOW_METRICS_FOR_ANONYMOUS_USERS:
self.permission_classes = (AllowAny,)
return super(APIView, self).initialize_request(request, *args, **kwargs)

def get(self, request):
'''Show Metrics Details'''
if request.user.is_superuser or request.user.is_system_auditor:
if settings.ALLOW_METRICS_FOR_ANONYMOUS_USERS or request.user.is_superuser or request.user.is_system_auditor:
metrics_to_show = ''
if not request.query_params.get('subsystemonly', "0") == "1":
metrics_to_show += metrics().decode('UTF-8')
Expand Down
3 changes: 3 additions & 0 deletions awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@
# when trying to access a UI page that requries authentication.
LOGIN_REDIRECT_OVERRIDE = ''

# Note: This setting may be overridden by database settings.
ALLOW_METRICS_FOR_ANONYMOUS_USERS = False

DEVSERVER_DEFAULT_ADDR = '0.0.0.0'
DEVSERVER_DEFAULT_PORT = '8013'

Expand Down

0 comments on commit 884ab42

Please sign in to comment.