Files
threshold/internal/server/service_rotate_roots_test.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.8 KiB
Go
Raw Normal View History

// 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
import (
"context"
"crypto/rand"
"testing"
"time"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/kms"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/types/scope"
"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))
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}
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()
// 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))
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()
// 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)
// And the old next root should now be current
2022-09-08 13:19:21 -04:00
assert.Equal(initialNext.PublicKeyPkix, rotatedCurrent.PublicKeyPkix)
}
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)
require.Error(err)
}