diff --git a/README.md b/README.md index 001abe1..735fc73 100644 --- a/README.md +++ b/README.md @@ -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] @@ -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 ``` \ No newline at end of file diff --git a/bin/dcm b/bin/dcm new file mode 100755 index 0000000..79db3b2 Binary files /dev/null and b/bin/dcm differ diff --git a/commands/certificate/check/expire.go b/commands/certificate/check/expire.go index 9ac95dd..6b03399 100644 --- a/commands/certificate/check/expire.go +++ b/commands/certificate/check/expire.go @@ -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" ) @@ -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 @@ -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 diff --git a/data/peer/output/sub/peer-cert.crt b/data/peer/output/sub/peer-cert.crt new file mode 100644 index 0000000..da05db8 --- /dev/null +++ b/data/peer/output/sub/peer-cert.crt @@ -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----- diff --git a/glossary/constant.go b/glossary/constant.go index 46f1ba0..caf1037 100644 --- a/glossary/constant.go +++ b/glossary/constant.go @@ -5,3 +5,6 @@ const DefaultOutputPath = ".dcm/output" //Program name const ProgramName = "dcm" + +// Regex cert name +const RegexCertName = "^.*\\.(pem|crt|PEM|CRT)$"