diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-05-02 16:16:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-02 16:16:28 +0200 |
commit | 2aa108000cd5bb29b7be5d7226185b5c1be59e29 (patch) | |
tree | 46d6e45c3e88bfa46bd868f6120896b2a52a2948 /apps | |
parent | a56ec1062af36c02666277f7f97f6a407a87d1e1 (diff) | |
parent | aff5fe68b31c3663be2a114666650d2f8723a22b (diff) | |
download | nextcloud-server-2aa108000cd5bb29b7be5d7226185b5c1be59e29.tar.gz nextcloud-server-2aa108000cd5bb29b7be5d7226185b5c1be59e29.zip |
Merge pull request #9293 from nextcloud/feature/9166/custom-auditlogfile
option for a seperate audit log file
Diffstat (limited to 'apps')
6 files changed, 115 insertions, 42 deletions
diff --git a/apps/admin_audit/appinfo/info.xml b/apps/admin_audit/appinfo/info.xml index 054ed9580f2..3006551b409 100644 --- a/apps/admin_audit/appinfo/info.xml +++ b/apps/admin_audit/appinfo/info.xml @@ -17,4 +17,7 @@ <dependencies> <nextcloud min-version="14" max-version="14" /> </dependencies> + <background-jobs> + <job>OCA\AdminAudit\BackgroundJobs\Rotate</job> + </background-jobs> </info> diff --git a/apps/admin_audit/composer/composer/autoload_classmap.php b/apps/admin_audit/composer/composer/autoload_classmap.php index c08200c7c20..95ddaac7452 100644 --- a/apps/admin_audit/composer/composer/autoload_classmap.php +++ b/apps/admin_audit/composer/composer/autoload_classmap.php @@ -18,4 +18,5 @@ return array( 'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php', 'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', + 'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php', ); diff --git a/apps/admin_audit/composer/composer/autoload_static.php b/apps/admin_audit/composer/composer/autoload_static.php index ef088bd22d9..1c01a35ceb2 100644 --- a/apps/admin_audit/composer/composer/autoload_static.php +++ b/apps/admin_audit/composer/composer/autoload_static.php @@ -33,6 +33,7 @@ class ComposerStaticInitAdminAudit 'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php', 'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', + 'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index df39e3eb111..77b76885a20 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -53,8 +53,26 @@ use OCP\Share; class Application extends App { + /** @var ILogger */ + protected $logger; + public function __construct() { parent::__construct('admin_audit'); + $this->initLogger(); + } + + public function initLogger() { + $c = $this->getContainer()->getServer(); + $config = $c->getConfig(); + + $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log'; + $logFile = $config->getAppValue('admin_audit', 'logfile', $default); + if($logFile === null) { + $this->logger = $c->getLogger(); + return; + } + $this->logger = $c->getLogFactory()->getCustomLogger($logFile); + } public function register() { @@ -65,26 +83,24 @@ class Application extends App { * Register hooks in order to log them */ protected function registerHooks() { - $logger = $this->getContainer()->getServer()->getLogger(); - - $this->userManagementHooks($logger); - $this->groupHooks($logger); - $this->authHooks($logger); + $this->userManagementHooks(); + $this->groupHooks(); + $this->authHooks(); - $this->consoleHooks($logger); - $this->appHooks($logger); + $this->consoleHooks(); + $this->appHooks(); - $this->sharingHooks($logger); + $this->sharingHooks(); - $this->fileHooks($logger); - $this->trashbinHooks($logger); - $this->versionsHooks($logger); + $this->fileHooks(); + $this->trashbinHooks(); + $this->versionsHooks(); - $this->securityHooks($logger); + $this->securityHooks(); } - protected function userManagementHooks(ILogger $logger) { - $userActions = new UserManagement($logger); + protected function userManagementHooks() { + $userActions = new UserManagement($this->logger); Util::connectHook('OC_User', 'post_createUser', $userActions, 'create'); Util::connectHook('OC_User', 'post_deleteUser', $userActions, 'delete'); @@ -97,8 +113,8 @@ class Application extends App { $userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']); } - protected function groupHooks(ILogger $logger) { - $groupActions = new GroupManagement($logger); + protected function groupHooks() { + $groupActions = new GroupManagement($this->logger); /** @var IGroupManager|Manager $groupManager */ $groupManager = $this->getContainer()->getServer()->getGroupManager(); @@ -108,8 +124,8 @@ class Application extends App { $groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']); } - protected function sharingHooks(ILogger $logger) { - $shareActions = new Sharing($logger); + protected function sharingHooks() { + $shareActions = new Sharing($this->logger); Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared'); Util::connectHook(Share::class, 'post_unshare', $shareActions, 'unshare'); @@ -119,42 +135,42 @@ class Application extends App { Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed'); } - protected function authHooks(ILogger $logger) { - $authActions = new Auth($logger); + protected function authHooks() { + $authActions = new Auth($this->logger); Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt'); Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful'); Util::connectHook('OC_User', 'logout', $authActions, 'logout'); } - protected function appHooks(ILogger $logger) { + protected function appHooks() { $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) { + $appActions = new AppManagement($this->logger); $appActions->enableApp($event->getAppID()); }); - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, function(ManagerEvent $event) { + $appActions = new AppManagement($this->logger); $appActions->enableAppForGroups($event->getAppID(), $event->getGroups()); }); - $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) use ($logger) { - $appActions = new AppManagement($logger); + $eventDispatcher->addListener(ManagerEvent::EVENT_APP_DISABLE, function(ManagerEvent $event) { + $appActions = new AppManagement($this->logger); $appActions->disableApp($event->getAppID()); }); } - protected function consoleHooks(ILogger $logger) { + protected function consoleHooks() { $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); - $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) use ($logger) { - $appActions = new Console($logger); + $eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function(ConsoleEvent $event) { + $appActions = new Console($this->logger); $appActions->runCommand($event->getArguments()); }); } - protected function fileHooks(ILogger $logger) { - $fileActions = new Files($logger); + protected function fileHooks() { + $fileActions = new Files($this->logger); $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); $eventDispatcher->addListener( IPreview::EVENT, @@ -215,26 +231,26 @@ class Application extends App { ); } - protected function versionsHooks(ILogger $logger) { - $versionsActions = new Versions($logger); + protected function versionsHooks() { + $versionsActions = new Versions($this->logger); Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback'); Util::connectHook('\OCP\Versions', 'delete',$versionsActions, 'delete'); } - protected function trashbinHooks(ILogger $logger) { - $trashActions = new Trashbin($logger); + protected function trashbinHooks() { + $trashActions = new Trashbin($this->logger); Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); } - protected function securityHooks(ILogger $logger) { + protected function securityHooks() { $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); - $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) use ($logger) { - $security = new Security($logger); + $eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function(GenericEvent $event) { + $security = new Security($this->logger); $security->twofactorSuccess($event->getSubject(), $event->getArguments()); }); - $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) use ($logger) { - $security = new Security($logger); + $eventDispatcher->addListener(IProvider::EVENT_FAILED, function(GenericEvent $event) { + $security = new Security($this->logger); $security->twofactorFailed($event->getSubject(), $event->getArguments()); }); } diff --git a/apps/admin_audit/lib/BackgroundJobs/Rotate.php b/apps/admin_audit/lib/BackgroundJobs/Rotate.php new file mode 100644 index 00000000000..421ee65d643 --- /dev/null +++ b/apps/admin_audit/lib/BackgroundJobs/Rotate.php @@ -0,0 +1,52 @@ +<?php +/** + * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program 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 (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\AdminAudit\BackgroundJobs; + +use OC\BackgroundJob\TimedJob; +use OCP\Log\RotationTrait; + +class Rotate extends TimedJob { + use RotationTrait; + + public function __construct() { + $this->setInterval(60*60*3); + } + + protected function run($argument) { + $config = \OC::$server->getConfig(); + $default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log'; + $this->filePath = $config->getAppValue('admin_audit', 'logfile', $default); + + if($this->filePath === '') { + // default log file, nothing to do + return; + } + + $this->maxSize = $config->getSystemValue('log_rotate_size', 100 * 1024 * 1024); + + if($this->shouldRotateBySize()) { + $this->rotate(); + } + } +} diff --git a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php index c1d48a7ce5d..1de9333207f 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php @@ -69,7 +69,7 @@ class ExceptionLoggerPluginTest extends TestCase { }); $this->server = new Server(); - $this->logger = new TestLogger(Log\File::class, $config); + $this->logger = new TestLogger(new Log\File(\OC::$SERVERROOT.'/data/nextcloud.log', '', $config), $config); $this->plugin = new PluginToTest('unit-test', $this->logger); $this->plugin->initialize($this->server); } |