Skip to content

Commit

Permalink
Merge pull request #24 from Daanra/feat/subject-alternative-names
Browse files Browse the repository at this point in the history
Add Subject Alternative Names support
  • Loading branch information
Daanra authored Sep 16, 2022
2 parents 46964d8 + 76e5612 commit a077905
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ $certificate->delete();
$certificate->forceDelete();
```


## Subject Alternative Names

It's also possible to specify Subject Alternative Names as below (requires >= 0.5.0):

```php
LetsEncrypt::certificate('mydomain.com')
->setSubjectAlternativeNames(['mydomain2.com'])
->create();
```

## Failure events

If one of the jobs fails, one of the following events will be dispatched:
Expand Down
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;

class AddLetsEncryptCertificatesSubjectAlternativeNames extends Migration
{
public function up()
{
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
$table->json('subject_alternative_names')->default('[]')->after('domain');
});
}

public function down()
{
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
$table->dropColumn('subject_alternative_names');
});
}
}
2 changes: 1 addition & 1 deletion src/Jobs/RequestCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(LetsEncryptCertificate $certificate, int $tries = nu

public function handle()
{
$distinguishedName = new DistinguishedName($this->certificate->domain);
$distinguishedName = new DistinguishedName($this->certificate->domain, null, null, null, null, null, null, $this->certificate->subject_alternative_names);
$csr = new CertificateRequest($distinguishedName, (new KeyPairGenerator())->generateKeyPair());
$client = LetsEncrypt::createClient();
$certificateResponse = $client->requestCertificate($this->certificate->domain, $csr);
Expand Down
7 changes: 7 additions & 0 deletions src/LetsEncryptServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function boot()
__DIR__ . "/../database/migrations/{$migrationFileName}.stub" => database_path('migrations/' . date('Y_m_d_His', time()) . '_' . $migrationFileName),
], 'lets-encrypt');
}

$sanMigrationFileName = 'add_lets_encrypt_certificates_subject_alternative_names.php';
if (! $this->migrationFileExists($sanMigrationFileName)) {
$this->publishes([
__DIR__ . "/../database/migrations/{$sanMigrationFileName}.stub" => database_path('migrations/' . date('Y_m_d_His', time() + 1) . '_' . $sanMigrationFileName),
], ['lets-encrypt', 'lets-encrypt-0.5']);
}
}

$this->commands([
Expand Down
2 changes: 2 additions & 0 deletions src/Models/LetsEncryptCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property array $subject_alternative_names
* @property-read bool $has_expired
* @method static \Daanra\LaravelLetsEncrypt\Builders\LetsEncryptCertificateBuilder|\Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate query()
* @method static \Daanra\LaravelLetsEncrypt\Builders\LetsEncryptCertificateBuilder|\Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate newQuery()
Expand All @@ -42,6 +43,7 @@ class LetsEncryptCertificate extends Model

protected $casts = [
'created' => 'boolean',
'subject_alternative_names' => 'array',
];

public function newEloquentBuilder($query): LetsEncryptCertificateBuilder
Expand Down
19 changes: 18 additions & 1 deletion src/PendingCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

class PendingCertificate
{

/**
* @var string
*/
Expand Down Expand Up @@ -43,6 +42,11 @@ class PendingCertificate
*/
protected $delay = 0;

/**
* @var array
*/
protected $subjectAlternativeNames = [];

/**
* PendingCertificate constructor.
* @param string $domain
Expand All @@ -67,6 +71,7 @@ public function create(): LetsEncryptCertificate

$certificate = LetsEncryptCertificate::create([
'domain' => $this->domain,
'subject_alternative_names' => $this->subjectAlternativeNames,
]);

RegisterAccount::withChain(array_merge([
Expand Down Expand Up @@ -118,6 +123,18 @@ public function renew(): LetsEncryptCertificate
return $certificate;
}


/**
* @param array $domains
* @return static
*/
public function setSubjectAlternativeNames(array $domains): self
{
$this->subjectAlternativeNames = $domains;

return $this;
}

/**
* @param int $tries
* @return static
Expand Down
1 change: 0 additions & 1 deletion src/Traits/Retryable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

trait Retryable
{

/**
* The number of times the job may be attempted.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Facades/LetsEncryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,26 @@ public function test_can_create_pending()
$certificate = LetsEncrypt::certificate('test.test')->create();

$this->assertEquals('test.test', $certificate->domain);
$this->assertEquals([], $certificate->subject_alternative_names);

Queue::assertPushedWithChain(RegisterAccount::class, [
RequestAuthorization::class,
RequestCertificate::class,
]);
}

/** @test */
public function test_can_create_now_with_san()
{
Bus::fake();

$certificate = LetsEncrypt::certificate('somedomain.com')
->setSubjectAlternativeNames(['other.somedomain.com'])
->create();

$this->assertEquals('somedomain.com', $certificate->domain);
$this->assertEquals(['other.somedomain.com'], $certificate->subject_alternative_names);

Bus::assertDispatched(RegisterAccount::class);
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function getEnvironmentSetUp($app)
]);

include_once __DIR__.'/../database/migrations/create_lets_encrypt_certificates_table.php.stub';
include_once __DIR__.'/../database/migrations/add_lets_encrypt_certificates_subject_alternative_names.php.stub';
(new \CreateLetsEncryptCertificatesTable())->up();
(new \AddLetsEncryptCertificatesSubjectAlternativeNames())->up();
}
}

0 comments on commit a077905

Please sign in to comment.