[Policy as Code] Monkey patch opa_client.base.BaseClient (#6865)

Workaround bug described in https://github.com/Turall/OPA-python-client/issues/29
This commit is contained in:
Hao Liu 2025-02-26 16:09:51 -05:00 committed by GitHub
parent 529ee73fcd
commit 698d769a7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -2,10 +2,13 @@ import json
import tempfile
import contextlib
from typing import Optional, Union
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from flags.state import flag_enabled
from opa_client import OpaClient
from opa_client.base import BaseClient
from requests import HTTPError
from rest_framework import serializers
from rest_framework import fields
@ -14,6 +17,29 @@ from awx.main import models
from awx.main.exceptions import PolicyEvaluationError
# Monkey patching opa_client.base.BaseClient to fix retries and timeout settings
_original_opa_base_client_init = BaseClient.__init__
def _opa_base_client_init_fix(
self,
host: str = "localhost",
port: int = 8181,
version: str = "v1",
ssl: bool = False,
cert: Optional[Union[str, tuple]] = None,
headers: Optional[dict] = None,
retries: int = 2,
timeout: float = 1.5,
):
_original_opa_base_client_init(self, host, port, version, ssl, cert, headers)
self.retries = retries
self.timeout = timeout
BaseClient.__init__ = _opa_base_client_init_fix
class _UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
@ -212,8 +238,8 @@ def opa_client(headers=None):
headers=headers,
ssl=settings.OPA_SSL,
cert=cert_temp_file_name,
retries=settings.OPA_REQUEST_RETRIES,
timeout=settings.OPA_REQUEST_TIMEOUT,
retries=settings.OPA_REQUEST_RETRIES,
) as client:
yield client

View File

@ -39,7 +39,7 @@ Markdown # used for formatting API help
maturin # pydantic-core build dep
msgpack<1.0.6 # 1.0.6+ requires cython>=3
msrestazure
OPA-python-client
OPA-python-client==2.0.2 # Code contain monkey patch targeted to 2.0.2 to fix https://github.com/Turall/OPA-python-client/issues/29
openshift
opentelemetry-api~=1.24 # new y streams can be drastically different, in a good way
opentelemetry-sdk~=1.24