Skip to content

Commit

Permalink
add ability to read certificate from pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
nothinux committed Apr 15, 2022
1 parent 3eb2ca9 commit f515e5e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ $ certify server.local expiry:1d
Also, you can see information from certificate
$ certify -show server.local.pem
⚡️ Show certificate information with filename server.local.pem
$ certify -read server.local.pem
⚡️ Read certificate information from certificate with filename server.local.pem
$ certify -connect google.com:443
⚡️ Show certificate information from remote host
Expand Down
13 changes: 13 additions & 0 deletions cmd/certify/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ func isExist(path string) bool {
return !errors.Is(err, os.ErrNotExist)
}

func isPipe(stdin *os.File) error {
info, err := stdin.Stat()
if err != nil {
return err
}

if info.Mode()&os.ModeCharDevice != 0 || info.Size() != 0 {
return errors.New("can't read certificate, please provide certificate path or certificate content from stdin")
}

return nil
}

func tlsDial(host string) (*x509.Certificate, error) {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
Expand Down
32 changes: 21 additions & 11 deletions cmd/certify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -37,8 +38,8 @@ $ certify server.local expiry:1d
Also, you can see information from certificate
$ certify -show server.local.pem
⚡️ Show certificate information with filename server.local.pem
$ certify -read server.local.pem
⚡️ Read certificate information from certificate with filename server.local.pem
$ certify -connect google.com:443
⚡️ Show certificate information from remote host
Expand All @@ -57,7 +58,7 @@ var (
caKeyPath = "ca-key.pem"
Version = "No version provided"
initialize = flag.Bool("init", false, "initialize new CA Certificate and Key")
show = flag.Bool("show", false, "show information about certificate")
read = flag.Bool("read", false, "read information from certificate")
match = flag.Bool("match", false, "check if private key match with certificate")
ver = flag.Bool("version", false, "see program version")
connect = flag.Bool("connect", false, "show information about certificate on remote host")
Expand Down Expand Up @@ -97,18 +98,27 @@ func main() {
return
}

if *show {
if *read {
var certByte []byte
var err error

if len(os.Args) < 3 {
fmt.Printf("you must provide certificate path.\n")
os.Exit(1)
}
if err := isPipe(os.Stdin); err != nil {
log.Fatal(err)
}

f, err := os.ReadFile(os.Args[2])
if err != nil {
log.Fatal(err)
certByte, err = io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
} else {
certByte, err = os.ReadFile(os.Args[2])
if err != nil {
log.Fatal(err)
}
}

cert, err := certify.ParseCertificate(f)
cert, err := certify.ParseCertificate(certByte)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit f515e5e

Please sign in to comment.