Skip to content

Commit

Permalink
Update check expire cmd support sub-folder and crt extension
Browse files Browse the repository at this point in the history
  • Loading branch information
phanhoc committed Feb 1, 2021
1 parent 3fa7134 commit b7155eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
2. Run make
3. Locate the binary in the bin directory
4. Add the binary to your PATH
5. Execute fabric for more information
5. Execute dcm for more information
## II. Commands Reference.
```shell
dcm [command]
Expand Down Expand Up @@ -73,6 +73,5 @@ dcm certificate check -c=../data/peer/output/renew-peer-cert.pem

#### b. Folder certificate.
```shell script
```shell script
dcm certificate check -f=../data/peer/output
```
Binary file added bin/dcm
Binary file not shown.
31 changes: 20 additions & 11 deletions commands/certificate/check/expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package check

import (
"akc-dcm-cli/commands/common"
"akc-dcm-cli/glossary"
"akc-dcm-cli/utilities"
"fmt"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
"path/filepath"
"sort"
"regexp"
"time"
)

Expand Down Expand Up @@ -59,17 +61,24 @@ func (c *ExpireCommand) Run() error {
if len(c.CertPath) > 0 {
return checkExpireCert(c.CertPath)
} else {
certificateFiles, err := filepath.Glob(filepath.Join(c.FolderPath, "*.pem"))
certRegex, err := regexp.Compile(glossary.RegexCertName)
if err != nil {
return errors.WithMessage(err, "Failed to scan certificate dir")
return errors.WithMessage(err, "Failed to compile certificate regex")
}
sort.Strings(certificateFiles)
for _, file := range certificateFiles {
err := checkExpireCert(file)
err = filepath.Walk(c.FolderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(fmt.Sprintf("Unable to check expire date of \"%s\" certificate", file))
continue
return errors.WithMessage(err, "Failed to scan certificate dir")
}
if certRegex.MatchString(info.Name()) {
err := checkExpireCert(path)
if err != nil {
fmt.Println(fmt.Sprintf("Unable to check expire date of \"%s\" certificate", path))
}
}
return nil
})
if err != nil {
return errors.WithMessage(err, "Failed to scan certificate dir")
}
}
return nil
Expand All @@ -82,11 +91,11 @@ func checkExpireCert(certPath string) error {
}

fileName := filepath.Base(certPath)
fmt.Println("Certificate", color.YellowString("%s", fileName), "will be expire at", color.YellowString("%s", cert.NotAfter.String()))
fmt.Println("Certificate", color.YellowString("%s - path (%s)", fileName, certPath), "will be expire at", color.YellowString("%s", cert.NotAfter.String()))
if cert.NotAfter.Before(time.Now()) {
fmt.Println("Certificate", color.RedString("%s was expired!!!", fileName))
fmt.Println("Certificate", color.RedString("%s - path (%s) was expired!!!", fileName, certPath))
} else {
fmt.Println("Certificate", color.GreenString("%s is good today.", fileName))
fmt.Println("Certificate", color.GreenString("%s - path (%s) is good today.", fileName, certPath))
}

return nil
Expand Down
13 changes: 13 additions & 0 deletions data/peer/output/sub/peer-cert.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
MIIB3jCCAYSgAwIBAgIUahCeZ8TR8gqLfkeZyiKsLR9LSeYwCgYIKoZIzj0EAwIw
ZTELMAkGA1UEBhMCVVMxFzAVBgNVBAgMDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK
DAtIeXBlcmxlZGdlcjEPMA0GA1UECwwGY2xpZW50MRYwFAYDVQQDDA1yY2EtYWtj
LWFkbWluMB4XDTIxMDExODEwMTk1MloXDTIxMDQyODEwMTk1MlowPDELMAkGA1UE
BhMCVk4xDjAMBgNVBAgMBUhhbm9pMQ0wCwYDVQQLDARwZWVyMQ4wDAYDVQQDDAVw
ZWVyMDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABI8DxhTYbIr0io1s/XACTUKw
qbk3jRk7RvSmS5pdeW+rw87k3ec+bFBoKT8Gb5A/IPcV5Ueeq+i7QfOIsnK1U5mj
OzA5MAsGA1UdDwQEAwIHgDAJBgNVHRMEAjAAMB8GA1UdIwQYMBaAFH+jWKiGltna
q5FlFGuRyBsWiL3QMAoGCCqGSM49BAMCA0gAMEUCIQDGZz2VWOeADXsCx/dQHQFs
eIayMRXNC2rW6jh+nKk7DwIgfpGbEzlrDP1pXQ/qVblGWb0tIH9II9TMisJq8PJS
GK0=
-----END CERTIFICATE-----
3 changes: 3 additions & 0 deletions glossary/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ const DefaultOutputPath = ".dcm/output"

//Program name
const ProgramName = "dcm"

// Regex cert name
const RegexCertName = "^.*\\.(pem|crt|PEM|CRT)$"

0 comments on commit b7155eb

Please sign in to comment.