Skip to content

Commit 73f7ea5

Browse files
authored
Fix: SSL dashboard/api validity problem (apache#2947)
Signed-off-by: Fatih USTA <[email protected]>
1 parent ad697c6 commit 73f7ea5

File tree

2 files changed

+62
-46
lines changed

2 files changed

+62
-46
lines changed

api/internal/handler/ssl/ssl.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
198198
for _, item := range ret.Rows {
199199
ssl := &entity.SSL{}
200200
_ = utils.ObjectClone(item, ssl)
201+
x509_validity, _ := x509CertValidity(ssl.Cert)
202+
if x509_validity != nil {
203+
ssl.ValidityStart = x509_validity.NotBefore
204+
ssl.ValidityEnd = x509_validity.NotAfter
205+
}
201206
ssl.Key = ""
202207
ssl.Keys = nil
203208
list = append(list, ssl)
@@ -327,6 +332,35 @@ func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
327332
return nil, nil
328333
}
329334

335+
// validity allows unmarshaling the certificate validity date range
336+
type validity struct {
337+
NotBefore, NotAfter int64
338+
}
339+
340+
func x509CertValidity(crt string) (*validity, error) {
341+
if crt == "" {
342+
return nil, consts.ErrSSLCertificate
343+
}
344+
345+
certDERBlock, _ := pem.Decode([]byte(crt))
346+
if certDERBlock == nil {
347+
return nil, consts.ErrSSLCertificateResolution
348+
}
349+
350+
x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)
351+
352+
if err != nil {
353+
return nil, consts.ErrSSLCertificateResolution
354+
}
355+
356+
val := validity{}
357+
358+
val.NotBefore = x509Cert.NotBefore.Unix()
359+
val.NotAfter = x509Cert.NotAfter.Unix()
360+
361+
return &val, nil
362+
}
363+
330364
func ParseCert(crt, key string) (*entity.SSL, error) {
331365
if crt == "" || key == "" {
332366
return nil, consts.ErrSSLCertificate
@@ -383,8 +417,6 @@ func ParseCert(crt, key string) (*entity.SSL, error) {
383417

384418
ssl.Snis = snis
385419
ssl.Key = key
386-
ssl.ValidityStart = x509Cert.NotBefore.Unix()
387-
ssl.ValidityEnd = x509Cert.NotAfter.Unix()
388420
ssl.Cert = crt
389421

390422
return &ssl, nil
@@ -424,6 +456,12 @@ func (h *Handler) Validate(c droplet.Context) (interface{}, error) {
424456
return nil, err
425457
}
426458

459+
x509_validity, _ := x509CertValidity(input.Cert)
460+
if x509_validity != nil {
461+
ssl.ValidityStart = x509_validity.NotBefore
462+
ssl.ValidityEnd = x509_validity.NotAfter
463+
}
464+
427465
return ssl, nil
428466
}
429467

api/internal/handler/ssl/ssl_test.go

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,8 @@ func TestSSL_Create(t *testing.T) {
288288
"env": "production",
289289
"version": "v2",
290290
},
291-
Snis: []string{"test2.com", "*.test2.com"},
292-
ValidityStart: 1586038672,
293-
ValidityEnd: 4739638672,
294-
Status: 1,
291+
Snis: []string{"test2.com", "*.test2.com"},
292+
Status: 1,
295293
},
296294
wantInput: &entity.SSL{
297295
BaseInfo: entity.BaseInfo{
@@ -304,10 +302,8 @@ func TestSSL_Create(t *testing.T) {
304302
"env": "production",
305303
"version": "v2",
306304
},
307-
Snis: []string{"test2.com", "*.test2.com"},
308-
ValidityStart: 1586038672,
309-
ValidityEnd: 4739638672,
310-
Status: 1,
305+
Snis: []string{"test2.com", "*.test2.com"},
306+
Status: 1,
311307
},
312308
wantRet: &entity.SSL{
313309
BaseInfo: entity.BaseInfo{
@@ -320,10 +316,8 @@ func TestSSL_Create(t *testing.T) {
320316
"env": "production",
321317
"version": "v2",
322318
},
323-
Snis: []string{"test2.com", "*.test2.com"},
324-
ValidityStart: 1586038672,
325-
ValidityEnd: 4739638672,
326-
Status: 1,
319+
Snis: []string{"test2.com", "*.test2.com"},
320+
Status: 1,
327321
},
328322
wantErr: nil,
329323
},
@@ -348,10 +342,8 @@ func TestSSL_Create(t *testing.T) {
348342
"env": "production",
349343
"version": "v2",
350344
},
351-
Snis: []string{"test2.com", "*.test2.com"},
352-
ValidityStart: 1586038672,
353-
ValidityEnd: 4739638672,
354-
Status: 1,
345+
Snis: []string{"test2.com", "*.test2.com"},
346+
Status: 1,
355347
},
356348
wantErr: fmt.Errorf("create failed"),
357349
wantRet: handler.SpecCodeResponse(fmt.Errorf("create failed")),
@@ -419,10 +411,8 @@ func TestSSL_Update(t *testing.T) {
419411
"env": "production",
420412
"version": "v2",
421413
},
422-
Snis: []string{"test2.com", "*.test2.com"},
423-
ValidityStart: 1586038672,
424-
ValidityEnd: 4739638672,
425-
Status: 1,
414+
Snis: []string{"test2.com", "*.test2.com"},
415+
Status: 1,
426416
},
427417
wantInput: &entity.SSL{
428418
BaseInfo: entity.BaseInfo{
@@ -435,10 +425,8 @@ func TestSSL_Update(t *testing.T) {
435425
"env": "production",
436426
"version": "v2",
437427
},
438-
Snis: []string{"test2.com", "*.test2.com"},
439-
ValidityStart: 1586038672,
440-
ValidityEnd: 4739638672,
441-
Status: 1,
428+
Snis: []string{"test2.com", "*.test2.com"},
429+
Status: 1,
442430
},
443431
wantRet: &entity.SSL{
444432
BaseInfo: entity.BaseInfo{
@@ -451,10 +439,8 @@ func TestSSL_Update(t *testing.T) {
451439
"env": "production",
452440
"version": "v2",
453441
},
454-
Snis: []string{"test2.com", "*.test2.com"},
455-
ValidityStart: 1586038672,
456-
ValidityEnd: 4739638672,
457-
Status: 1,
442+
Snis: []string{"test2.com", "*.test2.com"},
443+
Status: 1,
458444
},
459445
},
460446
{
@@ -561,10 +547,8 @@ func TestSSL_Patch(t *testing.T) {
561547
"env": "production",
562548
"version": "v2",
563549
},
564-
Snis: []string{"test2.com", "*.test2.com"},
565-
ValidityStart: 1586038672,
566-
ValidityEnd: 4739638672,
567-
Status: 1,
550+
Snis: []string{"test2.com", "*.test2.com"},
551+
Status: 1,
568552
},
569553
giveInput: &PatchInput{
570554
ID: "ssl1",
@@ -597,10 +581,8 @@ func TestSSL_Patch(t *testing.T) {
597581
"env": "production",
598582
"version": "v2",
599583
},
600-
Snis: []string{"test2.com", "*.test2.com"},
601-
ValidityStart: 1586038672,
602-
ValidityEnd: 4739638672,
603-
Status: 1,
584+
Snis: []string{"test2.com", "*.test2.com"},
585+
Status: 1,
604586
},
605587
getCalled: true,
606588
},
@@ -622,10 +604,8 @@ func TestSSL_Patch(t *testing.T) {
622604
"env": "production",
623605
"version": "v2",
624606
},
625-
Snis: []string{"test2.com", "*.test2.com"},
626-
ValidityStart: 1586038672,
627-
ValidityEnd: 4739638672,
628-
Status: 1,
607+
Snis: []string{"test2.com", "*.test2.com"},
608+
Status: 1,
629609
},
630610
wantInput: &entity.SSL{
631611
BaseInfo: entity.BaseInfo{
@@ -653,10 +633,8 @@ func TestSSL_Patch(t *testing.T) {
653633
"env": "production",
654634
"version": "v2",
655635
},
656-
Snis: []string{"test2.com", "*.test2.com"},
657-
ValidityStart: 1586038672,
658-
ValidityEnd: 4739638672,
659-
Status: 1,
636+
Snis: []string{"test2.com", "*.test2.com"},
637+
Status: 1,
660638
},
661639
getCalled: true,
662640
},

0 commit comments

Comments
 (0)