- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathjetstream_tpm_test.go
248 lines (210 loc) · 5.71 KB
/
jetstream_tpm_test.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows
package server
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/nats-io/nats.go"
)
// The tests in this file are not complete, but are a start to test the TPM
// with default settings. For a complete test we would need to use a TPM
// configured with an owner password and a SRK password. This is not
// typically the default or setup in CI/CD environments.
varjsTPMConfigPassword=`
listen: 127.0.0.1:-1
jetstream: {
store_dir: %q
tpm {
keys_file: %q
encryption_password: "s3cr3t!"
}
}`
varjsTPMConfigBadPassword=`
listen: 127.0.0.1:-1
jetstream: {
store_dir: %q
tpm {
keys_file: %q
encryption_password: "garbage"
}
}`
varjsTPMConfigPasswordPcr=`
listen: 127.0.0.1:-1
jetstream: {
store_dir: %q
tpm {
keys_file: %q
encryption_password: "s3cr3t!"
pcr: %d
}
}`
varjsTPMConfigAllFields=`
listen: 127.0.0.1:-1
jetstream: {
store_dir: %q
tpm {
keys_file: %q
encryption_password: "s3cr3t!"
pcr: %d
srk_password: %q
cipher: "aes"
}
}`
varjsTPMConfigInvalidBothOptionsSet=`
listen: 127.0.0.1:-1
jetstream: {
store_dir: %q
encryption_key: "foo"
tpm {
keys_file: "bar.json"
encryption_password: "s3cr3t!"
}
}`
funcgetTPMFileName(t*testing.T) string {
returnt.TempDir() +"/"+t.Name() +"/jskeys.json"
}
funccheckSendMessage(t*testing.T, s*Server) {
nc, js:=jsClientConnect(t, s)
defernc.Close()
_, err:=js.AddStream(&nats.StreamConfig{
Name: "tpm_test",
Subjects: []string{"tpm_test"},
})
require_NoError(t, err)
_, err=js.Publish("tpm_test", []byte("hello"))
require_NoError(t, err)
}
funccheckReceiveMessage(t*testing.T, s*Server) {
// reconnect and get the message
nc, js:=jsClientConnect(t, s)
defernc.Close()
// get the message
sub, err:=js.PullSubscribe("tpm_test", "cls")
iferr!=nil {
t.Fatalf("Unexpected error: %v", err)
}
m:=fetchMsgs(t, sub, 1, 5*time.Second)
ifm==nil {
t.Fatalf("Did not receive the message")
}
iflen(m) !=1 {
t.Fatalf("Expected 1 message, got %d", len(m))
}
}
funcTestJetStreamTPMBasic(t*testing.T) {
fileName:=getTPMFileName(t)
cf:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigPassword, t.TempDir(), fileName)))
s, _:=RunServerWithConfig(cf)
defers.Shutdown()
// Note, actual encryption is tested elsewhere. This just tests that the key is generated.
key:=s.opts.JetStreamKey
if!strings.HasPrefix(key, "SU") {
t.Fatalf("expected a TPM key to be generated. Key = %q", key)
}
if_, err:=os.Stat(fileName); err!=nil {
t.Fatalf("keys file was not created")
}
checkSendMessage(t, s)
// restart the server.
s.Shutdown()
s, _=RunServerWithConfig(cf)
ifs.Running() ==false {
t.Fatalf("Server should be running")
}
defers.Shutdown()
// double check we're encrypted with the same key.
ifs.opts.JetStreamKey!=key {
t.Fatalf("expected same key")
}
checkReceiveMessage(t, s)
}
// Warning: Running this test too frequently will considerably slow down
// these tests and eventually lock out the TPM.
//
// Depending on the environment this varies from 10 failures per day with
// a 24 hour lockout (Dell) to 32 failures per day with a 10
// minute healing time (decrementing one failure every 10 minutes).
funcTestJetStreamTPMKeyBadPassword(t*testing.T) {
fileName:=getTPMFileName(t)
cf:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigPassword,
t.TempDir(), fileName)))
s, _:=RunServerWithConfig(cf)
defers.Shutdown()
checkSendMessage(t, s)
s.Shutdown()
// restart the server.
deferfunc() {
r:=recover()
ifr==nil {
t.Fatalf("Expected server panic, unexpected start.")
}
}()
cf2:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigBadPassword,
t.TempDir(), fileName)))
s, _=RunServerWithConfig(cf2)
time.Sleep(3*time.Second)
ifs.Running() {
s.Shutdown()
t.Fatalf("Server should NOT be running")
}
}
funcTestJetStreamTPMKeyWithPCR(t*testing.T) {
cf:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigPasswordPcr,
t.TempDir(), getTPMFileName(t), 18)))
s, _:=RunServerWithConfig(cf)
defers.Shutdown()
checkSendMessage(t, s)
s.Shutdown()
// restart the server
s, _=RunServerWithConfig(cf)
if!s.Running() {
t.Fatalf("Server should be running")
}
}
funcTestJetStreamTPMAll(t*testing.T) {
fileName:=getTPMFileName(t)
// TODO: When the CI/CD environment supports updating the TPM,
// expand this test with the SRK password.
cf:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigAllFields,
getTPMFileName(t), fileName, 19, "")))
s, _:=RunServerWithConfig(cf)
defers.Shutdown()
checkSendMessage(t, s)
s.Shutdown()
// restart the server.
s, _=RunServerWithConfig(cf)
ifs.Running() ==false {
t.Fatalf("Server should be running")
}
defers.Shutdown()
checkReceiveMessage(t, s)
}
funcTestJetStreamInvalidConfig(t*testing.T) {
cf:=createConfFile(t, []byte(fmt.Sprintf(jsTPMConfigInvalidBothOptionsSet,
getTPMFileName(t))))
deferfunc() {
r:=recover()
ifr==nil {
t.Fatalf("Expected server panic, unexpected start.")
}
}()
s, _:=RunServerWithConfig(cf)
ifs.Running() ==true {
s.Shutdown()
t.Fatalf("Server should NOT be running")
}
}