-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(proxy): add testing proxy setup for strapi testing environment
- Loading branch information
1 parent
67b46d1
commit 6fd4767
Showing
8 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM nginx:1.27.3 | ||
|
||
RUN apt-get update && \ | ||
apt-get install -y gettext-base && \ | ||
apt-get clean && rm -rf /var/lib/apt/lists/* | ||
|
||
COPY nginx.conf /etc/nginx/nginx.conf.template | ||
|
||
CMD ["sh", "-c", "envsubst '$IAP_ID_TOKEN' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && exec nginx -g 'daemon off;'"] | ||
|
||
|
||
EXPOSE 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Strapi Testing Environment | ||
|
||
To connect the project to the Strapi testing environment, ensure the testing proxy is correctly set up and running. You can use the following commands: | ||
|
||
- Start the testing proxy server: | ||
|
||
```bash | ||
yarn proxy:testing | ||
``` | ||
|
||
This launches the testing proxy server to connect to Strapi. | ||
|
||
- Run the proxy server in infinite mode: | ||
|
||
```bash | ||
yarn proxy:testing:infinite | ||
``` | ||
|
||
This mode ensures the proxy regenerates the JWT token automatically every hour to avoid token expiration issues. | ||
|
||
- Stop the testing proxy server: | ||
|
||
```bash | ||
yarn proxy:testing:stop | ||
``` | ||
|
||
This stops the currently running proxy server. | ||
|
||
### Common Issues and Considerations | ||
|
||
- **Token Expiration**: JWT tokens for the Strapi testing environment expire after 1 hour. If you encounter access issues due to expired tokens, use the infinite mode to regenerate tokens automatically. | ||
|
||
- **Data Consistency**: The testing environment may contain test-specific data that differs from the local database. Always validate the data before running tests or debugging features. | ||
|
||
- **Data Transfer**: You can transfer data from the testing environment to your local setup (excluding files) using: | ||
|
||
```bash | ||
yarn proxy:transfer | ||
``` | ||
|
||
This ensures your local instance is synced with the testing environment. | ||
|
||
By following these steps, you can easily point to the Strapi testing server, manage JWT token lifecycles, and handle potential data inconsistencies during development and testing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/bin/bash | ||
|
||
# Script usage function | ||
usage() { | ||
echo "⭐ Usage: $0 [-d AUTH_DIR] [-l true|false]" | ||
echo "⭐ -d AUTH_DIR Directory containing the get-iap-tokens.py script (default: ../../iap-local-authentication)" | ||
echo "⭐ Repository: https://github.com/pass-culture/iap-local-authentication" | ||
echo "⭐ -l LOOP_MODE Enable infinite loop to refresh JWT (true/false, default: false)" | ||
exit 1 | ||
} | ||
|
||
# Default values | ||
DEFAULT_AUTH_DIR="../../iap-local-authentication" | ||
CLOUDSDK_PYTHON="/usr/bin/python3" | ||
IMAGE_NAME="nginx-strapi-testing-proxy" | ||
CONTAINER_NAME="nginx-strapi-testing-proxy" | ||
VENV_DIR="venv" | ||
JWT_EXPIRATION_TIME=3600 # JWT valid for 1 hour (3600 seconds) | ||
LOOP_MODE=false | ||
|
||
# Parse command-line arguments | ||
while getopts "d:l:" opt; do | ||
case $opt in | ||
d) AUTH_DIR="$OPTARG" ;; | ||
l) LOOP_MODE="$OPTARG" ;; | ||
*) usage ;; | ||
esac | ||
done | ||
|
||
# Use the default AUTH_DIR if none is provided | ||
AUTH_DIR=${AUTH_DIR:-$DEFAULT_AUTH_DIR} | ||
|
||
# Step 1: Create virtual environment and install dependencies | ||
echo "✨ Setting up the virtual environment and installing dependencies..." | ||
cd "$AUTH_DIR" || { echo "⚠️ Error: Cannot access $AUTH_DIR"; exit 1; } | ||
|
||
if [ ! -d "$VENV_DIR" ]; then | ||
python3 -m venv "$VENV_DIR" | ||
echo "⭐ Virtual environment created in $AUTH_DIR/$VENV_DIR." | ||
fi | ||
|
||
# Activate the virtual environment and install dependencies | ||
# shellcheck disable=SC1091 | ||
source "$VENV_DIR/bin/activate" | ||
if [ -f "requirements.txt" ]; then | ||
echo "🛠️ Installing dependencies from requirements.txt..." | ||
pip install --upgrade pip | ||
pip install -r requirements.txt || { echo "⚠️ Error: Failed to install dependencies."; exit 1; } | ||
else | ||
echo "⚠️ Error: requirements.txt not found in $AUTH_DIR." | ||
exit 1 | ||
fi | ||
|
||
# Step 2: Google Cloud authentication | ||
echo "💳 Authenticating with Google Cloud..." | ||
export CLOUDSDK_PYTHON="$CLOUDSDK_PYTHON" | ||
|
||
gcloud auth application-default login || { echo "⚠️ Error: Google Cloud authentication failed"; exit 1; } | ||
|
||
# Step 3: Generate the IAP token | ||
generate_jwt() { | ||
echo "✨ Generating the IAP token..." | ||
python3 get-iap-tokens.py || { echo "⚠️ Error: Failed to generate IAP token."; exit 1; } | ||
|
||
# Load environment variables containing the token | ||
echo "📂 Loading IAP credentials..." | ||
# shellcheck disable=SC1090 | ||
source ~/.iap-credentials || { echo "⚠️ Error: Unable to load ~/.iap-credentials"; exit 1; } | ||
|
||
# Verify that the IAP_ID_TOKEN is set | ||
if [ -z "$IAP_ID_TOKEN" ]; then | ||
echo "⚠️ Error: IAP_ID_TOKEN is empty. Check the get-iap-tokens.py script." | ||
exit 1 | ||
fi | ||
} | ||
|
||
generate_jwt | ||
|
||
# Return to the Docker directory | ||
echo "👉 Returning to the Docker directory..." | ||
deactivate | ||
cd - >/dev/null || exit 1 | ||
|
||
# Step 4: Build the Docker image | ||
echo "🔧 Building the Docker image..." | ||
docker build -t "$IMAGE_NAME" . || { echo "⚠️ Error: Docker build failed"; exit 1; } | ||
|
||
# Step 5: Run the Docker container | ||
start_container() { | ||
echo "🚀 Running the Docker container..." | ||
docker stop "$CONTAINER_NAME" >/dev/null 2>&1 || true | ||
docker run -d --rm -p 8080:8080 -e IAP_ID_TOKEN="$IAP_ID_TOKEN" --name "$CONTAINER_NAME" "$IMAGE_NAME" || { | ||
echo "⚠️ Error: Docker container failed to start"; exit 1; | ||
} | ||
echo "🎉 Container $CONTAINER_NAME is now running on port 8080. (http://localhost:8080/)" | ||
} | ||
|
||
start_container | ||
|
||
# Step 6: Infinite loop for JWT refresh if enabled | ||
if [ "$LOOP_MODE" = "true" ]; then | ||
echo "🔄 Infinite loop mode enabled. Monitoring JWT expiration..." | ||
while true; do | ||
sleep $((JWT_EXPIRATION_TIME - 60)) # Refresh 1 minute before expiration | ||
|
||
echo "⏳ JWT about to expire. Regenerating..." | ||
generate_jwt | ||
|
||
echo "⟳ Restarting container with new JWT..." | ||
start_container | ||
done | ||
else | ||
echo "✅ Infinite loop mode disabled. Script execution complete." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
events {} | ||
|
||
http { | ||
server { | ||
listen 8080; | ||
|
||
location / { | ||
proxy_pass https://siteinstit-cms.testing.passculture.team/; | ||
|
||
proxy_set_header Proxy-Authorization "Bearer $IAP_ID_TOKEN"; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "Upgrade"; | ||
proxy_pass_request_headers on; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
|
||
# Configuration | ||
CONTAINER_NAME="nginx-strapi-testing-proxy" | ||
PROXY_URL="http://localhost:8080/admin" | ||
TRANSFER_COMMAND="yarn strapi transfer --from $PROXY_URL --exclude files" | ||
|
||
# Step 1: Check if the Docker container is running | ||
echo "Checking if the Docker proxy container '$CONTAINER_NAME' is running..." | ||
|
||
if docker ps --filter "name=$CONTAINER_NAME" --format "{{.Names}}" | grep -q "$CONTAINER_NAME"; then | ||
echo "✅ Docker proxy container '$CONTAINER_NAME' is running." | ||
else | ||
echo "❌ Error: Docker proxy container '$CONTAINER_NAME' is not running." | ||
echo "Please start the proxy container before running this command." | ||
echo "=> yarn proxy:testing" | ||
exit 1 | ||
fi | ||
|
||
# Step 2: Execute the transfer command | ||
cd ../content_management_system >/dev/null || exit 1 | ||
echo "Executing the Strapi transfer command..." | ||
if $TRANSFER_COMMAND; then | ||
echo "✅ Strapi transfer completed successfully." | ||
else | ||
echo "❌ Error: Strapi transfer failed. Please check the logs for details." | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters