-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_db_client.go
90 lines (79 loc) · 2.02 KB
/
file_db_client.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package db
import (
"bufio"
"context"
"fmt"
"os"
"strings"
)
// FileDBClient is an implementation of the CADBClient interface. It scans the
// certificate revocation status from a DB file.
// The DB file format is based on the index file of 'https://github.com/openssl/openssl'.
type FileDBClient struct {
caName string
dbFile string
}
// NewFileDBClient creates and returns a new instance of FileDBClient.
func NewFileDBClient(caName string, dbFile string) FileDBClient {
return FileDBClient{
caName: caName,
dbFile: dbFile,
}
}
// Indexes of tab delimited columns in DB file.
const (
// Revocation Type.
FileDBColRevTypeIdx int = 0
// Expiration Date.
FileDBColExpDateIdx int = 1
// Comma delimited Revocation Date and CRL Reason.
FileDBColRevDateAndCRLReasonIdx int = 2
// Serial Number.
FileDBColSerialIdx int = 3
)
// Indexes of comma delimited RevDate and CRLReason.
const (
// Revocation Date.
IdxRevDate int = 0
// CRL Reason.
IdxCRLReason int = 1
)
// Scan reads a file and parses each line into an IntermediateEntry.
func (h FileDBClient) Scan(ctx context.Context) (entries []IntermidiateEntry, err error) {
file, err := os.Open(h.dbFile)
if err != nil {
return nil, fmt.Errorf("could not read file DB %s: %w", h.dbFile, err)
}
defer func() {
closeErr := file.Close()
if err == nil {
err = closeErr
}
}()
scanner := bufio.NewScanner(file)
entries = make([]IntermidiateEntry, 0)
for scanner.Scan() {
var entry IntermidiateEntry
entry.Ca = h.caName
s := scanner.Text()
cols := strings.Split(s, "\t")
for idx := range cols {
switch idx {
case FileDBColRevTypeIdx:
entry.RevType = cols[idx]
case FileDBColExpDateIdx:
entry.ExpDate = cols[idx]
case FileDBColRevDateAndCRLReasonIdx:
if rc := strings.Split(cols[idx], ","); len(rc) == IdxCRLReason+1 {
entry.RevDate = rc[IdxRevDate]
entry.CRLReason = rc[IdxCRLReason]
}
case FileDBColSerialIdx:
entry.Serial = cols[idx]
default:
}
}
entries = append(entries, entry)
}
return entries, nil
}