2018-08-21 15:20:57 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2022-03-23 21:47:43 +00:00
|
|
|
"context"
|
2018-08-21 15:20:57 +00:00
|
|
|
"crypto/x509"
|
2022-04-06 13:14:41 +00:00
|
|
|
"crypto/x509/pkix"
|
2018-08-21 15:20:57 +00:00
|
|
|
"testing"
|
2022-04-06 13:14:41 +00:00
|
|
|
"time"
|
2018-08-21 15:20:57 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-08-21 15:20:57 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
2022-04-06 13:14:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-21 15:20:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_CRL_EnableDisable(t *testing.T) {
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
LogicalBackends: map[string]logical.Factory{
|
|
|
|
"pki": Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
client := cluster.Cores[0].Client
|
|
|
|
var err error
|
2022-03-23 21:47:43 +00:00
|
|
|
err = client.Sys().MountWithContext(context.Background(), "pki", &api.MountInput{
|
2018-08-21 15:20:57 +00:00
|
|
|
Type: "pki",
|
|
|
|
Config: api.MountConfigInput{
|
|
|
|
DefaultLeaseTTL: "16h",
|
|
|
|
MaxLeaseTTL: "60h",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := client.Logical().WriteWithContext(context.Background(), "pki/root/generate/internal", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"ttl": "40h",
|
|
|
|
"common_name": "myvault.com",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
caSerial := resp.Data["serial_number"]
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
_, err = client.Logical().WriteWithContext(context.Background(), "pki/roles/test", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"allow_bare_domains": true,
|
|
|
|
"allow_subdomains": true,
|
|
|
|
"allowed_domains": "foobar.com",
|
|
|
|
"generate_lease": true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
serials := make(map[int]string)
|
2018-08-21 15:20:57 +00:00
|
|
|
for i := 0; i < 6; i++ {
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := client.Logical().WriteWithContext(context.Background(), "pki/issue/test", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"common_name": "test.foobar.com",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
serials[i] = resp.Data["serial_number"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
test := func(num int) {
|
2022-04-06 13:14:41 +00:00
|
|
|
certList := getCrlCertificateList(t, client)
|
|
|
|
lenList := len(certList.RevokedCertificates)
|
2018-08-21 15:20:57 +00:00
|
|
|
if lenList != num {
|
2022-04-06 13:14:41 +00:00
|
|
|
t.Fatalf("expected %d revoked certificates, found %d", num, lenList)
|
2018-08-21 15:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
revoke := func(num int) {
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err = client.Logical().WriteWithContext(context.Background(), "pki/revoke", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"serial_number": serials[num],
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err = client.Logical().WriteWithContext(context.Background(), "pki/revoke", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"serial_number": caSerial,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle := func(disabled bool) {
|
2022-03-23 21:47:43 +00:00
|
|
|
_, err = client.Logical().WriteWithContext(context.Background(), "pki/config/crl", map[string]interface{}{
|
2018-08-21 15:20:57 +00:00
|
|
|
"disable": disabled,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test(0)
|
|
|
|
revoke(0)
|
|
|
|
revoke(1)
|
|
|
|
test(2)
|
|
|
|
toggle(true)
|
|
|
|
test(0)
|
|
|
|
revoke(2)
|
|
|
|
revoke(3)
|
|
|
|
test(0)
|
|
|
|
toggle(false)
|
|
|
|
test(4)
|
|
|
|
revoke(4)
|
|
|
|
revoke(5)
|
|
|
|
test(6)
|
|
|
|
toggle(true)
|
|
|
|
test(0)
|
|
|
|
toggle(false)
|
|
|
|
test(6)
|
2022-04-06 13:14:41 +00:00
|
|
|
|
|
|
|
// The rotate command should reset the update time of the CRL.
|
|
|
|
crlCreationTime1 := getCrlCertificateList(t, client).ThisUpdate
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
_, err = client.Logical().Read("pki/crl/rotate")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
crlCreationTime2 := getCrlCertificateList(t, client).ThisUpdate
|
|
|
|
require.NotEqual(t, crlCreationTime1, crlCreationTime2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCrlCertificateList(t *testing.T, client *api.Client) pkix.TBSCertificateList {
|
|
|
|
resp, err := client.Logical().ReadWithContext(context.Background(), "pki/cert/crl")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
crlPem := resp.Data["certificate"].(string)
|
|
|
|
certList, err := x509.ParseCRL([]byte(crlPem))
|
|
|
|
require.NoError(t, err)
|
|
|
|
return certList.TBSCertList
|
2018-08-21 15:20:57 +00:00
|
|
|
}
|