Skip to content

Commit

Permalink
Review suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed May 2, 2023
1 parent ad71cad commit 27b8714
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 99 deletions.
5 changes: 0 additions & 5 deletions coordinator/clientapi/clientapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ func TestGetCertQuote(t *testing.T) {
var intermediateCert, rootCert *x509.Certificate
if !tc.wantErr {
intermediateCert = testutil.GetCertificate(t, tc.store, constants.SKCoordinatorIntermediateCert)
require.NoError(err)
rootCert = testutil.GetCertificate(t, tc.store, constants.SKCoordinatorRootCert)
require.NoError(err)
}

cert, quote, err := api.GetCertQuote(context.Background())
Expand Down Expand Up @@ -204,9 +202,7 @@ func TestGetManifestSignature(t *testing.T) {
var rawManifest, manifestSignature, manifestHash []byte
if !tc.wantErr {
rawManifest = testutil.GetRawManifest(t, tc.store)
require.NoError(err)
manifestSignature = testutil.GetManifestSignature(t, tc.store)
require.NoError(err)
h := sha256.Sum256(rawManifest)
manifestHash = h[:]
}
Expand Down Expand Up @@ -336,7 +332,6 @@ func TestGetSecrets(t *testing.T) {
}

storedSecrets := testutil.GetSecretMap(t, tc.store)
require.NoError(err)

secrets, err := api.GetSecrets(context.Background(), tc.request, tc.user)
if tc.wantErr {
Expand Down
1 change: 0 additions & 1 deletion coordinator/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,5 @@ func TestUnsetRestart(t *testing.T) {
c2State := testutil.GetState(t, c2.txHandle)
assert.Equal(state.AcceptingManifest, c2State)
c2Cert := testutil.GetCertificate(t, c2.txHandle, constants.SKCoordinatorRootCert)

assert.NotEqual(*cCert, *c2Cert)
}
7 changes: 0 additions & 7 deletions coordinator/core/marbleapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,8 @@ func (ms *marbleSpawner) newMarble(t *testing.T, marbleType string, infraName st
ms.assert.Equal(cert.IPAddresses, newLeafCert.IPAddresses)

rootCert := testutil.GetCertificate(t, ms.coreServer.txHandle, constants.SKCoordinatorRootCert)
ms.assert.NoError(err)
intermediateCert := testutil.GetCertificate(t, ms.coreServer.txHandle, constants.SKCoordinatorIntermediateCert)
ms.assert.NoError(err)
marbleRootCert := testutil.GetCertificate(t, ms.coreServer.txHandle, constants.SKMarbleRootCert)
ms.assert.NoError(err)
// Check Signature for both, intermediate certificate and leaf certificate
ms.assert.NoError(rootCert.CheckSignature(intermediateCert.SignatureAlgorithm, intermediateCert.RawTBSCertificate, intermediateCert.Signature))
ms.assert.NoError(newMarbleRootCert.CheckSignature(newMarbleRootCert.SignatureAlgorithm, newMarbleRootCert.RawTBSCertificate, newMarbleRootCert.Signature))
Expand Down Expand Up @@ -488,7 +485,6 @@ func TestSecurityLevelUpdate(t *testing.T) {
require.NoError(err)

admin := testutil.GetUser(t, coreServer.txHandle, "admin")
assert.NoError(err)

// try to activate another first backend, should succeed as SecurityLevel matches the definition in the manifest
spawner.newMarble(t, "frontend", "Azure", true)
Expand All @@ -504,9 +500,7 @@ func TestSecurityLevelUpdate(t *testing.T) {
coreServer2, err := NewCore([]string{"localhost"}, validator, issuer, stdstore.New(sealer), recovery, zapLogger, nil, nil)
require.NoError(err)
coreServer2State := testutil.GetState(t, coreServer2.txHandle)
assert.NoError(err)
coreServer2UpdatedPkg := testutil.GetPackage(t, coreServer2.txHandle, "frontend")
assert.NoError(err)
assert.Equal(state.AcceptingMarbles, coreServer2State)
assert.EqualValues(5, *coreServer2UpdatedPkg.SecurityVersion)

Expand Down Expand Up @@ -554,7 +548,6 @@ func (ms *marbleSpawner) shortMarbleActivation(t *testing.T, marbleType string,
params := resp.GetParameters()
// Get the marble from the manifest set on the coreServer since this one sets default values for empty values
coreServerManifest := testutil.GetManifest(t, ms.coreServer.txHandle)
ms.assert.NoError(err)
marble = coreServerManifest.Marbles[marbleType]
// Validate Files
for k, v := range marble.Parameters.Files {
Expand Down
1 change: 0 additions & 1 deletion coordinator/core/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func TestStoreWrapperMetrics(t *testing.T) {
_, err = clientAPI.Recover(ctx, key)
require.NoError(err)
state := testutil.GetState(t, c.txHandle)
require.NoError(err)
assert.Equal(1, promtest.CollectAndCount(c.metrics.coordinatorState))
assert.Equal(float64(state), promtest.ToFloat64(c.metrics.coordinatorState))
}
Expand Down
159 changes: 82 additions & 77 deletions coordinator/store/wrapper/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,112 +26,117 @@ type transactionHandle interface {
BeginTransaction(context.Context) (store.Transaction, error)
}

// GetState returns the current state of the store.
func GetState(t *testing.T, txHandle transactionHandle) state.State {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
state, err := tx.GetState()
require.NoError(t, err)
return state
// GetActivations returns the number of activations for a given Marble.
func GetActivations(t *testing.T, txHandle transactionHandle, name string) uint {
return get(t, txHandle, func(tx wrapper.Wrapper) (uint, error) {
return tx.GetActivations(name)
})
}

// GetCertificate returns the certificate with the given name.
func GetCertificate(t *testing.T, txHandle transactionHandle, name string) *x509.Certificate {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
cert, err := tx.GetCertificate(name)
require.NoError(t, err)
return cert
return get(t, txHandle, func(tx wrapper.Wrapper) (*x509.Certificate, error) {
return tx.GetCertificate(name)
})
}

// GetInfrastructure returns infrastructure information.
func GetInfrastructure(t *testing.T, txHandle transactionHandle, name string) quote.InfrastructureProperties {
return get(t, txHandle, func(tx wrapper.Wrapper) (quote.InfrastructureProperties, error) {
return tx.GetInfrastructure(name)
})
}

// GetMarble returns the marble with the given name.
func GetMarble(t *testing.T, txHandle transactionHandle, name string) manifest.Marble {
return get(t, txHandle, func(tx wrapper.Wrapper) (manifest.Marble, error) {
return tx.GetMarble(name)
})
}

// GetPackage returns the package with the given name.
func GetPackage(t *testing.T, txHandle transactionHandle, name string) quote.PackageProperties {
return get(t, txHandle, func(tx wrapper.Wrapper) (quote.PackageProperties, error) {
return tx.GetPackage(name)
})
}

// GetPrivateKey returns the private key with the given name.
func GetPrivateKey(t *testing.T, txHandle transactionHandle, name string) *ecdsa.PrivateKey {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
privKey, err := tx.GetPrivateKey(name)
require.NoError(t, err)
return privKey
return get(t, txHandle, func(tx wrapper.Wrapper) (*ecdsa.PrivateKey, error) {
return tx.GetPrivateKey(name)
})
}

// GetManifest returns the manifest.
func GetManifest(t *testing.T, txHandle transactionHandle) manifest.Manifest {
return get(t, txHandle, func(tx wrapper.Wrapper) (manifest.Manifest, error) {
return tx.GetManifest()
})
}

// GetRawManifest returns the raw manifest.
func GetRawManifest(t *testing.T, txHandle transactionHandle) []byte {
return get(t, txHandle, func(tx wrapper.Wrapper) ([]byte, error) {
return tx.GetRawManifest()
})
}

// GetManifestSignature returns the manifest signature.
func GetManifestSignature(t *testing.T, txHandle transactionHandle) []byte {
return get(t, txHandle, func(tx wrapper.Wrapper) ([]byte, error) {
return tx.GetManifestSignature()
})
}

// GetSecret returns the secret with the given name.
func GetSecret(t *testing.T, txHandle transactionHandle, name string) manifest.Secret {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
secret, err := tx.GetSecret(name)
require.NoError(t, err)
return secret
return get(t, txHandle, func(tx wrapper.Wrapper) (manifest.Secret, error) {
return tx.GetSecret(name)
})
}

// GetSecretMap returns a map of all secrets in the store.
func GetSecretMap(t *testing.T, txHandle transactionHandle) map[string]manifest.Secret {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
secretMap, err := tx.GetSecretMap()
require.NoError(t, err)
return secretMap
return get(t, txHandle, func(tx wrapper.Wrapper) (map[string]manifest.Secret, error) {
return tx.GetSecretMap()
})
}

// GetUser returns the user with the given name.
func GetUser(t *testing.T, txHandle transactionHandle, name string) *user.User {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
user, err := tx.GetUser(name)
require.NoError(t, err)
return user
// GetState returns the current state of the store.
func GetState(t *testing.T, txHandle transactionHandle) state.State {
return get(t, txHandle, func(tx wrapper.Wrapper) (state.State, error) {
return tx.GetState()
})
}

// GetPackage returns the package with the given name.
func GetPackage(t *testing.T, txHandle transactionHandle, name string) quote.PackageProperties {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
pkg, err := tx.GetPackage(name)
require.NoError(t, err)
return pkg
// GetTLS returns the TLS config with the given name.
func GetTLS(t *testing.T, txHandle transactionHandle, name string) manifest.TLStag {
return get(t, txHandle, func(tx wrapper.Wrapper) (manifest.TLStag, error) {
return tx.GetTLS(name)
})
}

// GetManifest returns the manifest.
func GetManifest(t *testing.T, txHandle transactionHandle) manifest.Manifest {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
manifest, err := tx.GetManifest()
require.NoError(t, err)
return manifest
// GetUpdateLog returns the update log.
func GetUpdateLog(t *testing.T, txHandle transactionHandle) string {
return get(t, txHandle, func(tx wrapper.Wrapper) (string, error) {
return tx.GetUpdateLog()
})
}

// GetRawManifest returns the raw manifest.
func GetRawManifest(t *testing.T, txHandle transactionHandle) []byte {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
manifest, err := tx.GetRawManifest()
require.NoError(t, err)
return manifest
// GetUser returns the user with the given name.
func GetUser(t *testing.T, txHandle transactionHandle, name string) *user.User {
return get(t, txHandle, func(tx wrapper.Wrapper) (*user.User, error) {
return tx.GetUser(name)
})
}

// GetManifestSignature returns the manifest signature.
func GetManifestSignature(t *testing.T, txHandle transactionHandle) []byte {
func get[T any](t *testing.T, txHandle transactionHandle, getter func(wrapper.Wrapper) (T, error)) T {
t.Helper()
tx, rollback, _, err := wrapper.WrapTransaction(context.Background(), txHandle)
require.NoError(t, err)
defer rollback()
sig, err := tx.GetManifestSignature()
val, err := getter(tx)
require.NoError(t, err)
return sig
return val
}
9 changes: 1 addition & 8 deletions coordinator/store/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ func WrapTransaction(ctx context.Context, txHandle transactionHandle,
if err != nil {
return Wrapper{}, nil, nil, err
}
wrapper = New(tx)
rollback = func() {
tx.Rollback()
}
commit = func(ctx context.Context) error {
return tx.Commit(ctx)
}
return wrapper, rollback, commit, nil
return New(tx), tx.Rollback, tx.Commit, nil
}

// Wrapper wraps store functions to provide a more convenient interface,
Expand Down

0 comments on commit 27b8714

Please sign in to comment.