|
2 | 2 |
|
3 | 3 | // Proxy script that fetches version.json from a given URL |
4 | 4 |
|
5 | | -require_once __DIR__ . '/../vendor/autoload.php'; |
| 5 | +require_once __DIR__ . '/util.php'; |
6 | 6 |
|
7 | | -use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
8 | 7 | use GuzzleHttp\Client; |
9 | 8 | use GuzzleHttp\Exception\RequestException; |
10 | | -$cache = new FilesystemAdapter( |
11 | | - namespace: 'version_proxy', |
12 | | - defaultLifetime: 300, // 5 minutes |
13 | | - directory: __DIR__ . '/../.cache' |
14 | | -); |
15 | 9 |
|
16 | | -$env_path = getenv('ENV_PATH') ?: __DIR__ . '/..'; |
17 | | -$dotenv = Dotenv\Dotenv::createImmutable($env_path); |
18 | | -$dotenv->load(); |
| 10 | +handleRequest('version_proxy', 'fetch_version_info'); |
19 | 11 |
|
20 | | -$mtls = [ |
21 | | - 'cert' => $_ENV['MTLS_CERT'] ?: null, |
22 | | - 'key' => $_ENV['MTLS_KEY'] ?: null, |
23 | | - 'ca' => $_ENV['MTLS_CA'] ?: null |
24 | | -]; |
25 | | -$env = $_ENV['SERVICES_ENVIRONMENT'] ?: null; |
26 | | - |
27 | | -// Get requested Service |
28 | | -if (!isset($_GET['service'])) { |
29 | | - http_response_code(400); |
30 | | - echo json_encode(['error' => 'Missing service parameter']); |
31 | | - exit; |
32 | | -} |
33 | | - |
34 | | -// Get requested Env |
35 | | -if (!isset($_GET['env'])) { |
36 | | - http_response_code(400); |
37 | | - echo json_encode(['error' => 'Missing env parameter']); |
38 | | - exit; |
39 | | -} |
40 | | - |
41 | | -if (isset($env) && $env !== $_GET['env']) { |
42 | | - http_response_code(400); |
43 | | - echo json_encode(['error' => 'Environment not allowed']); |
44 | | - exit; |
45 | | -} |
46 | | - |
47 | | -$service = get_service(__DIR__ . '/../services.json', $_GET['service'], $_GET['env']); |
48 | | -if (!$service) { |
49 | | - http_response_code(400); |
50 | | - echo json_encode(['Service' => 'Requested service not found']); |
51 | | - exit; |
52 | | -} |
53 | | - |
54 | | -// Check cache |
55 | | -$versionUrl = $service['version_url'] ?? $service['url']; |
56 | | -$cacheKey = sha1($versionUrl); |
57 | | -$cachedItem = $cache->getItem($cacheKey); |
58 | | - |
59 | | -if ($cachedItem->isHit()) { |
60 | | - header('Content-Type: application/json'); |
61 | | - echo json_encode($cachedItem->get()); |
62 | | - exit; |
63 | | -} |
64 | | - |
65 | | -// Fetch fresh status |
66 | | -$data = fetch_version_info($service, $mtls); |
67 | | - |
68 | | -// Cache and return result |
69 | | -$cachedItem->set($data); |
70 | | -$cache->save($cachedItem); |
71 | | - |
72 | | -header('Content-Type: application/json'); |
73 | | -echo json_encode($data); |
74 | | -exit; |
75 | | - |
76 | | -/** |
77 | | - * Get the passed service for the given serviceName from services.json |
78 | | - * @param array $services |
79 | | - * @param string $serviceName |
80 | | - * @return array|null |
81 | | - */ |
82 | | -function get_service(string $settingsFile, string $serviceName, string $envName): ?array |
83 | | -{ |
84 | | - if (!file_exists($settingsFile)) { |
85 | | - http_response_code(500); |
86 | | - echo json_encode(['error' => 'Config missing']); |
87 | | - exit; |
88 | | - } |
89 | | - $data = json_decode(file_get_contents($settingsFile), true); |
90 | | - |
91 | | - if (empty($data) || !is_array($data) || !isset($data[$envName]) || !is_array($data[$envName])) { |
92 | | - return null; |
93 | | - } |
94 | | - |
95 | | - // Search for the service by name |
96 | | - foreach ($data[$envName] as $service) { |
97 | | - if (isset($service['name']) && $service['name'] === $serviceName) { |
98 | | - return $service; |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - return null; |
103 | | -} |
104 | | - |
105 | | -function fetch_version_info(array $service, array $mtls) { |
| 12 | +function fetch_version_info(array $service) { |
| 13 | + $mtls = getMtlsConfig(); |
106 | 14 | $client = new Client([ |
107 | 15 | 'timeout' => 4, |
108 | 16 | 'headers' => [ |
109 | 17 | ], |
110 | | - 'cert' => $mtls['cert'] ?? null, |
111 | | - 'ssl_key' => $mtls['key'] ?? null, |
112 | | - 'verify' => $mtls['ca'] ?? true |
| 18 | + 'cert' => $mtls['cert'], |
| 19 | + 'ssl_key' => $mtls['key'], |
| 20 | + 'verify' => $mtls['ca'] |
113 | 21 | ]); |
114 | 22 | try { |
115 | 23 | if(strcmp($service['type'], "HAPI") == 0){ |
|
0 commit comments