Skip to content

Commit 92de712

Browse files
committed
put options to validate certs in an object
1 parent 903d753 commit 92de712

File tree

3 files changed

+205
-147
lines changed

3 files changed

+205
-147
lines changed

cmd/gcp/main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,30 @@ func main() {
8181
if err != nil {
8282
klog.Exitf("Can't create secret manager signer: %v", err)
8383
}
84-
cpSigner, err := sctfe.NewCpSigner(signer, *origin, timeSource)
84+
85+
chainValidationConfig := sctfe.ChainValidationConfig{
86+
RootsPemFile: *rootsPemFile,
87+
RejectExpired: *rejectExpired,
88+
RejectUnexpired: *rejectUnexpired,
89+
ExtKeyUsages: *extKeyUsages,
90+
RejectExtensions: *rejectExtensions,
91+
NotAfterStart: notAfterStart.t,
92+
NotAfterLimit: notAfterLimit.t,
93+
}
94+
95+
vCfg, err := sctfe.ValidateLogConfig(chainValidationConfig, *origin, signer)
8596
if err != nil {
86-
klog.Exitf("Failed to create checkpoint signer: %v", err)
97+
klog.Exitf("Failed to initialize log config: %v", err)
8798
}
8899

89-
storage, err := newGCPStorage(ctx, cpSigner)
100+
cpSigner, err := sctfe.NewCpSigner(signer, vCfg.Origin, timeSource)
90101
if err != nil {
91-
klog.Exitf("Failed to initiate storage backend: %v", err)
102+
klog.Exitf("Failed to create checkpoint signer: %v", err)
92103
}
93104

94-
vCfg, err := sctfe.ValidateLogConfig(*origin, *rootsPemFile, *rejectExpired, *rejectUnexpired, *extKeyUsages, *rejectExtensions, notAfterStart.t, notAfterLimit.t, signer)
105+
storage, err := newGCPStorage(ctx, cpSigner)
95106
if err != nil {
96-
klog.Exitf("Invalid config: %v", err)
107+
klog.Exitf("Failed to initiate storage backend: %v", err)
97108
}
98109

99110
opts := sctfe.HandlerOptions{

config.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ import (
2929
"k8s.io/klog/v2"
3030
)
3131

32+
type ChainValidationConfig struct {
33+
// Path to the file containing root certificates that are acceptable to the
34+
// log. The certs are served through get-roots endpoint.
35+
RootsPemFile string
36+
// If RejectExpired is true then the certificate validity period will be
37+
// checked against the current time during the validation of submissions.
38+
// This will cause expired certificates to be rejected.
39+
RejectExpired bool
40+
// If RejectUnexpired is true then CTFE rejects certificates that are either
41+
// currently valid or not yet valid.
42+
RejectUnexpired bool
43+
// If set, ExtKeyUsages will restrict the set of such usages that the
44+
// server will accept. By default all are accepted. The values specified
45+
// must be ones known to the x509 package, comma separated.
46+
ExtKeyUsages string
47+
// A comma separated list of X.509 extension OIDs, in dotted string form
48+
// (e.g. "2.3.4.5") which, if present, should cause submissions to be
49+
// rejected.
50+
RejectExtensions string
51+
// NotAfterStart defines the start of the range of acceptable NotAfter
52+
// values, inclusive.
53+
// Leaving this unset implies no lower bound to the range.
54+
NotAfterStart *time.Time
55+
// NotAfterLimit defines the end of the range of acceptable NotAfter values,
56+
// exclusive.
57+
// Leaving this unset implies no upper bound to the range.
58+
NotAfterLimit *time.Time
59+
}
60+
3261
// ValidatedLogConfig represents the LogConfig with the information that has
3362
// been successfully parsed as a result of validating it.
3463
type ValidatedLogConfig struct {
@@ -49,17 +78,18 @@ type ValidatedLogConfig struct {
4978
// - Merge delays (if present) are correct.
5079
//
5180
// Returns the validated structures (useful to avoid double validation).
52-
func ValidateLogConfig(origin string, rootsPemFile string, rejectExpired bool, rejectUnexpired bool, extKeyUsages string, rejectExtensions string, notAfterStart *time.Time, notAfterLimit *time.Time, signer crypto.Signer) (*ValidatedLogConfig, error) {
81+
// TODO(phboneff): change the name of this function.
82+
func ValidateLogConfig(cfg ChainValidationConfig, origin string, signer crypto.Signer) (*ValidatedLogConfig, error) {
5383
if origin == "" {
5484
return nil, errors.New("empty origin")
5585
}
5686

5787
// Load the trusted roots.
58-
if rootsPemFile == "" {
88+
if cfg.RootsPemFile == "" {
5989
return nil, errors.New("empty rootsPemFile")
6090
}
6191
roots := x509util.NewPEMCertPool()
62-
if err := roots.AppendCertsFromPEMFile(rootsPemFile); err != nil {
92+
if err := roots.AppendCertsFromPEMFile(cfg.RootsPemFile); err != nil {
6393
return nil, fmt.Errorf("failed to read trusted roots: %v", err)
6494
}
6595

@@ -73,27 +103,27 @@ func ValidateLogConfig(origin string, rootsPemFile string, rejectExpired bool, r
73103
return nil, fmt.Errorf("unsupported key type: %v", keyType)
74104
}
75105

76-
if rejectExpired && rejectUnexpired {
106+
if cfg.RejectExpired && cfg.RejectUnexpired {
77107
return nil, errors.New("configuration would reject all certificates")
78108
}
79109

80110
// Validate the time interval.
81-
if notAfterStart != nil && notAfterLimit != nil && (notAfterLimit).Before(*notAfterStart) {
82-
return nil, fmt.Errorf("'Not After' limit %q before start %q", notAfterLimit.Format(time.RFC3339), notAfterStart.Format(time.RFC3339))
111+
if cfg.NotAfterStart != nil && cfg.NotAfterLimit != nil && (cfg.NotAfterLimit).Before(*cfg.NotAfterStart) {
112+
return nil, fmt.Errorf("'Not After' limit %q before start %q", cfg.NotAfterLimit.Format(time.RFC3339), cfg.NotAfterStart.Format(time.RFC3339))
83113
}
84114

85115
validationOpts := CertValidationOpts{
86116
trustedRoots: roots,
87-
rejectExpired: rejectExpired,
88-
rejectUnexpired: rejectUnexpired,
89-
notAfterStart: notAfterStart,
90-
notAfterLimit: notAfterLimit,
117+
rejectExpired: cfg.RejectExpired,
118+
rejectUnexpired: cfg.RejectUnexpired,
119+
notAfterStart: cfg.NotAfterStart,
120+
notAfterLimit: cfg.NotAfterLimit,
91121
}
92122

93123
// Filter which extended key usages are allowed.
94124
lExtKeyUsages := []string{}
95-
if extKeyUsages != "" {
96-
lExtKeyUsages = strings.Split(extKeyUsages, ",")
125+
if cfg.ExtKeyUsages != "" {
126+
lExtKeyUsages = strings.Split(cfg.ExtKeyUsages, ",")
97127
}
98128
// Validate the extended key usages list.
99129
for _, kuStr := range lExtKeyUsages {
@@ -112,8 +142,8 @@ func ValidateLogConfig(origin string, rootsPemFile string, rejectExpired bool, r
112142
}
113143
// Filter which extensions are rejected.
114144
var err error
115-
if rejectExtensions != "" {
116-
lRejectExtensions := strings.Split(rejectExtensions, ",")
145+
if cfg.RejectExtensions != "" {
146+
lRejectExtensions := strings.Split(cfg.RejectExtensions, ",")
117147
validationOpts.rejectExtIds, err = parseOIDs(lRejectExtensions)
118148
if err != nil {
119149
return nil, fmt.Errorf("failed to parse RejectExtensions: %v", err)

0 commit comments

Comments
 (0)