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

Add websocket support #962

Merged
merged 3 commits into from
Aug 15, 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
2 changes: 1 addition & 1 deletion conformance/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uninstall-nkg: uninstall-k8s-components undo-manifests-update ## Uninstall NKG o

.PHONY: uninstall-k8s-components
uninstall-k8s-components: ## Uninstall installed components on configured kind cluster
kubectl delete -f $(NKG_MANIFEST)
-kubectl delete -f $(NKG_MANIFEST)
ciarams87 marked this conversation as resolved.
Show resolved Hide resolved
./scripts/uninstall-gateway.sh $(GW_API_VERSION)
kubectl delete clusterrole nginx-gateway-provisioner
kubectl delete clusterrolebinding nginx-gateway-provisioner
Expand Down
7 changes: 7 additions & 0 deletions internal/mode/static/nginx/config/maps_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ map $http_host $gw_api_compliant_host {
'' $host;
default $http_host;
}

# Set $connection_header variable to upgrade when the $http_upgrade header is set, otherwise, set it to close. This
# allows support for websocket connections. See https://nginx.org/en/docs/http/websocket.html.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
`
1 change: 1 addition & 0 deletions internal/mode/static/nginx/config/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestExecuteMaps(t *testing.T) {
"~.* ${http_my_second_add_header},;": 1,
"map ${http_my_set_header} $my_set_header_header_var {": 0,
"map $http_host $gw_api_compliant_host {": 1,
"map $http_upgrade $connection_upgrade {": 1,
}

maps := string(executeMaps(conf))
Expand Down
3 changes: 3 additions & 0 deletions internal/mode/static/nginx/config/servers_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ server {
proxy_set_header {{ $h.Name }} "{{ $h.Value }}";
{{- end }}
proxy_set_header Host $gw_api_compliant_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
pleshakov marked this conversation as resolved.
Show resolved Hide resolved
proxy_set_header Connection $connection_upgrade;
proxy_pass {{ $l.ProxyPass }}$request_uri;
{{- end }}
}
Expand Down
14 changes: 10 additions & 4 deletions internal/mode/static/nginx/config/validation/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,25 @@ func validateEscapedStringNoVarExpansion(value string, examples []string) error
}

const (
invalidHostHeaderErrMsg string = "redefining the Host request header is not supported"
maxHeaderLength int = 256
invalidHeadersErrMsg string = "unsupported header name configured, unsupported names are: "
maxHeaderLength int = 256
)

var invalidHeaders = map[string]struct{}{
"host": {},
"connection": {},
"upgrade": {},
}

func validateHeaderName(name string) error {
if len(name) > maxHeaderLength {
return errors.New(k8svalidation.MaxLenError(maxHeaderLength))
}
if msg := k8svalidation.IsHTTPHeaderName(name); msg != nil {
return errors.New(msg[0])
}
if strings.ToLower(name) == "host" {
return errors.New(invalidHostHeaderErrMsg)
if valid, invalidHeadersAsStrings := validateNoUnsupportedValues(strings.ToLower(name), invalidHeaders); !valid {
return errors.New(invalidHeadersErrMsg + strings.Join(invalidHeadersAsStrings, ", "))
}
return nil
}
2 changes: 2 additions & 0 deletions internal/mode/static/nginx/config/validation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestValidateValidHeaderName(t *testing.T) {
`$test`,
"Host",
"host",
"connection",
"upgrade",
"my-header[]",
"my-header&",
strings.Repeat("very-long-header", 16)+"1",
Expand Down
10 changes: 10 additions & 0 deletions internal/mode/static/nginx/config/validation/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func validateInSupportedValues[T configValue](
return false, getSortedKeysAsString(supportedValues)
}

func validateNoUnsupportedValues[T configValue](
value T,
unsupportedValues map[T]struct{},
) (valid bool, unsupportedValuesAsStrings []string) {
if _, exist := unsupportedValues[value]; exist {
return false, getSortedKeysAsString(unsupportedValues)
}
return true, nil
}

func getSortedKeysAsString[T configValue](m map[T]struct{}) []string {
keysAsString := make([]string, 0, len(m))

Expand Down
28 changes: 28 additions & 0 deletions internal/mode/static/nginx/config/validation/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ func TestValidateInSupportedValues(t *testing.T) {
)
}

func TestValidateNoUnsupportedValues(t *testing.T) {
unsupportedValues := map[string]struct{}{
"badvalue1": {},
"badvalue2": {},
"badvalue3": {},
}

validator := func(value string) (bool, []string) {
return validateNoUnsupportedValues(value, unsupportedValues)
}

testValidValuesForSupportedValuesValidator(
t,
validator,
"value1",
"value2",
"value3",
)
testInvalidValuesForSupportedValuesValidator(
t,
validator,
unsupportedValues,
"badvalue1",
"badvalue2",
"badvalue3",
)
}

func TestGetSortedKeysAsString(t *testing.T) {
values := map[string]struct{}{
"value3": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestValidateRequestHeaderName(t *testing.T) {
t,
validator.ValidateRequestHeaderName,
"Content-Encoding",
"Connection",
"MyBespokeHeader",
)

testInvalidValuesForSimpleValidator(t, validator.ValidateRequestHeaderName, "$Content-Encoding")
Expand Down
4 changes: 2 additions & 2 deletions internal/mode/static/state/dataplane/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,8 +1609,8 @@ func TestCreateFilters(t *testing.T) {
RequestHeaderModifier: &v1beta1.HTTPHeaderFilter{
Set: []v1beta1.HTTPHeader{
{
Name: "Connection",
Value: "close",
Name: "MyBespokeHeader",
Value: "my-value",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/mode/static/state/graph/httproute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ func TestValidateFilterRequestHeaderModifier(t *testing.T) {
Type: v1beta1.HTTPRouteFilterRequestHeaderModifier,
RequestHeaderModifier: &v1beta1.HTTPHeaderFilter{
Set: []v1beta1.HTTPHeader{
{Name: "Connection", Value: "close"},
{Name: "MyBespokeHeader", Value: "my-value"},
},
Add: []v1beta1.HTTPHeader{
{Name: "Accept-Encoding", Value: "gzip"},
Expand Down