mirror of
https://github.com/ansible/awx.git
synced 2026-08-04 03:50:01 -02:30
committed by
Hao Liu
parent
4eefce622d
commit
f4347d05a9
@@ -50,9 +50,6 @@ from ansible_base.lib.utils.models import get_type_for_model
|
|||||||
from ansible_base.rbac.models import RoleEvaluation, ObjectRole
|
from ansible_base.rbac.models import RoleEvaluation, ObjectRole
|
||||||
from ansible_base.rbac import permission_registry
|
from ansible_base.rbac import permission_registry
|
||||||
|
|
||||||
# django-flags
|
|
||||||
from flags.state import flag_enabled
|
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.access import get_user_capabilities
|
from awx.main.access import get_user_capabilities
|
||||||
from awx.main.constants import ACTIVE_STATES, CENSOR_VALUE, org_role_to_permission
|
from awx.main.constants import ACTIVE_STATES, CENSOR_VALUE, org_role_to_permission
|
||||||
@@ -745,13 +742,10 @@ class EmptySerializer(serializers.Serializer):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class OpaQueryPathEnabledMixin(serializers.Serializer):
|
class OpaQueryPathMixin(serializers.Serializer):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
if not flag_enabled("FEATURE_POLICY_AS_CODE_ENABLED") and 'opa_query_path' in self.fields:
|
|
||||||
self.fields.pop('opa_query_path')
|
|
||||||
|
|
||||||
def validate_opa_query_path(self, value):
|
def validate_opa_query_path(self, value):
|
||||||
# Decode the URL and re-encode it
|
# Decode the URL and re-encode it
|
||||||
decoded_value = urllib.parse.unquote(value)
|
decoded_value = urllib.parse.unquote(value)
|
||||||
@@ -763,7 +757,7 @@ class OpaQueryPathEnabledMixin(serializers.Serializer):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class UnifiedJobTemplateSerializer(BaseSerializer, OpaQueryPathEnabledMixin):
|
class UnifiedJobTemplateSerializer(BaseSerializer, OpaQueryPathMixin):
|
||||||
# As a base serializer, the capabilities prefetch is not used directly,
|
# As a base serializer, the capabilities prefetch is not used directly,
|
||||||
# instead they are derived from the Workflow Job Template Serializer and the Job Template Serializer, respectively.
|
# instead they are derived from the Workflow Job Template Serializer and the Job Template Serializer, respectively.
|
||||||
capabilities_prefetch = []
|
capabilities_prefetch = []
|
||||||
@@ -1440,7 +1434,7 @@ class OAuth2ApplicationSerializer(BaseSerializer):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class OrganizationSerializer(BaseSerializer, OpaQueryPathEnabledMixin):
|
class OrganizationSerializer(BaseSerializer, OpaQueryPathMixin):
|
||||||
show_capabilities = ['edit', 'delete']
|
show_capabilities = ['edit', 'delete']
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -1800,7 +1794,7 @@ class LabelsListMixin(object):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
class InventorySerializer(LabelsListMixin, BaseSerializerWithVariables, OpaQueryPathEnabledMixin):
|
class InventorySerializer(LabelsListMixin, BaseSerializerWithVariables, OpaQueryPathMixin):
|
||||||
show_capabilities = ['edit', 'delete', 'adhoc', 'copy']
|
show_capabilities = ['edit', 'delete', 'adhoc', 'copy']
|
||||||
capabilities_prefetch = ['admin', 'adhoc', {'copy': 'organization.inventory_admin'}]
|
capabilities_prefetch = ['admin', 'adhoc', {'copy': 'organization.inventory_admin'}]
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import logging
|
|||||||
# Django
|
# Django
|
||||||
from django.core.checks import Error
|
from django.core.checks import Error
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
# Django REST Framework
|
# Django REST Framework
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
@@ -997,7 +996,6 @@ def csrf_trusted_origins_validate(serializer, attrs):
|
|||||||
register_validate('system', csrf_trusted_origins_validate)
|
register_validate('system', csrf_trusted_origins_validate)
|
||||||
|
|
||||||
|
|
||||||
if settings.FEATURE_POLICY_AS_CODE_ENABLED: # Unable to use flag_enabled due to AppRegistryNotReady error
|
|
||||||
register(
|
register(
|
||||||
'OPA_HOST',
|
'OPA_HOST',
|
||||||
field_class=fields.CharField,
|
field_class=fields.CharField,
|
||||||
@@ -1118,10 +1116,12 @@ if settings.FEATURE_POLICY_AS_CODE_ENABLED: # Unable to use flag_enabled due to
|
|||||||
category_slug='policyascode',
|
category_slug='policyascode',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def policy_as_code_validate(serializer, attrs):
|
def policy_as_code_validate(serializer, attrs):
|
||||||
opa_host = attrs.get('OPA_HOST', '')
|
opa_host = attrs.get('OPA_HOST', '')
|
||||||
if opa_host and (opa_host.startswith('http://') or opa_host.startswith('https://')):
|
if opa_host and (opa_host.startswith('http://') or opa_host.startswith('https://')):
|
||||||
raise serializers.ValidationError({'OPA_HOST': _("OPA_HOST should not include 'http://' or 'https://' prefixes. Please enter only the hostname.")})
|
raise serializers.ValidationError({'OPA_HOST': _("OPA_HOST should not include 'http://' or 'https://' prefixes. Please enter only the hostname.")})
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
register_validate('policyascode', policy_as_code_validate)
|
register_validate('policyascode', policy_as_code_validate)
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ from typing import Optional, Union
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from flags.state import flag_enabled
|
|
||||||
from opa_client import OpaClient
|
from opa_client import OpaClient
|
||||||
from opa_client.base import BaseClient
|
from opa_client.base import BaseClient
|
||||||
from requests import HTTPError
|
from requests import HTTPError
|
||||||
@@ -364,9 +363,6 @@ def opa_client(headers=None):
|
|||||||
|
|
||||||
def evaluate_policy(instance):
|
def evaluate_policy(instance):
|
||||||
# Policy evaluation for Policy as Code feature
|
# Policy evaluation for Policy as Code feature
|
||||||
if not flag_enabled("FEATURE_POLICY_AS_CODE_ENABLED"):
|
|
||||||
return
|
|
||||||
|
|
||||||
if not settings.OPA_HOST:
|
if not settings.OPA_HOST:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -36,11 +36,9 @@ def _parse_exception_message(exception: PolicyEvaluationError):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def enable_flag():
|
def setup_opa_settings():
|
||||||
with override_settings(
|
with override_settings(
|
||||||
OPA_HOST='opa.example.com',
|
OPA_HOST='opa.example.com',
|
||||||
FLAGS={"FEATURE_POLICY_AS_CODE_ENABLED": [("boolean", True)]},
|
|
||||||
FLAG_SOURCES=('flags.sources.SettingsFlagsSource',),
|
|
||||||
):
|
):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|||||||
@@ -1255,8 +1255,8 @@ OPA_REQUEST_RETRIES = 2 # The number of retry attempts for connecting to the OP
|
|||||||
# feature flags
|
# feature flags
|
||||||
FLAG_SOURCES = ('flags.sources.SettingsFlagsSource',)
|
FLAG_SOURCES = ('flags.sources.SettingsFlagsSource',)
|
||||||
FLAGS = {
|
FLAGS = {
|
||||||
'FEATURE_POLICY_AS_CODE_ENABLED': [{'condition': 'boolean', 'value': False}],
|
|
||||||
'FEATURE_INDIRECT_NODE_COUNTING_ENABLED': [{'condition': 'boolean', 'value': False}],
|
'FEATURE_INDIRECT_NODE_COUNTING_ENABLED': [{'condition': 'boolean', 'value': False}],
|
||||||
|
'FEATURE_DISPATCHERD_ENABLED': [{'condition': 'boolean', 'value': False}],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dispatcher worker lifetime. If set to None, workers will never be retired
|
# Dispatcher worker lifetime. If set to None, workers will never be retired
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ needs_development = ['inventory_script', 'instance']
|
|||||||
needs_param_development = {
|
needs_param_development = {
|
||||||
'host': ['instance_id'],
|
'host': ['instance_id'],
|
||||||
'workflow_approval': ['description', 'execution_environment'],
|
'workflow_approval': ['description', 'execution_environment'],
|
||||||
|
'inventory': ['opa_query_path'],
|
||||||
|
'job_template': ['opa_query_path'],
|
||||||
|
'organization': ['opa_query_path'],
|
||||||
}
|
}
|
||||||
# -----------------------------------------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user