From 9fc60a22b13587a4569b618adfbcee892943939e Mon Sep 17 00:00:00 2001 From: Yuxiang Cao Date: Fri, 26 Apr 2024 15:11:52 -0700 Subject: [PATCH] feat: showing more test info - Add code to get info about plugin or sobject so that test name could contain all necessary information. --- cmd/common.go | 18 ++++++++++++++++++ cmd/loadTestAsymmetricCrypto.go | 9 +++++++++ cmd/loadTestGenerateKey.go | 2 +- cmd/loadTestInvokePlugin.go | 21 ++++++++++++++++----- cmd/loadTestSignVerify.go | 8 ++++++++ cmd/loadTestSymmetricCrypto.go | 11 ++++++++++- 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index aa95d68..12d4475 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -7,8 +7,10 @@ package cmd import ( + "context" "crypto/tls" "fmt" + "log" "math" "net" "net/http" @@ -166,3 +168,19 @@ func Max(a, b int) int { } return b } + +// GetSobject retrieves a sobject through SDKMS client. +// It takes a keyID as a parameter and returns a pointer to a Sobject. +// If the keyID is empty, it will return an error. +// If an error occurs during the retrieval process, the function will log a fatal error and exit. +func GetSobject(kid *string) *sdkms.Sobject { + client := sdkmsClient() + client.Auth = sdkms.APIKey(apiKey) + key, err := client.GetSobject(context.Background(), nil, sdkms.SobjectDescriptor{ + Kid: kid, + }) + if err != nil { + log.Fatalf("Fatal error: %v\n", err) + } + return key +} diff --git a/cmd/loadTestAsymmetricCrypto.go b/cmd/loadTestAsymmetricCrypto.go index db1cb8b..cc09678 100644 --- a/cmd/loadTestAsymmetricCrypto.go +++ b/cmd/loadTestAsymmetricCrypto.go @@ -8,6 +8,7 @@ package cmd import ( "context" + "fmt" "net/http" "time" @@ -35,6 +36,9 @@ func init() { } func asymmetricCryptoLoadTest() { + // get basic info of the given sobject + key := GetSobject(&keyID) + setup := func(client *sdkms.Client) (interface{}, error) { if createSession { _, err := client.AuthenticateWithAPIKey(context.Background(), apiKey) @@ -56,6 +60,8 @@ func asymmetricCryptoLoadTest() { } return asymmetricEncrypt(client) } + + // construct test name name := "asymmetric encryption" if decryptOpt { name = "asymmetric decryption" @@ -63,6 +69,9 @@ func asymmetricCryptoLoadTest() { if createSession { name += " with session" } + name = fmt.Sprintf("%s %d %s", key.ObjType, *key.KeySize, name) + + // start the load test loadTest(name, setup, test, cleanup) } diff --git a/cmd/loadTestGenerateKey.go b/cmd/loadTestGenerateKey.go index 96065d8..5e653f9 100644 --- a/cmd/loadTestGenerateKey.go +++ b/cmd/loadTestGenerateKey.go @@ -61,7 +61,7 @@ func loadTestGenerateKey() { d, p, err := generateKey(client) return nil, d, p, err } - name := fmt.Sprintf("generate %v keys", keyType) + name := fmt.Sprintf("generate %v key", keyType) if keyType == objectTypeAES || keyType == objectTypeRSA { name += fmt.Sprintf(" (%v bits)", keySize) } diff --git a/cmd/loadTestInvokePlugin.go b/cmd/loadTestInvokePlugin.go index 593a9c4..3904a69 100644 --- a/cmd/loadTestInvokePlugin.go +++ b/cmd/loadTestInvokePlugin.go @@ -10,8 +10,8 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" - "os" "time" "github.com/fortanix/sdkms-client-go/sdkms" @@ -39,11 +39,18 @@ func init() { } func invokePluginLoadTest() { + // Get the given plugin from the server + client := sdkmsClient() + client.Auth = sdkms.APIKey(apiKey) + plugin, err := client.GetPlugin(context.Background(), pluginID) + if err != nil { + log.Fatalf("Fatal error: %v\n", err) + } + input := json.RawMessage(pluginInput) - _, err := json.Marshal(&input) + _, err = json.Marshal(&input) if err != nil { - fmt.Printf("plugin input must be valid JSON: %v\n", err) - os.Exit(1) + log.Fatalf("Plugin input must be valid JSON: %v\n", err) } setup := func(client *sdkms.Client) (interface{}, error) { @@ -62,10 +69,14 @@ func invokePluginLoadTest() { test := func(client *sdkms.Client, stage loadTestStage, arg interface{}) (interface{}, time.Duration, profilingDataStr, error) { return invokePlugin(client) } - name := "plugin invocation" + + // construct test name + name := fmt.Sprintf("invoke plugin '%s'", plugin.Name) if createSession { name += " with session" } + + // start the load test loadTest(name, setup, test, cleanup) } diff --git a/cmd/loadTestSignVerify.go b/cmd/loadTestSignVerify.go index 927ca1a..91759ec 100644 --- a/cmd/loadTestSignVerify.go +++ b/cmd/loadTestSignVerify.go @@ -8,6 +8,7 @@ package cmd import ( "context" + "fmt" "net/http" "time" @@ -38,6 +39,9 @@ func init() { } func signVerifyLoadTest() { + // get basic info of the given sobject + key := GetSobject(&signKeyID) + setup := func(client *sdkms.Client) (interface{}, error) { if createSession { _, err := client.AuthenticateWithAPIKey(context.Background(), apiKey) @@ -59,6 +63,8 @@ func signVerifyLoadTest() { } return sign(client) } + + // construct test name name := "sign" if verifyOpt { name = "verify" @@ -66,6 +72,8 @@ func signVerifyLoadTest() { if createSession { name += " with session" } + name = fmt.Sprintf("%s %d %s", key.ObjType, *key.KeySize, name) + loadTest(name, setup, test, cleanup) } diff --git a/cmd/loadTestSymmetricCrypto.go b/cmd/loadTestSymmetricCrypto.go index 6029e2b..412f617 100644 --- a/cmd/loadTestSymmetricCrypto.go +++ b/cmd/loadTestSymmetricCrypto.go @@ -8,6 +8,7 @@ package cmd import ( "context" + "fmt" "log" "net/http" "time" @@ -40,10 +41,13 @@ func init() { symmetricCryptoLoadTestCmd.PersistentFlags().StringVar(&keyID, "kid", "", "Key ID to use for symmetric crypto") symmetricCryptoLoadTestCmd.PersistentFlags().BoolVar(&decryptOpt, "decrypt", false, "Perform decryption instead of encryption") symmetricCryptoLoadTestCmd.PersistentFlags().StringVar(&cipherModeStr, "mode", "CBC", "Cipher mode used for encryption/decryption, support: CBC, GCM, FPE") + cipherMode = validateCipherMode(cipherModeStr) } func symmetricCryptoLoadTest() { - cipherMode = validateCipherMode(cipherModeStr) + // get basic info of the given sobject + key := GetSobject(&keyID) + setup := func(client *sdkms.Client) (interface{}, error) { if createSession { _, err := client.AuthenticateWithAPIKey(context.Background(), apiKey) @@ -65,6 +69,8 @@ func symmetricCryptoLoadTest() { } return encrypt(client) } + + // construct test name name := "symmetric encryption" if decryptOpt { name = "symmetric decryption" @@ -73,6 +79,9 @@ func symmetricCryptoLoadTest() { if createSession { name += " with session" } + name = fmt.Sprintf("%s %d %s", key.ObjType, *key.KeySize, name) + + // start the load test loadTest(name, setup, test, cleanup) }