vault/builtin/logical/pki/acme_state_test.go
Alexander Scheel b1f0d4e495
Add nonce service to sdk/helpers, use in PKI (#20688)
* Build a better nonce service

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add internal nonce service for testing

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add benchmarks for nonce service

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add statistics around how long tidy took

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Replace ACME nonces with shared nonce service

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add an initialize method to nonce services

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Use the new initialize helper on nonce service in PKI

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add additional tests for nonces

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Format sdk/helper/nonce

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Use default 90s nonce expiry in PKI

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Remove parallel test case as covered by benchmark

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add additional commentary to encrypted nonce implementation

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

* Add nonce to test_packages

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>

---------

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
2023-05-23 19:44:05 +00:00

44 lines
842 B
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package pki
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAcmeNonces(t *testing.T) {
t.Parallel()
a := NewACMEState()
a.nonces.Initialize()
// Simple operation should succeed.
nonce, _, err := a.GetNonce()
require.NoError(t, err)
require.NotEmpty(t, nonce)
require.True(t, a.RedeemNonce(nonce))
require.False(t, a.RedeemNonce(nonce))
// Redeeming in opposite order should work.
var nonces []string
for i := 0; i < len(nonce); i++ {
nonce, _, err = a.GetNonce()
require.NoError(t, err)
require.NotEmpty(t, nonce)
}
for i := len(nonces) - 1; i >= 0; i-- {
nonce = nonces[i]
require.True(t, a.RedeemNonce(nonce))
}
for i := 0; i < len(nonces); i++ {
nonce = nonces[i]
require.False(t, a.RedeemNonce(nonce))
}
}