Skip to content

Commit 3313a8e

Browse files
Merge pull request #16 from minvws/feat/add-latest-version
2 parents 4b8e408 + 8780568 commit 3313a8e

6 files changed

Lines changed: 225 additions & 289 deletions

File tree

public/check_status.php

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,20 @@
11
<?php
22
// Proxy script that checks HTTP status for allowed URLs
33

4-
require_once __DIR__ . '/../vendor/autoload.php';
4+
require_once __DIR__ . '/util.php';
55

66
use GuzzleHttp\Exception\ConnectException;
7-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
87
use GuzzleHttp\Client;
98
use GuzzleHttp\Exception\RequestException;
109

11-
$cache = new FilesystemAdapter(
12-
namespace: 'status_checker',
13-
defaultLifetime: 300, // 5 minutes cache
14-
directory: __DIR__ . '/../.cache'
15-
);
16-
17-
$env_path = getenv('ENV_PATH') ?: __DIR__ . '/..';
18-
$dotenv = Dotenv\Dotenv::createImmutable($env_path);
19-
$dotenv->load();
20-
21-
$mtls = [
22-
'cert' => $_ENV['MTLS_CERT'] ?: null,
23-
'key' => $_ENV['MTLS_KEY'] ?: null,
24-
'ca' => $_ENV['MTLS_CA'] ?: null
25-
];
26-
27-
$env = $_ENV['SERVICES_ENVIRONMENT'] ?: null;
28-
29-
// Get requested Service
30-
if (!isset($_GET['service'])) {
31-
http_response_code(400);
32-
echo json_encode(['error' => 'Missing service parameter']);
33-
exit;
34-
}
35-
36-
// Get requested Env
37-
if (!isset($_GET['env'])) {
38-
http_response_code(400);
39-
echo json_encode(['error' => 'Missing env parameter']);
40-
exit;
41-
}
42-
43-
if (isset($env) && $env !== $_GET['env']) {
44-
http_response_code(400);
45-
echo json_encode(['error' => 'Environment not allowed']);
46-
exit;
47-
}
48-
49-
$service = get_service(__DIR__ . '/../services.json', $_GET['service'], $_GET['env']);
50-
if (!$service) {
51-
http_response_code(400);
52-
echo json_encode(['Service' => 'Requested service not found']);
53-
exit;
54-
}
55-
56-
// Check cache
57-
$cacheKey = sha1($service['url']);
58-
$cachedItem = $cache->getItem($cacheKey);
59-
60-
if ($cachedItem->isHit()) {
61-
header('Content-Type: application/json');
62-
echo json_encode($cachedItem->get());
63-
exit;
64-
}
65-
66-
// Fetch fresh status
67-
$data = fetch_http_status($service, $mtls);
68-
69-
// Cache and return result
70-
$cachedItem->set($data);
71-
$cache->save($cachedItem);
72-
73-
header('Content-Type: application/json');
74-
echo json_encode($data);
75-
exit;
76-
77-
78-
/**
79-
* Get the passed service for the given serviceName from services.json
80-
* @param array $services
81-
* @param string $serviceName
82-
* @return array|null
83-
*/
84-
function get_service(string $settingsFile, string $serviceName, string $envName): ?array
85-
{
86-
if (!file_exists($settingsFile)) {
87-
http_response_code(500);
88-
echo json_encode(['error' => 'Config missing']);
89-
exit;
90-
}
91-
$data = json_decode(file_get_contents($settingsFile), true);
92-
if (empty($data) || !is_array($data) || !isset($data[$envName]) || !is_array($data[$envName])) {
93-
return null;
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-
}
10+
handleRequest('status_checker', 'fetch_http_status');
10411

10512
/**
10613
* Fetch HTTP status for a given URL
10714
*/
108-
function fetch_http_status(array $service, array $mtls): array
15+
function fetch_http_status(array $service): array
10916
{
17+
$mtls = getMtlsConfig();
11018
$client = new Client([
11119
'timeout' => 4,
11220
'allow_redirects' => [

public/check_version.php

Lines changed: 7 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,22 @@
22

33
// Proxy script that fetches version.json from a given URL
44

5-
require_once __DIR__ . '/../vendor/autoload.php';
5+
require_once __DIR__ . '/util.php';
66

7-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
87
use GuzzleHttp\Client;
98
use GuzzleHttp\Exception\RequestException;
10-
$cache = new FilesystemAdapter(
11-
namespace: 'version_proxy',
12-
defaultLifetime: 300, // 5 minutes
13-
directory: __DIR__ . '/../.cache'
14-
);
159

16-
$env_path = getenv('ENV_PATH') ?: __DIR__ . '/..';
17-
$dotenv = Dotenv\Dotenv::createImmutable($env_path);
18-
$dotenv->load();
10+
handleRequest('version_proxy', 'fetch_version_info');
1911

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();
10614
$client = new Client([
10715
'timeout' => 4,
10816
'headers' => [
10917
],
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']
11321
]);
11422
try {
11523
if(strcmp($service['type'], "HAPI") == 0){

public/github_latest_version.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
// Proxy script for fetching Github pull request info
4+
5+
require_once __DIR__ . '/util.php';
6+
7+
use Github\Client;
8+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9+
10+
handleRequest('prs_proxy', 'fetch_github_data');
11+
12+
function fetch_github_data(array $service) {
13+
$repo = $service['github'] ?? null;
14+
error_log("Fetching Github data for $repo");
15+
if (!$repo) {
16+
return [];
17+
}
18+
$client = new Client();
19+
20+
$token = $_ENV['GITHUB_TOKEN'] ?? null;
21+
if ($token) {
22+
$client->authenticate($token, null, Client::AUTH_ACCESS_TOKEN);
23+
}
24+
25+
try {
26+
[$owner, $name] = explode('/', $repo, 2);
27+
error_log("Fetching Github data for $owner $name");
28+
$latest = $client->api('repo')->releases()->latest($owner, $name);
29+
error_log("Latest release: " . json_encode($latest));
30+
return [
31+
'tag_name' => $latest['tag_name'],
32+
'published_at' => $latest['published_at'],
33+
];
34+
} catch (Exception $e) {
35+
return ['error' => 'GitHub API error', 'detail' => $e->getMessage()];
36+
}
37+
}

public/github_prs.php

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,19 @@
22

33
// Proxy script for fetching Github pull request info
44

5-
require_once __DIR__ . '/../vendor/autoload.php';
5+
require_once __DIR__ . '/util.php';
66

77
use Github\Client;
88
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
99

10-
$env_path = getenv('ENV_PATH') ?: __DIR__ . '/..';
11-
$dotenv = Dotenv\Dotenv::createImmutable($env_path);
12-
$dotenv->load();
10+
handleRequest('prs_proxy', 'fetch_github_data');
1311

14-
$cache = new FilesystemAdapter(
15-
namespace: 'prs_proxy',
16-
defaultLifetime: 300, // 5 minutes
17-
directory: __DIR__ . '/../.cache'
18-
);
19-
20-
header('Content-Type: application/json');
21-
22-
// Validate input
23-
if (!isset($_GET['repo'])) {
24-
http_response_code(400);
25-
echo json_encode(['error' => 'Missing repo']);
26-
exit;
27-
}
28-
29-
$repo = $_GET['repo'];
30-
31-
// Validate repo format
32-
if (!preg_match('#^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+$#', $repo)) {
33-
http_response_code(400);
34-
echo json_encode(['error' => 'Invalid repo format']);
35-
exit;
36-
}
37-
38-
$allowedRepos = get_allowed_repos(__DIR__ . '/../services.json');
39-
if (!in_array($repo, $allowedRepos)) {
40-
http_response_code(403);
41-
echo json_encode(['error' => 'Repository not allowed']);
42-
exit;
43-
}
44-
45-
46-
// Check if item is cached, if so, return that
47-
$cacheKey = sha1($repo);
48-
$cachedItem = $cache->getItem($cacheKey);
49-
if ($cachedItem->isHit()) {
50-
header('Content-Type: application/json');
51-
echo json_encode($cachedItem->get());
52-
exit;
53-
}
54-
55-
$data = fetch_github_data($repo);
56-
if (isset($data['error'])) {
57-
http_response_code(502);
58-
echo json_encode($data);
59-
exit;
60-
}
61-
62-
$cachedItem->set($data);
63-
$cache->save($cachedItem);
64-
echo json_encode($data);
65-
exit;
66-
67-
68-
function fetch_github_data(string $repo) {
12+
function fetch_github_data(array $service) {
13+
$repo = $service['github'] ?? null;
14+
error_log("Fetching Github data for $repo");
15+
if (!$repo) {
16+
return [];
17+
}
6918
$client = new Client();
7019

7120
$token = $_ENV['GITHUB_TOKEN'] ?? null;
@@ -81,29 +30,4 @@ function fetch_github_data(string $repo) {
8130
} catch (Exception $e) {
8231
return ['error' => 'GitHub API error', 'detail' => $e->getMessage()];
8332
}
84-
85-
}
86-
87-
88-
function get_allowed_repos(string $settingsFile)
89-
{
90-
if (!file_exists($settingsFile)) {
91-
http_response_code(500);
92-
echo json_encode(['error' => 'Config missing']);
93-
exit;
94-
}
95-
96-
$allowedRepos = [];
97-
$services = json_decode(file_get_contents($settingsFile), true);
98-
99-
// Extract allowed GitHub repos (in org/repo format)
100-
foreach ($services as $env => $entries) {
101-
foreach ($entries as $svc) {
102-
if (isset($svc['github']) && preg_match('#([^/]+/[^/]+)#', $svc['github'], $m)) {
103-
$allowedRepos[] = $m[1]; // e.g., org/repo
104-
}
105-
}
106-
}
107-
108-
return $allowedRepos;
10933
}

0 commit comments

Comments
 (0)