@@ -10,7 +10,9 @@ import (
1010 "io"
1111 "reflect"
1212 "testing"
13+ "time"
1314
15+ "github.com/stretchr/testify/assert"
1416 "golang.org/x/crypto/ssh"
1517)
1618
@@ -71,6 +73,7 @@ func mustGeneratePublicKey(t *testing.T) ssh.PublicKey {
7173}
7274
7375func TestNewCertificate (t * testing.T ) {
76+ now := time .Now ().Truncate (time .Second )
7477 key := mustGeneratePublicKey (t )
7578 cr := CertificateRequest {
7679 Key : key ,
@@ -100,8 +103,8 @@ func TestNewCertificate(t *testing.T) {
100103 Type : UserCert ,
101104102105 Principals : []string {"jane" },
103- ValidAfter : 0 ,
104- ValidBefore : 0 ,
106+ ValidAfter : time. Time {} ,
107+ ValidBefore : time. Time {} ,
105108 CriticalOptions : nil ,
106109 Extensions : map [string ]string {
107110 "permit-X11-forwarding" : "" ,
@@ -121,8 +124,8 @@ func TestNewCertificate(t *testing.T) {
121124 Type : HostCert ,
122125 KeyID : "foobar" ,
123126 Principals : []string {"foo.internal" , "bar.internal" },
124- ValidAfter : 0 ,
125- ValidBefore : 0 ,
127+ ValidAfter : time. Time {} ,
128+ ValidBefore : time. Time {} ,
126129 CriticalOptions : nil ,
127130 Extensions : nil ,
128131 Reserved : nil ,
@@ -136,8 +139,8 @@ func TestNewCertificate(t *testing.T) {
136139 Type : HostCert ,
137140 KeyID : `foobar", "criticalOptions": {"foo": "bar"},"foo":"` ,
138141 Principals : []string {"foo.internal" , "bar.internal" },
139- ValidAfter : 0 ,
140- ValidBefore : 0 ,
142+ ValidAfter : time. Time {} ,
143+ ValidBefore : time. Time {} ,
141144 CriticalOptions : nil ,
142145 Extensions : nil ,
143146 Reserved : nil ,
@@ -159,8 +162,8 @@ func TestNewCertificate(t *testing.T) {
159162 Type : UserCert ,
160163161164 Principals : []
string {
"john" ,
"[email protected] " },
162- ValidAfter : 0 ,
163- ValidBefore : 0 ,
165+ ValidAfter : time. Time {} ,
166+ ValidBefore : time. Time {} ,
164167 CriticalOptions : nil ,
165168 Extensions : map [string ]string {
166169@@ -174,15 +177,47 @@ func TestNewCertificate(t *testing.T) {
174177 SignatureKey : nil ,
175178 Signature : nil ,
176179 }, 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 },
177212 {"base64" , args {cr , []Option {WithTemplateBase64 (base64 .StdEncoding .EncodeToString ([]byte (DefaultTemplate )), CreateTemplateData (HostCert , "foo.internal" , nil ))}}, & Certificate {
178213 Nonce : nil ,
179214 Key : key ,
180215 Serial : 0 ,
181216 Type : HostCert ,
182217 KeyID : "foo.internal" ,
183218 Principals : nil ,
184- ValidAfter : 0 ,
185- ValidBefore : 0 ,
219+ ValidAfter : time. Time {} ,
220+ ValidBefore : time. Time {} ,
186221 CriticalOptions : nil ,
187222 Extensions : nil ,
188223 Reserved : nil ,
@@ -203,6 +238,15 @@ func TestNewCertificate(t *testing.T) {
203238 t .Errorf ("NewCertificate() error = %v, wantErr %v" , err , tt .wantErr )
204239 return
205240 }
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+ }
206250 if ! reflect .DeepEqual (got , tt .want ) {
207251 t .Errorf ("NewCertificate() = %v, want %v" , got , tt .want )
208252 }
@@ -212,6 +256,7 @@ func TestNewCertificate(t *testing.T) {
212256
213257func TestCertificate_GetCertificate (t * testing.T ) {
214258 key := mustGeneratePublicKey (t )
259+ now := time .Now ()
215260
216261 type fields struct {
217262 Nonce []byte
@@ -220,8 +265,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
220265 Type CertType
221266 KeyID string
222267 Principals []string
223- ValidAfter uint64
224- ValidBefore uint64
268+ ValidAfter time. Time
269+ ValidBefore time. Time
225270 CriticalOptions map [string ]string
226271 Extensions map [string ]string
227272 Reserved []byte
@@ -240,8 +285,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
240285 Type : UserCert ,
241286 KeyID : "key-id" ,
242287 Principals : []string {"john" },
243- ValidAfter : 1111 ,
244- ValidBefore : 2222 ,
288+ ValidAfter : now ,
289+ ValidBefore : now . Add ( time . Hour ) ,
245290 CriticalOptions : map [string ]string {"foo" : "bar" },
246291 Extensions :
map [
string ]
string {
"[email protected] " :
"john" },
247292 Reserved : []byte ("reserved" ),
@@ -254,8 +299,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
254299 CertType : ssh .UserCert ,
255300 KeyId : "key-id" ,
256301 ValidPrincipals : []string {"john" },
257- ValidAfter : 1111 ,
258- ValidBefore : 2222 ,
302+ ValidAfter : uint64 ( now . Unix ()) ,
303+ ValidBefore : uint64 ( now . Add ( time . Hour ). Unix ()) ,
259304 Permissions : ssh.Permissions {
260305 CriticalOptions : map [string ]string {"foo" : "bar" },
261306 Extensions :
map [
string ]
string {
"[email protected] " :
"john" },
@@ -269,8 +314,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
269314 Type : HostCert ,
270315 KeyID : "key-id" ,
271316 Principals : []string {"foo.internal" , "bar.internal" },
272- ValidAfter : 1111 ,
273- ValidBefore : 2222 ,
317+ ValidAfter : time. Time {} ,
318+ ValidBefore : time. Time {} ,
274319 CriticalOptions : map [string ]string {"foo" : "bar" },
275320 Extensions : nil ,
276321 Reserved : []byte ("reserved" ),
@@ -283,8 +328,8 @@ func TestCertificate_GetCertificate(t *testing.T) {
283328 CertType : ssh .HostCert ,
284329 KeyId : "key-id" ,
285330 ValidPrincipals : []string {"foo.internal" , "bar.internal" },
286- ValidAfter : 1111 ,
287- ValidBefore : 2222 ,
331+ ValidAfter : 0 ,
332+ ValidBefore : 0 ,
288333 Permissions : ssh.Permissions {
289334 CriticalOptions : map [string ]string {"foo" : "bar" },
290335 Extensions : nil ,
0 commit comments