Skip to content

Commit

Permalink
feat: check GEWISDB API to ensure it is safe to sync
Browse files Browse the repository at this point in the history
  • Loading branch information
tomudding committed Aug 24, 2024
1 parent 81d40b4 commit 230fd4a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ MH_SMTP_BIND_ADDR=0.0.0.0:25
# Settings for Pwned Passwords API to check if passwords are present in known breaches
PWNED_PASSWORDS_HOST=https://pwned-passwords.gewis.nl/api

# Settings for GEWISDB API health check
GEWISDB_API_HOST=https://database.test.gewis.nl/api
GEWISDB_API_KEY='thiskeyisnotvalid'

# Google Calendar API (Option Calendar) settings
DOCKER_GOOGLE_API_KEY=unknown
DOCKER_GOOGLE_CALENDAR_KEY=unknown
Expand Down
63 changes: 62 additions & 1 deletion importdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,68 @@
* It is a simple PostgreSQL to MySQL copy script.
*/

$apiKey = getenv('GEWISDB_API_KEY');
$apiHost = getenv('GEWISDB_API_HOST');

if (
false === $apiKey
|| false === $apiHost
) {
echo 'API: no sync, environment variables are not set properly...' . PHP_EOL;
exit(1);
}

$ch = curl_init();

$headers = [
sprintf('Authorization: Bearer %s', $apiKey),
];

curl_setopt($ch, CURLOPT_URL, $apiHost . '/health');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($ch);

if (false === $response) {
echo 'API: no sync, unexpected cURL error...' . PHP_EOL;
curl_close($ch);
exit(1);
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (
200 === $httpCode
|| 403 === $httpCode
) {
if (!json_validate($response)) {
echo 'API: no sync, invalid JSON returned...' . PHP_EOL;
curl_close($ch);
exit(1);
}

$health = json_decode($response, true);

if (
$health['healthy']
&& !$health['sync_paused']
) {
echo 'API: sync, healthy and syncs are allowed...' . PHP_EOL;
} else {
echo 'API: no sync, sync is paused or API is not healthy...' . PHP_EOL;
curl_close($ch);
exit(1);
}
} else {
echo 'API: no sync, unexpected response...' . PHP_EOL;
curl_close($ch);
exit(1);
}

curl_close($ch);

echo 'Commencing sync with GEWISDB...' . PHP_EOL;

try {
Expand Down Expand Up @@ -182,7 +244,6 @@ function ($a) use ($i) {
/**
* Removing old data
*/

// Tables without primary keys are skipped
if (0 === count($pks[$table])) continue;

Expand Down

0 comments on commit 230fd4a

Please sign in to comment.