Skip to content

Commit

Permalink
Merge pull request #97 from PKISharp/96-fix-autogen
Browse files Browse the repository at this point in the history
96 fix autogen
  • Loading branch information
glatzert authored Jul 30, 2020
2 parents e0080cd + b55bd0e commit 32dc6f5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
6 changes: 4 additions & 2 deletions ACME-PS/functions/Order/Complete-Order.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ function Complete-Order {
$ErrorActionPreference = 'Stop';

if($GenerateCertificateKey) {
$CertificateKey = $State.GetOrderCertificateKey($Order);
$OrderCertificateKey = $State.GetOrderCertificateKey($Order);

if($null -eq $CertificateKey) {
if($null -eq $OrderCertificateKey) {
$SaveCertificateKey = $true;
$CertificateKey = New-CertificateKey -SkipKeyExport -WarningAction 'SilentlyContinue';
} else {
$CertificateKey = $OrderCertificateKey;
}
}

Expand Down
15 changes: 15 additions & 0 deletions ACME-PS/internal/classes/AcmeState.InMemory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class AcmeInMemoryState : AcmeState {
[ValidateNotNull()] hidden [AcmeAccount] $Account;

hidden [hashtable] $Orders = @{};
hidden [hashtable] $CertKeys = @{}

AcmeInMemoryState() {
}
Expand Down Expand Up @@ -33,4 +34,18 @@ class AcmeInMemoryState : AcmeState {
$orderHash = $order.GetHashString();
$this.Orders.Remove($orderHash);
}

[ICertificateKey] GetOrderCertificateKey([AcmeOrder] $order) {
$orderHash = $order.GetHashString();
if ($this.CertKeys.ContainsKey($orderHash)) {
return $this.CertKeys[$orderHash];
}

return $null;
}

[void] SetOrderCertificateKey([AcmeOrder] $order, [ICertificateKey] $certificateKey) {
$orderHash = $order.GetHashString();
$this.CertKeys[$orderHash] = $certificateKey;
}
}
20 changes: 15 additions & 5 deletions ACME-PS/tests/Complete-Order.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ InModuleScope ACME-PS {
AuthorizationUrl = "https://service.acme/Order1/AuthZ";
FinalizeUrl = "https://service.acme/Order1/Finalize";
};

Mock Invoke-AcmeWebRequest {
$mockResult = [AcmeHttpResponse]::new();
$mockResult.NextNonce = "NextNonce";
Expand All @@ -23,7 +23,7 @@ InModuleScope ACME-PS {
$state.Set($simpleState.GetAccountKey());
$state.Set($simpleState.GetAccount());

$order = [AcmeOrder]::new([PSCustomObject]@{
$orderData = [PSCustomObject]@{
Status = "ready";
Expires = [DateTime]::Now.AddDays(1);

Expand All @@ -39,16 +39,26 @@ InModuleScope ACME-PS {
CSROptions = [AcmeCsrOptions]::new([PSCustomObject]@{
DistinguishedName = "CN=CommonName";
});
});

$certificateKey = New-CertificateKey -RSA -SkipKeyExport -WarningAction 'SilentlyContinue';
};


Context 'CustomKey parameter set' {
$order = [AcmeOrder]::new($orderData);
$certificateKey = New-CertificateKey -RSA -SkipKeyExport -WarningAction 'SilentlyContinue';
Complete-Order -State $state -Order $order -CertificateKey $certificateKey;

It 'called ACME service to finalize the order' {
Assert-VerifiableMock
}
}

Context 'GenerateKey paraeter set' {
$order = [AcmeOrder]::new($orderData);
Complete-Order -State $state -Order $order -GenerateCertificateKey;

It 'called ACME service to finalize the order' {
Assert-VerifiableMock
}
}
}
}
9 changes: 4 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ process {

Write-Information "Finished building - running tests";

if ($null -ne (Get-Module PSScriptAnalyzer -ListAvailable)) {
& Invoke-ScriptAnalyzer -Path $ModuleOutFile;
}

<# Run tests #>
& Invoke-Command -ScriptBlock {
Import-Module "$ModuleOutPath\ACME-PS.psd1" -ErrorAction 'Stop'
Invoke-Pester -Path "$ModuleSourcePath\tests"
if ($null -ne (Get-Module PSScriptAnalyzer -ListAvailable)) {
& Invoke-ScriptAnalyzer -Path $ModuleOutFile;
}
& Invoke-Pester -Path "$ModuleSourcePath\tests"
}
}
21 changes: 19 additions & 2 deletions dist/ACME-PS/ACME-PS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ class AcmeInMemoryState : AcmeState {
[ValidateNotNull()] hidden [AcmeAccount] $Account;

hidden [hashtable] $Orders = @{};
hidden [hashtable] $CertKeys = @{}

AcmeInMemoryState() {
}
Expand Down Expand Up @@ -948,6 +949,20 @@ class AcmeInMemoryState : AcmeState {
$orderHash = $order.GetHashString();
$this.Orders.Remove($orderHash);
}

[ICertificateKey] GetOrderCertificateKey([AcmeOrder] $order) {
$orderHash = $order.GetHashString();
if ($this.CertKeys.ContainsKey($orderHash)) {
return $this.CertKeys[$orderHash];
}

return $null;
}

[void] SetOrderCertificateKey([AcmeOrder] $order, [ICertificateKey] $certificateKey) {
$orderHash = $order.GetHashString();
$this.CertKeys[$orderHash] = $certificateKey;
}
}

class AcmeStatePaths {
Expand Down Expand Up @@ -2760,11 +2775,13 @@ function Complete-Order {
$ErrorActionPreference = 'Stop';

if($GenerateCertificateKey) {
$CertificateKey = $State.GetOrderCertificateKey($Order);
$OrderCertificateKey = $State.GetOrderCertificateKey($Order);

if($null -eq $CertificateKey) {
if($null -eq $OrderCertificateKey) {
$SaveCertificateKey = $true;
$CertificateKey = New-CertificateKey -SkipKeyExport -WarningAction 'SilentlyContinue';
} else {
$CertificateKey = $OrderCertificateKey;
}
}

Expand Down

0 comments on commit 32dc6f5

Please sign in to comment.