Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKI migration writes out empty migration log entry #15091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions builtin/logical/pki/storage_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
// and we need to perform it again...
const latestMigrationVersion = 1

type legacyBundleMigration struct {
Hash string `json:"hash" structs:"hash" mapstructure:"hash"`
Created time.Time `json:"created" structs:"created" mapstructure:"created"`
MigrationVersion int `json:"migrationVersion" structs:"migrationVersion" mapstructure:"migrationVersion"`
}

func migrateStorage(ctx context.Context, req *logical.InitializationRequest, logger log.Logger) error {
s := req.Storage
legacyBundle, err := getLegacyCertBundle(ctx, s)
Expand All @@ -40,27 +46,27 @@ func migrateStorage(ctx context.Context, req *logical.InitializationRequest, log

if migrationEntry != nil {
// At this point we have already migrated something previously.
if migrationEntry.hash == hash &&
migrationEntry.migrationVersion == latestMigrationVersion {
if migrationEntry.Hash == hash &&
migrationEntry.MigrationVersion == latestMigrationVersion {
// The hashes are the same, no need to try and re-import...
logger.Debug("existing migration hash found and matched legacy bundle, skipping migration.")
return nil
}
}

logger.Warn("performing PKI migration to new keys/issuers layout")
logger.Info("performing PKI migration to new keys/issuers layout")

anIssuer, aKey, err := writeCaBundle(ctx, s, legacyBundle, "current", "current")
if err != nil {
return err
}
logger.Info("Migration generated the following ids and set them as defaults",
logger.Debug("Migration generated the following ids and set them as defaults",
"issuer id", anIssuer.ID, "key id", aKey.ID)

err = setLegacyBundleMigrationLog(ctx, s, &legacyBundleMigration{
hash: hash,
created: time.Now(),
migrationVersion: latestMigrationVersion,
Hash: hash,
Created: time.Now(),
MigrationVersion: latestMigrationVersion,
})
if err != nil {
return err
Expand All @@ -84,12 +90,6 @@ func computeHashOfLegacyBundle(bundle *certutil.CertBundle) (string, error) {
return hex.EncodeToString(hasher.Sum(nil)), nil
}

type legacyBundleMigration struct {
hash string
created time.Time
migrationVersion int
}

func getLegacyBundleMigrationLog(ctx context.Context, s logical.Storage) (*legacyBundleMigration, error) {
entry, err := s.Get(ctx, legacyMigrationBundleLogKey)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions builtin/logical/pki/storage_migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package pki

import (
"context"
"strings"
"testing"
"time"

"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -30,6 +32,7 @@ func Test_migrateStorageEmptyStorage(t *testing.T) {
}

func Test_migrateStorageSimpleBundle(t *testing.T) {
startTime := time.Now()
ctx := context.Background()
b, s := createBackendWithStorage(t)

Expand All @@ -55,6 +58,11 @@ func Test_migrateStorageSimpleBundle(t *testing.T) {
logEntry, err := getLegacyBundleMigrationLog(ctx, s)
require.NoError(t, err)
require.NotNil(t, logEntry)
require.Equal(t, latestMigrationVersion, logEntry.MigrationVersion)
require.True(t, len(strings.TrimSpace(logEntry.Hash)) > 0,
"Hash value (%s) should not have been empty", logEntry.Hash)
require.True(t, startTime.Before(logEntry.Created),
"created log entry time (%v) was before our start time(%v)?", logEntry.Created, startTime)

issuerId := issuerIds[0]
keyId := keyIds[0]
Expand Down Expand Up @@ -89,4 +97,14 @@ func Test_migrateStorageSimpleBundle(t *testing.T) {
issuersConfig, err := getIssuersConfig(ctx, s)
require.NoError(t, err)
require.Equal(t, &issuerConfig{DefaultIssuerId: issuerId}, issuersConfig)

// Make sure if we attempt to re-run the migration nothing happens...
err = migrateStorage(ctx, request, b.Logger())
require.NoError(t, err)
logEntry2, err := getLegacyBundleMigrationLog(ctx, s)
require.NoError(t, err)
require.NotNil(t, logEntry2)

require.Equal(t, logEntry.Created, logEntry2.Created)
require.Equal(t, logEntry.Hash, logEntry2.Hash)
}