Skip to content

Commit 86719cc

Browse files
authored
Resolve cipher suites directly from tlsProfile (#1989)
Cipher suites were being filtered out of the tls configuration when tls min version is 1.3 by the controller-runtime-commmon lib. We need the ciphers to be present because we need to set them on the mesh config for other components to use. This commit copies the cipher conversion directly from that lib. Signed-off-by: Nick Fox <nfox@redhat.com>
1 parent 1ea29bd commit 86719cc

2 files changed

Lines changed: 78 additions & 19 deletions

File tree

pkg/config/tls.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,46 @@ type OpenShiftTLS struct {
5151
TLSConfigFunc func(*tls.Config)
5252
}
5353

54+
// These functions are ripped directly from:
55+
// https://github.com/openshift/controller-runtime-common/blob/64ee174f5e2ebc630fbb554dd114d7a7a878693f/pkg/tls/tls.go#L134-L168
56+
// until https://github.com/openshift/controller-runtime-common/issues/19 is resolved.
57+
58+
// cipherCode returns the TLS cipher code for an OpenSSL or IANA cipher name.
59+
// Returns 0 if the cipher is not supported.
60+
func cipherCode(cipher string) uint16 {
61+
// First try as IANA name directly.
62+
if code, err := openshiftcrypto.CipherSuite(cipher); err == nil {
63+
return code
64+
}
65+
66+
// Try converting from OpenSSL name to IANA name.
67+
ianaCiphers := openshiftcrypto.OpenSSLToIANACipherSuites([]string{cipher})
68+
if len(ianaCiphers) == 1 {
69+
if code, err := openshiftcrypto.CipherSuite(ianaCiphers[0]); err == nil {
70+
return code
71+
}
72+
}
73+
74+
// Return 0 if the cipher is not supported.
75+
return 0
76+
}
77+
78+
// cipherCodes converts a list of cipher names (OpenSSL or IANA format) to their uint16 codes.
79+
// Returns the converted codes and a list of any unsupported cipher names.
80+
func cipherCodes(ciphers []string) (codes []uint16, unsupportedCiphers []string) {
81+
for _, cipher := range ciphers {
82+
code := cipherCode(cipher)
83+
if code == 0 {
84+
unsupportedCiphers = append(unsupportedCiphers, cipher)
85+
continue
86+
}
87+
88+
codes = append(codes, code)
89+
}
90+
91+
return codes, unsupportedCiphers
92+
}
93+
5494
// NewTLSConfigForOpenShift fetches TLS configuration from the OpenShift
5595
// APIServer and returns a TLSConfig with the OpenShift field populated.
5696
func NewTLSConfigForOpenShift(ctx context.Context, log logr.Logger, cl client.Client) (*TLSConfig, error) {
@@ -72,17 +112,22 @@ func NewTLSConfigForOpenShift(ctx context.Context, log logr.Logger, cl client.Cl
72112
}
73113

74114
if openshiftcrypto.ShouldHonorClusterTLSProfile(adherencePolicy) {
75-
tlsConfigFunc, unsupportedCiphers := openshifttls.NewTLSConfigFromProfile(profileSpec)
115+
tlsConfigFunc, _ := openshifttls.NewTLSConfigFromProfile(profileSpec)
76116
tlsConfig.OpenShift.TLSConfigFunc = tlsConfigFunc
77117

78-
if len(unsupportedCiphers) > 0 {
79-
log.Info("Some ciphers from TLS profile are unsupported and will be ignored", "unsupportedCiphers", unsupportedCiphers)
80-
}
81-
82118
goTLSConfig := &tls.Config{MinVersion: tls.VersionTLS12}
83119
tlsConfigFunc(goTLSConfig)
84-
tlsConfig.CipherSuites = goTLSConfig.CipherSuites
85120
tlsConfig.MinVersion = goTLSConfig.MinVersion
121+
// Resolve cipher suites directly from the profile spec
122+
// instead of reading them back from the tls.Config set by tlsConfigFunc,
123+
// because the controller-runtime-common lib filters out the CipherSuites when
124+
// MinTLSVersion is 1.3. We still need the cipher IDs to configure non-Go components like Envoy.
125+
cipherSuites, unsupportedCiphers := cipherCodes(profileSpec.Ciphers)
126+
tlsConfig.CipherSuites = cipherSuites
127+
128+
if len(unsupportedCiphers) > 0 {
129+
log.Info("Some ciphers from TLS profile are unsupported and will be ignored", "unsupportedCiphers", unsupportedCiphers)
130+
}
86131
}
87132

88133
return tlsConfig, nil

pkg/config/tls_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ package config
1616

1717
import (
1818
"crypto/tls"
19+
"fmt"
1920
"testing"
2021

2122
configv1 "github.com/openshift/api/config/v1"
23+
openshifttls "github.com/openshift/controller-runtime-common/pkg/tls"
2224
"github.com/stretchr/testify/assert"
2325
"github.com/stretchr/testify/require"
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -27,6 +29,11 @@ import (
2729
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2830
)
2931

32+
var (
33+
defaultTLSCiphers, _ = cipherCodes(openshifttls.DefaultTLSCiphers)
34+
modernTLSCiphers, _ = cipherCodes(configv1.TLSProfiles[configv1.TLSProfileModernType].Ciphers)
35+
)
36+
3037
func TestNewTLSConfigForOpenShift(t *testing.T) {
3138
log := zap.New(zap.UseDevMode(true))
3239
scheme := runtime.NewScheme()
@@ -44,6 +51,7 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
4451
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
4552
},
4653
expected: TLSConfig{
54+
MinVersion: tls.VersionTLS12,
4755
OpenShift: &OpenShiftTLS{
4856
TLSAdherencePolicy: configv1.TLSAdherencePolicyNoOpinion,
4957
},
@@ -61,6 +69,7 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
6169
},
6270
},
6371
expected: TLSConfig{
72+
MinVersion: tls.VersionTLS12,
6473
OpenShift: &OpenShiftTLS{
6574
TLSAdherencePolicy: configv1.TLSAdherencePolicyLegacyAdheringComponentsOnly,
6675
},
@@ -73,11 +82,13 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
7382
Spec: configv1.APIServerSpec{
7483
TLSAdherence: configv1.TLSAdherencePolicyStrictAllComponents,
7584
TLSSecurityProfile: &configv1.TLSSecurityProfile{
76-
Type: configv1.TLSProfileOldType,
85+
Type: configv1.TLSProfileModernType,
7786
},
7887
},
7988
},
8089
expected: TLSConfig{
90+
MinVersion: tls.VersionTLS13,
91+
CipherSuites: modernTLSCiphers,
8192
OpenShift: &OpenShiftTLS{
8293
TLSAdherencePolicy: configv1.TLSAdherencePolicyStrictAllComponents,
8394
},
@@ -92,6 +103,8 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
92103
},
93104
},
94105
expected: TLSConfig{
106+
MinVersion: tls.VersionTLS12,
107+
CipherSuites: defaultTLSCiphers,
95108
OpenShift: &OpenShiftTLS{
96109
TLSAdherencePolicy: configv1.TLSAdherencePolicyStrictAllComponents,
97110
},
@@ -105,6 +118,8 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
105118

106119
for _, tt := range tests {
107120
t.Run(tt.name, func(t *testing.T) {
121+
require := require.New(t)
122+
assert := assert.New(t)
108123
builder := fake.NewClientBuilder().WithScheme(scheme)
109124
if tt.apiServer != nil {
110125
builder = builder.WithObjects(tt.apiServer)
@@ -113,24 +128,23 @@ func TestNewTLSConfigForOpenShift(t *testing.T) {
113128

114129
tlsConfig, err := NewTLSConfigForOpenShift(t.Context(), log, cl)
115130
if tt.wantErr {
116-
require.Error(t, err)
131+
require.Error(err)
117132
return
118133
}
119-
require.NoError(t, err)
120-
require.NotNil(t, tlsConfig)
121-
require.NotNil(t, tlsConfig.OpenShift)
134+
require.NoError(err)
135+
require.NotNil(tlsConfig)
136+
require.NotNil(tlsConfig.OpenShift)
122137

123-
assert.Equal(t, tt.expected.OpenShift.TLSAdherencePolicy, tlsConfig.OpenShift.TLSAdherencePolicy)
138+
assert.Equal(tt.expected.OpenShift.TLSAdherencePolicy, tlsConfig.OpenShift.TLSAdherencePolicy)
124139

125140
if tt.expected.OpenShift.TLSAdherencePolicy == configv1.TLSAdherencePolicyStrictAllComponents {
126-
assert.NotEmpty(t, tlsConfig.CipherSuites)
127-
require.NotNil(t, tlsConfig.OpenShift.TLSConfigFunc)
128-
goTLS := &tls.Config{MinVersion: tls.VersionTLS12}
129-
tlsConfig.OpenShift.TLSConfigFunc(goTLS)
130-
assert.NotEmpty(t, goTLS.CipherSuites)
141+
assert.Equal(tt.expected.CipherSuites, tlsConfig.CipherSuites)
142+
assert.Equal(tt.expected.MinVersion, tlsConfig.MinVersion,
143+
fmt.Sprintf("TLS MinVersion mismatch: expected %s, got %s", tls.VersionName(tt.expected.MinVersion), tls.VersionName(tlsConfig.MinVersion)))
144+
require.NotNil(tlsConfig.OpenShift.TLSConfigFunc)
131145
} else {
132-
assert.Empty(t, tlsConfig.CipherSuites)
133-
assert.Nil(t, tlsConfig.OpenShift.TLSConfigFunc)
146+
assert.Empty(tlsConfig.CipherSuites)
147+
assert.Nil(tlsConfig.OpenShift.TLSConfigFunc)
134148
}
135149
})
136150
}

0 commit comments

Comments
 (0)