Skip to content

Commit

Permalink
certificate list: detect status of certificate not issued yet
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Oct 31, 2024
1 parent d104448 commit 681fdbb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
41 changes: 29 additions & 12 deletions tsuru/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,27 @@ func (c *CertificateList) Run(context *cmd.Context) error {

for router, routerCerts := range appCerts.RouterCertificates {
for cname, cnameCert := range routerCerts.CNameCertificates {
cert, err := parseCert([]byte(cnameCert.Certificate))
if err != nil {
rows = append(rows, tablecli.Row{router, cname, err.Error(), "-"})
continue
var publicKeyInfo string
var certificateValidity string
var ready bool

if cnameCert.Certificate != "" {
cert, err := parseCert([]byte(cnameCert.Certificate))
if err != nil {
rows = append(rows, tablecli.Row{router, cname, err.Error(), "-"})
continue
}

ready = true
publicKeyInfo = formatPublicKeyInfo(*cert)
certificateValidity = formatCertificateValidity(*cert)
}

rows = append(rows, tablecli.Row{
router,
formatCName(cname, cnameCert.Issuer),
formatPublicKeyInfo(*cert),
formatCertificateValidity(*cert),
formatCName(cname, cnameCert.Issuer, ready),
publicKeyInfo,
certificateValidity,
})
}
}
Expand Down Expand Up @@ -274,15 +285,21 @@ func publicKeySize(publicKey interface{}) int {
return 0
}

func formatCName(cname string, issuer string) (cnameStr string) {
cnameStr += fmt.Sprintf("%s\n", cname)
func formatCName(cname string, issuer string, ready bool) string {
lines := []string{
cname,
}

if issuer != "" {
cnameStr += fmt.Sprintln(" managed by: cert-manager")
cnameStr += fmt.Sprintf(" issuer: %s\n", issuer)
lines = append(lines, " managed by: cert-manager", fmt.Sprintf(" issuer: %s", issuer))

if !ready {
lines = append(lines, " status: not ready")
}

}

return
return strings.Join(lines, "\n")
}

func formatPublicKeyInfo(cert x509.Certificate) (pkInfo string) {
Expand Down
38 changes: 21 additions & 17 deletions tsuru/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (s *S) TestCertificateListRunSuccessfully(c *check.C) {
},
"myapp.other.io": {
Certificate: "",
Issuer: "pki",
},
},
},
Expand All @@ -153,23 +154,26 @@ func (s *S) TestCertificateListRunSuccessfully(c *check.C) {
c.Assert(err, check.IsNil)
notBeforeStr := expectedNotBefore.UTC().Format(time.RFC3339)
notAfterStr := expectedNotAfter.UTC().Format(time.RFC3339)
expected := `+----------------+----------------------------+-----------------------+----------------------+
| Router | CName | Public Key Info | Certificate Validity |
+----------------+----------------------------+-----------------------+----------------------+
| a-new-router | myapp.io | Algorithm | Not before |
| | | RSA | ` + notBeforeStr + ` |
| | | | |
| | | Key size (in bits) | Not after |
| | | 2048 | ` + notAfterStr + ` |
+----------------+----------------------------+-----------------------+----------------------+
| ingress-router | myapp.io | Algorithm | Not before |
| | managed by: cert-manager | RSA | ` + notBeforeStr + ` |
| | issuer: lets-encrypt | | |
| | | Key size (in bits) | Not after |
| | | 2048 | ` + notAfterStr + ` |
+----------------+----------------------------+-----------------------+----------------------+
| ingress-router | myapp.other.io | failed to decode data | - |
+----------------+----------------------------+-----------------------+----------------------+
expected := `+----------------+----------------------------+--------------------+----------------------+
| Router | CName | Public Key Info | Certificate Validity |
+----------------+----------------------------+--------------------+----------------------+
| a-new-router | myapp.io | Algorithm | Not before |
| | | RSA | ` + notBeforeStr + ` |
| | | | |
| | | Key size (in bits) | Not after |
| | | 2048 | ` + notAfterStr + ` |
+----------------+----------------------------+--------------------+----------------------+
| ingress-router | myapp.io | Algorithm | Not before |
| | managed by: cert-manager | RSA | ` + notBeforeStr + ` |
| | issuer: lets-encrypt | | |
| | | Key size (in bits) | Not after |
| | | 2048 | ` + notAfterStr + ` |
+----------------+----------------------------+--------------------+----------------------+
| ingress-router | myapp.other.io | | |
| | managed by: cert-manager | | |
| | issuer: pki | | |
| | status: not ready | | |
+----------------+----------------------------+--------------------+----------------------+
`
trans := &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{
Expand Down

0 comments on commit 681fdbb

Please sign in to comment.