Skip to content

Commit

Permalink
Add the ability to specify the maximum acceptable TLS version (#414)
Browse files Browse the repository at this point in the history
Signed-off-by: Lyas Spiehler <[email protected]>
  • Loading branch information
lspiehler authored Dec 8, 2022
1 parent bebc731 commit 87b669d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,13 @@ func NewTLSConfig(cfg *TLSConfig) (*tls.Config, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: cfg.InsecureSkipVerify,
MinVersion: uint16(cfg.MinVersion),
MaxVersion: uint16(cfg.MaxVersion),
}

if cfg.MaxVersion != 0 && cfg.MinVersion != 0 {
if cfg.MaxVersion < cfg.MinVersion {
return nil, fmt.Errorf("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
}
}

// If a CA cert is provided then let's read it in so we can validate the
Expand Down Expand Up @@ -826,6 +833,8 @@ type TLSConfig struct {
InsecureSkipVerify bool `yaml:"insecure_skip_verify" json:"insecure_skip_verify"`
// Minimum TLS version.
MinVersion TLSVersion `yaml:"min_version,omitempty" json:"min_version,omitempty"`
// Maximum TLS version.
MaxVersion TLSVersion `yaml:"max_version,omitempty" json:"max_version,omitempty"`
}

// SetDirectory joins any relative file paths with dir.
Expand Down
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version.bad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"max_version": "TLS11",
"min_version": "TLS12"}
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version.bad.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_version: TLS11
min_version: TLS12
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version.good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"max_version": "TLS12",
"min_version": "TLS11"}
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version.good.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_version: TLS12
min_version: TLS11
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version_same.good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"max_version": "TLS12",
"min_version": "TLS12"}
2 changes: 2 additions & 0 deletions config/testdata/tls_config.max_and_min_version_same.good.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_version: TLS12
min_version: TLS12
1 change: 1 addition & 0 deletions config/testdata/tls_config.max_version.good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"max_version": "TLS12"}
1 change: 1 addition & 0 deletions config/testdata/tls_config.max_version.good.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_version: TLS12
36 changes: 36 additions & 0 deletions config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"encoding/json"
Expand Down Expand Up @@ -64,6 +65,9 @@ var expectedTLSConfigs = []struct {
}, {
filename: "tls_config.tlsversion.good.json",
config: &tls.Config{MinVersion: tls.VersionTLS11},
}, {
filename: "tls_config.max_version.good.json",
config: &tls.Config{MaxVersion: tls.VersionTLS12},
},
{
filename: "tls_config.empty.good.yml",
Expand All @@ -74,6 +78,15 @@ var expectedTLSConfigs = []struct {
}, {
filename: "tls_config.tlsversion.good.yml",
config: &tls.Config{MinVersion: tls.VersionTLS11},
}, {
filename: "tls_config.max_version.good.yml",
config: &tls.Config{MaxVersion: tls.VersionTLS12},
}, {
filename: "tls_config.max_and_min_version.good.yml",
config: &tls.Config{MaxVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS11},
}, {
filename: "tls_config.max_and_min_version_same.good.yml",
config: &tls.Config{MaxVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12},
},
}

Expand All @@ -91,6 +104,29 @@ func TestValidTLSConfig(t *testing.T) {
}
}

var invalidTLSConfigs = []struct {
filename string
errMsg string
}{
{
filename: "tls_config.max_and_min_version.bad.yml",
errMsg: "tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified",
},
}

func TestInvalidTLSConfig(t *testing.T) {
for _, ee := range invalidTLSConfigs {
_, err := LoadTLSConfig("testdata/" + ee.filename)
if err == nil {
t.Error("Expected error with config but got none")
continue
}
if !strings.Contains(err.Error(), ee.errMsg) {
t.Errorf("Expected error for invalid HTTP client configuration to contain %q but got: %s", ee.errMsg, err)
}
}
}

func TestStringer(t *testing.T) {
if s := (TLSVersion)(tls.VersionTLS13); s.String() != "TLS13" {
t.Fatalf("tls.VersionTLS13 string should be TLS13, got %s", s.String())
Expand Down

0 comments on commit 87b669d

Please sign in to comment.