Skip to content
Merged
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
33 changes: 32 additions & 1 deletion public_html/php/WbstackMagnusOauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class WbstackMagnusOauth {

public const platformIngressHostAndPort = "platform-nginx.default.svc.cluster.local:8080";
public const platformApiBackendHost = "api-app-backend.default.svc.cluster.local";

/**
* @var bool
Expand All @@ -29,6 +30,36 @@ class WbstackMagnusOauth {
*/
private static $callbackUrlTail;

public static function getVersionedMediawikiBackendHost() {
$host = getenv('PLATFORM_MW_BACKEND_HOST'); // default fallback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like having a fallback. I wonder if we actually want to introduce a "we're mid updates flag" that we turn on and only in that case we do we do the API lookup.

Not for this PR though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, as these requests probably won't get cached, as nginx does. But also shouldnt be a problem, as OAuth requests shouldn't happen too often I suppose


$requestUrl = 'http://'
.self::platformApiBackendHost
.'/backend/ingress/getWikiVersionForDomain?domain='
.$_SERVER['SERVER_NAME'];

$headers = get_headers($requestUrl, true);

if (is_array($headers)) {
if (isset($headers['x-version'])) {
$wikiVersion = $headers['x-version'];

// mapping like in https://github.com/wmde/wbaas-deploy/blob/main/k8s/helmfile/env/local/platform-nginx.nginx.conf#L4
// TODO https://phabricator.wikimedia.org/T409078
switch ($wikiVersion) {
case 'mw1.39-wbs1':
$host = 'mediawiki-139-app-backend.default.svc.cluster.local';
break;
case 'mw1.43-wbs1':
$host = 'mediawiki-143-app-backend.default.svc.cluster.local';
break;
}
}
}

return $host;
}

/**
* Calling this means that oauth.php will just do the right thing in terms of wbstack.
* Not calling this before MW_OAuth is used in a magnus tool will result in an error.
Expand Down Expand Up @@ -104,7 +135,7 @@ public static function parse_ini_file($filename) {
'callbackUrlTail' => self::$callbackUrlTail,
];

$client = curl_init('http://' . getenv( 'PLATFORM_MW_BACKEND_HOST' ) . '/w/api.php?action=wbstackPlatformOauthGet&format=json');
$client = curl_init('http://' . self::getVersionedMediawikiBackendHost() . '/w/api.php?action=wbstackPlatformOauthGet&format=json');
curl_setopt($client, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - " . self::$consumerName . " - WbstackMagnusOauth::parse_ini_file" );
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
Expand Down