-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow configuring min tls for grpc
Signed-off-by: Or Shachar <[email protected]>
- Loading branch information
1 parent
aae3192
commit 3e2e6fa
Showing
6 changed files
with
147 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tls | ||
|
||
import ( | ||
ctls "crypto/tls" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type tlsVersion string | ||
|
||
const ( | ||
TLS10 tlsVersion = "TLS10" | ||
TLS11 tlsVersion = "TLS11" | ||
TLS12 tlsVersion = "TLS12" | ||
TLS13 tlsVersion = "TLS13" | ||
) | ||
|
||
type tlsEnvVariableName string | ||
|
||
const ( | ||
minHTTPTLSVersionEnv tlsEnvVariableName = "KEDA_HTTP_MIN_TLS_VERSION" | ||
minGrpcTLSVersionEnv tlsEnvVariableName = "KEDA_GRPC_MIN_TLS_VERSION" | ||
) | ||
|
||
const ( | ||
defaultMinHTTPTLSVersion = TLS12 | ||
defaultMinGrpcTLSVersion = TLS13 | ||
) | ||
|
||
func getMinTLSVersion(envKey tlsEnvVariableName, defaultVal tlsVersion) (uint16, error) { | ||
version := string(defaultVal) | ||
if val, ok := os.LookupEnv(string(envKey)); ok { | ||
version = val | ||
} | ||
mapping := map[string]uint16{ | ||
string(TLS10): ctls.VersionTLS10, | ||
string(TLS11): ctls.VersionTLS11, | ||
string(TLS12): ctls.VersionTLS12, | ||
string(TLS13): ctls.VersionTLS13, | ||
} | ||
if v, ok := mapping[version]; ok { | ||
return v, nil | ||
} | ||
fallback := mapping[string(defaultVal)] | ||
return fallback, fmt.Errorf("invalid TLS version: %s, using %s", version, defaultVal) | ||
} | ||
|
||
func GetMinHTTPTLSVersion() (uint16, error) { | ||
return getMinTLSVersion(minHTTPTLSVersionEnv, defaultMinHTTPTLSVersion) | ||
} | ||
|
||
func GetMinGrpcTLSVersion() (uint16, error) { | ||
return getMinTLSVersion(minGrpcTLSVersionEnv, defaultMinGrpcTLSVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package tls | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
type minTLSVersionTestData struct { | ||
name string | ||
envSet bool | ||
envValue string | ||
expectedVersion uint16 | ||
shouldError bool | ||
} | ||
|
||
var minTLSVersionTestDatas = []minTLSVersionTestData{ | ||
{ | ||
name: "Set to TLS10", | ||
envSet: true, | ||
envValue: "TLS10", | ||
expectedVersion: tls.VersionTLS10, | ||
}, | ||
{ | ||
name: "Set to TLS11", | ||
envSet: true, | ||
envValue: "TLS11", | ||
expectedVersion: tls.VersionTLS11, | ||
}, | ||
{ | ||
name: "Set to TLS12", | ||
envSet: true, | ||
envValue: "TLS12", | ||
expectedVersion: tls.VersionTLS12, | ||
}, | ||
{ | ||
name: "Set to TLS13", | ||
envSet: true, | ||
envValue: "TLS13", | ||
expectedVersion: tls.VersionTLS13, | ||
}, | ||
{ | ||
name: "No setting", | ||
envSet: false, | ||
}, | ||
{ | ||
name: "Invalid settings", | ||
envSet: true, | ||
envValue: "TLS9", | ||
shouldError: true, | ||
}, | ||
} | ||
|
||
func testResolveMinTLSVersion(t *testing.T, minVersionFunc func() (uint16, error), envName string, defaultVersion uint16) { | ||
defer os.Unsetenv(envName) | ||
for _, testData := range minTLSVersionTestDatas { | ||
name := fmt.Sprintf("%s: %s", envName, testData.name) | ||
t.Run(name, func(t *testing.T) { | ||
os.Unsetenv(envName) | ||
expectedVersion := defaultVersion | ||
if testData.expectedVersion != 0 { | ||
expectedVersion = testData.expectedVersion | ||
} | ||
if testData.envSet { | ||
os.Setenv(envName, testData.envValue) | ||
} | ||
minVersion, err := minVersionFunc() | ||
if testData.shouldError && err == nil { | ||
t.Error("Expected error but got none") | ||
} | ||
if expectedVersion != minVersion { | ||
t.Error("Failed to resolve minTLSVersion correctly", "wants", testData.expectedVersion, "got", minVersion) | ||
} | ||
}) | ||
} | ||
} | ||
func TestResolveMinTLSVersion(t *testing.T) { | ||
testResolveMinTLSVersion(t, GetMinHTTPTLSVersion, "KEDA_HTTP_MIN_TLS_VERSION", tls.VersionTLS12) | ||
testResolveMinTLSVersion(t, GetMinGrpcTLSVersion, "KEDA_GRPC_MIN_TLS_VERSION", tls.VersionTLS13) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters