Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure CORS in docker environment variables #608

Open
pasquy73 opened this issue Jun 20, 2023 · 6 comments
Open

How to configure CORS in docker environment variables #608

pasquy73 opened this issue Jun 20, 2023 · 6 comments

Comments

@pasquy73
Copy link
Contributor

I'm using STH-Comet in a docker-compose yaml file and have CORS issues. Here is an extract of yaml file (I'm using .env file too):

  sth-comet:
    image: fiware/sth-comet:${STH_COMET_VERSION}
    hostname: sth-comet
    container_name: fiware-sth-comet
    restart: always
    depends_on:
      - database-mongo
    networks:
      - monitoring-net
    ports:
      - ${STH_COMET_PORT}:${STH_COMET_PORT}
    environment:
      - STH_HOST=0.0.0.0
      - STH_PORT=${STH_COMET_PORT}
      - DB_PREFIX=sth_
      - DB_URI=database-mongo:${MONGO_DB_PORT}
      - LOGOPS_LEVEL=INFO

I want to enable CORS using environment variables like STH_HOST or DB_PREFIX.
I couldn't find the env var to set it from the documentation. Is it possible?
At the moment, I'm fixing the problem by simply copying the config.js file (within enabled: 'true' in config.cors) into the container in this way:

docker cp config.js <CONTAINER_ID>:/opt/sth

@fgalan
Copy link
Member

fgalan commented Jun 27, 2023

The env vars STH supports are described in https://github.com/telefonicaid/fiware-sth-comet/blob/master/doc/manuals/running.md. It seems there isn't any one related with CORS.

However, it shouldn't be difficult to add them, looking how others env vars have been implemented and do a similar thing. It would be great if you could provide a pull request with the improvement, pls ;)

Having said that, we don't use to enable CORS in backend services. But if you need it and want to implement it in STH, ok with us.

@pasquy73
Copy link
Contributor Author

I changed the sthConfiguration.js file in the lib/configuration folder (I used ENV.CORS_ENABLED var and I added an if/else clause before of 226 line) as follow:

if (ENV.CORS_ENABLED && !isNull(ENV.CORS_ENABLED)) {
    module.exports.corsEnabled = JSON.parse(ENV.CORS_ENABLED);
    sthLogger.info(module.exports.LOGGING_CONTEXT.STARTUP, 'CORS value set to: ' + module.exports.corsEnabled);
} else if (config && config.cors && config.cors.enabled) {

I tried a test locally (on my Win PC);
I also created (locally) a docker image (i.e. sth-comet:2.11.0) just copying the Dockerfile into the root directory, and I edited it to use my local changes.
It worked using - CORS_ENABLED=true env var in my docker-compose file (see above). In the log file: msg=CORS value set to: true

Please let me know if it should be updated docs and tests

@fgalan
Copy link
Member

fgalan commented Sep 14, 2023

I'd suggest to create a PR with the proposed fix, so we can provide feedback on it, please.

@pasquy73
Copy link
Contributor Author

I created locally a task/cors_conf_docker branch but I cannot push it in the repo because I'm not authorized.
Is it the right way to create a PR?

@fgalan
Copy link
Member

fgalan commented Sep 14, 2023

I created locally a task/cors_conf_docker branch but I cannot push it in the repo because I'm not authorized. Is it the right way to create a PR?

You can do it using a fork (eg: PR #605)

@fgalan
Copy link
Member

fgalan commented Oct 3, 2023

PR #620

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants