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 27f36e2 commit fc700bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
11 changes: 7 additions & 4 deletions app/Containers/CertbotCloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CertbotCloudflare extends Container{
protected string $service_name = 'certbot-cloudflare';

const PATH_LETSENCRYPT_DIR = 'certbot/letsencrypt';
const PATH_TOKENS_FOLDER = 'certbot/tokens';
const PATH_CLOUDFLARE_INI = 'certbot/cloudflare.ini';

private string $cloudflare_token;
Expand All @@ -20,7 +21,7 @@ class CertbotCloudflare extends Container{

protected array $volumes = [
self::HOST_CONFIG_VOLUME_PATH . self::PATH_LETSENCRYPT_DIR => '/etc/letsencrypt',
self::HOST_CONFIG_VOLUME_PATH . self::PATH_CLOUDFLARE_INI => '/root/cloudflare.ini',
self::HOST_CONFIG_VOLUME_PATH . self::PATH_TOKENS_FOLDER => '/root/tokens',
];

public function __construct(string $cloudflare_token){
Expand All @@ -29,16 +30,18 @@ public function __construct(string $cloudflare_token){
$this->cloudflare_token = $cloudflare_token;
}

public function publish_assets(){
public function publish_assets(): void
{
$this->publish_cloudflare_ini();
}

private function publish_cloudflare_ini(){
private function publish_cloudflare_ini(): void
{
$template = Storage::get(self::PATH_CLOUDFLARE_INI);
$this->compile_template($template, [
'dns_cloudflare_api_token' => $this->cloudflare_token,
]);
$this->disk()->put(self::PATH_CLOUDFLARE_INI, $template);
$this->disk()->put(self::PATH_TOKENS_FOLDER.'/default.ini', $template);
}

public function commands(): array{
Expand Down
25 changes: 6 additions & 19 deletions app/Containers/Commands/CertbotCloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CertbotCloudflare extends Command{

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

protected $description = 'Manage SSL certificates throught certbot cloudflare provider';
Expand All @@ -37,7 +37,7 @@ public function handle(DockerService $docker_service, TerminalService $terminal)

}

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

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

Expand All @@ -49,13 +49,6 @@ 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 @@ -67,7 +60,7 @@ private function create_certificate(DockerService $docker_service, TerminalServi
'certonly',
'--dns-cloudflare',
'--dns-cloudflare-credentials',
$token_file,
"/root/tokens/$token.ini",
'--dns-cloudflare-propagation-seconds',
60,
"--email",
Expand All @@ -85,26 +78,20 @@ private function create_certificate(DockerService $docker_service, TerminalServi

}

private function renew_certificates(DockerService $docker_service, TerminalService $terminal, string|null $token): bool{
private function renew_certificates(DockerService $docker_service, TerminalService $terminal, string $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 ($token_file, $terminal, $docker_service) {
->each(function (string $domain) use ($token, $terminal, $docker_service) {
$command = [
'renew',
'--dns-cloudflare',
'--dns-cloudflare-credentials',
$token_file,
"/root/tokens/$token.ini",
'--dns-cloudflare-propagation-seconds',
60,
"--agree-tos",
Expand Down

0 comments on commit fc700bd

Please sign in to comment.