-
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
5c52d03
commit ae7defe
Showing
5 changed files
with
146 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
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