Skip to content

Commit

Permalink
custom cloudflare token
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Mar 11, 2024
1 parent 6c04422 commit 035c93a
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/Containers/Commands/CertbotCloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ class CertbotCloudflare extends Command{

protected $signature = 'certbot:cloudflare
{task : task to execute (create|renew)}
{--token=}
';

protected $description = 'Manage SSL certificates throught certbot cloudflare provider';

public function handle(DockerService $docker_service, TerminalService $terminal): bool{

$task = $this->argument('task');
$token = $this->option('token');

switch($task){
case 'create':
return $this->create_certificate($docker_service, $terminal);
return $this->create_certificate($docker_service, $terminal, $token);
case 'renew':
return $this->renew_certificates($docker_service, $terminal);
return $this->renew_certificates($docker_service, $terminal, $token);
default:
$this->error("Unrecognized task");
$this->line("please run 'dock help certbot:cloudflare' for a list of available options");
Expand All @@ -35,7 +37,7 @@ public function handle(DockerService $docker_service, TerminalService $terminal)

}

private function create_certificate(DockerService $docker_service, TerminalService $terminal): bool{
private function create_certificate(DockerService $docker_service, TerminalService $terminal, string|null $token): bool{

$this->title('Certbot certificate creation');

Expand All @@ -47,6 +49,12 @@ private function create_certificate(DockerService $docker_service, TerminalServi
$domains[] = $domain;
}

if($token === null){
$token_file = '/root/cloudflare.ini';
}else{
$token_file = "/root/cloudflare-$token.ini";
}


if(empty($domains)){
$this->error('No domain selected');
Expand All @@ -58,7 +66,7 @@ private function create_certificate(DockerService $docker_service, TerminalServi
'certonly',
'--dns-cloudflare',
'--dns-cloudflare-credentials',
'/root/cloudflare.ini',
$token_file,
'--dns-cloudflare-propagation-seconds',
60,
"--email",
Expand All @@ -76,20 +84,26 @@ private function create_certificate(DockerService $docker_service, TerminalServi

}

private function renew_certificates(DockerService $docker_service, TerminalService $terminal): bool{
private function renew_certificates(DockerService $docker_service, TerminalService $terminal, string|null $token): bool{
$this->title('Certbot certificate renewal');

if($token === null){
$token_file = '/root/cloudflare.ini';
}else{
$token_file = "/root/cloudflare-$token.ini";
}

collect(Storage::disk('configs')->files('certbot/letsencrypt/renewal'))
->filter(function (string $file) {
$content = Storage::disk('configs')->get($file);
return Str::of($content)->contains("authenticator = dns-cloudflare");
})->map(fn (string $file): string => Str::of($file)->afterLast("/")->before('.conf'))
->each(function (string $domain) use ($terminal, $docker_service) {
->each(function (string $domain) use ($token_file, $terminal, $docker_service) {
$command = [
'renew',
'--dns-cloudflare',
'--dns-cloudflare-credentials',
'/root/cloudflare.ini',
$token_file,
'--dns-cloudflare-propagation-seconds',
60,
"--agree-tos",
Expand Down

0 comments on commit 035c93a

Please sign in to comment.