Skip to content

Commit 6df2e22

Browse files
authored
Merge pull request #31540 from nextcloud/backport/31454/stable23
[stable23] Fix the logger that is imported for critical actions
2 parents e4b68e4 + a0de96d commit 6df2e22

File tree

7 files changed

+148
-35
lines changed

7 files changed

+148
-35
lines changed

apps/admin_audit/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
'OCA\\AdminAudit\\Actions\\UserManagement' => $baseDir . '/../lib/Actions/UserManagement.php',
2020
'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php',
2121
'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
22+
'OCA\\AdminAudit\\AuditLogger' => $baseDir . '/../lib/AuditLogger.php',
2223
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
24+
'OCA\\AdminAudit\\IAuditLogger' => $baseDir . '/../lib/IAuditLogger.php',
2325
'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => $baseDir . '/../lib/Listener/CriticalActionPerformedEventListener.php',
2426
);

apps/admin_audit/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class ComposerStaticInitAdminAudit
3434
'OCA\\AdminAudit\\Actions\\UserManagement' => __DIR__ . '/..' . '/../lib/Actions/UserManagement.php',
3535
'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php',
3636
'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
37+
'OCA\\AdminAudit\\AuditLogger' => __DIR__ . '/..' . '/../lib/AuditLogger.php',
3738
'OCA\\AdminAudit\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
39+
'OCA\\AdminAudit\\IAuditLogger' => __DIR__ . '/..' . '/../lib/IAuditLogger.php',
3840
'OCA\\AdminAudit\\Listener\\CriticalActionPerformedEventListener' => __DIR__ . '/..' . '/../lib/Listener/CriticalActionPerformedEventListener.php',
3941
);
4042

apps/admin_audit/lib/Actions/Action.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
*/
2929
namespace OCA\AdminAudit\Actions;
3030

31-
use Psr\Log\LoggerInterface;
31+
use OCA\AdminAudit\IAuditLogger;
3232

3333
class Action {
34-
/** @var LoggerInterface */
34+
/** @var IAuditLogger */
3535
private $logger;
3636

37-
public function __construct(LoggerInterface $logger) {
37+
public function __construct(IAuditLogger $logger) {
3838
$this->logger = $logger;
3939
}
4040

apps/admin_audit/lib/AppInfo/Application.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
use OCA\AdminAudit\Actions\Trashbin;
4949
use OCA\AdminAudit\Actions\UserManagement;
5050
use OCA\AdminAudit\Actions\Versions;
51+
use OCA\AdminAudit\AuditLogger;
52+
use OCA\AdminAudit\IAuditLogger;
5153
use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener;
5254
use OCP\App\ManagerEvent;
5355
use OCP\AppFramework\App;
@@ -65,6 +67,7 @@
6567
use OCP\Log\ILogFactory;
6668
use OCP\Share;
6769
use OCP\Util;
70+
use Psr\Container\ContainerInterface;
6871
use Psr\Log\LoggerInterface;
6972
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7073
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -79,14 +82,16 @@ public function __construct() {
7982
}
8083

8184
public function register(IRegistrationContext $context): void {
85+
$context->registerService(IAuditLogger::class, function (ContainerInterface $c) {
86+
return new AuditLogger($c->get(ILogFactory::class), $c->get(Iconfig::class));
87+
});
88+
8289
$context->registerEventListener(CriticalActionPerformedEvent::class, CriticalActionPerformedEventListener::class);
8390
}
8491

8592
public function boot(IBootContext $context): void {
86-
/** @var LoggerInterface $logger */
87-
$logger = $context->injectFn(
88-
Closure::fromCallable([$this, 'getLogger'])
89-
);
93+
/** @var IAuditLogger $logger */
94+
$logger = $context->getAppContainer()->get(IAuditLogger::class);
9095

9196
/*
9297
* TODO: once the hooks are migrated to lazy events, this should be done
@@ -95,26 +100,10 @@ public function boot(IBootContext $context): void {
95100
$this->registerHooks($logger, $context->getServerContainer());
96101
}
97102

98-
private function getLogger(IConfig $config,
99-
ILogFactory $logFactory): LoggerInterface {
100-
$auditType = $config->getSystemValueString('log_type_audit', 'file');
101-
$defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
102-
$auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
103-
$logFile = $config->getSystemValueString('logfile_audit', '');
104-
105-
if ($auditType === 'file' && !$logFile) {
106-
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
107-
// Legacy way was appconfig, now it's paralleled with the normal log config
108-
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
109-
}
110-
111-
return $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
112-
}
113-
114103
/**
115104
* Register hooks in order to log them
116105
*/
117-
private function registerHooks(LoggerInterface $logger,
106+
private function registerHooks(IAuditLogger $logger,
118107
IServerContainer $serverContainer): void {
119108
$this->userManagementHooks($logger, $serverContainer->get(IUserSession::class));
120109
$this->groupHooks($logger, $serverContainer->get(IGroupManager::class));
@@ -134,7 +123,7 @@ private function registerHooks(LoggerInterface $logger,
134123
$this->securityHooks($logger, $eventDispatcher);
135124
}
136125

137-
private function userManagementHooks(LoggerInterface $logger,
126+
private function userManagementHooks(IAuditLogger $logger,
138127
IUserSession $userSession): void {
139128
$userActions = new UserManagement($logger);
140129

@@ -148,7 +137,7 @@ private function userManagementHooks(LoggerInterface $logger,
148137
$userSession->listen('\OC\User', 'postUnassignedUserId', [$userActions, 'unassign']);
149138
}
150139

151-
private function groupHooks(LoggerInterface $logger,
140+
private function groupHooks(IAuditLogger $logger,
152141
IGroupManager $groupManager): void {
153142
$groupActions = new GroupManagement($logger);
154143

@@ -159,7 +148,7 @@ private function groupHooks(LoggerInterface $logger,
159148
$groupManager->listen('\OC\Group', 'postCreate', [$groupActions, 'createGroup']);
160149
}
161150

162-
private function sharingHooks(LoggerInterface $logger): void {
151+
private function sharingHooks(IAuditLogger $logger): void {
163152
$shareActions = new Sharing($logger);
164153

165154
Util::connectHook(Share::class, 'post_shared', $shareActions, 'shared');
@@ -171,15 +160,15 @@ private function sharingHooks(LoggerInterface $logger): void {
171160
Util::connectHook(Share::class, 'share_link_access', $shareActions, 'shareAccessed');
172161
}
173162

174-
private function authHooks(LoggerInterface $logger): void {
163+
private function authHooks(IAuditLogger $logger): void {
175164
$authActions = new Auth($logger);
176165

177166
Util::connectHook('OC_User', 'pre_login', $authActions, 'loginAttempt');
178167
Util::connectHook('OC_User', 'post_login', $authActions, 'loginSuccessful');
179168
Util::connectHook('OC_User', 'logout', $authActions, 'logout');
180169
}
181170

182-
private function appHooks(LoggerInterface $logger,
171+
private function appHooks(IAuditLogger $logger,
183172
EventDispatcherInterface $eventDispatcher): void {
184173
$eventDispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function (ManagerEvent $event) use ($logger) {
185174
$appActions = new AppManagement($logger);
@@ -195,15 +184,15 @@ private function appHooks(LoggerInterface $logger,
195184
});
196185
}
197186

198-
private function consoleHooks(LoggerInterface $logger,
187+
private function consoleHooks(IAuditLogger $logger,
199188
EventDispatcherInterface $eventDispatcher): void {
200189
$eventDispatcher->addListener(ConsoleEvent::EVENT_RUN, function (ConsoleEvent $event) use ($logger) {
201190
$appActions = new Console($logger);
202191
$appActions->runCommand($event->getArguments());
203192
});
204193
}
205194

206-
private function fileHooks(LoggerInterface $logger,
195+
private function fileHooks(IAuditLogger $logger,
207196
EventDispatcherInterface $eventDispatcher): void {
208197
$fileActions = new Files($logger);
209198
$eventDispatcher->addListener(
@@ -265,19 +254,19 @@ function (GenericEvent $event) use ($fileActions) {
265254
);
266255
}
267256

268-
private function versionsHooks(LoggerInterface $logger): void {
257+
private function versionsHooks(IAuditLogger $logger): void {
269258
$versionsActions = new Versions($logger);
270259
Util::connectHook('\OCP\Versions', 'rollback', $versionsActions, 'rollback');
271260
Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete');
272261
}
273262

274-
private function trashbinHooks(LoggerInterface $logger): void {
263+
private function trashbinHooks(IAuditLogger $logger): void {
275264
$trashActions = new Trashbin($logger);
276265
Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete');
277266
Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore');
278267
}
279268

280-
private function securityHooks(LoggerInterface $logger,
269+
private function securityHooks(IAuditLogger $logger,
281270
EventDispatcherInterface $eventDispatcher): void {
282271
$eventDispatcher->addListener(IProvider::EVENT_SUCCESS, function (GenericEvent $event) use ($logger) {
283272
$security = new Security($logger);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu>
4+
*
5+
* @author Carl Schwan <carl@carlschwan.eu>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\AdminAudit;
25+
26+
use OCP\IConfig;
27+
use OCP\Log\ILogFactory;
28+
use Psr\Log\LoggerInterface;
29+
30+
/**
31+
* Logger that logs in the audit log file instead of the normal log file
32+
*/
33+
class AuditLogger implements IAuditLogger {
34+
35+
/** @var LoggerInterface */
36+
private $parentLogger;
37+
38+
public function __construct(ILogFactory $logFactory, IConfig $config) {
39+
$auditType = $config->getSystemValueString('log_type_audit', 'file');
40+
$defaultTag = $config->getSystemValueString('syslog_tag', 'Nextcloud');
41+
$auditTag = $config->getSystemValueString('syslog_tag_audit', $defaultTag);
42+
$logFile = $config->getSystemValueString('logfile_audit', '');
43+
44+
if ($auditType === 'file' && !$logFile) {
45+
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/audit.log';
46+
// Legacy way was appconfig, now it's paralleled with the normal log config
47+
$logFile = $config->getAppValue('admin_audit', 'logfile', $default);
48+
}
49+
50+
$this->parentLogger = $logFactory->getCustomPsrLogger($logFile, $auditType, $auditTag);
51+
}
52+
53+
public function emergency($message, array $context = array()) {
54+
$this->parentLogger->emergency($message, $context);
55+
}
56+
57+
public function alert($message, array $context = array()) {
58+
$this->parentLogger->alert($message, $context);
59+
}
60+
61+
public function critical($message, array $context = array()) {
62+
$this->parentLogger->critical($message, $context);
63+
}
64+
65+
public function error($message, array $context = array()) {
66+
$this->parentLogger->error($message, $context);
67+
}
68+
69+
public function warning($message, array $context = array()) {
70+
$this->parentLogger->warning($message, $context);
71+
}
72+
73+
public function notice($message, array $context = array()) {
74+
$this->parentLogger->notice($message, $context);
75+
}
76+
77+
public function info($message, array $context = array()) {
78+
$this->parentLogger->info($message, $context);
79+
}
80+
81+
public function debug($message, array $context = array()) {
82+
$this->parentLogger->debug($message, $context);
83+
}
84+
85+
public function log($level, $message, array $context = array()) {
86+
$this->parentLogger->log($level, $message, $context);
87+
}
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu>
4+
*
5+
* @author Carl Schwan <carl@carlschwan.eu>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\AdminAudit;
25+
26+
use Psr\Log\LoggerInterface;
27+
28+
/**
29+
* Interface for a logger that logs in the audit log file instead of the normal log file
30+
*/
31+
interface IAuditLogger extends LoggerInterface {
32+
}

apps/admin_audit/tests/Actions/SecurityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class SecurityTest extends TestCase {
4444
protected function setUp(): void {
4545
parent::setUp();
4646

47-
$this->logger = $this->createMock(LoggerInterface::class);
47+
$this->logger = $this->createMock(AuditLogger::class);
4848
$this->security = new Security($this->logger);
4949

5050
$this->user = $this->createMock(IUser::class);

0 commit comments

Comments
 (0)