Enable CORS in tutor AWS AMI

Hey Everyone,

We’re building a vue3 app on top of openedx APIs, while calling the APIs we’re getting CORS errors.

I tired updating CORS wishlist urls in production.py both in LMS and CMS, still the error persists.

To install Openedx, I have used AWS tutor AMI.
I also tried creating a plugin, but couldn’t add plugin indexes to enable plugin.

What’s the suggested why, I can add few URLs on my server so that apps from those URLs don’t get CORS errors.

Thanks,
Aish

1 Like

I have also tried changing the config to allow all CORS, still my app is getting CORS error

Tutor webui commands I followed

config save --set "FEATURES_CORS_ORIGIN_ALLOW_ALL=true"

config save --set "CMS_FEATURES_CORS_ORIGIN_ALLOW_ALL=true"

config save

tutor local quickstart

Error I am getting:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://edx.myllama.co/oauth2/access_token?username=d&password=dd&grant_type=password&client_id=********. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 400

I think you need to write a plugin for that and add your hosts, added to LOGIN_REDIRECT_WHITELIST, CORS_ORIGIN_WHITELIST and CSRF_TRUSTED_ORIGINS. something like:

#let's call this file myplugin.py
from tutor import hooks

hooks.Filters.ENV_PATCHES.add_items(
    [
        (
            "openedx-lms-production-settings",
            """
LOGIN_REDIRECT_WHITELIST.append("{{ YOUR_HOST }}")
CORS_ORIGIN_WHITELIST.append("{% if ENABLE_HTTPS %}https://{% else %}http://{% endif %}{{ YOUR_HOST }}")
CSRF_TRUSTED_ORIGINS.append("{{ YOUR_HOST }}")
    """,
        ),
        (
            "openedx-lms-production-settings",
            """
LOGIN_REDIRECT_WHITELIST.append("{{ YOUR_HOST }}")
CORS_ORIGIN_WHITELIST.append("{% if ENABLE_HTTPS %}https://{% else %}http://{% endif %}{{ YOUR_HOST }}")
CSRF_TRUSTED_ORIGINS.append("{{ YOUR_HOST }}")
    """,
        ),
    ]
)

save the plugin above inside your tutor-plugins directory, and:

tutor config save -s YOUR_HOST=example.com
tutor plugins enable myplugin
tutor config save

I hope this helps.

2 Likes

Thanks for the reply Emad!

I tried creating the file in the proper location : /home/tutor/.local/share/tutor-plugins, with the name cors.yml

Got this error while trying to add the plugin

> config save -s YOUR_HOST=staging-lms.myllama.co
Configuration saved to /home/tutor/.local/share/tutor/config.yml
Environment generated in /home/tutor/.local/share/tutor/env

>  plugins enable cors
Error: plugin 'cors' is not installed.

> plugins install cors
Error: Plugin 'cors' could not be found in indexes

> plugins index add cors
Configuration saved to /home/tutor/.local/share/tutor/config.yml
Fetching index https://overhang.io/tutor/main/palm/plugins.yml...
Fetching index mycorsplugin/palm/plugins.yml...
  Failed to update index. File could not be found: [Errno 2] No such file or directory: 'mycorsplugin/palm/plugins.yml'
Fetching index cors/palm/plugins.yml...
  Failed to update index. File could not be found: [Errno 2] No such file or directory: 'cors/palm/plugins.yml'
Plugin index local cache: /home/tutor/.local/share/tutor/env/plugins/index/cache.yml
>

Created the file using ssh to my instance, can’t use tutor there, so I used webui to add the plugin.

Thanks Emad,

I was able to solve this issue with the code you provided, was having some trouble in enabling the plugin.
Was able to achive that from the steps mentioned in this doc Creating a Tutor plugin — Tutor documentation