Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flysystem usage after upgrade #425

Merged
merged 7 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"oat-sa/lib-lti1p3-ags": "~2",
"oat-sa/lib-lti1p3-core": "~7",
"oat-sa/generis" : ">=15.40.0",
"oat-sa/tao-core" : ">=54.10.0"
"oat-sa/generis": ">=16.0.0",
"oat-sa/tao-core": ">=54.26.0"
},
"autoload" : {
"psr-4" : {
Expand Down
2 changes: 1 addition & 1 deletion models/classes/ExceptionInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ private function log($msg)
->get(FileSystemService::SERVICE_ID)
->getFileSystem(self::FILESYSTEM_ID_TO_LOG);

$fs->put('lti_' . $this->exception->getKey() . '.log', $msg);
$fs->write('lti_' . $this->exception->getKey() . '.log', $msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
namespace oat\taoLti\models\classes\Security\DataAccess\Repository;

use common_exception_NoImplementation;
use League\Flysystem\FilesystemInterface;
use OAT\Library\Lti1p3Core\Security\Key\Key;
use OAT\Library\Lti1p3Core\Security\Key\KeyChain;
use OAT\Library\Lti1p3Core\Security\Key\KeyChainInterface;
use OAT\Library\Lti1p3Core\Security\Key\KeyChainRepositoryInterface;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
use oat\tao\model\security\Business\Domain\Key\Key as TaoKey;
Expand Down Expand Up @@ -69,25 +70,23 @@ protected function save(KeyChainInterface $keyChain, string $identifier): void

$publicKeyPath = $configs[self::OPTION_DEFAULT_PUBLIC_KEY_PATH] ?? null;
$privateKeyPath = $configs[self::OPTION_DEFAULT_PRIVATE_KEY_PATH] ?? null;
$isPublicKeySaved = false;
$isPrivateKeySaved = false;

if ($publicKeyPath !== null && $privateKeyPath !== null) {
$isPublicKeySaved = $this->getFileSystem()
->put(
ltrim($publicKeyPath, DIRECTORY_SEPARATOR),
$keyChain->getPublicKey()->getContent()
);

$isPrivateKeySaved = $this->getFileSystem()
->put(
ltrim($privateKeyPath, DIRECTORY_SEPARATOR),
$keyChain->getPrivateKey()->getContent()
);
}

if (!$isPublicKeySaved || !$isPrivateKeySaved) {
throw new PlatformKeyChainException('Impossible to write LTI keys');
try {
$this->getFileSystem()
->write(
ltrim($publicKeyPath, DIRECTORY_SEPARATOR),
$keyChain->getPublicKey()->getContent()
);

$this->getFileSystem()
->write(
ltrim($privateKeyPath, DIRECTORY_SEPARATOR),
$keyChain->getPrivateKey()->getContent()
);
} catch (\Exception $e) {
throw new PlatformKeyChainException('Impossible to write LTI keys', 0, $e);
}
}
}

Expand All @@ -114,8 +113,8 @@ public function find(string $identifier): ?KeyChainInterface
throw new PlatformKeyChainException('The key path is not defined');
}

$publicKey = $this->getFileSystem()->read($publicKeyPath);
$privateKey = $this->getFileSystem()->read($privateKeyPath);
$publicKey = $this->readKey($publicKeyPath);
$privateKey = $this->readKey($privateKeyPath);

if ($publicKey === false || $privateKey === false) {
throw new PlatformKeyChainException('Impossible to read LTI keys');
Expand All @@ -140,8 +139,8 @@ public function findAll(KeyChainQuery $query): KeyChainCollection
$privateKeyPassphrase = $configs[self::OPTION_DEFAULT_PRIVATE_KEY_PASSPHRASE] ?? null;

if ($defaultKeyId && $publicKeyPath && $privateKeyPath) {
$publicKey = $this->getFileSystem()->read($publicKeyPath);
$privateKey = $this->getFileSystem()->read($privateKeyPath);
$publicKey = $this->readKey($publicKeyPath);
$privateKey = $this->readKey($privateKeyPath);

$keyChains[] = new TaoKeyChain(
$defaultKeyId,
Expand All @@ -168,6 +167,15 @@ public function findByKeySetName(string $keySetName): array
throw new common_exception_NoImplementation();
}

private function readKey(string $path): string
{
try {
return $this->getFileSystem()->read($path);
} catch (FilesystemException $e) {
throw new PlatformKeyChainException('Impossible to read LTI keys');
}
}

private function getFileSystem(): FilesystemInterface
{
/** @var FileSystemService $fileSystemService */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace oat\taoLti\test\unit\models\classes\Security\DataAccess\Repository;

use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;
use oat\generis\test\ServiceManagerMockTrait;
use OAT\Library\Lti1p3Core\Security\Key\Key;
use OAT\Library\Lti1p3Core\Security\Key\KeyChain;
Expand Down Expand Up @@ -129,7 +131,7 @@ public function testFindFails(): void
{
$this->fileSystem
->method('read')
->willReturn(false);
->willThrowException(new UnableToReadFile());

$keyChain = $this->subject->find('');

Expand All @@ -147,8 +149,7 @@ public function testFindWithEmptyPathFails(): void
public function testSaveDefaultKeyChain(): void
{
$this->fileSystem
->method('put')
->willReturn(true);
->method('write');

$this->subject->saveDefaultKeyChain(
new KeyChain('keyId', '', new Key(''), new Key(''))
Expand All @@ -160,8 +161,8 @@ public function testSaveDefaultKeyChain(): void
public function testSaveDefaultKeyChainFails(): void
{
$this->fileSystem
->method('put')
->willReturn(false);
->method('write')
->willThrowException(new UnableToWriteFile());

$this->expectException(PlatformKeyChainException::class);
$this->expectExceptionMessage('Impossible to write LTI keys');
Expand Down
Loading