-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from joomla-projects/feature/available-versions
Added service to fetch available versions from TUF #11
- Loading branch information
Showing
19 changed files
with
944 additions
and
22 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 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,6 +1,6 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
namespace App\Http\Traits; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TufMetadata extends Model | ||
{ | ||
public $timestamps = false; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use App\Models\TufMetadata; | ||
use App\TUF\EloquentModelStorage; | ||
use App\TUF\HttpLoader; | ||
use GuzzleHttp\Client; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\ServiceProvider; | ||
use Tuf\Client\Updater; | ||
use Tuf\Loader\SizeCheckingLoader; | ||
use Tuf\Metadata\StorageInterface; | ||
|
||
class TUFServiceProvider extends ServiceProvider | ||
{ | ||
public const REPO_PATH = "https://update.joomla.org/cms/"; | ||
|
||
/** | ||
* Register services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->singleton(StorageInterface::class, function ($app) { | ||
// Setup loader | ||
$httpLoader = new HttpLoader( | ||
self::REPO_PATH, | ||
App::make(Client::class) | ||
); | ||
|
||
$sizeCheckingLoader = new SizeCheckingLoader($httpLoader); | ||
|
||
// Setup storage | ||
$storage = new EloquentModelStorage(TufMetadata::findOrFail(1)); | ||
|
||
// Create updater | ||
$updater = new Updater( | ||
$sizeCheckingLoader, | ||
$storage | ||
); | ||
|
||
// Fetch Updates | ||
$updater->refresh(); | ||
|
||
$storage->persist(); | ||
|
||
return $storage; | ||
}); | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
app/Enum/WebserviceEndpoint.php → app/RemoteSite/WebserviceEndpoint.php
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\TUF; | ||
|
||
use App\Models\TufMetadata; | ||
use Tuf\Metadata\StorageBase; | ||
|
||
class EloquentModelStorage extends StorageBase | ||
{ | ||
public const METADATA_COLUMNS = ['root', 'targets', 'snapshot', 'timestamp', 'mirrors']; | ||
|
||
protected TufMetadata $model; | ||
|
||
/** | ||
* @var array<string, string> | ||
*/ | ||
protected array $container = []; | ||
|
||
public function __construct(TufMetadata $model) | ||
{ | ||
$this->model = $model; | ||
|
||
foreach (self::METADATA_COLUMNS as $column) { | ||
if ($this->model->$column === null) { | ||
continue; | ||
} | ||
|
||
$this->write($column, $this->model->$column); | ||
} | ||
} | ||
|
||
public function read(string $name): ?string | ||
{ | ||
return $this->container[$name] ?? null; | ||
} | ||
|
||
public function write(string $name, string $data): void | ||
{ | ||
$this->container[$name] = $data; | ||
} | ||
|
||
public function delete(string $name): void | ||
{ | ||
unset($this->container[$name]); | ||
} | ||
|
||
public function persist(): bool | ||
{ | ||
foreach (self::METADATA_COLUMNS as $column) { | ||
if (!\array_key_exists($column, $this->container)) { | ||
continue; | ||
} | ||
|
||
$this->model->$column = $this->container[$column]; | ||
} | ||
|
||
return $this->model->save(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\TUF; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Promise\Create; | ||
use GuzzleHttp\Promise\PromiseInterface; | ||
use Tuf\Exception\RepoFileNotFound; | ||
use Tuf\Loader\LoaderInterface; | ||
|
||
class HttpLoader implements LoaderInterface | ||
{ | ||
public function __construct(private readonly string $repositoryPath, private readonly Client $http) | ||
{ | ||
} | ||
|
||
public function load(string $locator, int $maxBytes): PromiseInterface | ||
{ | ||
try { | ||
$response = $this->http->get($this->repositoryPath . $locator); | ||
} catch (RequestException $e) { | ||
if ($e->getResponse()?->getStatusCode() !== 200) { | ||
throw new RepoFileNotFound(); | ||
} | ||
|
||
throw new HttpLoaderException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
||
// Rewind to start | ||
$response->getBody()->rewind(); | ||
|
||
// Return response | ||
return Create::promiseFor($response->getBody()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\TUF; | ||
|
||
use Tuf\Exception\TufException; | ||
|
||
class HttpLoaderException extends TufException | ||
{ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\TUF; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Cache; | ||
use Tuf\Exception\MetadataException; | ||
use Tuf\Metadata\StorageInterface; | ||
|
||
class TufFetcher | ||
{ | ||
protected StorageInterface $updateStorage; | ||
|
||
public function __construct() | ||
{ | ||
$this->updateStorage = App::make(StorageInterface::class); | ||
} | ||
|
||
public function getReleases(): mixed | ||
{ | ||
// Cache response to avoid to make constant calls on the fly | ||
return Cache::remember( | ||
'cms_targets', | ||
(int) config('autoupdates.tuf_repo_cachetime') * 60, // @phpstan-ignore-line | ||
function () { | ||
$targets = $this->updateStorage->getTargets(); | ||
|
||
// Make sure we have a valid list of targets | ||
if (is_null($targets)) { | ||
throw new MetadataException("Empty targetlist in metadata"); | ||
} | ||
|
||
// Convert format | ||
return (new Collection($targets->getSigned()['targets'])) | ||
->mapWithKeys(function (mixed $target) { | ||
if (!is_array($target) || empty($target['custom']) || !is_array($target['custom'])) { | ||
throw new MetadataException("Empty target custom attribute"); | ||
} | ||
|
||
return [$target['custom']['version'] => $target['custom']]; | ||
}); | ||
} | ||
); | ||
} | ||
} |
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
Oops, something went wrong.