fiware-sth-comet
fiware-sth-comet copied to clipboard
How to configure CORS in docker environment variables
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
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.
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
I'd suggest to create a PR with the proposed fix, so we can provide feedback on it, please.
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?
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 https://github.com/telefonicaid/fiware-sth-comet/pull/605)
PR #620