| | | 1 | | using DotNetEnv; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator; |
| | | 6 | | |
| | | 7 | | public static class EnvironmentHelper |
| | | 8 | | { |
| | | 9 | | private const string KicktippUsernameEnvVar = "KICKTIPP_USERNAME"; |
| | | 10 | | private const string KicktippPasswordEnvVar = "KICKTIPP_PASSWORD"; |
| | | 11 | | |
| | | 12 | | public static void LoadEnvironmentVariables(ILogger logger) |
| | | 13 | | { |
| | | 14 | | try |
| | | 15 | | { |
| | | 16 | | // Use PathUtility to get the correct .env file path |
| | 1 | 17 | | var envPath = PathUtility.GetEnvFilePath("Orchestrator"); |
| | | 18 | | |
| | 1 | 19 | | if (File.Exists(envPath)) |
| | | 20 | | { |
| | 1 | 21 | | Env.Load(envPath); |
| | 1 | 22 | | logger.LogInformation("Loaded .env file from: {EnvPath}", envPath); |
| | | 23 | | } |
| | | 24 | | else |
| | | 25 | | { |
| | 1 | 26 | | logger.LogWarning("No .env file found at: {EnvPath}", envPath); |
| | 1 | 27 | | logger.LogInformation("Please create a .env file in the secrets directory based on .env.example"); |
| | 1 | 28 | | logger.LogInformation("Alternatively, set environment variables directly"); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | // Load Firebase credentials if available |
| | 1 | 32 | | LoadFirebaseCredentials(logger); |
| | 1 | 33 | | } |
| | 0 | 34 | | catch (Exception ex) |
| | | 35 | | { |
| | 0 | 36 | | logger.LogWarning(ex, "Could not load .env file: {Message}", ex.Message); |
| | 0 | 37 | | } |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static void LoadCommunityKicktippCredentials(ILogger logger, string community) |
| | | 41 | | { |
| | 1 | 42 | | ArgumentException.ThrowIfNullOrWhiteSpace(community); |
| | | 43 | | |
| | 1 | 44 | | var envPath = PathUtility.GetEnvFilePath("Orchestrator", community.Trim()); |
| | 1 | 45 | | if (!File.Exists(envPath)) |
| | | 46 | | { |
| | 1 | 47 | | logger.LogWarning( |
| | 1 | 48 | | "No community-specific Kicktipp credentials file found at: {EnvPath}. Existing environment variables wil |
| | 1 | 49 | | envPath); |
| | 1 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | Env.Load(envPath); |
| | | 54 | | |
| | 1 | 55 | | EnsureRequiredEnvironmentVariableIsPresent(KicktippUsernameEnvVar, envPath); |
| | 1 | 56 | | EnsureRequiredEnvironmentVariableIsPresent(KicktippPasswordEnvVar, envPath); |
| | | 57 | | |
| | 1 | 58 | | logger.LogInformation("Loaded community-specific Kicktipp credentials from: {EnvPath}", envPath); |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static void LoadFirebaseCredentials(ILogger logger) |
| | | 62 | | { |
| | | 63 | | try |
| | | 64 | | { |
| | | 65 | | // Check if Firebase credentials are already set via environment variables |
| | 1 | 66 | | var existingFirebaseJson = Environment.GetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON"); |
| | 1 | 67 | | var existingProjectId = Environment.GetEnvironmentVariable("FIREBASE_PROJECT_ID"); |
| | | 68 | | |
| | 1 | 69 | | if (!string.IsNullOrEmpty(existingFirebaseJson) && !string.IsNullOrEmpty(existingProjectId)) |
| | | 70 | | { |
| | 1 | 71 | | logger.LogInformation("Firebase credentials already set via environment variables"); |
| | 1 | 72 | | return; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Try to load from firebase.json file |
| | 1 | 76 | | var firebaseJsonPath = PathUtility.GetFirebaseJsonPath(); |
| | | 77 | | |
| | 1 | 78 | | if (File.Exists(firebaseJsonPath)) |
| | | 79 | | { |
| | 1 | 80 | | var firebaseJson = File.ReadAllText(firebaseJsonPath); |
| | | 81 | | |
| | | 82 | | // Parse the JSON to extract project_id |
| | | 83 | | try |
| | | 84 | | { |
| | 1 | 85 | | using var document = JsonDocument.Parse(firebaseJson); |
| | 1 | 86 | | var root = document.RootElement; |
| | | 87 | | |
| | 1 | 88 | | if (root.TryGetProperty("project_id", out var projectIdElement)) |
| | | 89 | | { |
| | 1 | 90 | | var projectId = projectIdElement.GetString(); |
| | | 91 | | |
| | 1 | 92 | | if (!string.IsNullOrEmpty(projectId)) |
| | | 93 | | { |
| | | 94 | | // Set both environment variables |
| | 1 | 95 | | Environment.SetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON", firebaseJson); |
| | 1 | 96 | | Environment.SetEnvironmentVariable("FIREBASE_PROJECT_ID", projectId); |
| | | 97 | | |
| | 1 | 98 | | logger.LogInformation("Loaded Firebase credentials from: {FirebasePath}", firebaseJsonPath); |
| | 1 | 99 | | logger.LogInformation("Firebase project ID: {ProjectId}", projectId); |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | logger.LogWarning("Firebase JSON file is missing or has empty project_id field"); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | logger.LogWarning("Firebase JSON file is missing project_id field"); |
| | | 109 | | } |
| | 1 | 110 | | } |
| | 1 | 111 | | catch (JsonException ex) |
| | | 112 | | { |
| | 1 | 113 | | logger.LogError(ex, "Failed to parse Firebase JSON file: {Message}", ex.Message); |
| | 1 | 114 | | } |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 1 | 118 | | logger.LogInformation("No Firebase credentials file found at: {FirebasePath}", firebaseJsonPath); |
| | 1 | 119 | | logger.LogInformation("Firebase integration will be disabled unless FIREBASE_PROJECT_ID and FIREBASE_SER |
| | | 120 | | } |
| | 1 | 121 | | } |
| | 0 | 122 | | catch (Exception ex) |
| | | 123 | | { |
| | 0 | 124 | | logger.LogWarning(ex, "Could not load Firebase credentials: {Message}", ex.Message); |
| | 0 | 125 | | } |
| | 1 | 126 | | } |
| | | 127 | | |
| | | 128 | | private static void EnsureRequiredEnvironmentVariableIsPresent(string variableName, string envPath) |
| | | 129 | | { |
| | 1 | 130 | | if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(variableName))) |
| | | 131 | | { |
| | 0 | 132 | | throw new InvalidOperationException($"{Path.GetFileName(envPath)} must define {variableName}."); |
| | | 133 | | } |
| | 1 | 134 | | } |
| | | 135 | | } |