Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Use `https` for hyperlinked APIs by proxying through NGINX (#574)
Dhruv Bhanushali1ed4146889fd
diff --git a/api/catalog/api/serializers/base.py b/api/catalog/api/serializers/base.pyindex 1bd6dfa73..f07234d0b 100644--- a/api/catalog/api/serializers/base.py+++ b/api/catalog/api/serializers/base.py@@ -16,4 +16,9 @@ class SchemableHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):def get_url(self, *args, **kwargs):url = super().get_url(*args, **kwargs)- return re.sub(r"^\w+://", f"{self.scheme}://", url, 1)++ # Only rewrite URLs if a fixed scheme is provided+ if self.scheme is not None:+ re.sub(r"^\w+://", f"{self.scheme}://", url, 1)++ return urldiff --git a/api/catalog/settings.py b/api/catalog/settings.pyindex 16e92888a..7a925bbc9 100644--- a/api/catalog/settings.py+++ b/api/catalog/settings.py@@ -53,6 +53,7 @@ if lb_url := config("LOAD_BALANCER_URL", default=""):if DEBUG:ALLOWED_HOSTS += [+ "dev.openverse.test", # used in local development"localhost","127.0.0.1","0.0.0.0",@@ -307,7 +308,14 @@ VERBOSE_ES_RESPONSE = config("DEBUG_SCORES", default=False, cast=bool)USE_RANK_FEATURES = config("USE_RANK_FEATURES", default=True, cast=bool)# The scheme to use for the hyperlinks in the API responses-API_LINK_SCHEME = config("API_LINK_SCHEME", default="https")+API_LINK_SCHEME = config("API_LINK_SCHEME", default=None)++# Proxy handling, for production+if config("IS_PROXIED", default=True, cast=bool):+ # https://docs.djangoproject.com/en/4.0/ref/settings/#use-x-forwarded-host+ USE_X_FORWARDED_HOST = True+ # https://docs.djangoproject.com/en/4.0/ref/settings/#secure-proxy-ssl-header+ SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")SENTRY_DSN = config("SENTRY_DSN",diff --git a/api/docs/guides/https.md b/api/docs/guides/https.mdnew file mode 100644index 000000000..c78e06802--- /dev/null+++ b/api/docs/guides/https.md@@ -0,0 +1,13 @@+# Testing HTTPS++To emulate the proxying behavior of production, we have set up an NGINX proxy that serves the API over `https` by proxying to the Gunicorn server which serves over `http`.++This proxy uses certificates from the `nginx/certs/` directory. These certificates can be generated using `mkcert` with the `just cert` command.++Additionally, to test with the dev hostname `dev.openverse.test`, add the following line to your hosts file.++```text+127.0.0.1 dev.openverse.test+```++Visiting `http://dev.openverse.test:9080` in your browser should permanently redirect you to `https://dev.openverse.test:9443`.diff --git a/api/docs/guides/quickstart.md b/api/docs/guides/quickstart.mdindex 89fec9d7f..73bc51d41 100644--- a/api/docs/guides/quickstart.md+++ b/api/docs/guides/quickstart.md@@ -6,6 +6,9 @@- [Docker](https://docs.docker.com/install/)- [Docker Compose](https://docs.docker.com/compose/install/)- [Just](https://github.com/casey/just)+- [mkcert](https://github.com/FiloSottile/mkcert)++Ensure that you have installed `mkcert` (and the corresponding NSS tools). You can run `mkcert -install` to verify your installation.## Steps@@ -17,20 +20,25 @@cd openverse-api/```-3. From the monorepo root, bring up the Docker Compose system. Docker Compose will automatically read the necessary environment variables from `env.docker` files from project directories.+3. Generate locally-trusted certificates. These will be used by the NGINX proxy to serve the API over `https`.+ ```bash+ just cert+ ```++4. From the monorepo root, bring up the Docker Compose system. Docker Compose will automatically read the necessary environment variables from `env.docker` files from project directories.```bashjust up```-4. Point your browser to `http://localhost:8000`. You should be able to see the API documentation.+5. Point your browser to `http://localhost:8000`. You should be able to see the API documentation.-5. Load the sample data. This could take a couple of minutes.+6. Load the sample data. This could take a couple of minutes.```bashjust init```-6. Make an API request using cURL. You should receive a JSON response.+7. Make an API request using cURL. You should receive a JSON response.```bashjust stats```@@ -50,13 +58,13 @@}```-7. When done, bring the system down. To remove all volumes as well, pass the `-v` flag.+8. When done, bring the system down. To remove all volumes as well, pass the `-v` flag.```bashjust downjust down -v # removes volumes```-8. Use the `logs` command access the logs from all services. To isolate a service, pass the service name as an argument.+9. Use the `logs` command access the logs from all services. To isolate a service, pass the service name as an argument.```bashjust logsjust logs web # only shows logs web service@@ -72,6 +80,7 @@ The command `just up` spawns the following services:- [Elasticsearch](https://www.elastic.co/elasticsearch/)- [Redis](https://redis.io/)- [imageproxy](https://github.com/willnorris/imageproxy)+- [NGINX](http://nginx.org)- **web** (`api/`)- **ingestion_server** and **indexer_worker** (`ingestion_server/`)- **analytics** (`analytics/`)diff --git a/api/env.docker b/api/env.dockerindex 9bdd68123..e76c27c5a 100644--- a/api/env.docker+++ b/api/env.docker@@ -16,5 +16,3 @@ UPSTREAM_DATABASE_PORT="5432"SEMANTIC_VERSION="1.0.0"ELASTICSEARCH_URL="es"--API_LINK_SCHEME="http"diff --git a/api/env.template b/api/env.templateindex d911f5ff1..a59c4c706 100644--- a/api/env.template+++ b/api/env.template@@ -51,7 +51,7 @@ SEMANTIC_VERSION="1.0.0"#DEBUG_SCORES="False"#USE_RANK_FEATURES="True"-API_LINK_SCHEME="http"+IS_PROXIED="False"#SENTRY_SAMPLE_RATE=1.0#SENTRY_DSN="https://[email protected]/6107216"diff --git a/docker-compose.yml b/docker-compose.ymlindex 7aa69fbdb..b0182cac8 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -123,6 +123,16 @@ services:env_file:- ./analytics/env.docker+ proxy:+ image: nginx:alpine+ ports:+ - "9080:9080"+ - "9443:9443"+ depends_on:+ - web+ volumes:+ - ./nginx:/etc/nginx/conf.d+volumes:api-postgres:catalog-postgres:diff --git a/justfile b/justfileindex b9953584b..3312aeb6f 100644--- a/justfile+++ b/justfile@@ -81,6 +81,13 @@ precommit:lint:cd api && pipenv run pre-commit run --all-files+# Make locally trusted certificates+cert:+ mkdir -p nginx/certs/+ mkcert \+ -cert-file nginx/certs/openverse.crt \+ -key-file nginx/certs/openverse.key \+ dev.openverse.test localhost 127.0.0.1 ::1################## Elasticsearch #diff --git a/nginx/.gitignore b/nginx/.gitignorenew file mode 100644index 000000000..df9128702--- /dev/null+++ b/nginx/.gitignore@@ -0,0 +1 @@+certs/diff --git a/nginx/web.conf b/nginx/web.confnew file mode 100644index 000000000..807e96d5b--- /dev/null+++ b/nginx/web.conf@@ -0,0 +1,24 @@+server {+ listen 9080;+ server_name localhost;++ location / {+ return 301 https://$host:9443$request_uri;+ }+}++server {+ listen 9443 ssl;+ server_name localhost;++ location / {+ proxy_pass http://web:8000;++ proxy_set_header Host $host:$server_port;+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ proxy_set_header X-Forwarded-Proto $scheme;+ }++ ssl_certificate /etc/nginx/conf.d/certs/openverse.crt;+ ssl_certificate_key /etc/nginx/conf.d/certs/openverse.key;+}