Skip to content

Commit

Permalink
Merge pull request #144 from arnoldknott/stage
Browse files Browse the repository at this point in the history
Stage: refactors jwks database and disconnects frontend from redis at shutdown
  • Loading branch information
arnoldknott authored Dec 6, 2024
2 parents fdc645a + 860a3f7 commit 0b13d44
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 44 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/backendAPI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
REDIS_ARGS: ${{ secrets.REDIS_ARGS }}
REDIS_HOST: ${{ vars.REDIS_HOST }}
REDIS_PORT: ${{ vars.REDIS_PORT }}
REDIS_JWKS_DB: ${{ vars.REDIS_JWKS_DB }}
REDIS_SESSION_DB: ${{ vars.REDIS_SESSION_DB }}
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
MONGODB_HOST: ${{ vars.MONGODB_HOST }}
Expand Down Expand Up @@ -92,8 +91,6 @@ jobs:
# echo $REDIS_HOST
# echo "=== REDIS_PORT ==="
# echo $REDIS_PORT
# echo "=== REDIS_JWKS_DB ==="
# echo $REDIS_JWKS_DB
# echo "=== REDIS_SESSION_DB ==="
# echo $REDIS_SESSION_DB
- uses: actions/checkout@v4
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/infrastructure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ jobs:
# Redis settings:
REDIS_PORT: ${{ secrets.REDIS_PORT }}
REDIS_INSIGHT_PORT: ${{ secrets.REDIS_INSIGHT_PORT }}
REDIS_JWKS_DB: ${{ secrets.REDIS_JWKS_DB }}
REDIS_SESSION_DB: ${{ secrets.REDIS_SESSION_DB }}
# public ssh key for the VM:
SSH_PUBLIC_KEY: ${{ secrets.SSH_PUBLIC_KEY }}
Expand Down Expand Up @@ -196,7 +195,6 @@ jobs:
-var "postgres_port=${POSTGRES_PORT}" \
-var "redis_port=${REDIS_PORT}" \
-var "redis_insight_port=${REDIS_INSIGHT_PORT}" \
-var "redis_jwks_db=${REDIS_JWKS_DB}" \
-var "redis_session_db=${REDIS_SESSION_DB}" \
-var "public_ssh_key_path=${PUBLIC_SSH_KEY_PATH}"
Expand Down
1 change: 0 additions & 1 deletion backendAPI/src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ PGADMIN_DEFAULT_PASSWORD=""
REDIS_HOST=''
REDIS_PORT=''
# sync database numbers with all other services!
REDIS_JWKS_DB=''
REDIS_SESSION_DB=''
REDIS_PASSWORD=""
REDIS_ARGS="--save 500 1 --requirepass <...>"
8 changes: 4 additions & 4 deletions backendAPI/src/core/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
# print(config.REDIS_HOST)
# print("=== config.REDIS_PORT ===")
# print(config.REDIS_PORT)
# print("=== config.REDIS_JWKS_DB ===")
# print(config.REDIS_JWKS_DB)
# print("=== config.REDIS_SESSION_DB ===")
# print(config.REDIS_SESSION_DB)

redis_jwks_client = redis.Redis(
redis_session_client = redis.Redis(
host=config.REDIS_HOST,
port=config.REDIS_PORT,
password=config.REDIS_PASSWORD,
db=config.REDIS_JWKS_DB,
db=config.REDIS_SESSION_DB,
)

# print("=== cache.py finished ===")
6 changes: 3 additions & 3 deletions backendAPI/src/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def build_postgres_url(cls, url: Optional[str], values: ValidationInfo) -> Any:
REDIS_PORT: int = int(os.getenv("REDIS_PORT"))
# print("=== REDIS_PORT ===")
# print(REDIS_PORT)
# print("=== get_variable('REDIS_REDIS_JWKS_DB') ===")
# print(get_variable("REDIS_JWKS_DB"))
REDIS_JWKS_DB: int = int(get_variable("REDIS_JWKS_DB"))
# print("=== get_variable('REDIS_REDIS_SESSION_DB') ===")
# print(get_variable("REDIS_SESSION_DB"))
REDIS_SESSION_DB: int = int(get_variable("REDIS_SESSION_DB"))
REDIS_PASSWORD: str = get_variable("REDIS_PASSWORD")


Expand Down
9 changes: 4 additions & 5 deletions backendAPI/src/core/security.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
from typing import List, Optional

Expand All @@ -11,7 +10,7 @@
from fastapi import Depends, HTTPException, Request
from jwt.algorithms import RSAAlgorithm

from core.cache import redis_jwks_client
from core.cache import redis_session_client
from core.config import config
from core.types import CurrentUserData, GuardTypes
from crud.identity import UserCRUD
Expand Down Expand Up @@ -54,12 +53,12 @@ async def get_azure_jwks(no_cache: bool = False):
if no_cache is False:
# print("=== no_cache ===")
# print(no_cache)
jwks = redis_jwks_client.json().get("jwks")
jwks = redis_session_client.json().get("jwks:microsoft")
# print("=== jwks ===")
# print(jwks)
if jwks:
# print("=== 🔑 JWKS fetched from cache ===")
return json.loads(jwks)
return jwks
else:
await get_azure_jwks(no_cache=True)
else:
Expand All @@ -78,7 +77,7 @@ async def get_azure_jwks(no_cache: bool = False):
)
try:
# TBD: for real multi-tenant applications, the cache-key should be tenant specific
redis_jwks_client.json().set("jwks", ".", json.dumps(jwks))
redis_session_client.json().set("jwks:microsoft", ".", jwks)
logger.info("🔑 Setting JWKs in cache")
print("=== 🔑 JWKS set in cache ===")
return jwks
Expand Down
1 change: 0 additions & 1 deletion backendAPI/src/tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ POSTGRES_PASSWORD=""
REDIS_HOST=""
REDIS_PORT=""
# sync database numbers with all other services!
REDIS_JWKS_DB=''
REDIS_SESSION_DB=''
REDIS_PASSWORD=""
REDIS_ARGS="--save 500 1 --requirepass <...>"
3 changes: 0 additions & 3 deletions compose.override.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ services:
- POSTGRES_PASSWORD=$POSTGRES_PASSWORD
- REDIS_HOST=$REDIS_HOST
- REDIS_PORT=$REDIS_PORT
- REDIS_JWKS_DB=$REDIS_JWKS_DB
- REDIS_SESSION_DB=$REDIS_SESSION_DB
- REDIS_PASSWORD=$REDIS_PASSWORD
networks:
Expand Down Expand Up @@ -52,7 +51,6 @@ services:
args:
- REDIS_HOST=$REDIS_HOST
- REDIS_PORT=$REDIS_PORT
- REDIS_JWKS_DB=$REDIS_JWKS_DB
- REDIS_SESSION_DB=$REDIS_SESSION_DB
- REDIS_PASSWORD=$REDIS_PASSWORD
- APP_REG_CLIENT_ID=$APP_REG_CLIENT_ID
Expand All @@ -62,7 +60,6 @@ services:
environment:
- REDIS_HOST=$REDIS_HOST
- REDIS_PORT=$REDIS_PORT
- REDIS_JWKS_DB=$REDIS_JWKS_DB
- REDIS_SESSION_DB=$REDIS_SESSION_DB
- REDIS_PASSWORD=$REDIS_PASSWORD
- APP_REG_CLIENT_ID=$APP_REG_CLIENT_ID
Expand Down
1 change: 0 additions & 1 deletion frontend_svelte/src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ API_SCOPE=''
REDIS_HOST=''
REDIS_PORT=''
# sync database numbers with all other services!
REDIS_JWKS_DB=''
REDIS_SESSION_DB=''
REDIS_PASSWORD=""
REDIS_ARGS="--save 500 1 --requirepass <...>"
7 changes: 6 additions & 1 deletion frontend_svelte/src/lib/server/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RedisCache {
}
}

// TBD: what about disconnecting?
// TBD: what about disconnecting?
private async connectClient() {
try {
await this.redisClient?.connect();
Expand Down Expand Up @@ -78,7 +78,10 @@ class RedisCache {

// TBD: stop the client when the server is stopped!
public stopClient() {
this.redisClient?.disconnect();
console.log('👍 🥞 cache - server - stopClient - redisClient disconnected');
this.redisClient?.quit();
console.log('👍 🥞 cache - server - stopClient - redisClient quit');
}

public async setSession(
Expand Down Expand Up @@ -156,6 +159,8 @@ class RedisCache {

export const redisCache = new RedisCache();

process.on('exit', () => redisCache?.stopClient());

// OLD CODE:

// let redisClient: RedisClientType | null = null;
Expand Down
8 changes: 6 additions & 2 deletions frontend_svelte/src/lib/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ export default class AppConfig {
// console.log("📜 app_config - process.env.AZ_CLIENT_ID:");
// console.log(process.env.AZ_CLIENT_ID);
if (!process.env.AZ_CLIENT_ID || !process.env.AZ_KEYVAULT_HOST) {
console.error('🥞 app_config - server - connectKeyvault - keyvault connection data missing');
throw new Error('🥞 app_config - server - connectKeyvault - keyvault connection data missing');
console.error(
'🥞 app_config - server - connectKeyvault - keyvault connection data missing'
);
throw new Error(
'🥞 app_config - server - connectKeyvault - keyvault connection data missing'
);
}
const credential = new ManagedIdentityCredential(process.env.AZ_CLIENT_ID);
const client = new SecretClient(process.env.AZ_KEYVAULT_HOST, credential);
Expand Down
1 change: 0 additions & 1 deletion infrastructure/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ POSTGRES_PORT=
# cache settings:
REDIS_PORT =
REDIS_INSIGHT_PORT =
REDIS_JWKS_DB =
REDIS_SESSION_DB =

# public ssh key for virtual machine login:
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/containerApps.tf
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ resource "azurerm_container_app" "BackendContainer" {
value = var.redis_port
}
env {
name = "REDIS_JWKS_DB"
value = var.redis_jwks_db
name = "REDIS_SESSION_DB"
value = var.redis_session_db
}
}
volume {
Expand Down
6 changes: 0 additions & 6 deletions infrastructure/security.tf
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,6 @@ resource "azurerm_key_vault_secret" "redisArgs" {
key_vault_id = azurerm_key_vault.keyVault.id
}

resource "azurerm_key_vault_secret" "redisJwksDb" {
name = "redis-jwks-db"
value = 14
key_vault_id = azurerm_key_vault.keyVault.id
}

resource "azurerm_key_vault_secret" "redisSessionDb" {
name = "redis-session-db"
value = 15
Expand Down
5 changes: 0 additions & 5 deletions infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ variable "redis_insight_port" {
type = number
}

variable "redis_jwks_db" {
description = "Database number for storing JSON Web Keys in Redis cache"
type = number
}

variable "redis_session_db" {
description = "Database number for storing session data in Redis cache"
type = number
Expand Down
4 changes: 0 additions & 4 deletions scripts/deploy_infrastructure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ tofu plan -out=${WORKSPACE}.tfplan \
-var "postgres_port=${POSTGRES_PORT}" \
-var "redis_port=${REDIS_PORT}" \
-var "redis_insight_port=${REDIS_INSIGHT_PORT}" \
-var "redis_jwks_db=${REDIS_JWKS_DB}" \
-var "redis_session_db=${REDIS_SESSION_DB}" \
-var "public_ssh_key_path=${PUBLIC_SSH_KEY_PATH}"'

Expand All @@ -214,7 +213,6 @@ tofu plan -out=${WORKSPACE}.tfplan \
# -var "postgres_port=${POSTGRES_PORT}" \
# -var "redis_port=${REDIS_PORT}" \
# -var "redis_insight_port=${REDIS_INSIGHT_PORT}" \
# -var "redis_jwks_db=${REDIS_JWKS_DB}" \
# -var "redis_session_db=${REDIS_SESSION_DB}" \
# -var "public_ssh_key_path=${PUBLIC_SSH_KEY_PATH}"
# -var "owner_user_principal_name=${OWNER_USER_PRINCIPAL_NAME}" \
Expand Down Expand Up @@ -300,7 +298,6 @@ exit $tofu_changes_applied
# -var "postgres_port=${POSTGRES_PORT}" \
# -var "redis_port=${REDIS_PORT}" \
# -var "redis_insight_port=${REDIS_INSIGHT_PORT}" \
# -var "redis_jwks_db=${REDIS_JWKS_DB}" \
# -var "redis_session_db=${REDIS_SESSION_DB}" \
# -var "public_ssh_key_path=${PUBLIC_SSH_KEY_PATH}" &&
# echo "=== tofu - approval before apply ===" &&
Expand All @@ -320,7 +317,6 @@ exit $tofu_changes_applied
# -var "postgres_port=${POSTGRES_PORT}" \
# -var "redis_port=${REDIS_PORT}" \
# -var "redis_insight_port=${REDIS_INSIGHT_PORT}" \
# -var "redis_jwks_db=${REDIS_JWKS_DB}" \
# -var "redis_session_db=${REDIS_SESSION_DB}" \
# -var "public_ssh_key_path=${PUBLIC_SSH_KEY_PATH}" &&
# tofu_plan_exit_code=$? &&
Expand Down

0 comments on commit 0b13d44

Please sign in to comment.