Skip to content

Commit 222f387

Browse files
authored
Remove FEATURE_POLICY_AS_CODE_ENABLED flag (ansible#16006)
* remove FEATURE_POLICY_AS_CODE_ENABLED flag * rename to OpaQueryPathMixin * add OpaQueryPath docs to awx collection * bypass test for awx collection
1 parent d7ca19f commit 222f387

File tree

6 files changed

+127
-143
lines changed

6 files changed

+127
-143
lines changed

awx/api/serializers.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
from ansible_base.rbac.models import RoleEvaluation, ObjectRole
4747
from ansible_base.rbac import permission_registry
4848

49-
# django-flags
50-
from flags.state import flag_enabled
51-
5249
# AWX
5350
from awx.main.access import get_user_capabilities
5451
from awx.main.constants import ACTIVE_STATES, org_role_to_permission
@@ -737,13 +734,10 @@ class EmptySerializer(serializers.Serializer):
737734
pass
738735

739736

740-
class OpaQueryPathEnabledMixin(serializers.Serializer):
737+
class OpaQueryPathMixin(serializers.Serializer):
741738
def __init__(self, *args, **kwargs):
742739
super().__init__(*args, **kwargs)
743740

744-
if not flag_enabled("FEATURE_POLICY_AS_CODE_ENABLED") and 'opa_query_path' in self.fields:
745-
self.fields.pop('opa_query_path')
746-
747741
def validate_opa_query_path(self, value):
748742
# Decode the URL and re-encode it
749743
decoded_value = urllib.parse.unquote(value)
@@ -755,7 +749,7 @@ def validate_opa_query_path(self, value):
755749
return value
756750

757751

758-
class UnifiedJobTemplateSerializer(BaseSerializer, OpaQueryPathEnabledMixin):
752+
class UnifiedJobTemplateSerializer(BaseSerializer, OpaQueryPathMixin):
759753
# As a base serializer, the capabilities prefetch is not used directly,
760754
# instead they are derived from the Workflow Job Template Serializer and the Job Template Serializer, respectively.
761755
capabilities_prefetch = []
@@ -1188,7 +1182,7 @@ class Meta:
11881182
fields = ('*', '-is_system_auditor')
11891183

11901184

1191-
class OrganizationSerializer(BaseSerializer, OpaQueryPathEnabledMixin):
1185+
class OrganizationSerializer(BaseSerializer, OpaQueryPathMixin):
11921186
show_capabilities = ['edit', 'delete']
11931187

11941188
class Meta:
@@ -1547,7 +1541,7 @@ def get_summary_fields(self, obj):
15471541
return res
15481542

15491543

1550-
class InventorySerializer(LabelsListMixin, BaseSerializerWithVariables, OpaQueryPathEnabledMixin):
1544+
class InventorySerializer(LabelsListMixin, BaseSerializerWithVariables, OpaQueryPathMixin):
15511545
show_capabilities = ['edit', 'delete', 'adhoc', 'copy']
15521546
capabilities_prefetch = ['admin', 'adhoc', {'copy': 'organization.inventory_admin'}]
15531547

awx/main/conf.py

Lines changed: 119 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Django
55
from django.core.checks import Error
66
from django.utils.translation import gettext_lazy as _
7-
from django.conf import settings
87

98
# Django REST Framework
109
from rest_framework import serializers
@@ -975,123 +974,122 @@ def csrf_trusted_origins_validate(serializer, attrs):
975974
register_validate('system', csrf_trusted_origins_validate)
976975

977976

978-
if settings.FEATURE_POLICY_AS_CODE_ENABLED: # Unable to use flag_enabled due to AppRegistryNotReady error
979-
register(
980-
'OPA_HOST',
981-
field_class=fields.CharField,
982-
label=_('OPA server hostname'),
983-
default='',
984-
help_text=_('The hostname used to connect to the OPA server. If empty, policy enforcement will be disabled.'),
985-
category=('PolicyAsCode'),
986-
category_slug='policyascode',
987-
allow_blank=True,
988-
)
989-
990-
register(
991-
'OPA_PORT',
992-
field_class=fields.IntegerField,
993-
label=_('OPA server port'),
994-
default=8181,
995-
help_text=_('The port used to connect to the OPA server. Defaults to 8181.'),
996-
category=('PolicyAsCode'),
997-
category_slug='policyascode',
998-
)
999-
1000-
register(
1001-
'OPA_SSL',
1002-
field_class=fields.BooleanField,
1003-
label=_('Use SSL for OPA connection'),
1004-
default=False,
1005-
help_text=_('Enable or disable the use of SSL to connect to the OPA server. Defaults to false.'),
1006-
category=('PolicyAsCode'),
1007-
category_slug='policyascode',
1008-
)
1009-
1010-
register(
1011-
'OPA_AUTH_TYPE',
1012-
field_class=fields.ChoiceField,
1013-
label=_('OPA authentication type'),
1014-
choices=[OPA_AUTH_TYPES.NONE, OPA_AUTH_TYPES.TOKEN, OPA_AUTH_TYPES.CERTIFICATE],
1015-
default=OPA_AUTH_TYPES.NONE,
1016-
help_text=_('The authentication type that will be used to connect to the OPA server: "None", "Token", or "Certificate".'),
1017-
category=('PolicyAsCode'),
1018-
category_slug='policyascode',
1019-
)
1020-
1021-
register(
1022-
'OPA_AUTH_TOKEN',
1023-
field_class=fields.CharField,
1024-
label=_('OPA authentication token'),
1025-
default='',
1026-
help_text=_(
1027-
'The token for authentication to the OPA server. Required when OPA_AUTH_TYPE is "Token". If an authorization header is defined in OPA_AUTH_CUSTOM_HEADERS, it will be overridden by OPA_AUTH_TOKEN.'
1028-
),
1029-
category=('PolicyAsCode'),
1030-
category_slug='policyascode',
1031-
allow_blank=True,
1032-
encrypted=True,
1033-
)
1034-
1035-
register(
1036-
'OPA_AUTH_CLIENT_CERT',
1037-
field_class=fields.CharField,
1038-
label=_('OPA client certificate content'),
1039-
default='',
1040-
help_text=_('The content of the client certificate file for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1041-
category=('PolicyAsCode'),
1042-
category_slug='policyascode',
1043-
allow_blank=True,
1044-
)
1045-
1046-
register(
1047-
'OPA_AUTH_CLIENT_KEY',
1048-
field_class=fields.CharField,
1049-
label=_('OPA client key content'),
1050-
default='',
1051-
help_text=_('The content of the client key for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1052-
category=('PolicyAsCode'),
1053-
category_slug='policyascode',
1054-
allow_blank=True,
1055-
encrypted=True,
1056-
)
1057-
1058-
register(
1059-
'OPA_AUTH_CA_CERT',
1060-
field_class=fields.CharField,
1061-
label=_('OPA CA certificate content'),
1062-
default='',
1063-
help_text=_('The content of the CA certificate for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1064-
category=('PolicyAsCode'),
1065-
category_slug='policyascode',
1066-
allow_blank=True,
1067-
)
1068-
1069-
register(
1070-
'OPA_AUTH_CUSTOM_HEADERS',
1071-
field_class=fields.DictField,
1072-
label=_('OPA custom authentication headers'),
1073-
default={},
1074-
help_text=_('Optional custom headers included in requests to the OPA server. Defaults to empty dictionary ({}).'),
1075-
category=('PolicyAsCode'),
1076-
category_slug='policyascode',
1077-
)
1078-
1079-
register(
1080-
'OPA_REQUEST_TIMEOUT',
1081-
field_class=fields.FloatField,
1082-
label=_('OPA request timeout'),
1083-
default=1.5,
1084-
help_text=_('The number of seconds after which the connection to the OPA server will time out. Defaults to 1.5 seconds.'),
1085-
category=('PolicyAsCode'),
1086-
category_slug='policyascode',
1087-
)
1088-
1089-
register(
1090-
'OPA_REQUEST_RETRIES',
1091-
field_class=fields.IntegerField,
1092-
label=_('OPA request retry count'),
1093-
default=2,
1094-
help_text=_('The number of retry attempts for connecting to the OPA server. Default is 2.'),
1095-
category=('PolicyAsCode'),
1096-
category_slug='policyascode',
1097-
)
977+
register(
978+
'OPA_HOST',
979+
field_class=fields.CharField,
980+
label=_('OPA server hostname'),
981+
default='',
982+
help_text=_('The hostname used to connect to the OPA server. If empty, policy enforcement will be disabled.'),
983+
category=('PolicyAsCode'),
984+
category_slug='policyascode',
985+
allow_blank=True,
986+
)
987+
988+
register(
989+
'OPA_PORT',
990+
field_class=fields.IntegerField,
991+
label=_('OPA server port'),
992+
default=8181,
993+
help_text=_('The port used to connect to the OPA server. Defaults to 8181.'),
994+
category=('PolicyAsCode'),
995+
category_slug='policyascode',
996+
)
997+
998+
register(
999+
'OPA_SSL',
1000+
field_class=fields.BooleanField,
1001+
label=_('Use SSL for OPA connection'),
1002+
default=False,
1003+
help_text=_('Enable or disable the use of SSL to connect to the OPA server. Defaults to false.'),
1004+
category=('PolicyAsCode'),
1005+
category_slug='policyascode',
1006+
)
1007+
1008+
register(
1009+
'OPA_AUTH_TYPE',
1010+
field_class=fields.ChoiceField,
1011+
label=_('OPA authentication type'),
1012+
choices=[OPA_AUTH_TYPES.NONE, OPA_AUTH_TYPES.TOKEN, OPA_AUTH_TYPES.CERTIFICATE],
1013+
default=OPA_AUTH_TYPES.NONE,
1014+
help_text=_('The authentication type that will be used to connect to the OPA server: "None", "Token", or "Certificate".'),
1015+
category=('PolicyAsCode'),
1016+
category_slug='policyascode',
1017+
)
1018+
1019+
register(
1020+
'OPA_AUTH_TOKEN',
1021+
field_class=fields.CharField,
1022+
label=_('OPA authentication token'),
1023+
default='',
1024+
help_text=_(
1025+
'The token for authentication to the OPA server. Required when OPA_AUTH_TYPE is "Token". If an authorization header is defined in OPA_AUTH_CUSTOM_HEADERS, it will be overridden by OPA_AUTH_TOKEN.'
1026+
),
1027+
category=('PolicyAsCode'),
1028+
category_slug='policyascode',
1029+
allow_blank=True,
1030+
encrypted=True,
1031+
)
1032+
1033+
register(
1034+
'OPA_AUTH_CLIENT_CERT',
1035+
field_class=fields.CharField,
1036+
label=_('OPA client certificate content'),
1037+
default='',
1038+
help_text=_('The content of the client certificate file for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1039+
category=('PolicyAsCode'),
1040+
category_slug='policyascode',
1041+
allow_blank=True,
1042+
)
1043+
1044+
register(
1045+
'OPA_AUTH_CLIENT_KEY',
1046+
field_class=fields.CharField,
1047+
label=_('OPA client key content'),
1048+
default='',
1049+
help_text=_('The content of the client key for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1050+
category=('PolicyAsCode'),
1051+
category_slug='policyascode',
1052+
allow_blank=True,
1053+
encrypted=True,
1054+
)
1055+
1056+
register(
1057+
'OPA_AUTH_CA_CERT',
1058+
field_class=fields.CharField,
1059+
label=_('OPA CA certificate content'),
1060+
default='',
1061+
help_text=_('The content of the CA certificate for mTLS authentication to the OPA server. Required when OPA_AUTH_TYPE is "Certificate".'),
1062+
category=('PolicyAsCode'),
1063+
category_slug='policyascode',
1064+
allow_blank=True,
1065+
)
1066+
1067+
register(
1068+
'OPA_AUTH_CUSTOM_HEADERS',
1069+
field_class=fields.DictField,
1070+
label=_('OPA custom authentication headers'),
1071+
default={},
1072+
help_text=_('Optional custom headers included in requests to the OPA server. Defaults to empty dictionary ({}).'),
1073+
category=('PolicyAsCode'),
1074+
category_slug='policyascode',
1075+
)
1076+
1077+
register(
1078+
'OPA_REQUEST_TIMEOUT',
1079+
field_class=fields.FloatField,
1080+
label=_('OPA request timeout'),
1081+
default=1.5,
1082+
help_text=_('The number of seconds after which the connection to the OPA server will time out. Defaults to 1.5 seconds.'),
1083+
category=('PolicyAsCode'),
1084+
category_slug='policyascode',
1085+
)
1086+
1087+
register(
1088+
'OPA_REQUEST_RETRIES',
1089+
field_class=fields.IntegerField,
1090+
label=_('OPA request retry count'),
1091+
default=2,
1092+
help_text=_('The number of retry attempts for connecting to the OPA server. Default is 2.'),
1093+
category=('PolicyAsCode'),
1094+
category_slug='policyascode',
1095+
)

awx/main/tasks/policy.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from django.conf import settings
1010
from django.utils.translation import gettext_lazy as _
11-
from flags.state import flag_enabled
1211
from opa_client import OpaClient
1312
from opa_client.base import BaseClient
1413
from requests import HTTPError
@@ -364,9 +363,6 @@ def opa_client(headers=None):
364363

365364
def evaluate_policy(instance):
366365
# Policy evaluation for Policy as Code feature
367-
if not flag_enabled("FEATURE_POLICY_AS_CODE_ENABLED"):
368-
return
369-
370366
if not settings.OPA_HOST:
371367
return
372368

awx/main/tests/functional/test_policy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ def _parse_exception_message(exception: PolicyEvaluationError):
3636

3737

3838
@pytest.fixture(autouse=True)
39-
def enable_flag():
39+
def setup_opa_settings():
4040
with override_settings(
4141
OPA_HOST='opa.example.com',
42-
FLAGS={"FEATURE_POLICY_AS_CODE_ENABLED": [("boolean", True)]},
43-
FLAG_SOURCES=('flags.sources.SettingsFlagsSource',),
4442
):
4543
yield
4644

awx/settings/defaults.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,6 @@
10921092
# Older records will be cleaned up
10931093
INDIRECT_HOST_AUDIT_RECORD_MAX_AGE_DAYS = 7
10941094

1095-
1096-
# setting for Policy as Code feature
1097-
FEATURE_POLICY_AS_CODE_ENABLED = False
1098-
10991095
OPA_HOST = '' # The hostname used to connect to the OPA server. If empty, policy enforcement will be disabled.
11001096
OPA_PORT = 8181 # The port used to connect to the OPA server. Defaults to 8181.
11011097
OPA_SSL = False # Enable or disable the use of SSL to connect to the OPA server. Defaults to false.
@@ -1113,7 +1109,6 @@
11131109
FLAG_SOURCES = ('flags.sources.SettingsFlagsSource',)
11141110
FLAGS = {
11151111
'FEATURE_INDIRECT_NODE_COUNTING_ENABLED': [{'condition': 'boolean', 'value': False}],
1116-
'FEATURE_POLICY_AS_CODE_ENABLED': [{'condition': 'boolean', 'value': False}],
11171112
'FEATURE_DISPATCHERD_ENABLED': [{'condition': 'boolean', 'value': False}],
11181113
}
11191114

awx_collection/test/awx/test_completeness.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
needs_param_development = {
9494
'host': ['instance_id'],
9595
'workflow_approval': ['description', 'execution_environment'],
96+
'inventory': ['opa_query_path'],
97+
'job_template': ['opa_query_path'],
98+
'organization': ['opa_query_path'],
9699
}
97100
# -----------------------------------------------------------------------------------------------------------
98101

0 commit comments

Comments
 (0)