Skip to content

Commit

Permalink
Ask for email when generating LetsEncrypt SSLs (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jan 29, 2025
1 parent 270928a commit 53e20cb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/Actions/SSL/CreateSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function create(Site $site, array $input): void
'pk' => $input['private'] ?? null,
'expires_at' => $input['type'] === SslType::LETSENCRYPT ? now()->addMonths(3) : $input['expires_at'],
'status' => SslStatus::CREATING,
'email' => $input['email'] ?? null,
]);
$ssl->domains = [$site->domain];
if (isset($input['aliases']) && $input['aliases']) {
Expand Down Expand Up @@ -69,6 +70,12 @@ public static function rules(array $input): array
'after_or_equal:'.now(),
];
}
if (isset($input['type']) && $input['type'] == SslType::LETSENCRYPT) {
$rules['email'] = [
'required',
'email',
];
}

return $rules;
}
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Ssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @property string $ca_path
* @property ?array $domains
* @property int $log_id
* @property string $email
* @property ?ServerLog $log
*/
class Ssl extends AbstractModel
Expand All @@ -36,6 +37,7 @@ class Ssl extends AbstractModel
'status',
'domains',
'log_id',
'email',
];

protected $casts = [
Expand Down Expand Up @@ -143,4 +145,13 @@ public function log(): BelongsTo
{
return $this->belongsTo(ServerLog::class);
}

public function getEmailAttribute(?string $value): string
{
if ($value) {
return $value;
}

return $this->site->server->creator->email;
}
}
2 changes: 1 addition & 1 deletion app/SSH/Services/Webserver/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function setupSSL(Ssl $ssl): void
$domains .= ' -d '.$domain;
}
$command = view('ssh.services.webserver.nginx.create-letsencrypt-ssl', [
'email' => $ssl->site->server->creator->email,
'email' => $ssl->email,
'domain' => $ssl->site->domain,
'domains' => $domains,
]);
Expand Down
16 changes: 13 additions & 3 deletions app/Web/Pages/Servers/Sites/Pages/SSL/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace App\Web\Pages\Servers\Sites\Pages\SSL;

use App\Actions\SSL\CreateSSL;
use App\Enums\SslType;
use App\Models\Ssl;
use App\Web\Fields\AlertField;
use App\Web\Pages\Servers\Sites\Page;
use Filament\Actions\Action;
use Filament\Actions\CreateAction;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Support\Enums\MaxWidth;

Expand Down Expand Up @@ -45,25 +48,32 @@ protected function getHeaderActions(): array
->label('New Certificate')
->icon('heroicon-o-lock-closed')
->form([
AlertField::make('letsencrypt-info')
->warning()
->message('Let\'s Encrypt has rate limits. Read more about them <a href="https://letsencrypt.org/docs/rate-limits/" target="_blank" class="underline">here</a>.'),
Select::make('type')
->options(
collect(config('core.ssl_types'))->mapWithKeys(fn ($type) => [$type => $type])
)
->rules(fn (Get $get) => CreateSSL::rules($get())['type'])
->reactive(),
TextInput::make('email')
->rules(fn (Get $get) => CreateSSL::rules($get())['email'] ?? [])
->visible(fn (Get $get) => $get('type') === SslType::LETSENCRYPT)
->helperText('Email address to provide to Certbot.'),
Textarea::make('certificate')
->rows(5)
->rules(fn (Get $get) => CreateSSL::rules($get())['certificate'])
->visible(fn (Get $get) => $get('type') === 'custom'),
->visible(fn (Get $get) => $get('type') === SslType::CUSTOM),
Textarea::make('private')
->label('Private Key')
->rows(5)
->rules(fn (Get $get) => CreateSSL::rules($get())['private'])
->visible(fn (Get $get) => $get('type') === 'custom'),
->visible(fn (Get $get) => $get('type') === SslType::CUSTOM),
DatePicker::make('expires_at')
->format('Y-m-d')
->rules(fn (Get $get) => CreateSSL::rules($get())['expires_at'])
->visible(fn (Get $get) => $get('type') === 'custom'),
->visible(fn (Get $get) => $get('type') === SslType::CUSTOM),
Checkbox::make('aliases')
->label("Set SSL for site's aliases as well"),
])
Expand Down
22 changes: 22 additions & 0 deletions database/migrations/2025_01_29_192733_add_email_to_ssls_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('ssls', function (Blueprint $table) {
$table->string('email')->nullable();
});
}

public function down(): void
{
Schema::table('ssls', function (Blueprint $table) {
$table->dropColumn('email');
});
}
};
2 changes: 1 addition & 1 deletion resources/views/fields/alert.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div
class="border-{{ $getColor() }}-500 bg-{{ $getColor() }}-100 text-{{ $getColor() }}-700 dark:bg-{{ $getColor() }}-500 dark:text-{{ $getColor() }}-500 rounded-lg border border-l-4 px-4 py-3 dark:bg-opacity-10"
>
{{ $getMessage() }}
{!! $getMessage() !!}
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions tests/Feature/SslTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function test_letsencrypt_ssl()
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
'email' => '[email protected]',
])
->assertSuccessful();

Expand All @@ -52,6 +53,7 @@ public function test_letsencrypt_ssl()
'type' => SslType::LETSENCRYPT,
'status' => SslStatus::CREATED,
'domains' => json_encode([$this->site->domain]),
'email' => '[email protected]',
]);
}

Expand All @@ -67,6 +69,7 @@ public function test_letsencrypt_ssl_with_aliases()
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
'email' => '[email protected]',
'aliases' => true,
])
->assertSuccessful();
Expand All @@ -76,6 +79,7 @@ public function test_letsencrypt_ssl_with_aliases()
'type' => SslType::LETSENCRYPT,
'status' => SslStatus::CREATED,
'domains' => json_encode(array_merge([$this->site->domain], $this->site->aliases)),
'email' => '[email protected]',
]);
}

Expand Down

0 comments on commit 53e20cb

Please sign in to comment.