Skip to content

Commit d245b32

Browse files
committed
feat(federation): add occ command to manage trusted servers
Signed-off-by: Tom Wezepoel <tom.wezepoel@surf.nl>
1 parent b63fed1 commit d245b32

4 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎apps/federation/appinfo/info.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</background-jobs>
3131

3232
<commands>
33+
<command>OCA\Federation\Command\ManageTrustedServers</command>
3334
<command>OCA\Federation\Command\SyncFederationAddressBooks</command>
3435
<command>OCA\Federation\Command\SyncFederationCalendars</command>
3536
</commands>

‎apps/federation/composer/composer/autoload_classmap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'OCA\\Federation\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1111
'OCA\\Federation\\BackgroundJob\\GetSharedSecret' => $baseDir . '/../lib/BackgroundJob/GetSharedSecret.php',
1212
'OCA\\Federation\\BackgroundJob\\RequestSharedSecret' => $baseDir . '/../lib/BackgroundJob/RequestSharedSecret.php',
13+
'OCA\\Federation\\Command\\ManageTrustedServers' => $baseDir . '/../lib/Command/ManageTrustedServers.php',
1314
'OCA\\Federation\\Command\\SyncFederationAddressBooks' => $baseDir . '/../lib/Command/SyncFederationAddressBooks.php',
1415
'OCA\\Federation\\Command\\SyncFederationCalendars' => $baseDir . '/../lib/Command/SyncFederationCalendars.php',
1516
'OCA\\Federation\\Controller\\OCSAuthAPIController' => $baseDir . '/../lib/Controller/OCSAuthAPIController.php',

‎apps/federation/composer/composer/autoload_static.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ComposerStaticInitFederation
2525
'OCA\\Federation\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
2626
'OCA\\Federation\\BackgroundJob\\GetSharedSecret' => __DIR__ . '/..' . '/../lib/BackgroundJob/GetSharedSecret.php',
2727
'OCA\\Federation\\BackgroundJob\\RequestSharedSecret' => __DIR__ . '/..' . '/../lib/BackgroundJob/RequestSharedSecret.php',
28+
'OCA\\Federation\\Command\\ManageTrustedServers' => __DIR__ . '/..' . '/../lib/Command/ManageTrustedServers.php',
2829
'OCA\\Federation\\Command\\SyncFederationAddressBooks' => __DIR__ . '/..' . '/../lib/Command/SyncFederationAddressBooks.php',
2930
'OCA\\Federation\\Command\\SyncFederationCalendars' => __DIR__ . '/..' . '/../lib/Command/SyncFederationCalendars.php',
3031
'OCA\\Federation\\Controller\\OCSAuthAPIController' => __DIR__ . '/..' . '/../lib/Controller/OCSAuthAPIController.php',
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Federation\Command;
11+
12+
use OCA\Federation\TrustedServers;
13+
use OCP\Console\Attribute\Argument;
14+
use OCP\Console\Attribute\AsCommand;
15+
use OCP\Console\ExitCode;
16+
use OCP\Console\IOutput;
17+
18+
class ManageTrustedServers {
19+
private const STATUS_LABELS = [
20+
TrustedServers::STATUS_OK => 'ok',
21+
TrustedServers::STATUS_PENDING => 'pending',
22+
TrustedServers::STATUS_FAILURE => 'failure',
23+
TrustedServers::STATUS_ACCESS_REVOKED => 'access revoked',
24+
];
25+
26+
public function __construct(
27+
private TrustedServers $trustedServersManager,
28+
) {
29+
}
30+
31+
#[AsCommand(
32+
name: 'federation:trusted-servers:add',
33+
description: 'Add trusted servers',
34+
)]
35+
public function add(
36+
IOutput $output,
37+
#[Argument(description: 'space-separated list of server URLs to add to the trusted servers list')]
38+
array $servers = [],
39+
): ExitCode {
40+
if (empty($servers)) {
41+
$output->writeln('<error>You must provide at least one server URL.</error>');
42+
return ExitCode::Invalid;
43+
}
44+
45+
$output->writeln('Adding trusted servers:');
46+
$hasFailure = false;
47+
foreach ($servers as $server) {
48+
if ($this->trustedServersManager->isTrustedServer($server)) {
49+
$output->writeln(" - $server already trusted, skipping");
50+
continue;
51+
}
52+
53+
try {
54+
$this->trustedServersManager->addServer($server);
55+
$output->writeln(" - $server added");
56+
} catch (\Exception $e) {
57+
$output->writeln(" - $server failed: " . $e->getMessage());
58+
$hasFailure = true;
59+
}
60+
}
61+
62+
return $hasFailure ? ExitCode::Failure : ExitCode::Success;
63+
}
64+
65+
#[AsCommand(
66+
name: 'federation:trusted-servers:remove',
67+
description: 'Remove trusted servers',
68+
)]
69+
public function remove(
70+
IOutput $output,
71+
#[Argument(description: 'space-separated list of server URLs to remove from the trusted servers list')]
72+
array $servers = [],
73+
): ExitCode {
74+
if (empty($servers)) {
75+
$output->writeln('<error>You must provide at least one server URL.</error>');
76+
return ExitCode::Invalid;
77+
}
78+
79+
$output->writeln('Removing trusted servers:');
80+
$hasFailure = false;
81+
foreach ($servers as $server) {
82+
$id = $this->findServerId($server);
83+
if ($id === null) {
84+
$output->writeln(" - $server not found, skipping");
85+
continue;
86+
}
87+
88+
try {
89+
$this->trustedServersManager->removeServer($id);
90+
$output->writeln(" - $server removed");
91+
} catch (\Exception $e) {
92+
$output->writeln(" - $server failed: " . $e->getMessage());
93+
$hasFailure = true;
94+
}
95+
}
96+
97+
return $hasFailure ? ExitCode::Failure : ExitCode::Success;
98+
}
99+
100+
#[AsCommand(
101+
name: 'federation:trusted-servers:list',
102+
description: 'List trusted servers',
103+
supportsOutputFormat: true,
104+
)]
105+
public function list(
106+
IOutput $output,
107+
): ExitCode {
108+
$servers = $this->trustedServersManager->getServers();
109+
110+
if (empty($servers)) {
111+
$output->writeln('No trusted servers configured.');
112+
return ExitCode::Success;
113+
}
114+
115+
$output->writeTableInOutputFormat(
116+
array_map(fn (array $server) => [
117+
'id' => $server['id'],
118+
'url' => $server['url'],
119+
'status' => self::STATUS_LABELS[$server['status']] ?? (string)$server['status'],
120+
], $servers),
121+
);
122+
123+
return ExitCode::Success;
124+
}
125+
126+
/**
127+
* Trusted servers are stored with a normalized (https:// prefixed,
128+
* trailing-slash trimmed) URL, so removal has to look up the id by
129+
* matching that same normalization rather than the raw user input.
130+
*/
131+
private function findServerId(string $url): ?int {
132+
$normalized = rtrim(
133+
(str_starts_with($url, 'http://') || str_starts_with($url, 'https://'))
134+
? $url
135+
: 'https://' . $url,
136+
'/'
137+
);
138+
139+
foreach ($this->trustedServersManager->getServers() ?? [] as $server) {
140+
if (rtrim($server['url'], '/') === $normalized) {
141+
return $server['id'];
142+
}
143+
}
144+
145+
return null;
146+
}
147+
}

0 commit comments

Comments
 (0)