Skip to content
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
21 changes: 20 additions & 1 deletion pkg/rhsm/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ type ConsumerSecrets struct {
ConsumerCert string
}

// defaultCACert picks the CA certificate to use when a repo does not set
// sslcacert explicitly. On Satellite-registered hosts content is served through
// the Satellite content proxy whose TLS certificate is signed by the Katello CA;
// the default redhat-uep.pem only holds the Red Hat CDN CA and cannot validate
// it. When katello-server-ca.pem is present (installed by Satellite
// registration) it contains the CA chain needed for the content proxy, so prefer
// it; otherwise fall back to redhat-uep.pem. A repo's explicit sslcacert still
// takes precedence over this default (see GetSecretsForBaseurl).
// katelloCACert is a variable so tests can point it at a temp file; production
// code never reassigns it.
var katelloCACert = "/etc/rhsm/ca/katello-server-ca.pem"

func defaultCACert() string {
if _, err := os.Stat(katelloCACert); err == nil {
return katelloCACert
}
return "/etc/rhsm/ca/redhat-uep.pem"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make both consts and swap between them? I dunno, stylistic only.

}

func getRHSMSecrets() (*RHSMSecrets, error) {
// search /etc first to allow container users to override the entitlements
globs := []string{
Expand All @@ -58,7 +77,7 @@ func getRHSMSecrets() (*RHSMSecrets, error) {
cert := strings.TrimSuffix(key, "-key.pem") + ".pem"
if _, err := os.Stat(cert); err == nil {
return &RHSMSecrets{
SSLCACert: "/etc/rhsm/ca/redhat-uep.pem",
SSLCACert: defaultCACert(),
SSLClientKey: key,
SSLClientCert: cert,
}, nil
Expand Down
19 changes: 19 additions & 0 deletions pkg/rhsm/secrets_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rhsm

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -132,6 +134,23 @@ func TestGetSecretsForBaseurlFallback(t *testing.T) {
assert.Equal(t, "/etc/pki/entitlement/fallback-key.pem", secrets.SSLClientKey)
}

// defaultCACert prefers the Katello CA when it exists (Satellite-registered
// host) and otherwise falls back to redhat-uep.pem.
func TestDefaultCACert(t *testing.T) {
orig := katelloCACert
t.Cleanup(func() { katelloCACert = orig })

// Katello CA absent: fall back to the Red Hat CDN CA.
katelloCACert = filepath.Join(t.TempDir(), "does-not-exist.pem")
assert.Equal(t, "/etc/rhsm/ca/redhat-uep.pem", defaultCACert())

// Katello CA present: prefer it.
present := filepath.Join(t.TempDir(), "katello-server-ca.pem")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter but why an additional present var here, can't it just assign directly to katelloCACert?

require.NoError(t, os.WriteFile(present, []byte("ca"), 0600))
katelloCACert = present
assert.Equal(t, present, defaultCACert())
}

// mirrors the cases in osbuild's test_util_rhsm.py::TestUrlMatching.
func TestBaseurlToRegex(t *testing.T) {
for _, tc := range []struct {
Expand Down
Loading