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

chore: set VersionTLS12 as min default TLS version #4192

Merged
merged 9 commits into from
Feb 9, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Here is an overview of all new **experimental** features:

- **General**: Add a warning when KEDA run outside supported k8s versions ([#4130](https://github.com/kedacore/keda/issues/4130))
- **General**: Use (self-signed) certificates for all the communications (internals and externals) ([#3931](https://github.com/kedacore/keda/issues/3931))
- **General**: Use TLS1.2 as minimum TLS version ([#4193](https://github.com/kedacore/keda/issues/4193))
- **Azure Pipelines Scaler**: Improve error logging for `validatePoolID` ([#3996](https://github.com/kedacore/keda/issues/3996))
- **Hashicorp Vault**: Add support to secrets backend version 1 ([#2645](https://github.com/kedacore/keda/issues/2645))
- **RabbitMQ Scaler**: Add TLS support ([#967](https://github.com/kedacore/keda/issues/967))
Expand Down
5 changes: 4 additions & 1 deletion pkg/scalers/authentication/authentication_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func NewTLSConfig(auth *AuthMeta) (*tls.Config, error) {
}

func CreateHTTPRoundTripper(roundTripperType TransportType, auth *AuthMeta, conf ...*HTTPTransport) (rt http.RoundTripper, err error) {
tlsConfig := &tls.Config{InsecureSkipVerify: false}
tlsConfig := &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: false,
}
if auth != nil && (auth.CA != "" || auth.EnableTLS) {
tlsConfig, err = NewTLSConfig(auth)
if err != nil || tlsConfig == nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/scalers/elasticsearch_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ func newElasticsearchClient(meta *elasticsearchMetadata, logger logr.Logger) (*e
}

transport := http.DefaultTransport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: meta.unsafeSsl}
transport.TLSClientConfig = &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: meta.unsafeSsl,
}
config.Transport = transport

esClient, err := elasticsearch.NewClient(config)
Expand Down
5 changes: 4 additions & 1 deletion pkg/scalers/ibmmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ func (s *IBMMQScaler) getQueueDepthViaHTTP(ctx context.Context) (int64, error) {
req.SetBasicAuth(s.metadata.username, s.metadata.password)

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: s.metadata.tlsDisabled},
TLSClientConfig: &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: s.metadata.tlsDisabled,
},
}
client := kedautil.CreateHTTPClient(s.defaultHTTPTimeout, false)
client.Transport = tr
Expand Down
5 changes: 4 additions & 1 deletion pkg/scalers/influxdb_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func NewInfluxDBScaler(config *ScalerConfig) (Scaler, error) {
client := influxdb2.NewClientWithOptions(
meta.serverURL,
meta.authToken,
influxdb2.DefaultOptions().SetTLSConfig(&tls.Config{InsecureSkipVerify: meta.unsafeSsl}))
influxdb2.DefaultOptions().SetTLSConfig(&tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: meta.unsafeSsl,
}))

return &influxDBScaler{
client: client,
Expand Down
1 change: 1 addition & 0 deletions pkg/scalers/predictkube_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (s *PredictKubeScaler) setupClientConn() error {
if !grpcConf.Conn.Insecure {
clientOpt = append(clientOpt, grpc.WithTransportCredentials(
credentials.NewTLS(&tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
ServerName: mlEngineHost,
}),
))
Expand Down
3 changes: 3 additions & 0 deletions pkg/scalers/redis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ func getRedisClusterClient(ctx context.Context, info redisConnectionInfo) (*redi
}
if info.enableTLS {
options.TLSConfig = &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: info.unsafeSsl,
}
}
Expand All @@ -489,6 +490,7 @@ func getRedisSentinelClient(ctx context.Context, info redisConnectionInfo, dbInd
}
if info.enableTLS {
options.TLSConfig = &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: info.unsafeSsl,
}
}
Expand All @@ -510,6 +512,7 @@ func getRedisClient(ctx context.Context, info redisConnectionInfo, dbIndex int)
}
if info.enableTLS {
options.TLSConfig = &tls.Config{
MinVersion: kedautil.GetMinTLSVersion(),
InsecureSkipVerify: info.unsafeSsl,
}
}
Expand Down
41 changes: 39 additions & 2 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,48 @@ package util

import (
"crypto/tls"
"fmt"
"net/http"
"os"
"time"

"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
)

var disableKeepAlives bool
var minTLSVersion uint16

func init() {
setupLog := ctrl.Log.WithName("http_setup")
var err error
disableKeepAlives, err = ResolveOsEnvBool("KEDA_HTTP_DISABLE_KEEP_ALIVE", false)
if err != nil {
disableKeepAlives = false
}

minTLSVersion = initMinTLSVersion(setupLog)
}

func initMinTLSVersion(logger logr.Logger) uint16 {
version, found := os.LookupEnv("KEDA_HTTP_MIN_TLS_VERSION")
minVersion := tls.VersionTLS12
if found {
switch version {
case "TLS13":
minVersion = tls.VersionTLS13
case "TLS12":
minVersion = tls.VersionTLS12
case "TLS11":
minVersion = tls.VersionTLS11
case "TLS10":
minVersion = tls.VersionTLS10
default:
logger.Info(fmt.Sprintf("%s is not a valid value, using `TLS12`. Allowed values are: `TLS13`,`TLS12`,`TLS11`,`TLS10`", version))
minVersion = tls.VersionTLS12
}
}
return uint16(minVersion)
}

// HTTPDoer is an interface that matches the Do method on
Expand All @@ -48,8 +78,11 @@ func CreateHTTPClient(timeout time.Duration, unsafeSsl bool) *http.Client {
timeout = 300 * time.Millisecond
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: unsafeSsl},
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: unsafeSsl,
MinVersion: GetMinTLSVersion(),
},
Proxy: http.ProxyFromEnvironment,
}
if disableKeepAlives {
// disable keep http connection alive
Expand All @@ -62,3 +95,7 @@ func CreateHTTPClient(timeout time.Duration, unsafeSsl bool) *http.Client {
}
return httpClient
}

func GetMinTLSVersion() uint16 {
return minTLSVersion
}
73 changes: 73 additions & 0 deletions pkg/util/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2023 The KEDA Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"crypto/tls"
"os"
"testing"

"github.com/go-logr/logr"
)

type minTLSVersionTestData struct {
envSet bool
envValue string
expectedVersion uint16
}

var minTLSVersionTestDatas = []minTLSVersionTestData{
{
envSet: true,
envValue: "TLS10",
expectedVersion: tls.VersionTLS10,
},
{
envSet: true,
envValue: "TLS11",
expectedVersion: tls.VersionTLS11,
},
{
envSet: true,
envValue: "TLS12",
expectedVersion: tls.VersionTLS12,
},
{
envSet: true,
envValue: "TLS13",
expectedVersion: tls.VersionTLS13,
},
{
envSet: false,
expectedVersion: tls.VersionTLS12,
},
}

func TestResolveMinTLSVersion(t *testing.T) {
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
defer os.Unsetenv("KEDA_HTTP_MIN_TLS_VERSION")
for _, testData := range minTLSVersionTestDatas {
os.Unsetenv("KEDA_HTTP_MIN_TLS_VERSION")
if testData.envSet {
os.Setenv("KEDA_HTTP_MIN_TLS_VERSION", testData.envValue)
}
minVersion := initMinTLSVersion(logr.Discard())

if testData.expectedVersion != minVersion {
t.Error("Failed to resolve minTLSVersion correctly", "wants", testData.expectedVersion, "got", minVersion)
}
}
}
4 changes: 3 additions & 1 deletion pkg/util/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func decryptClientKey(clientKey, clientKeyPassword string) ([]byte, error) {
func NewTLSConfigWithPassword(clientCert, clientKey, clientKeyPassword, caCert string) (*tls.Config, error) {
valid := false

config := &tls.Config{}
config := &tls.Config{
MinVersion: GetMinTLSVersion(),
}

if clientCert != "" && clientKey != "" {
key := []byte(clientKey)
Expand Down