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

PoC Config objects #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions config-templates/module_logpeek.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
logFile: /var/log/simplesamlphp.log
lines: 1500
blockSize: 8192
154 changes: 154 additions & 0 deletions lib/Config/Logpeek.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Module\logpeek\Config;

use SimpleSAML\Assert\Assert;
use SimpleSAML\Configuration;
use SimpleSAML\Utils;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

/**
* Configuration for the module logpeek.
*/

class Logpeek {
/** @var string */
public const DEFAULT_CONFIGFILE = 'module_logpeek.yml';

/** @var int */
public const MAX_BLOCKSIZE = 8192;

/** @var int */
public const DEFAULT_BLOCKSIZE = 8192;

/** @var int */
public const DEFAULT_LINES = 500;

/** @var string */
private string $logFile;

/** @var int */
private int $lines;

/** @var int */
private int $blockSize;


/**
* @param string|null $logFile
* @param int $blockSize
* @param int $lines
*/
public function __construct(?string $logFile, ?int $blockSize = null, ?int $lines = null)
{
if ($logFile === null) {
$config = Configuration::getInstance();
$loggingDir = $config->getPathValue('loggingdir', 'log/');
$logFile = $loggingDir . $config->getString('logging.logfile', 'simplesamlphp.log');
}

$this->setLogFile($logFile);
$this->setBlockSize($blockSize ?? self::DEFAULT_BLOCKSIZE);
$this->setLines($lines ?? self::DEFAULT_LINES);
}


/**
* @return string
*/
public function getLogFile(): string
{
return $this->logFile;
}


/**
* @param string $logFile
* @return void
*/
protected function setLogFile(string $logFile): void
{
$this->logFile = $logFile;
}


/**
* @return int
*/
public function getLines(): int
{
return $this->lines;
}


/**
* @param int $lines
* @return void
*/
protected function setLines(int $lines): void
{
Assert::positiveInteger($lines);
Copy link
Member Author

Choose a reason for hiding this comment

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

We can add many assertions to test the config values

$this->lines = $lines;
}


/**
* @return int
*/
public function getBlockSize(): int
{
return $this->blockSize;
}


/**
* @param int $blockSize
* @return void
*/
protected function setBlockSize(int $blockSize): void
{
Assert::range($blockSize, 0, self::MAX_BLOCKSIZE);
$this->blockSize = $blockSize;
}


/**
* @param string $file
* @return self
*/
public static function fromPhp(string $configFile = 'module_logpeek.php'): self
Copy link
Member Author

Choose a reason for hiding this comment

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

For BC compatibility

{
$configUtils = new Utils\Config();
$configDir = $configUtils->getConfigDir();
include($configDir . '/' . $configFile);

return static::fromArray($config ?? []);
}


/**
* @param string $file
* @return self
*/
public static function fromYaml(string $configFile = self::DEFAULT_CONFIGFILE): self
Copy link
Member Author

Choose a reason for hiding this comment

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

Is completely generic, so could be moved to an AbstractConfigObject

{
$configUtils = new Utils\Config();
$configDir = $configUtils->getConfigDir();
$yamlConfig = Yaml::parse(file_get_contents($configDir . '/' . $configFile));

return static::fromArray($yamlConfig ?? []);
}


/**
* @param array $config
* @return self
*/
public static function fromArray(array $config): self
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be implemented to assist unit testing

{
return new self($config['logFile'], $config['blockSize'], $config['lines']);
}
};
27 changes: 19 additions & 8 deletions lib/Controller/Logpeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Configuration;
use SimpleSAML\Module\logpeek\Config;
use SimpleSAML\Module\logpeek\File;
use SimpleSAML\Module\logpeek\Syslog;
use SimpleSAML\Session;
Expand All @@ -33,8 +34,8 @@ class Logpeek
/** @var \SimpleSAML\Configuration */
protected Configuration $config;

/** @var \SimpleSAML\Configuration */
protected Configuration $moduleConfig;
/** @var \SimpleSAML\Module\logpeek\Config\Logpeek */
protected Config\Logpeek $moduleConfig;

/** @var \SimpleSAML\Session */
protected Session $session;
Expand All @@ -59,7 +60,7 @@ public function __construct(
) {
$this->authUtils = new Utils\Auth();
$this->config = $config;
$this->moduleConfig = Configuration::getConfig('module_logpeek.php');
$this->moduleConfig = Config\Logpeek::fromYaml();
$this->session = $session;
}

Expand All @@ -75,6 +76,17 @@ public function setAuthUtils(Utils\Auth $authUtils): void
}


/**
* Inject the \SimpleSAML\Module\logpeek\Config\Logpeek dependency.
*
* @param \SimpleSAML\Module\logpeek\Config\Logpeek $moduleConfig
*/
public function setModuleConfig(Config\Logpeek $moduleConfig): void
{
$this->moduleConfig = $moduleConfig;
}


/**
* Main index controller.
*
Expand All @@ -86,18 +98,17 @@ public function main(Request $request): Template
{
$this->authUtils->requireAdmin();

$logfile = $this->moduleConfig->getValue('logfile', '/var/log/simplesamlphp.log');
$blockSize = $this->moduleConfig->getValue('blocksz', 8192);

$myLog = new File\ReverseRead($logfile, $blockSize);
$logFile = $this->moduleConfig->getLogFile();
$blockSize = $this->moduleConfig->getBlockSize();
$myLog = new File\ReverseRead($logFile, $blockSize);

$results = [];
if ($request->query->has('tag') === true) {
/** @psalm-var string $tag */
$tag = $request->query->get('tag');
Assert::notNull($tag);

$results = $this->logFilter($myLog, $tag, $this->moduleConfig->getValue('lines', 500));
$results = $this->logFilter($myLog, $tag, $this->moduleConfig->getLines());
}

$fileModYear = intval(date("Y", $myLog->getFileMtime()));
Expand Down
35 changes: 18 additions & 17 deletions tests/lib/Controller/LogpeekTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
use SimpleSAML\Module\logpeek\Config;
use SimpleSAML\Module\logpeek\Controller;
use SimpleSAML\Session;
use SimpleSAML\Utils;
Expand All @@ -29,6 +30,9 @@ class LogpeekTest extends TestCase
/** @var \SimpleSAML\Configuration */
protected Configuration $config;

/** @var \SimpleSAML\Module\logpeek\Config\Logpeek */
protected Config\Logpeek $moduleConfig;

/** @var \SimpleSAML\Session */
protected Session $session;

Expand Down Expand Up @@ -77,20 +81,14 @@ public function requireAdmin(): void
}
};

Configuration::setPreLoadedConfig(
Configuration::loadFromArray(
[
'logfile' => $this->tmpfile,
'lines' => 1500,

// Read block size. 8192 is max, limited by fread.
'blocksz' => 8192,
],
'[ARRAY]',
'simplesaml'
),
'module_logpeek.php',
'simplesaml'
$this->moduleConfig = Config\Logpeek::fromArray(
Copy link
Member Author

Choose a reason for hiding this comment

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

We can easily create a test-config from an array

[
'logFile' => $this->tmpfile,
'lines' => 1500,

// Read block size. 8192 is max, limited by fread.
'blockSize' => 8192,
]
);
}

Expand All @@ -115,8 +113,9 @@ public function testMain(): void
'GET'
);

$c = new Controller\Logpeek($this->config, $this->session);
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig);
$c->setAuthUtils($this->authUtils);
$c->setModuleConfig($this->moduleConfig);
$response = $c->main($request);

$this->assertTrue($response->isSuccessful());
Expand All @@ -133,8 +132,9 @@ public function testMainWithTag(): void
['tag' => $this->session->getTrackID()]
);

$c = new Controller\Logpeek($this->config, $this->session);
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually 'wrong';
We either have to pass the config through the constructor, or use the setter two lines below.
The setter is probably the way to go for now, until our module-system allows us to dynamically register services.. The config could then be passed as a service by the routing system, the same way we pass the configuration/session classes

$c->setAuthUtils($this->authUtils);
$c->setModuleConfig($this->moduleConfig);
$response = $c->main($request);

$this->assertTrue($response->isSuccessful());
Expand All @@ -151,8 +151,9 @@ public function testMainWithInvalidTag(): void
['tag' => 'WRONG']
);

$c = new Controller\Logpeek($this->config, $this->session);
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig);
$c->setAuthUtils($this->authUtils);
$c->setModuleConfig($this->moduleConfig);

$this->expectException(Exception::class);
$c->main($request);
Expand Down