2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2022-09-08 13:19:21 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2022-06-20 18:20:17 -07:00
|
|
|
package server
|
2022-06-03 12:13:31 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-10-12 10:33:41 -07:00
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/kms"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/types/scope"
|
2022-06-03 12:13:31 -04:00
|
|
|
"github.com/hashicorp/nodeenrollment"
|
|
|
|
|
"github.com/hashicorp/nodeenrollment/types"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRotateRoots(t *testing.T) {
|
|
|
|
|
require, assert := require.New(t), assert.New(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
wrapper := db.TestWrapper(t)
|
|
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
|
|
|
err := kmsCache.CreateKeys(context.Background(), scope.Global.String(), kms.WithRandomReader(rand.Reader))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
rw := db.New(conn)
|
|
|
|
|
workerAuthRepo, err := NewRepositoryStorage(ctx, rw, rw, kmsCache)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
// Check that we have no roots first
|
|
|
|
|
rootIds, err := workerAuthRepo.List(ctx, (*types.RootCertificate)(nil))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Len(rootIds, 0)
|
|
|
|
|
|
|
|
|
|
// Generate roots
|
2022-09-08 13:19:21 -04:00
|
|
|
roots, err := RotateRoots(ctx, workerAuthRepo, nodeenrollment.WithCertificateLifetime(time.Second*5))
|
2022-06-03 12:13:31 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
// Check that we have roots now
|
|
|
|
|
rootIds, err = workerAuthRepo.List(ctx, (*types.RootCertificate)(nil))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Len(rootIds, 2)
|
2022-09-08 13:19:21 -04:00
|
|
|
certAuthority := &types.RootCertificates{Id: CaId}
|
2022-06-03 12:13:31 -04:00
|
|
|
err = workerAuthRepo.Load(ctx, certAuthority)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
require.NotNil(certAuthority.GetNext())
|
|
|
|
|
require.NotNil(certAuthority.GetCurrent())
|
|
|
|
|
|
2022-09-08 13:19:21 -04:00
|
|
|
initialNext := roots.GetNext()
|
|
|
|
|
initialCurrent := roots.GetCurrent()
|
2022-06-03 12:13:31 -04:00
|
|
|
|
|
|
|
|
// Rotate roots and assert that they've rotated
|
2022-09-08 13:19:21 -04:00
|
|
|
newRoots, err := RotateRoots(ctx, workerAuthRepo, nodeenrollment.WithCertificateLifetime(time.Second*5))
|
2022-06-03 12:13:31 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2022-09-08 13:19:21 -04:00
|
|
|
require.NotNil(newRoots.GetNext())
|
|
|
|
|
require.NotNil(newRoots.GetCurrent())
|
|
|
|
|
rotatedNext := newRoots.GetNext()
|
|
|
|
|
rotatedCurrent := newRoots.GetCurrent()
|
2022-06-03 12:13:31 -04:00
|
|
|
|
|
|
|
|
// Next and current should have changed
|
2022-09-08 13:19:21 -04:00
|
|
|
assert.NotEqual(initialNext.PublicKeyPkix, rotatedNext.PublicKeyPkix)
|
|
|
|
|
assert.NotEqual(initialCurrent.PublicKeyPkix, rotatedCurrent.PublicKeyPkix)
|
2022-06-03 12:13:31 -04:00
|
|
|
|
|
|
|
|
// And the old next root should now be current
|
2022-09-08 13:19:21 -04:00
|
|
|
assert.Equal(initialNext.PublicKeyPkix, rotatedCurrent.PublicKeyPkix)
|
2022-06-03 12:13:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRotateRootsFailure(t *testing.T) {
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
wrapper := db.TestWrapper(t)
|
|
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
|
|
|
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
|
|
|
err := kmsCache.CreateKeys(context.Background(), scope.Global.String(), kms.WithRandomReader(rand.Reader))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
workerAuthRepo, err := NewRepositoryStorage(ctx, &db.Db{}, &db.Db{}, kmsCache)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
2022-09-08 13:19:21 -04:00
|
|
|
_, err = RotateRoots(ctx, workerAuthRepo)
|
2022-06-03 12:13:31 -04:00
|
|
|
require.Error(err)
|
|
|
|
|
}
|