Skip to content

Commit 834a908

Browse files
authored
feat: expose certificates pool creation (go-acme#2210)
1 parent c63be84 commit 834a908

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

challenge/http01/domain_matcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (m *hostMatcher) matches(r *http.Request, domain string) bool {
5757
return strings.HasPrefix(r.Host, domain)
5858
}
5959

60-
// hostMatcher checks whether the specified (*net/http.Request).Header value starts with a domain name.
60+
// arbitraryMatcher checks whether the specified (*net/http.Request).Header value starts with a domain name.
6161
type arbitraryMatcher string
6262

6363
func (m arbitraryMatcher) name() string {

lego/client_config.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,41 @@ func initCertPool() *x509.CertPool {
100100
return nil
101101
}
102102

103-
certPool := getCertPool()
103+
useSystemCertPool, _ := strconv.ParseBool(os.Getenv(caSystemCertPool))
104+
105+
caCerts := strings.Split(customCACertsPath, string(os.PathListSeparator))
106+
107+
certPool, err := CreateCertPool(caCerts, useSystemCertPool)
108+
if err != nil {
109+
panic(fmt.Sprintf("create certificates pool: %v", err))
110+
}
111+
112+
return certPool
113+
}
104114

105-
for _, customPath := range strings.Split(customCACertsPath, string(os.PathListSeparator)) {
115+
// CreateCertPool creates a *x509.CertPool populated with the PEM certificates.
116+
func CreateCertPool(caCerts []string, useSystemCertPool bool) (*x509.CertPool, error) {
117+
if len(caCerts) == 0 {
118+
return nil, nil
119+
}
120+
121+
certPool := newCertPool(useSystemCertPool)
122+
123+
for _, customPath := range caCerts {
106124
customCAs, err := os.ReadFile(customPath)
107125
if err != nil {
108-
panic(fmt.Sprintf("error reading %s=%q: %v",
109-
caCertificatesEnvVar, customPath, err))
126+
return nil, fmt.Errorf("error reading %q: %w", customPath, err)
110127
}
111128

112129
if ok := certPool.AppendCertsFromPEM(customCAs); !ok {
113-
panic(fmt.Sprintf("error creating x509 cert pool from %s=%q: %v",
114-
caCertificatesEnvVar, customPath, err))
130+
return nil, fmt.Errorf("error creating x509 cert pool from %q: %w", customPath, err)
115131
}
116132
}
117133

118-
return certPool
134+
return certPool, nil
119135
}
120136

121-
func getCertPool() *x509.CertPool {
122-
useSystemCertPool, _ := strconv.ParseBool(os.Getenv(caSystemCertPool))
137+
func newCertPool(useSystemCertPool bool) *x509.CertPool {
123138
if !useSystemCertPool {
124139
return x509.NewCertPool()
125140
}
@@ -128,5 +143,6 @@ func getCertPool() *x509.CertPool {
128143
if err == nil {
129144
return pool
130145
}
146+
131147
return x509.NewCertPool()
132148
}

providers/dns/acmedns/acmedns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (c mockUpdateClient) UpdateTXTRecord(acct goacmedns.Account, value string)
5959
return nil
6060
}
6161

62-
// errorRegisterClient is a mock implementing the acmeDNSClient interface that always
62+
// errorUpdateClient is a mock implementing the acmeDNSClient interface that always
6363
// returns errors from errorUpdateClient.
6464
type errorUpdateClient struct {
6565
mockClient

0 commit comments

Comments
 (0)