-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.go
75 lines (64 loc) · 1.53 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"bytes"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/sirupsen/logrus"
"os"
"strings"
)
type Parser struct {
bundlePath string
logger *logrus.Logger
}
func NewParser(certBundle string, logger *logrus.Logger) *Parser {
p := Parser{
bundlePath: certBundle,
logger: logger,
}
return &p
}
func (p *Parser) getCertificates() ([]x509.Certificate, error) {
f, err := os.Open(p.bundlePath)
defer f.Close()
if err != nil {
return nil, fmt.Errorf("unable to open certificate bundle file: %v", err)
}
s := bufio.NewScanner(f)
s.Split(splitByBeginCertificate)
var certs []x509.Certificate
for s.Scan() {
// Read PEM block
b, _ := pem.Decode(s.Bytes())
if b.Type != "CERTIFICATE" {
p.logger.Errorf("invalid type found: %s", b.Type)
continue
}
c, err := x509.ParseCertificate(b.Bytes)
if err != nil {
p.logger.Errorf("unable to parse certificate: %v", err)
continue
}
certs = append(certs, *c)
}
return certs, nil
}
func splitByBeginCertificate(data []byte, eof bool) (advance int, token []byte, err error) {
if eof && len(data) == 0 {
return 0, nil, nil
}
endCertificate := "-----END CERTIFICATE-----"
idx := bytes.Index(data, []byte(endCertificate))
if idx >= 0 {
trimmed := strings.TrimSpace(string(data[0 : idx+len(endCertificate)]))
return idx + len(endCertificate), []byte(trimmed), nil
}
// EOF but non terminated string (well, we can't terminate with a BEGIN CERTIFICATE)
if eof {
return len(data), nil, nil
}
// Get more data
return 0, nil, nil
}