Skip to content

Commit

Permalink
Upgraded PostgREST API
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Sep 9, 2023
1 parent d739f21 commit 796ac54
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
3 changes: 1 addition & 2 deletions _cli/sparrow_cli/commands/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
28 changes: 27 additions & 1 deletion _cli/sparrow_cli/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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]
Expand Down
9 changes: 0 additions & 9 deletions _cli/sparrow_cli/config/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 5 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 796ac54

Please sign in to comment.