-
Notifications
You must be signed in to change notification settings - Fork 72
/
jks.go
209 lines (180 loc) · 5.87 KB
/
jks.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package kafka
import (
"encoding/json"
"encoding/pem"
"fmt"
"log"
"os"
"github.com/grafana/sobek"
"github.com/pavlo-v-chernykh/keystore-go/v4"
"go.k6.io/k6/js/common"
)
type JKSConfig struct {
Path string `json:"path"`
Password string `json:"password"`
ClientCertAlias string `json:"clientCertAlias"`
ClientKeyAlias string `json:"clientKeyAlias"`
ClientKeyPassword string `json:"clientKeyPassword"`
ServerCaAlias string `json:"serverCaAlias"`
}
type JKS struct {
ClientCertsPem []string `json:"clientCertsPem"`
ClientKeyPem string `json:"clientKeyPem"`
ServerCaPem string `json:"serverCaPem"`
}
func (*Kafka) loadJKS(jksConfig *JKSConfig) (*JKS, *Xk6KafkaError) {
if jksConfig == nil {
return nil, nil
}
if err := fileExists(jksConfig.Path); err != nil {
return nil, NewXk6KafkaError(
fileNotFound, fmt.Sprintf("File not found: %s", jksConfig.Path), err)
}
jksFile, err := os.Open(jksConfig.Path)
if err != nil {
return nil, NewXk6KafkaError(
failedReadJKSFile, fmt.Sprintf("Failed to read JKS file: %s", jksConfig.Path), err)
}
ks := keystore.New()
if err := ks.Load(jksFile, []byte(jksConfig.Password)); err != nil {
return nil, NewXk6KafkaError(
failedDecodeJKSFile, fmt.Sprintf("Failed to decode JKS file: %s", jksConfig.Path), err)
}
// Load server's CA certificate if an alias is provided.
// This makes it possible to use mTLS using two separate
// JKS files for loading the certificates:
// one for the client and one for the server.
var serverCa keystore.TrustedCertificateEntry
if jksConfig.ServerCaAlias != "" {
serverCa, err = ks.GetTrustedCertificateEntry(jksConfig.ServerCaAlias)
if err != nil {
return nil, NewXk6KafkaError(
failedDecodeServerCa,
fmt.Sprintf("Failed to decode server's CA: %s", jksConfig.Path), err)
}
} else {
serverCa = keystore.TrustedCertificateEntry{}
// Log a message if no server's CA alias is provided.
log.Println("No server's CA alias provided, skipping server's CA")
}
// Load client's certificate and key if aliases are provided.
clientKey, err := ks.GetPrivateKeyEntry(
jksConfig.ClientKeyAlias, []byte(jksConfig.ClientKeyPassword))
if err != nil {
// Server CA is loaded, so it will be saved, even though the client key is not loaded.
// This is because one might not have enabled mutual TLS (mTLS).
serverCaFilename := "server-ca.pem"
if serverCa.Certificate.Content != nil {
if err := saveServerCaFile(serverCaFilename, &serverCa); err != nil {
return nil, err
}
}
return &JKS{
ClientCertsPem: nil,
ClientKeyPem: "",
ServerCaPem: serverCaFilename,
}, NewXk6KafkaError(
failedDecodePrivateKey,
fmt.Sprintf("Failed to decode client's private key: %s", jksConfig.Path), err)
}
clientCertsFilenames := make([]string, 0, len(clientKey.CertificateChain))
for idx, cert := range clientKey.CertificateChain {
filename := fmt.Sprintf("client-cert-%d.pem", idx)
if err := saveClientCertFile(filename, &cert); err != nil {
return nil, err
}
clientCertsFilenames = append(clientCertsFilenames, filename)
}
clientKeyFilename := "client-key.pem"
if err := saveClientKeyFile(clientKeyFilename, &clientKey); err != nil {
return nil, err
}
serverCaFilename := "server-ca.pem"
if serverCa.Certificate.Content != nil {
if err := saveServerCaFile(serverCaFilename, &serverCa); err != nil {
return nil, err
}
}
return &JKS{
ClientCertsPem: clientCertsFilenames,
ClientKeyPem: clientKeyFilename,
ServerCaPem: serverCaFilename,
}, nil
}
func (k *Kafka) loadJKSFunction(call sobek.FunctionCall) sobek.Value {
runtime := k.vu.Runtime()
var jksConfig *JKSConfig
if len(call.Arguments) == 0 {
common.Throw(runtime, ErrNotEnoughArguments)
}
if params, ok := call.Argument(0).Export().(map[string]interface{}); ok {
if b, err := json.Marshal(params); err != nil {
common.Throw(runtime, err)
} else {
if err = json.Unmarshal(b, &jksConfig); err != nil {
common.Throw(runtime, err)
}
}
}
if jksConfig == nil {
common.Throw(runtime, ErrNoJKSConfig)
}
jks, err := k.loadJKS(jksConfig)
if jks == nil && err != nil {
common.Throw(runtime, err)
}
obj := runtime.NewObject()
// Zero-value fields are not returned to JS.
if jks.ServerCaPem != "" {
if err := obj.Set("serverCaPem", jks.ServerCaPem); err != nil {
common.Throw(runtime, err)
}
}
if jks.ClientCertsPem != nil {
if err := obj.Set("clientCertsPem", jks.ClientCertsPem); err != nil {
common.Throw(runtime, err)
}
}
if jks.ClientKeyPem != "" {
if err := obj.Set("clientKeyPem", jks.ClientKeyPem); err != nil {
common.Throw(runtime, err)
}
}
return runtime.ToValue(obj)
}
// saveServerCaFile saves the server's CA certificate to a file.
func saveServerCaFile(filename string, cert *keystore.TrustedCertificateEntry) *Xk6KafkaError {
certPem := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Certificate.Content,
})
if err := os.WriteFile(filename, certPem, 0o644); err != nil {
return NewXk6KafkaError(
failedWriteServerCaFile, "Failed to write CA file", err)
}
return nil
}
// saveClientKeyFile saves the client's key to a file.
func saveClientKeyFile(filename string, key *keystore.PrivateKeyEntry) *Xk6KafkaError {
keyPem := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: key.PrivateKey,
})
if err := os.WriteFile(filename, keyPem, 0o644); err != nil {
return NewXk6KafkaError(
failedWriteKeyFile, "Failed to write key file", err)
}
return nil
}
// saveClientCertFile saves the client's certificate to a file.
func saveClientCertFile(filename string, cert *keystore.Certificate) *Xk6KafkaError {
clientCertPem := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Content,
})
if err := os.WriteFile(filename, clientCertPem, 0o644); err != nil {
return NewXk6KafkaError(
failedWriteCertFile, "Failed to write cert file", err)
}
return nil
}