Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions scripts/idp/idp-factory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash


# ============================================
# Identity Provider Factory
# ============================================
#
# This factory creates and returns the appropriate Identity Provider
# based on the configuration in silver.yaml
#
# Usage:
# source "$(dirname "$0")/../idp/idp-factory.sh"
# create_idp_provider "thunder"
# # Now you can call: thunder_initialize, thunder_wait_for_ready, etc.


# Get the directory where this script is located
IDP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROVIDERS_DIR="${IDP_DIR}/providers"


# Source the interface
source "${IDP_DIR}/idp-interface.sh"


# ============================================
# Function: Create IdP Provider
# ============================================
# Creates and initializes the specified Identity Provider
#
# Arguments:
# $1 - Provider name (thunder, keycloak, etc.)
#
# Returns:
# 0 on success, 1 on failure
#
# Exports:
# IDP_PROVIDER - The name of the loaded provider
# IDP_INITIALIZE - Function name for initialize
# IDP_WAIT_FOR_READY - Function name for wait_for_ready
# IDP_CONFIGURE - Function name for configure
# IDP_GET_COMPOSE_FILE - Function name for get_compose_file
# IDP_CLEANUP - Function name for cleanup
# ============================================
create_idp_provider() {
local provider_name="$1"


if [ -z "$provider_name" ]; then
echo -e "${RED}βœ— Provider name is required${NC}" >&2
return 1
fi


# Convert to lowercase
provider_name=$(echo "$provider_name" | tr '[:upper:]' '[:lower:]')


echo -e "${CYAN}Loading Identity Provider: ${provider_name}${NC}"


# Load the appropriate provider implementation
case "$provider_name" in
thunder|keycloak)
local provider_script="${PROVIDERS_DIR}/${provider_name}-idp.sh"
if [ ! -f "$provider_script" ]; then
echo -e "${RED}βœ— ${provider_name^} provider not found at ${provider_script}${NC}" >&2
return 1
fi
source "$provider_script"
;;
*)
echo -e "${RED}βœ— Unknown identity provider: ${provider_name}${NC}" >&2
echo -e "${YELLOW}Supported providers: thunder, keycloak${NC}" >&2
return 1
;;
esac


# Validate that the provider implements all required functions
if ! validate_provider_implementation "$provider_name"; then
echo -e "${RED}βœ— Provider '${provider_name}' does not implement the required interface${NC}" >&2
return 1
fi


# Export provider information
export IDP_PROVIDER="$provider_name"
export IDP_INITIALIZE="${provider_name}_initialize"
export IDP_WAIT_FOR_READY="${provider_name}_wait_for_ready"
export IDP_CONFIGURE="${provider_name}_configure"
export IDP_GET_COMPOSE_FILE="${provider_name}_get_compose_file"
export IDP_CLEANUP="${provider_name}_cleanup"


echo -e "${GREEN}βœ“ Identity Provider '${provider_name}' loaded successfully${NC}"
return 0
}


# ============================================
# Function: Get Provider from Config
# ============================================
# Reads the provider name from silver.yaml
#
# Arguments:
# $1 - Path to silver.yaml config file
#
# Returns:
# Provider name (stdout), empty if not found
# ============================================
get_provider_from_config() {
local config_file="$1"


if [ ! -f "$config_file" ]; then
echo -e "${RED}βœ— Configuration file not found: ${config_file}${NC}" >&2
return 1
fi


# Extract provider from YAML (simple grep-based parsing)
# Looking for: identity:
# provider: thunder
local provider=$(grep -A 1 '^identity:' "$config_file" | grep 'provider:' | sed 's/.*provider:\s*//' | xargs)


if [ -z "$provider" ]; then
echo -e "${YELLOW}⚠ No identity provider configured in ${config_file}${NC}" >&2
echo -e "${YELLOW} Defaulting to 'thunder'${NC}" >&2
echo "thunder"
else
echo "$provider"
fi
}


# Export functions
export -f create_idp_provider
export -f get_provider_from_config
157 changes: 157 additions & 0 deletions scripts/idp/idp-interface.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/bin/bash

# ============================================
# Identity Provider Interface Contract
# ============================================
#
# This file defines the interface that all Identity Provider
# implementations must follow. It serves as a contract for the
# Strategy pattern.
#
# Each IdP provider must implement these functions:
# - <provider>_initialize()
# - <provider>_wait_for_ready()
# - <provider>_configure()
# - <provider>_get_compose_file()
# - <provider>_cleanup()
#
# Where <provider> is the name of the provider (e.g., thunder, keycloak)

# Colors for output
CYAN="\033[0;36m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
NC="\033[0m" # No Color

# ============================================
# Interface: initialize
# ============================================
# Starts the Identity Provider service
#
# Arguments:
# $1 - Mail domain
#
# Returns:
# 0 on success, 1 on failure
#
# Example:
# thunder_initialize "example.com"
# keycloak_initialize "example.com"
# ============================================

# ============================================
# Interface: wait_for_ready
# ============================================
# Waits for the Identity Provider to be healthy and ready
#
# Arguments:
# $1 - IdP host
# $2 - IdP port
#
# Returns:
# 0 on success, 1 on timeout/failure
#
# Example:
# thunder_wait_for_ready "example.com" "8090"
# keycloak_wait_for_ready "example.com" "8080"
# ============================================

# ============================================
# Interface: configure
# ============================================
# Configures the Identity Provider with necessary settings
# This includes creating realms, clients, schemas, etc.
#
# Arguments:
# $1 - Mail domain
#
# Returns:
# 0 on success, 1 on failure
#
# Example:
# thunder_configure "example.com"
# keycloak_configure "example.com"
# ============================================

# ============================================
# Interface: get_compose_file
# ============================================
# Returns the path to the docker-compose file for this provider
#
# Arguments:
# None
#
# Returns:
# Path to docker-compose file (stdout)
#
# Example:
# compose_file=$(thunder_get_compose_file)
# compose_file=$(keycloak_get_compose_file)
# ============================================

# ============================================
# Interface: cleanup
# ============================================
# Cleans up and stops the Identity Provider service
#
# Arguments:
# None
#
# Returns:
# 0 on success, 1 on failure
#
# Example:
# thunder_cleanup
# keycloak_cleanup
# ============================================

# ============================================
# Helper: Validate Provider Implementation
# ============================================
# Validates that a provider implements all required functions
#
# Arguments:
# $1 - Provider name (e.g., "thunder", "keycloak")
#
# Returns:
# 0 if valid, 1 if missing functions
# ============================================
validate_provider_implementation() {
local provider_name="$1"

if [ -z "$provider_name" ]; then
echo -e "${RED}βœ— Provider name is required for validation${NC}" >&2
return 1
fi

local required_functions=(
"${provider_name}_initialize"
"${provider_name}_wait_for_ready"
"${provider_name}_configure"
"${provider_name}_get_compose_file"
"${provider_name}_cleanup"
)

local missing_functions=()

for func in "${required_functions[@]}"; do
if ! declare -f "$func" > /dev/null 2>&1; then
missing_functions+=("$func")
fi
done

if [ ${#missing_functions[@]} -gt 0 ]; then
echo -e "${RED}βœ— Provider '${provider_name}' is missing required functions:${NC}" >&2
for func in "${missing_functions[@]}"; do
echo -e "${RED} - ${func}${NC}" >&2
done
return 1
fi

echo -e "${GREEN}βœ“ Provider '${provider_name}' implements all required functions${NC}"
return 0
}

# Export validation function
export -f validate_provider_implementation
Loading