mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-19 22:14:45 +02:00
Add own action class for "rotate database password" (#822)
This commit is contained in:
parent
d43b99792f
commit
914f3dcdbd
@ -2,14 +2,11 @@
|
||||
|
||||
namespace App\Filament\Admin\Resources\DatabaseHostResource\RelationManagers;
|
||||
|
||||
use App\Filament\Components\Forms\Actions\RotateDatabasePasswordAction;
|
||||
use App\Filament\Components\Tables\Columns\DateTimeColumn;
|
||||
use App\Models\Database;
|
||||
use App\Services\Databases\DatabasePasswordService;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Forms\Set;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables\Actions\DeleteAction;
|
||||
use Filament\Tables\Actions\ViewAction;
|
||||
@ -30,25 +27,19 @@ class DatabasesRelationManager extends RelationManager
|
||||
TextInput::make('password')
|
||||
->password()
|
||||
->revealable()
|
||||
->hintAction(
|
||||
Action::make('rotate')
|
||||
->icon('tabler-refresh')
|
||||
->requiresConfirmation()
|
||||
->action(fn (DatabasePasswordService $service, Database $database, $set, $get) => $this->rotatePassword($service, $database, $set, $get))
|
||||
->authorize(fn (Database $database) => auth()->user()->can('update database', $database))
|
||||
)
|
||||
->hintAction(RotateDatabasePasswordAction::make())
|
||||
->formatStateUsing(fn (Database $database) => $database->password),
|
||||
TextInput::make('remote')
|
||||
->label('Connections From')
|
||||
->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote),
|
||||
->formatStateUsing(fn (Database $record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote),
|
||||
TextInput::make('max_connections')
|
||||
->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections),
|
||||
TextInput::make('JDBC')
|
||||
->formatStateUsing(fn (Database $record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections),
|
||||
TextInput::make('jdbc')
|
||||
->label('JDBC Connection String')
|
||||
->columnSpanFull()
|
||||
->password()
|
||||
->revealable()
|
||||
->formatStateUsing(fn (Get $get, Database $database) => 'jdbc:mysql://' . $get('username') . ':' . urlencode($database->password) . '@' . $database->host->host . ':' . $database->host->port . '/' . $get('database')),
|
||||
->formatStateUsing(fn (Database $database) => $database->jdbc),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -62,7 +53,7 @@ class DatabasesRelationManager extends RelationManager
|
||||
TextColumn::make('username')
|
||||
->icon('tabler-user'),
|
||||
TextColumn::make('remote')
|
||||
->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote),
|
||||
->formatStateUsing(fn (Database $record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote),
|
||||
TextColumn::make('server.name')
|
||||
->icon('tabler-brand-docker')
|
||||
->url(fn (Database $database) => route('filament.admin.resources.servers.edit', ['record' => $database->server_id])),
|
||||
@ -78,13 +69,4 @@ class DatabasesRelationManager extends RelationManager
|
||||
->hidden(fn () => !auth()->user()->can('viewList database')),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function rotatePassword(DatabasePasswordService $service, Database $database, Set $set, Get $get): void
|
||||
{
|
||||
$newPassword = $service->handle($database);
|
||||
$jdbcString = 'jdbc:mysql://' . $get('username') . ':' . urlencode($newPassword) . '@' . $database->host->host . ':' . $database->host->port . '/' . $get('database');
|
||||
|
||||
$set('password', $newPassword);
|
||||
$set('JDBC', $jdbcString);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use App\Enums\ContainerStatus;
|
||||
use App\Enums\ServerState;
|
||||
use App\Filament\Admin\Resources\ServerResource;
|
||||
use App\Filament\Admin\Resources\ServerResource\RelationManagers\AllocationsRelationManager;
|
||||
use App\Filament\Components\Forms\Actions\RotateDatabasePasswordAction;
|
||||
use App\Filament\Server\Pages\Console;
|
||||
use App\Models\Database;
|
||||
use App\Models\DatabaseHost;
|
||||
@ -14,7 +15,6 @@ use App\Models\Mount;
|
||||
use App\Models\Server;
|
||||
use App\Models\ServerVariable;
|
||||
use App\Services\Databases\DatabaseManagementService;
|
||||
use App\Services\Databases\DatabasePasswordService;
|
||||
use App\Services\Eggs\EggChangerService;
|
||||
use App\Services\Servers\RandomWordService;
|
||||
use App\Services\Servers\ReinstallServerService;
|
||||
@ -667,31 +667,24 @@ class EditServer extends EditRecord
|
||||
->password()
|
||||
->revealable()
|
||||
->columnSpan(1)
|
||||
->hintAction(
|
||||
Action::make('rotate')
|
||||
->authorize(fn (Database $database) => auth()->user()->can('update database', $database))
|
||||
->icon('tabler-refresh')
|
||||
->modalHeading('Change Database Password?')
|
||||
->action(fn (DatabasePasswordService $service, $record, $set, $get) => $this->rotatePassword($service, $record, $set, $get))
|
||||
->requiresConfirmation()
|
||||
)
|
||||
->hintAction(RotateDatabasePasswordAction::make())
|
||||
->formatStateUsing(fn (Database $database) => $database->password),
|
||||
TextInput::make('remote')
|
||||
->disabled()
|
||||
->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote)
|
||||
->formatStateUsing(fn (Database $record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote)
|
||||
->columnSpan(1)
|
||||
->label('Connections From'),
|
||||
TextInput::make('max_connections')
|
||||
->disabled()
|
||||
->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections)
|
||||
->formatStateUsing(fn (Database $record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections)
|
||||
->columnSpan(1),
|
||||
TextInput::make('JDBC')
|
||||
TextInput::make('jdbc')
|
||||
->disabled()
|
||||
->password()
|
||||
->revealable()
|
||||
->label('JDBC Connection String')
|
||||
->columnSpan(2)
|
||||
->formatStateUsing(fn (Get $get, $record) => 'jdbc:mysql://' . $get('username') . ':' . urlencode($record->password) . '@' . $record->host->host . ':' . $record->host->port . '/' . $get('database')),
|
||||
->formatStateUsing(fn (Database $record) => $record->jdbc),
|
||||
])
|
||||
->relationship('databases')
|
||||
->deletable(false)
|
||||
@ -950,13 +943,4 @@ class EditServer extends EditRecord
|
||||
->mapWithKeys(fn ($value) => [$value => $value])
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function rotatePassword(DatabasePasswordService $service, Database $record, Set $set, Get $get): void
|
||||
{
|
||||
$newPassword = $service->handle($record);
|
||||
$jdbcString = 'jdbc:mysql://' . $get('username') . ':' . urlencode($newPassword) . '@' . $record->host->host . ':' . $record->host->port . '/' . $get('database');
|
||||
|
||||
$set('password', $newPassword);
|
||||
$set('JDBC', $jdbcString);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Components\Forms\Actions;
|
||||
|
||||
use App\Models\Database;
|
||||
use App\Services\Databases\DatabasePasswordService;
|
||||
use Exception;
|
||||
use Filament\Actions\StaticAction;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Set;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
class RotateDatabasePasswordAction extends Action
|
||||
{
|
||||
public static function getDefaultName(): ?string
|
||||
{
|
||||
return 'rotate';
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->label('Rotate');
|
||||
|
||||
$this->icon('tabler-refresh');
|
||||
|
||||
$this->authorize(fn (Database $database) => auth()->user()->can('update database', $database));
|
||||
|
||||
$this->modalHeading('Rotate Password');
|
||||
|
||||
$this->modalIconColor('warning');
|
||||
|
||||
$this->modalSubmitAction(fn (StaticAction $action) => $action->color('warning'));
|
||||
|
||||
$this->requiresConfirmation();
|
||||
|
||||
$this->action(function (DatabasePasswordService $service, Database $database, Set $set) {
|
||||
try {
|
||||
$service->handle($database);
|
||||
|
||||
$database->refresh();
|
||||
|
||||
$set('password', $database->password);
|
||||
$set('jdbc', $database->jdbc);
|
||||
|
||||
Notification::make()
|
||||
->title('Password rotated')
|
||||
->success()
|
||||
->send();
|
||||
} catch (Exception $exception) {
|
||||
Notification::make()
|
||||
->title('Password rotation failed')
|
||||
->body($exception->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
report($exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Server\Resources\DatabaseResource\Pages;
|
||||
|
||||
use App\Filament\Components\Forms\Actions\RotateDatabasePasswordAction;
|
||||
use App\Filament\Components\Tables\Columns\DateTimeColumn;
|
||||
use App\Filament\Server\Resources\DatabaseResource;
|
||||
use App\Models\Database;
|
||||
@ -9,15 +10,12 @@ use App\Models\DatabaseHost;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Server;
|
||||
use App\Services\Databases\DatabaseManagementService;
|
||||
use App\Services\Databases\DatabasePasswordService;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Grid;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Filament\Tables\Actions\DeleteAction;
|
||||
use Filament\Tables\Actions\ViewAction;
|
||||
@ -45,16 +43,8 @@ class ListDatabases extends ListRecords
|
||||
->password()->revealable()
|
||||
->hidden(fn () => !auth()->user()->can(Permission::ACTION_DATABASE_VIEW_PASSWORD, $server))
|
||||
->hintAction(
|
||||
Action::make('rotate')
|
||||
RotateDatabasePasswordAction::make()
|
||||
->authorize(fn () => auth()->user()->can(Permission::ACTION_DATABASE_UPDATE, $server))
|
||||
->icon('tabler-refresh')
|
||||
->requiresConfirmation()
|
||||
->action(function (DatabasePasswordService $service, Database $database, $set, $get) {
|
||||
$newPassword = $service->handle($database);
|
||||
|
||||
$set('password', $newPassword);
|
||||
$set('JDBC', 'jdbc:mysql://' . $get('username') . ':' . urlencode($newPassword) . '@' . $database->host->host . ':' . $database->host->port . '/' . $get('database'));
|
||||
})
|
||||
)
|
||||
->suffixAction(CopyAction::make())
|
||||
->formatStateUsing(fn (Database $database) => $database->password),
|
||||
@ -62,13 +52,13 @@ class ListDatabases extends ListRecords
|
||||
->label('Connections From'),
|
||||
TextInput::make('max_connections')
|
||||
->formatStateUsing(fn (Database $database) => $database->max_connections === 0 ? $database->max_connections : 'Unlimited'),
|
||||
TextInput::make('JDBC')
|
||||
TextInput::make('jdbc')
|
||||
->label('JDBC Connection String')
|
||||
->password()->revealable()
|
||||
->hidden(!auth()->user()->can(Permission::ACTION_DATABASE_VIEW_PASSWORD, $server))
|
||||
->suffixAction(CopyAction::make())
|
||||
->columnSpanFull()
|
||||
->formatStateUsing(fn (Get $get, Database $database) => 'jdbc:mysql://' . $get('username') . ':' . urlencode($database->password) . '@' . $database->host->host . ':' . $database->host->port . '/' . $get('database')),
|
||||
->formatStateUsing(fn (Database $database) => $database->jdbc),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@ -14,6 +15,7 @@ use Illuminate\Support\Facades\DB;
|
||||
* @property string $remote
|
||||
* @property string $password
|
||||
* @property int $max_connections
|
||||
* @property string $jdbc
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property \App\Models\Server $server
|
||||
@ -87,6 +89,13 @@ class Database extends Model
|
||||
return $this->belongsTo(Server::class);
|
||||
}
|
||||
|
||||
protected function jdbc(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => 'jdbc:mysql://' . $this->username . ':' . urlencode($this->password) . '@' . $this->host->host . ':' . $this->host->port . '/' . $this->database,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the provided statement against the database on a given connection.
|
||||
*/
|
||||
|
@ -20,7 +20,7 @@ class DatabasePasswordService
|
||||
/**
|
||||
* Updates a password for a given database.
|
||||
*/
|
||||
public function handle(Database|int $database): string
|
||||
public function handle(Database|int $database): void
|
||||
{
|
||||
if (is_int($database)) {
|
||||
$database = Database::query()->findOrFail($database);
|
||||
@ -40,7 +40,5 @@ class DatabasePasswordService
|
||||
$database->assignUserToDatabase($database->database, $database->username, $database->remote);
|
||||
$database->flush();
|
||||
});
|
||||
|
||||
return $password;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user