Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[configtls] add include_system_ca_certs_pool #9142

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/UseSystemCACerts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configtls

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `include_system_ca_certs_pool` to configtls, allowing to load system certs and additional custom certs.

# One or more tracking issues or pull requests related to the change
issues: [7774]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
5 changes: 5 additions & 0 deletions config/configtls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ A certificate authority may also need to be defined:
system root CA. Should only be used if `insecure` is set to false.
- `ca_pem`: Alternative to `ca_file`. Provide the CA cert contents as a string instead of a filepath.

You can also combine defining a certificate authority with the system certificate authorities.

- `include_system_ca_certs_pool` (default = false): whether to load the system certificate authorities pool
alongside the certificate authority.

Additionally you can configure TLS to be enabled but skip verifying the server's
certificate chain. This cannot be combined with `insecure` since `insecure`
won't use TLS at all.
Expand Down
16 changes: 15 additions & 1 deletion config/configtls/configtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const defaultMinTLSVersion = tls.VersionTLS12
// Uses the default MaxVersion from "crypto/tls" which is the maximum supported version
const defaultMaxTLSVersion = 0

var systemCertPool = x509.SystemCertPool

// TLSSetting exposes the common client and server TLS configurations.
// Note: Since there isn't anything specific to a server connection. Components
// with server connections should use TLSSetting.
Expand All @@ -35,6 +37,10 @@ type TLSSetting struct {
// In memory PEM encoded cert. (optional)
CAPem configopaque.String `mapstructure:"ca_pem"`

// If true, load system CA certificates pool in addition to the certificates
// configured in this struct.
IncludeSystemCACertsPool bool `mapstructure:"include_system_ca_certs_pool"`

// Path to the TLS cert to use for TLS required connections. (optional)
CertFile string `mapstructure:"cert_file"`

Expand Down Expand Up @@ -304,7 +310,15 @@ func (c TLSSetting) loadCert(caPath string) (*x509.CertPool, error) {
return nil, fmt.Errorf("failed to load CA %s: %w", caPath, err)
}

certPool := x509.NewCertPool()
var certPool *x509.CertPool
if c.IncludeSystemCACertsPool {
if certPool, err = systemCertPool(); err != nil {
return nil, err
}
atoulme marked this conversation as resolved.
Show resolved Hide resolved
}
if certPool == nil {
certPool = x509.NewCertPool()
}
if !certPool.AppendCertsFromPEM(caPEM) {
return nil, fmt.Errorf("failed to parse CA %s", caPath)
}
Expand Down
67 changes: 67 additions & 0 deletions config/configtls/configtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package configtls
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -33,6 +34,10 @@ func TestOptionsToConfig(t *testing.T) {
name: "should load custom CA",
options: TLSSetting{CAFile: filepath.Join("testdata", "ca-1.crt")},
},
{
name: "should load system CA and custom CA",
options: TLSSetting{IncludeSystemCACertsPool: true, CAFile: filepath.Join("testdata", "ca-1.crt")},
},
{
name: "should fail with invalid CA file path",
options: TLSSetting{CAFile: filepath.Join("testdata", "not/valid")},
Expand Down Expand Up @@ -676,3 +681,65 @@ invalid TLS cipher suite: "BAR"`,
})
}
}

func TestSystemCertPool(t *testing.T) {
anError := errors.New("my error")
tests := []struct {
name string
tlsSetting TLSSetting
wantErr error
systemCertFn func() (*x509.CertPool, error)
}{
{
name: "not using system cert pool",
tlsSetting: TLSSetting{
IncludeSystemCACertsPool: false,
},
wantErr: nil,
systemCertFn: x509.SystemCertPool,
},
{
name: "using system cert pool",
tlsSetting: TLSSetting{
IncludeSystemCACertsPool: true,
},
wantErr: nil,
systemCertFn: x509.SystemCertPool,
},
{
name: "error loading system cert pool",
tlsSetting: TLSSetting{
IncludeSystemCACertsPool: true,
},
wantErr: anError,
systemCertFn: func() (*x509.CertPool, error) {
return nil, anError
},
},
{
name: "nil system cert pool",
tlsSetting: TLSSetting{
IncludeSystemCACertsPool: true,
},
wantErr: nil,
systemCertFn: func() (*x509.CertPool, error) {
return nil, nil
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
oldSystemCertPool := systemCertPool
systemCertPool = test.systemCertFn
defer func() {
systemCertPool = oldSystemCertPool
}()
certPool, err := test.tlsSetting.loadCert(filepath.Join("testdata", "ca-1.crt"))
if test.wantErr != nil {
assert.Equal(t, test.wantErr, err)
} else {
assert.NotNil(t, certPool)
}
})
}
}
Loading