Skip to content

Commit

Permalink
feat: Support sending OAuth token to codejail service
Browse files Browse the repository at this point in the history
  • Loading branch information
timmc-edx committed Jan 8, 2024
1 parent 83d104f commit 343bc99
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion xmodule/capa/safe_exec/remote_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from codejail.safe_exec import SafeExecException
from django.conf import settings
from edx_rest_api_client.client import OAuthAPIClient
from edx_toggles.toggles import SettingToggle
from requests.exceptions import RequestException, HTTPError
from simplejson import JSONDecodeError
Expand Down Expand Up @@ -48,6 +49,42 @@ def get_remote_exec(*args, **kwargs):
return remote_exec_function(*args, **kwargs)


# .. setting_name: CODE_JAIL_REST_SERVICE_OAUTH_URL
# .. setting_default: None
# .. setting_description: The OAuth server to get access tokens from when making calls to
# the codejail service. Requires setting CODE_JAIL_REST_SERVICE_CLIENT_ID and
# CODE_JAIL_REST_SERVICE_CLIENT_SECRET. If not specified, no authorization header will
# be sent.
CODE_JAIL_REST_SERVICE_OAUTH_URL = getattr(settings, 'CODE_JAIL_REST_SERVICE_OAUTH_URL', None)
# .. setting_name: CODE_JAIL_REST_SERVICE_CLIENT_ID
# .. setting_default: None
# .. setting_description: The OAuth client credential ID to use when making calls to
# the codejail service. If not specified, no authorization header will be sent.
CODE_JAIL_REST_SERVICE_CLIENT_ID = getattr(settings, 'CODE_JAIL_REST_SERVICE_CLIENT_ID', None)
# .. setting_name: CODE_JAIL_REST_SERVICE_CLIENT_SECRET
# .. setting_default: None
# .. setting_description: The OAuth client credential secret to use when making calls to
# the codejail service. If not specified, no authorization header will be sent.
CODE_JAIL_REST_SERVICE_CLIENT_SECRET = getattr(settings, 'CODE_JAIL_REST_SERVICE_CLIENT_SECRET', None)


def _get_codejail_client():
"""
Return a ``requests`` compatible HTTP client that has .get(...) and .post(...) methods.
The client will send an OAuth token if the appropriate CODE_JAIL_REST_SERVICE_* settings
are configured.
"""
if CODE_JAIL_REST_SERVICE_OAUTH_URL and CODE_JAIL_REST_SERVICE_CLIENT_ID and CODE_JAIL_REST_SERVICE_CLIENT_SECRET:
return OAuthAPIClient(
base_url=CODE_JAIL_REST_SERVICE_OAUTH_URL,
client_id=CODE_JAIL_REST_SERVICE_CLIENT_ID,
client_secret=CODE_JAIL_REST_SERVICE_CLIENT_SECRET,
)
else:
return requests


def get_codejail_rest_service_endpoint():
return f"{settings.CODE_JAIL_REST_SERVICE_HOST}/api/v0/code-exec"

Expand All @@ -68,8 +105,9 @@ def send_safe_exec_request_v0(data):
codejail_service_endpoint = get_codejail_rest_service_endpoint()
payload = json.dumps(data)

client = _get_codejail_client()
try:
response = requests.post(
response = client.post(
codejail_service_endpoint,
files=extra_files,
data={'payload': payload},
Expand Down

0 comments on commit 343bc99

Please sign in to comment.