@@ -10,7 +10,9 @@ import (
10
10
"io"
11
11
"reflect"
12
12
"testing"
13
+ "time"
13
14
15
+ "github.com/stretchr/testify/assert"
14
16
"golang.org/x/crypto/ssh"
15
17
)
16
18
@@ -71,6 +73,7 @@ func mustGeneratePublicKey(t *testing.T) ssh.PublicKey {
71
73
}
72
74
73
75
func TestNewCertificate (t * testing.T ) {
76
+ now := time .Now ().Truncate (time .Second )
74
77
key := mustGeneratePublicKey (t )
75
78
cr := CertificateRequest {
76
79
Key : key ,
@@ -100,8 +103,8 @@ func TestNewCertificate(t *testing.T) {
100
103
Type : UserCert ,
101
104
102
105
Principals : []string {"jane" },
103
- ValidAfter : 0 ,
104
- ValidBefore : 0 ,
106
+ ValidAfter : time. Time {} ,
107
+ ValidBefore : time. Time {} ,
105
108
CriticalOptions : nil ,
106
109
Extensions : map [string ]string {
107
110
"permit-X11-forwarding" : "" ,
@@ -121,8 +124,8 @@ func TestNewCertificate(t *testing.T) {
121
124
Type : HostCert ,
122
125
KeyID : "foobar" ,
123
126
Principals : []string {"foo.internal" , "bar.internal" },
124
- ValidAfter : 0 ,
125
- ValidBefore : 0 ,
127
+ ValidAfter : time. Time {} ,
128
+ ValidBefore : time. Time {} ,
126
129
CriticalOptions : nil ,
127
130
Extensions : nil ,
128
131
Reserved : nil ,
@@ -136,8 +139,8 @@ func TestNewCertificate(t *testing.T) {
136
139
Type : HostCert ,
137
140
KeyID : `foobar", "criticalOptions": {"foo": "bar"},"foo":"` ,
138
141
Principals : []string {"foo.internal" , "bar.internal" },
139
- ValidAfter : 0 ,
140
- ValidBefore : 0 ,
142
+ ValidAfter : time. Time {} ,
143
+ ValidBefore : time. Time {} ,
141
144
CriticalOptions : nil ,
142
145
Extensions : nil ,
143
146
Reserved : nil ,
@@ -159,8 +162,8 @@ func TestNewCertificate(t *testing.T) {
159
162
Type : UserCert ,
160
163
161
164
Principals : []
string {
"john" ,
"[email protected] " },
162
- ValidAfter : 0 ,
163
- ValidBefore : 0 ,
165
+ ValidAfter : time. Time {} ,
166
+ ValidBefore : time. Time {} ,
164
167
CriticalOptions : nil ,
165
168
Extensions : map [string ]string {
166
169
@@ -174,15 +177,47 @@ func TestNewCertificate(t *testing.T) {
174
177
SignatureKey : nil ,
175
178
Signature : nil ,
176
179
}, false },
180
+ {"file with dates" , args {cr , []Option {WithTemplateFile ("./testdata/date.tpl" , TemplateData {
181
+ TypeKey : UserCert ,
182
+
183
+ PrincipalsKey : []
string {
"john" ,
"[email protected] " },
184
+ ExtensionsKey : DefaultExtensions (UserCert ),
185
+ InsecureKey : TemplateData {
186
+ "User" : map [string ]interface {}{"username" : "john" },
187
+ },
188
+ WebhooksKey : TemplateData {
189
+ "Test" : map [string ]interface {}{"validity" : "16h" },
190
+ },
191
+ })}}, & Certificate {
192
+ Nonce : nil ,
193
+ Key : key ,
194
+ Serial : 0 ,
195
+ Type : UserCert ,
196
+
197
+ Principals : []
string {
"john" ,
"[email protected] " },
198
+ ValidAfter : now ,
199
+ ValidBefore : now .Add (16 * time .Hour ),
200
+ CriticalOptions : nil ,
201
+ Extensions : map [string ]string {
202
+ "permit-X11-forwarding" : "" ,
203
+ "permit-agent-forwarding" : "" ,
204
+ "permit-port-forwarding" : "" ,
205
+ "permit-pty" : "" ,
206
+ "permit-user-rc" : "" ,
207
+ },
208
+ Reserved : nil ,
209
+ SignatureKey : nil ,
210
+ Signature : nil ,
211
+ }, false },
177
212
{"base64" , args {cr , []Option {WithTemplateBase64 (base64 .StdEncoding .EncodeToString ([]byte (DefaultTemplate )), CreateTemplateData (HostCert , "foo.internal" , nil ))}}, & Certificate {
178
213
Nonce : nil ,
179
214
Key : key ,
180
215
Serial : 0 ,
181
216
Type : HostCert ,
182
217
KeyID : "foo.internal" ,
183
218
Principals : nil ,
184
- ValidAfter : 0 ,
185
- ValidBefore : 0 ,
219
+ ValidAfter : time. Time {} ,
220
+ ValidBefore : time. Time {} ,
186
221
CriticalOptions : nil ,
187
222
Extensions : nil ,
188
223
Reserved : nil ,
@@ -203,6 +238,15 @@ func TestNewCertificate(t *testing.T) {
203
238
t .Errorf ("NewCertificate() error = %v, wantErr %v" , err , tt .wantErr )
204
239
return
205
240
}
241
+ if got != nil && tt .want != nil {
242
+ if assert .WithinDuration (t , tt .want .ValidAfter , got .ValidAfter , 2 * time .Second ) {
243
+ tt .want .ValidAfter = got .ValidAfter
244
+ }
245
+ if assert .WithinDuration (t , tt .want .ValidBefore , got .ValidBefore , 2 * time .Second ) {
246
+ tt .want .ValidBefore = got .ValidBefore
247
+ }
248
+
249
+ }
206
250
if ! reflect .DeepEqual (got , tt .want ) {
207
251
t .Errorf ("NewCertificate() = %v, want %v" , got , tt .want )
208
252
}
@@ -212,6 +256,7 @@ func TestNewCertificate(t *testing.T) {
212
256
213
257
func TestCertificate_GetCertificate (t * testing.T ) {
214
258
key := mustGeneratePublicKey (t )
259
+ now := time .Now ()
215
260
216
261
type fields struct {
217
262
Nonce []byte
@@ -220,8 +265,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
220
265
Type CertType
221
266
KeyID string
222
267
Principals []string
223
- ValidAfter uint64
224
- ValidBefore uint64
268
+ ValidAfter time. Time
269
+ ValidBefore time. Time
225
270
CriticalOptions map [string ]string
226
271
Extensions map [string ]string
227
272
Reserved []byte
@@ -240,8 +285,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
240
285
Type : UserCert ,
241
286
KeyID : "key-id" ,
242
287
Principals : []string {"john" },
243
- ValidAfter : 1111 ,
244
- ValidBefore : 2222 ,
288
+ ValidAfter : now ,
289
+ ValidBefore : now . Add ( time . Hour ) ,
245
290
CriticalOptions : map [string ]string {"foo" : "bar" },
246
291
Extensions :
map [
string ]
string {
"[email protected] " :
"john" },
247
292
Reserved : []byte ("reserved" ),
@@ -254,8 +299,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
254
299
CertType : ssh .UserCert ,
255
300
KeyId : "key-id" ,
256
301
ValidPrincipals : []string {"john" },
257
- ValidAfter : 1111 ,
258
- ValidBefore : 2222 ,
302
+ ValidAfter : uint64 ( now . Unix ()) ,
303
+ ValidBefore : uint64 ( now . Add ( time . Hour ). Unix ()) ,
259
304
Permissions : ssh.Permissions {
260
305
CriticalOptions : map [string ]string {"foo" : "bar" },
261
306
Extensions :
map [
string ]
string {
"[email protected] " :
"john" },
@@ -269,8 +314,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
269
314
Type : HostCert ,
270
315
KeyID : "key-id" ,
271
316
Principals : []string {"foo.internal" , "bar.internal" },
272
- ValidAfter : 1111 ,
273
- ValidBefore : 2222 ,
317
+ ValidAfter : time. Time {} ,
318
+ ValidBefore : time. Time {} ,
274
319
CriticalOptions : map [string ]string {"foo" : "bar" },
275
320
Extensions : nil ,
276
321
Reserved : []byte ("reserved" ),
@@ -283,8 +328,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
283
328
CertType : ssh .HostCert ,
284
329
KeyId : "key-id" ,
285
330
ValidPrincipals : []string {"foo.internal" , "bar.internal" },
286
- ValidAfter : 1111 ,
287
- ValidBefore : 2222 ,
331
+ ValidAfter : 0 ,
332
+ ValidBefore : 0 ,
288
333
Permissions : ssh.Permissions {
289
334
CriticalOptions : map [string ]string {"foo" : "bar" },
290
335
Extensions : nil ,
0 commit comments