From 796ac54207e2474c36373cd2fa9c370276b75421 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 9 Sep 2023 06:00:13 -0500 Subject: [PATCH] Upgraded PostgREST API --- _cli/sparrow_cli/commands/up.py | 3 +-- _cli/sparrow_cli/config/__init__.py | 28 +++++++++++++++++++++++++- _cli/sparrow_cli/config/environment.py | 9 --------- docker-compose.yaml | 9 +++++---- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/_cli/sparrow_cli/commands/up.py b/_cli/sparrow_cli/commands/up.py index db44ee87..9c4d595c 100644 --- a/_cli/sparrow_cli/commands/up.py +++ b/_cli/sparrow_cli/commands/up.py @@ -7,7 +7,6 @@ from sparrow_cli.help import echo_messages -from ..config.environment import validate_environment from ..config import SparrowConfig from ..util import compose, cmd, log from ..config.command_cache import get_backend_help_info @@ -40,9 +39,9 @@ def sparrow_up(ctx, container="", force_recreate=False): # Validate the presence of SPARROW_SECREY_KEY only if we are bringing # the application up. Eventually, this should be wrapped into a Python # version of the `sparrow up` command. - validate_environment() cfg = ctx.find_object(SparrowConfig) + cfg.validate_environment() echo_messages(cfg) diff --git a/_cli/sparrow_cli/config/__init__.py b/_cli/sparrow_cli/config/__init__.py index 32e29c58..deca1858 100644 --- a/_cli/sparrow_cli/config/__init__.py +++ b/_cli/sparrow_cli/config/__init__.py @@ -6,7 +6,7 @@ from os import environ from macrostrat.utils.shell import git_revision_info from macrostrat.utils.logs import get_logger, setup_stderr_logs -from pydantic import BaseModel +import hashlib import docker from docker.client import DockerClient from enum import Enum @@ -133,6 +133,8 @@ def __init__(self, verbose=False, offline=False): details=f"SPARROW_PATH={environ['SPARROW_PATH']}", ) + self.enhance_secret_key() + self.project_name = self.infer_project_name() self.local_frontend = self.configure_local_frontend() @@ -172,6 +174,30 @@ def __init__(self, verbose=False, offline=False): else: self.messages += prepare_compose_overrides(self) + def enhance_secret_key(self): + secret_key = environ.get("SPARROW_SECRET_KEY") + if secret_key is None: + return + if len(secret_key) < 32: + self.add_message( + id="short-secret-key", + text="SPARROW_SECRET_KEY is shorter than 32 characters", + level=Level.WARNING, + details="This should be a randomly generated string of at least 32 characters.", + ) + environ["SPARROW_SECRET_KEY"] = hashlib.md5( + secret_key.encode("utf-8") + ).hexdigest() + + def validate_environment(self): + """Validate environment for services""" + # Ensure that the SPARROW_SECRET_KEY is set and >= 32 characters long + secret_key = environ.get("SPARROW_SECRET_KEY") + if secret_key is None: + raise SparrowCommandError( + "You must set the SPARROW_SECRET_KEY environment variable." + ) + def _setup_command_path(self): _bin = self.SPARROW_PATH / "_cli" / "bin" self.bin_directories = [_bin] diff --git a/_cli/sparrow_cli/config/environment.py b/_cli/sparrow_cli/config/environment.py index 282f6f9d..bb0a5514 100644 --- a/_cli/sparrow_cli/config/environment.py +++ b/_cli/sparrow_cli/config/environment.py @@ -157,12 +157,3 @@ def add_override(name): environ["COMPOSE_FILE"] = ":".join(str(c) for c in compose_files) log.info(f"Docker compose overrides: {compose_files}") return messages - - -def validate_environment(): - # Check for failing environment - if environ.get("SPARROW_SECRET_KEY") is None: - print( - "[red]You [underline]must[/underline] set [bold]SPARROW_SECRET_KEY[/bold]. Exiting..." - ) - sys.exit(1) diff --git a/docker-compose.yaml b/docker-compose.yaml index 3ef722be..cd021dbf 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -134,12 +134,13 @@ services: timeout: 5s retries: 3 pg_api: - image: postgrest/postgrest:v10.0.0 + image: postgrest/postgrest:v11.2.0 environment: - - PGRST_DB_URI=postgresql://postgres:@db:5432/sparrow + - PGRST_DB_URI=postgresql://authenticator:@db:5432/sparrow - PGRST_DB_SCHEMAS=sparrow_api - - PGRST_DB_ANON_ROLE=postgres - - PGRST_DB_MAX_ROWS=1000 + - PGRST_DB_ANON_ROLE=view_public + - PGRST_JWT_SECRET=${SPARROW_SECRET_KEY} + - PGRST_DB_MAX_ROWS=100 profiles: - core depends_on: