Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat(database): add flush cache option for Redis, KeyDB and Dragonfly

Aditya Tripathi52cad11f344d
diff --git a/app/Actions/Database/FlushCacheDatabase.php b/app/Actions/Database/FlushCacheDatabase.php
new file mode 100644
index 0000000000..998d2cd5c8
--- /dev/null
+++ b/app/Actions/Database/FlushCacheDatabase.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App\Actions\Database;
+
+use App\Models\StandaloneDragonfly;
+use App\Models\StandaloneKeydb;
+use App\Models\StandaloneRedis;
+use Lorisleiva\Actions\Concerns\AsAction;
+
+class FlushCacheDatabase
+{
+ use AsAction;
+
+ public function handle(StandaloneRedis|StandaloneKeydb|StandaloneDragonfly $database): void
+ {
+ $server = $database->destination->server;
+
+ if (! $server->isFunctional()) {
+ throw new \RuntimeException('Server is not functional.');
+ }
+
+ if (! $database->isRunning()) {
+ throw new \RuntimeException('Database is not running.');
+ }
+
+ $command = $this->buildFlushCommand($database->uuid, $this->resolvePassword($database));
+
+ instant_remote_process(command: [$command], server: $server, throwError: true);
+ }
+
+ /**
+ * Build the `redis-cli FLUSHALL ASYNC` command executed inside the running container.
+ *
+ * The command connects over the container's local plaintext port (available even when
+ * SSL is enabled, because Coolify never disables it), so no TLS flags are needed. The
+ * password is shell-escaped so it cannot break out of the argument and inject commands.
+ */
+ public function buildFlushCommand(string $containerName, ?string $password): string
+ {
+ $redisCli = 'redis-cli';
+ if (filled($password)) {
+ $redisCli .= ' -a '.escapeshellarg($password);
+ }
+
+ return "docker exec {$containerName} {$redisCli} FLUSHALL ASYNC";
+ }
+
+ private function resolvePassword(StandaloneRedis|StandaloneKeydb|StandaloneDragonfly $database): ?string
+ {
+ return match (true) {
+ $database instanceof StandaloneRedis => $database->redis_password,
+ $database instanceof StandaloneKeydb => $database->keydb_password,
+ $database instanceof StandaloneDragonfly => $database->dragonfly_password,
+ };
+ }
+}
diff --git a/app/Livewire/Project/Database/Heading.php b/app/Livewire/Project/Database/Heading.php
index a7e8cfedd8..354d9ed8d2 100644
--- a/app/Livewire/Project/Database/Heading.php
+++ b/app/Livewire/Project/Database/Heading.php
@@ -2,11 +2,15 @@
namespace App\Livewire\Project\Database;
+use App\Actions\Database\FlushCacheDatabase;
use App\Actions\Database\RestartDatabase;
use App\Actions\Database\StartDatabase;
use App\Actions\Database\StopDatabase;
use App\Actions\Docker\GetContainersStatus;
use App\Events\ServiceStatusChanged;
+use App\Models\StandaloneDragonfly;
+use App\Models\StandaloneKeydb;
+use App\Models\StandaloneRedis;
use App\Support\ResourceStartActivity;
use App\Traits\ListensToTeamChannel;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
@@ -146,6 +150,23 @@ class Heading extends Component
}
}
+ public function flush()
+ {
+ try {
+ $this->authorize('manage', $this->database);
+
+ if (! $this->isCacheDatabase()) {
+ throw new \RuntimeException('This database type does not support cache flushing.');
+ }
+
+ FlushCacheDatabase::run($this->database);
+ $this->auditDatabaseAction('ui.database.flushed');
+ $this->dispatch('success', 'Cache flushed successfully.');
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
+ }
+ }
+
public function restart()
{
try {
@@ -192,9 +213,17 @@ class Heading extends Component
'checkboxes' => [
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
],
+ 'isCacheDatabase' => $this->isCacheDatabase(),
]);
}
+ private function isCacheDatabase(): bool
+ {
+ return $this->database instanceof StandaloneRedis
+ || $this->database instanceof StandaloneKeydb
+ || $this->database instanceof StandaloneDragonfly;
+ }
+
private function auditDatabaseAction(string $event): void
{
auditLog($event, [
diff --git a/resources/views/livewire/project/database/heading.blade.php b/resources/views/livewire/project/database/heading.blade.php
index abf58177c7..43e6b5af4b 100644
--- a/resources/views/livewire/project/database/heading.blade.php
+++ b/resources/views/livewire/project/database/heading.blade.php
@@ -88,6 +88,13 @@
<x-reicon name="restart" class="size-3.5" x-show="!busy" />
<span x-text="busy ? 'Restarting…' : 'Restart'">Restart</span>
</x-slot:main>
+ @if ($isCacheDatabase)
+ <button type="button" class="listbox-option justify-start! gap-2.5!"
+ @click="open = false; document.getElementById('database-flush-trigger')?.click()" role="menuitem">
+ <x-reicon name="trash" class="size-3.5 text-error" />
+ Flush cache
+ </button>
+ @endif
<button type="button" class="listbox-option justify-start! gap-2.5!"
@click="open = false; document.getElementById('database-stop-trigger')?.click()" role="menuitem">
<x-reicon name="stop-circle" class="size-3.5 text-error" />
@@ -124,6 +131,13 @@
<x-reicon name="restart" class="size-3.5" x-show="!busy" />
<span x-text="busy ? 'Restarting…' : 'Restart'">Restart</span>
</x-slot:main>
+ @if ($isCacheDatabase)
+ <button type="button" class="listbox-option justify-start! gap-2.5!"
+ @click="open = false; document.getElementById('database-flush-trigger')?.click()" role="menuitem">
+ <x-reicon name="trash" class="size-3.5 text-error" />
+ Flush cache
+ </button>
+ @endif
<button type="button" class="listbox-option justify-start! gap-2.5!"
@click="open = false; document.getElementById('database-stop-trigger')?.click()" role="menuitem">
<x-reicon name="stop-circle" class="size-3.5 text-error" />
@@ -171,6 +185,19 @@
<button id="database-stop-trigger" type="button">Stop</button>
</x-slot:trigger>
</x-modal-confirmation>
+ @if ($isCacheDatabase)
+ <x-modal-confirmation title="Confirm Cache Flush?" buttonTitle="Flush cache" submitAction="flush"
+ :confirmWithText="true" :confirmationText="$database->name" :confirmWithPassword="false"
+ :actions="[
+ 'Every key stored in this cache will be permanently erased.',
+ 'This action cannot be undone and data cannot be recovered.',
+ 'The database container keeps running; only its data is removed.',
+ ]" step1ButtonText="Continue" step2ButtonText="Flush cache">
+ <x-slot:trigger>
+ <button id="database-flush-trigger" type="button">Flush cache</button>
+ </x-slot:trigger>
+ </x-modal-confirmation>
+ @endif
</div>
@endif
diff --git a/tests/Unit/FlushCacheDatabaseCommandTest.php b/tests/Unit/FlushCacheDatabaseCommandTest.php
new file mode 100644
index 0000000000..86982b457c
--- /dev/null
+++ b/tests/Unit/FlushCacheDatabaseCommandTest.php
@@ -0,0 +1,39 @@
+<?php
+
+use App\Actions\Database\FlushCacheDatabase;
+
+dataset('malicious_passwords', [
+ 'semicolon separator' => ['pass; id > /tmp/pwned; echo'],
+ 'command substitution $()' => ['pass$(id > /tmp/pwned)'],
+ 'backtick substitution' => ['pass`id > /tmp/pwned`'],
+ 'pipe operator' => ['pass | cat /etc/passwd'],
+ 'background operator' => ['pass & curl http://evil.com'],
+ 'single quote breakout' => ["pass' ; rm -rf / ; '"],
+]);
+
+test('flush command runs FLUSHALL ASYNC via docker exec against the container uuid', function () {
+ $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', 'secret');
+
+ expect($command)->toStartWith('docker exec abc123uuid redis-cli')
+ ->and($command)->toEndWith('FLUSHALL ASYNC')
+ ->and($command)->toContain("-a 'secret'");
+});
+
+test('flush command omits the -a flag when no password is set', function () {
+ $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', null);
+
+ expect($command)->toBe('docker exec abc123uuid redis-cli FLUSHALL ASYNC')
+ ->and($command)->not->toContain('-a');
+});
+
+test('flush command shell-escapes the password so it cannot inject shell commands', function (string $malicious) {
+ $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', $malicious);
+
+ // The password must appear only inside a single-quoted, escaped argument.
+ expect($command)->toContain(escapeshellarg($malicious))
+ ->and($command)->toEndWith('FLUSHALL ASYNC');
+
+ // Strip the escaped password out; nothing dangerous may remain in the raw command.
+ $withoutPassword = str_replace(escapeshellarg($malicious), '', $command);
+ expect($withoutPassword)->toBe('docker exec abc123uuid redis-cli -a FLUSHALL ASYNC');
+})->with('malicious_passwords');