/
loadTestSignVerify.go
111 lines (91 loc) · 3.07 KB
/
loadTestSignVerify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (c) Fortanix, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package cmd
import (
"context"
"net/http"
"time"
"github.com/fortanix/sdkms-client-go/sdkms"
"github.com/spf13/cobra"
)
const SIGN_EXAMPLE_DATA string = "0123456789abcdef"
var signKeyID string
var verifyOpt bool
var signVerifyLoadTestCmd = &cobra.Command{
Use: "sign-verify",
Aliases: []string{"sign", "verify"},
Short: "Perform sign/verify load test.",
Long: "Perform sign/verify load test.",
Run: func(cmd *cobra.Command, args []string) {
signVerifyLoadTest()
},
}
func init() {
loadTestCmd.AddCommand(signVerifyLoadTestCmd)
signVerifyLoadTestCmd.PersistentFlags().StringVar(&signKeyID, "kid", "", "Key ID to use for sign and verify")
signVerifyLoadTestCmd.PersistentFlags().BoolVar(&verifyOpt, "verify", false, "Perform verification instead of sign")
}
func signVerifyLoadTest() {
setup := func(client *sdkms.Client) (interface{}, error) {
if createSession {
_, err := client.AuthenticateWithAPIKey(context.Background(), apiKey)
return nil, err
}
client.Auth = sdkms.APIKey(apiKey)
return nil, nil
}
cleanup := func(client *sdkms.Client) {
if createSession {
client.TerminateSession(context.Background())
}
}
test := func(client *sdkms.Client, stage loadTestStage, arg interface{}) (interface{}, time.Duration, profilingDataStr, error) {
if signResp, ok := arg.(*sdkms.SignResponse); verifyOpt && ok {
_, d, p, err := verify(client, *signResp)
// return the sign response so we can verify in the next iteration
return signResp, d, p, err
}
return sign(client)
}
name := "sign"
if verifyOpt {
name = "verify"
}
if createSession {
name += " with session"
}
loadTest(name, setup, test, cleanup)
}
func sign(client *sdkms.Client) (*sdkms.SignResponse, time.Duration, profilingDataStr, error) {
req := sdkms.SignRequest{
Data: someBlob([]byte(SIGN_EXAMPLE_DATA)),
HashAlg: sdkms.DigestAlgorithmSha256,
Key: sdkms.SobjectByID(signKeyID),
}
ctx := context.WithValue(context.Background(), "ResponseHeader", http.Header{})
t0 := time.Now()
res, err := client.Sign(ctx, req)
d := time.Since(t0)
header := ctx.Value("ResponseHeader").(http.Header)
p := profilingDataStr(header.Get("Profiling-Data"))
return res, d, p, err
}
func verify(client *sdkms.Client, sr sdkms.SignResponse) (*sdkms.VerifyResponse, time.Duration, profilingDataStr, error) {
req := sdkms.VerifyRequest{
Signature: sr.Signature,
Key: sdkms.SobjectByID(signKeyID),
HashAlg: sdkms.DigestAlgorithmSha256,
Data: someBlob([]byte(SIGN_EXAMPLE_DATA)),
}
ctx := context.WithValue(context.Background(), "ResponseHeader", http.Header{})
t0 := time.Now()
res, err := client.Verify(ctx, req)
d := time.Since(t0)
header := ctx.Value("ResponseHeader").(http.Header)
p := profilingDataStr(header.Get("Profiling-Data"))
return res, d, p, err
}
func someBlob(blob sdkms.Blob) *sdkms.Blob { return &blob }