From ca939214bd8c7f481da7bda290f82508d6a5792a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 9 Apr 2021 10:32:45 +0200 Subject: [PATCH 1/3] Allow apps to log actions into the audit_log Signed-off-by: Joas Schilling --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + apps/admin_audit/lib/AppInfo/Application.php | 3 + .../lib/Listener/AuditEventListener.php | 44 ++++++++++ lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/public/Log/AuditEvent.php | 85 +++++++++++++++++++ 7 files changed, 136 insertions(+) create mode 100644 apps/admin_audit/lib/Listener/AuditEventListener.php create mode 100644 lib/public/Log/AuditEvent.php diff --git a/apps/admin_audit/composer/composer/autoload_classmap.php b/apps/admin_audit/composer/composer/autoload_classmap.php index a08bab937240b..1d9fde9045e4a 100644 --- a/apps/admin_audit/composer/composer/autoload_classmap.php +++ b/apps/admin_audit/composer/composer/autoload_classmap.php @@ -20,4 +20,5 @@ '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', + 'OCA\\AdminAudit\\Listener\\AuditEventListener' => $baseDir . '/../lib/Listener/AuditEventListener.php', ); diff --git a/apps/admin_audit/composer/composer/autoload_static.php b/apps/admin_audit/composer/composer/autoload_static.php index cbced7b5625d0..b6556883ed43d 100644 --- a/apps/admin_audit/composer/composer/autoload_static.php +++ b/apps/admin_audit/composer/composer/autoload_static.php @@ -35,6 +35,7 @@ class ComposerStaticInitAdminAudit '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', + 'OCA\\AdminAudit\\Listener\\AuditEventListener' => __DIR__ . '/..' . '/../lib/Listener/AuditEventListener.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 d7afb96ea87ea..47290e0525dd5 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -49,6 +49,7 @@ use OCA\AdminAudit\Actions\Trashbin; use OCA\AdminAudit\Actions\UserManagement; use OCA\AdminAudit\Actions\Versions; +use OCA\AdminAudit\Listener\AuditEventListener; use OCP\App\ManagerEvent; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; @@ -61,6 +62,7 @@ use OCP\IPreview; use OCP\IServerContainer; use OCP\IUserSession; +use OCP\Log\AuditEvent; use OCP\Log\ILogFactory; use OCP\Share; use OCP\Util; @@ -78,6 +80,7 @@ public function __construct() { } public function register(IRegistrationContext $context): void { + $context->registerEventListener(AuditEvent::class, AuditEventListener::class); } public function boot(IBootContext $context): void { diff --git a/apps/admin_audit/lib/Listener/AuditEventListener.php b/apps/admin_audit/lib/Listener/AuditEventListener.php new file mode 100644 index 0000000000000..0e2e556185890 --- /dev/null +++ b/apps/admin_audit/lib/Listener/AuditEventListener.php @@ -0,0 +1,44 @@ + + * + * @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 . + * + */ + +namespace OCA\AdminAudit\Listener; + +use OCA\AdminAudit\Actions\Action; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Log\AuditEvent; + +class AuditEventListener extends Action implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof AuditEvent)) { + return; + } + + $this->log( + $event->getLogMessage(), + $event->getParameters(), + array_keys($event->getParameters()), + $event->getObfuscateParameters() + ); + } +} diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 0f8e12274a293..49293353e91e1 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -422,6 +422,7 @@ 'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\ManuallyLockedException' => $baseDir . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php', + 'OCP\\Log\\AuditEvent' => $baseDir . '/lib/public/Log/AuditEvent.php', 'OCP\\Log\\IDataLogger' => $baseDir . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 5a1728a386da4..347f7fff8ee04 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -451,6 +451,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\ManuallyLockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php', + 'OCP\\Log\\AuditEvent' => __DIR__ . '/../../..' . '/lib/public/Log/AuditEvent.php', 'OCP\\Log\\IDataLogger' => __DIR__ . '/../../..' . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php', diff --git a/lib/public/Log/AuditEvent.php b/lib/public/Log/AuditEvent.php new file mode 100644 index 0000000000000..9041367be63cd --- /dev/null +++ b/lib/public/Log/AuditEvent.php @@ -0,0 +1,85 @@ + + * + * @author Joas Schilling + * + * @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 . + * + */ + +namespace OCP\Log; + +use OCP\EventDispatcher\Event; + +/** + * Emitted when the admin_audit app should log an entry + * + * @since 22.0.0 + */ +class AuditEvent extends Event { + + /** @var string */ + private $logMessage; + + /** @var array */ + private $parameters; + + /** @var bool */ + private $obfuscateParameters; + + /** + * @param string $logMessage + * @param array $parameters + * @param bool $obfuscateParameters + * @since 18.0.0 + */ + public function __construct(string $logMessage, + array $parameters = [], + bool $obfuscateParameters = false) { + parent::__construct(); + $this->logMessage = $logMessage; + $this->parameters = $parameters; + $this->obfuscateParameters = $obfuscateParameters; + } + + /** + * @return string + * @since 22.0.0 + */ + public function getLogMessage(): string { + return $this->logMessage; + } + + /** + * @return array + * @since 22.0.0 + */ + public function getParameters(): array { + return $this->parameters; + } + + /** + * @return bool + * @since 22.0.0 + */ + public function getObfuscateParameters(): bool { + return $this->obfuscateParameters; + } +} From 99e53672089b09bc1ed7a7bb5c83f8a552a5aa5e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 9 Apr 2021 12:50:02 +0200 Subject: [PATCH 2/3] Move to another namespace and class name Signed-off-by: Joas Schilling --- apps/admin_audit/composer/composer/autoload_classmap.php | 2 +- apps/admin_audit/composer/composer/autoload_static.php | 2 +- apps/admin_audit/lib/AppInfo/Application.php | 6 +++--- ...istener.php => CriticalActionPerformedEventListener.php} | 6 +++--- lib/composer/composer/autoload_classmap.php | 2 +- lib/composer/composer/autoload_static.php | 2 +- .../CriticalActionPerformedEvent.php} | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) rename apps/admin_audit/lib/Listener/{AuditEventListener.php => CriticalActionPerformedEventListener.php} (86%) rename lib/public/Log/{AuditEvent.php => Audit/CriticalActionPerformedEvent.php} (96%) diff --git a/apps/admin_audit/composer/composer/autoload_classmap.php b/apps/admin_audit/composer/composer/autoload_classmap.php index 1d9fde9045e4a..e52032ca3ea8a 100644 --- a/apps/admin_audit/composer/composer/autoload_classmap.php +++ b/apps/admin_audit/composer/composer/autoload_classmap.php @@ -20,5 +20,5 @@ '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', - 'OCA\\AdminAudit\\Listener\\AuditEventListener' => $baseDir . '/../lib/Listener/AuditEventListener.php', + 'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => $baseDir . '/../lib/Listener/CriticalActionPerformedEventListener.php', ); diff --git a/apps/admin_audit/composer/composer/autoload_static.php b/apps/admin_audit/composer/composer/autoload_static.php index b6556883ed43d..829bc2ab049c0 100644 --- a/apps/admin_audit/composer/composer/autoload_static.php +++ b/apps/admin_audit/composer/composer/autoload_static.php @@ -35,7 +35,7 @@ class ComposerStaticInitAdminAudit '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', - 'OCA\\AdminAudit\\Listener\\AuditEventListener' => __DIR__ . '/..' . '/../lib/Listener/AuditEventListener.php', + 'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => __DIR__ . '/..' . '/../lib/Listener/CriticalActionPerformedEventListener.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 47290e0525dd5..4ee49e7129b42 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -49,7 +49,7 @@ use OCA\AdminAudit\Actions\Trashbin; use OCA\AdminAudit\Actions\UserManagement; use OCA\AdminAudit\Actions\Versions; -use OCA\AdminAudit\Listener\AuditEventListener; +use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener; use OCP\App\ManagerEvent; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; @@ -62,7 +62,7 @@ use OCP\IPreview; use OCP\IServerContainer; use OCP\IUserSession; -use OCP\Log\AuditEvent; +use OCP\Log\Audit\CriticalActionPerformedEvent; use OCP\Log\ILogFactory; use OCP\Share; use OCP\Util; @@ -80,7 +80,7 @@ public function __construct() { } public function register(IRegistrationContext $context): void { - $context->registerEventListener(AuditEvent::class, AuditEventListener::class); + $context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class); } public function boot(IBootContext $context): void { diff --git a/apps/admin_audit/lib/Listener/AuditEventListener.php b/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php similarity index 86% rename from apps/admin_audit/lib/Listener/AuditEventListener.php rename to apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php index 0e2e556185890..75f7d86d4214f 100644 --- a/apps/admin_audit/lib/Listener/AuditEventListener.php +++ b/apps/admin_audit/lib/Listener/CriticalActionPerformedEventListener.php @@ -26,11 +26,11 @@ use OCA\AdminAudit\Actions\Action; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\Log\AuditEvent; +use OCP\Log\Audit\CriticalActionPerformedEvent; -class AuditEventListener extends Action implements IEventListener { +class CriticalActionPerformedEventListener extends Action implements IEventListener { public function handle(Event $event): void { - if (!($event instanceof AuditEvent)) { + if (!($event instanceof CriticalActionPerformedEvent)) { return; } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 49293353e91e1..20f8394c00e43 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -422,7 +422,7 @@ 'OCP\\Lock\\LockedException' => $baseDir . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\ManuallyLockedException' => $baseDir . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php', - 'OCP\\Log\\AuditEvent' => $baseDir . '/lib/public/Log/AuditEvent.php', + 'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => $baseDir . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php', 'OCP\\Log\\IDataLogger' => $baseDir . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 347f7fff8ee04..70e5e9dcd57e0 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -451,7 +451,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Lock\\LockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/LockedException.php', 'OCP\\Lock\\ManuallyLockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php', - 'OCP\\Log\\AuditEvent' => __DIR__ . '/../../..' . '/lib/public/Log/AuditEvent.php', + 'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => __DIR__ . '/../../..' . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php', 'OCP\\Log\\IDataLogger' => __DIR__ . '/../../..' . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php', diff --git a/lib/public/Log/AuditEvent.php b/lib/public/Log/Audit/CriticalActionPerformedEvent.php similarity index 96% rename from lib/public/Log/AuditEvent.php rename to lib/public/Log/Audit/CriticalActionPerformedEvent.php index 9041367be63cd..307696dff0bb3 100644 --- a/lib/public/Log/AuditEvent.php +++ b/lib/public/Log/Audit/CriticalActionPerformedEvent.php @@ -24,7 +24,7 @@ * */ -namespace OCP\Log; +namespace OCP\Log\Audit; use OCP\EventDispatcher\Event; @@ -33,7 +33,7 @@ * * @since 22.0.0 */ -class AuditEvent extends Event { +class CriticalActionPerformedEvent extends Event { /** @var string */ private $logMessage; From 6d502041e07f93ad4a17db69dc5dd0befae0e930 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 12 Apr 2021 10:31:10 +0200 Subject: [PATCH 3/3] Fix version Signed-off-by: Joas Schilling --- lib/public/Log/Audit/CriticalActionPerformedEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/public/Log/Audit/CriticalActionPerformedEvent.php b/lib/public/Log/Audit/CriticalActionPerformedEvent.php index 307696dff0bb3..98e46757b93f6 100644 --- a/lib/public/Log/Audit/CriticalActionPerformedEvent.php +++ b/lib/public/Log/Audit/CriticalActionPerformedEvent.php @@ -48,7 +48,7 @@ class CriticalActionPerformedEvent extends Event { * @param string $logMessage * @param array $parameters * @param bool $obfuscateParameters - * @since 18.0.0 + * @since 22.0.0 */ public function __construct(string $logMessage, array $parameters = [],