-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from DavidePrincipi/subscription
Refactor for Porthos multi-tenant configuration nethesis/dev#5641
- Loading branch information
Showing
14 changed files
with
436 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,103 @@ | ||
<?php | ||
|
||
include("lib.php"); | ||
/* | ||
* Copyright (C) 2019 Nethesis S.r.l. | ||
* http://www.nethesis.it - [email protected] | ||
* | ||
* This script is part of Dartagnan. | ||
* | ||
* Dartagnan is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, | ||
* or any later version. | ||
* | ||
* Dartagnan is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Dartagnan. If not, see COPYING. | ||
*/ | ||
|
||
if ( ! isset($_SERVER['PHP_AUTH_USER'])) { | ||
header('WWW-Authenticate: Basic realm="subscription"'); | ||
header('HTTP/1.1 401 Unauthorized'); | ||
echo "Provide system subscription credentials\n"; | ||
exit; | ||
require_once("lib.php"); | ||
require_once("config-" . $_SERVER['PORTHOS_SITE'] . ".php"); | ||
|
||
$uri = parse_uri($_SERVER['DOCUMENT_URI']); | ||
|
||
if ( isset($_SERVER['HTTPS']) && ! $uri['system_id'] && ! isset($_SERVER['PHP_AUTH_USER'])) { | ||
exit_basic_auth_required(); | ||
} else { | ||
if($uri['system_id']) { | ||
// override PHP authentication with system_id token: | ||
$_SERVER['PHP_AUTH_USER'] = $uri['system_id']; | ||
$_SERVER['PHP_AUTH_PW'] = $uri['system_id']; | ||
} | ||
} | ||
|
||
// Disable the Content-Type header in PHP, so that nginx x-accel can add its own | ||
ini_set('default_mimetype', FALSE); | ||
|
||
$access = get_access_descriptor($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); | ||
// Mask any repo/version/arch that does not belong to the site: | ||
if(! in_array($uri['repo'], $config['repositories']) | ||
|| ! in_array($uri['version'], $config['versions']) | ||
|| ! in_array($uri['arch'], $config['arches'])) { | ||
exit_http(404); | ||
} | ||
|
||
if($access['tier_id'] === FALSE) { | ||
if(! isset($_SERVER['PHP_AUTH_USER']) || ! isset($_SERVER['PHP_AUTH_PW'])) { | ||
exit_http(403); | ||
} elseif ($access['tier_id'] === '' || $access['secret'] != $_SERVER['PHP_AUTH_PW']) { | ||
} | ||
|
||
$access = get_access_descriptor($_SERVER['PHP_AUTH_USER']); | ||
$valid_credentials = $_SERVER['PHP_AUTH_PW'] === $access['secret']; | ||
if($config['legacy_auth']) { | ||
$valid_credentials = $valid_credentials || $_SERVER['PHP_AUTH_USER'] === $_SERVER['PHP_AUTH_PW']; | ||
} | ||
$has_access_disabled = ! is_numeric($access['tier_id']); | ||
if ($has_access_disabled || ! $valid_credentials) { | ||
exit_http(403); | ||
} else { | ||
if(basename($_SERVER['DOCUMENT_URI']) == 'repomd.xml') { | ||
header('Cache-Control: private'); | ||
} | ||
|
||
if($access['tier_id'] < 0) { | ||
$hash = 0; | ||
foreach(str_split($_SERVER['PHP_AUTH_USER']) as $c) { | ||
$hash += ord($c); | ||
} | ||
return_file('/t' . $access['tier_id'] . $_SERVER['DOCUMENT_URI']); | ||
$hash = $hash % 256; | ||
if($hash < 12) { // 5% | ||
$tier_id = 0; | ||
} elseif($hash < 38) { // 15% | ||
$tier_id = 1; | ||
} elseif($hash < 76) { // 30% | ||
$tier_id = 2; | ||
} else { // 50% | ||
$tier_id = 3; | ||
} | ||
$tier_id += $config['tier_id_base']; | ||
} else { | ||
$tier_id = intval($access['tier_id']); | ||
} | ||
|
||
if(basename($uri['rest']) == 'repomd.xml') { | ||
header('Cache-Control: private'); | ||
application_log(json_encode(array( | ||
'application' => 'porthos-' . $_SERVER['PORTHOS_SITE'], | ||
'connection' => $_SERVER['CONNECTION'] ?: '', | ||
'msg_type' => 'repomdxml-auth', | ||
'msg_severity' => 'notice', | ||
'server_id' => $_SERVER['PHP_AUTH_USER'], | ||
'repo' => $uri['repo'], | ||
'version' => $uri['version'], | ||
'arch' => $uri['arch'], | ||
'tier_id' => $uri['prefix'] == 'autoupdate' ? NULL : $tier_id, | ||
'tier_auto' => isset($hash), | ||
'tls' => isset($_SERVER['HTTPS']), | ||
))); | ||
} | ||
|
||
if($uri['prefix'] == 'autoupdate') { | ||
return_file('/T' . $tier_id . $uri['full_path']); | ||
} else { | ||
return_file('/head' . $uri['full_path']); | ||
} |
Oops, something went wrong.