-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfisical_plugin_test.go
More file actions
113 lines (88 loc) · 3.47 KB
/
Copy pathinfisical_plugin_test.go
File metadata and controls
113 lines (88 loc) · 3.47 KB
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
112
113
package main
import (
"bytes"
"context"
"crypto"
"flag"
"testing"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/kms/cliplugin"
"github.com/sigstore/sigstore/pkg/signature/options"
)
// This file tests the PluginClient against a pre-built plugin programs.
// It is skipped during normal `go test ./...` invocations. It can be invoked like
// `go test -tags=signer_program -count=1 ./... -key-resource-id [my-kms]://[my-key-ref]`
// See ./README.md for plugin program usage.
// We don't have a TestCryptoSigner since PluginClient.CryptoSigner()'s returned object is meant to be a wrapper around PluginClient.
var (
inputKeyResourceID = flag.String("key-resource-id", "", "key resource ID for the KMS, defaults to 'infisical://key-name'")
testHashFunc = crypto.SHA512
)
// getPluginClient parses the build flags for the KeyResourceID and returns a PluginClient.
func getPluginClient(t *testing.T) *cliplugin.PluginClient {
// t.Helper()
signerVerifier, err := cliplugin.LoadSignerVerifier(context.Background(), *inputKeyResourceID, testHashFunc)
if err != nil {
t.Fatal(err)
}
pluginClient := signerVerifier.(*cliplugin.PluginClient)
return pluginClient
}
// TestDefaultAlgorithm invokes DefaultAlgorithm against the compiled plugin program.
// Since implementations can vary, it merely checks that some non-empty value is returned.
func TestDefaultAlgorithm(t *testing.T) {
t.Parallel()
pluginClient := getPluginClient(t)
if defaultAlgorithm := pluginClient.DefaultAlgorithm(); defaultAlgorithm == "" {
t.Error("expected non-empty default algorithm")
}
}
// TestSupportedAlgorithms invokes DefaultAlgorithm against the compiled plugin program.
// Since implementations can vary, it merely checks that some non-empty value is returned.
func TestSupportedAlgorithms(t *testing.T) {
t.Parallel()
pluginClient := getPluginClient(t)
if supportedAlgorithms := pluginClient.SupportedAlgorithms(); len(supportedAlgorithms) == 0 {
t.Error("expected non-empty supported algorithms")
}
}
// and that the same signaure can be verified.
func TestSignMessageVerifySignature(t *testing.T) {
t.Parallel()
pluginClient := getPluginClient(t)
testMessageBytes := []byte("any message")
hasher := testHashFunc.New()
if _, err := hasher.Write(testMessageBytes); err != nil {
t.Fatal(err)
}
testDigest := hasher.Sum(nil) // Now this is the hash of "any message"
signOpts := []signature.SignOption{}
verifyOpts := []signature.VerifyOption{}
signOpts = append(signOpts, options.WithDigest(testDigest))
verifyOpts = append(verifyOpts, options.WithDigest(testDigest))
signature, err := pluginClient.SignMessage(bytes.NewReader(testMessageBytes), signOpts...)
if err != nil {
t.Errorf("unexpected error signing message: %s", err)
}
if err == nil && len(signature) == 0 {
t.Error("expected non-empty signature")
}
// verify the real signature
if err = pluginClient.VerifySignature(bytes.NewReader(signature), bytes.NewReader(testMessageBytes), verifyOpts...); err != nil {
t.Errorf("unexpected error verifying signature: %s", err)
}
}
func TestCreateKey(t *testing.T) {
t.Run("create key", func(t *testing.T) {
pluginClient := getPluginClient(t)
_, err := pluginClient.CreateKey(context.Background(), "RSA_4096")
if err != nil {
t.Errorf("unexpected error creating key: %s", err)
}
pk, err := pluginClient.PublicKey()
if err != nil {
t.Errorf("unexpected error getting public key: %s", err)
}
t.Logf("public key: %s", pk)
})
}