Skip to content

Commit

Permalink
switch to cloud sql connector for auth queue
Browse files Browse the repository at this point in the history
  • Loading branch information
bolyachevets committed Oct 25, 2024
1 parent 5f86ad7 commit 0fa2d1a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 5 deletions.
86 changes: 85 additions & 1 deletion queue_services/auth-queue/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions queue_services/auth-queue/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ zipp = "3.19.1"
auth-api = { git = "https://github.com/bcgov/sbc-auth.git", rev = "main", subdirectory = "auth-api" }
simple-cloudevent = { git = "https://github.com/daxiom/simple-cloudevent.py.git" }
build-deps = { git = "https://github.com/bcgov/sbc-auth.git", rev = "main", subdirectory = "build-deps" }
cloud-sql-python-connector = "^1.13.0"

[tool.poetry.group.dev.dependencies]
psycopg2 = "^2.9.9"
Expand Down
45 changes: 43 additions & 2 deletions queue_services/auth-queue/src/auth_queue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Resource package for the auth-queue service."""
import os
from dataclasses import dataclass

from auth_api.exceptions import ExceptionHandler
from auth_api.models import db
Expand All @@ -21,11 +22,39 @@
from auth_api.services.gcp_queue import queue
from auth_api.utils.cache import cache
from flask import Flask
from google.cloud.sql.connector import Connector

from auth_queue import config
from auth_queue import config as app_config
from auth_queue.resources.worker import bp as worker_endpoint


@dataclass
class DBConfig:
"""Database configuration settings."""
unix_sock: str
database: str
user: str
password: str


def getconn(connector: Connector, db_config: DBConfig) -> object:
"""Create a database connection.
Args:
connector (Connector): The Google Cloud SQL connector instance.
db_config (DBConfig): The database configuration.
Returns:
object: A connection object to the database.
"""
return connector.connect(
instance_connection_string=db_config.unix_sock.replace('/cloudsql/', ''),
ip_type='private',
user=db_config.user,
password=db_config.password,
db=db_config.database,
driver='pg8000',
)


def register_endpoints(app: Flask):
"""Register endpoints with the flask application."""
# Allow base route to match with, and without a trailing slash
Expand All @@ -41,9 +70,21 @@ def register_endpoints(app: Flask):
def create_app(run_mode=os.getenv('DEPLOYMENT_ENV', 'production')) -> Flask:
"""Return a configured Flask App using the Factory method."""
app = Flask(__name__)
app.config.from_object(config.get_named_config(run_mode))
app.config.from_object(app_config.get_named_config(run_mode))
app.config['ENV'] = run_mode

if app.config.get('DB_UNIX_SOCKET'):
connector = Connector(refresh_strategy='lazy')
db_config = DBConfig(
unix_sock=app.config.get('DB_UNIX_SOCKET'),
database=app.config.get('DB_NAME'),
user=app.config.get('DB_USER'),
password=app.config.get('DB_PASSWORD')
)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'creator': lambda: getconn(connector, db_config)
}

db.init_app(app)
flags.init_app(app)
cache.init_app(app)
Expand Down
3 changes: 1 addition & 2 deletions queue_services/auth-queue/src/auth_queue/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ class _Config: # pylint: disable=too-few-public-methods
DB_NAME = os.getenv('DATABASE_NAME', '')
DB_HOST = os.getenv('DATABASE_HOST', '')
DB_PORT = os.getenv('DATABASE_PORT', '5432')
SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}' # noqa: E231

if DB_UNIX_SOCKET := os.getenv('DATABASE_UNIX_SOCKET', None):
SQLALCHEMY_DATABASE_URI = f'postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}' # noqa: E231, E501
SQLALCHEMY_DATABASE_URI = 'postgresql+pg8000://'
else:
SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}' # noqa: E231, E501

Expand Down

0 comments on commit 0fa2d1a

Please sign in to comment.