Skip to content

Commit

Permalink
feat: HeadersWithUnderscoreActions on ClientTrafficPolicy
Browse files Browse the repository at this point in the history
Signed-off-by: David Alger <[email protected]>
  • Loading branch information
davidalger committed Mar 28, 2024
1 parent 2a38de6 commit 0c6b66e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 4 deletions.
22 changes: 22 additions & 0 deletions api/v1alpha1/clienttrafficpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,30 @@ type HeaderSettings struct {
// and responses.
// +optional
EnableEnvoyHeaders *bool `json:"enableEnvoyHeaders,omitempty"`

// WithUnderscoresAction configures the action to take when an HTTP header with underscores
// is encountered. The default action is to reject the request.
// +optional
WithUnderscoresAction *WithUnderscoresAction `json:"withUnderscoresAction,omitempty"`
}

// WithUnderscoresAction configures the action to take when an HTTP header with underscores
// is encountered.
// +kubebuilder:validation:Enum=Allow;RejectRequest;DropHeader
type WithUnderscoresAction string

const (
// WithUnderscoresActionAllow allows headers with underscores to be passed through.
WithUnderscoresActionAllow WithUnderscoresAction = "Allow"
// WithUnderscoresActionRejectRequest rejects the client request. HTTP/1 requests are rejected with
// the 400 status. HTTP/2 requests end with the stream reset.
WithUnderscoresActionRejectRequest WithUnderscoresAction = "RejectRequest"
// WithUnderscoresActionDropHeader drops the client header with name containing underscores. The header
// is dropped before the filter chain is invoked and as such filters will not see
// dropped headers.
WithUnderscoresActionDropHeader WithUnderscoresAction = "DropHeader"
)

// ClientIPDetectionSettings provides configuration for determining the original client IP address for requests.
//
// +kubebuilder:validation:XValidation:rule="!(has(self.xForwardedFor) && has(self.customHeader))",message="customHeader cannot be used in conjunction with xForwardedFor"
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ spec:
description: EnableEnvoyHeaders configures Envoy Proxy to add
the "X-Envoy-" headers to requests and responses.
type: boolean
withUnderscoresAction:
description: WithUnderscoresAction configures the action to take
when an HTTP header with underscores is encountered. The default
action is to reject the request.
enum:
- Allow
- RejectRequest
- DropHeader
type: string
type: object
http1:
description: HTTP1 provides HTTP/1 configuration on the listener.
Expand Down
3 changes: 2 additions & 1 deletion internal/gatewayapi/clienttrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ func translateListenerHeaderSettings(headerSettings *egv1a1.HeaderSettings, http
return
}
httpIR.Headers = &ir.HeaderSettings{
EnableEnvoyHeaders: ptr.Deref(headerSettings.EnableEnvoyHeaders, false),
EnableEnvoyHeaders: ptr.Deref(headerSettings.EnableEnvoyHeaders, false),
WithUnderscoresAction: ir.WithUnderscoresAction(ptr.Deref(headerSettings.WithUnderscoresAction, egv1a1.WithUnderscoresActionRejectRequest)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ clientTrafficPolicies:
spec:
headers:
enableEnvoyHeaders: true
withUnderscoresAction: Allow
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ clientTrafficPolicies:
spec:
headers:
enableEnvoyHeaders: true
withUnderscoresAction: Allow
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
Expand Down Expand Up @@ -129,6 +130,7 @@ xdsIR:
- address: 0.0.0.0
headers:
enableEnvoyHeaders: true
withUnderscoresAction: Allow
hostnames:
- '*'
isHTTP2: false
Expand All @@ -140,6 +142,7 @@ xdsIR:
- address: 0.0.0.0
headers:
enableEnvoyHeaders: true
withUnderscoresAction: Allow
hostnames:
- '*'
isHTTP2: false
Expand Down
13 changes: 13 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ type PathSettings struct {
EscapedSlashesAction PathEscapedSlashAction `json:"escapedSlashesAction" yaml:"escapedSlashesAction"`
}

type WithUnderscoresAction egv1a1.WithUnderscoresAction

const (
WithUnderscoresActionAllow = WithUnderscoresAction(egv1a1.WithUnderscoresActionAllow)
WithUnderscoresActionRejectRequest = WithUnderscoresAction(egv1a1.WithUnderscoresActionRejectRequest)
WithUnderscoresActionDropHeader = WithUnderscoresAction(egv1a1.WithUnderscoresActionDropHeader)
)

// ClientIPDetectionSettings provides configuration for determining the original client IP address for requests.
// +k8s:deepcopy-gen=true
type ClientIPDetectionSettings egv1a1.ClientIPDetectionSettings
Expand Down Expand Up @@ -394,6 +402,11 @@ type HeaderSettings struct {
// The default is to suppress these headers.
// Refer to https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/router/v3/router.proto#extensions-filters-http-router-v3-router
EnableEnvoyHeaders bool `json:"enableEnvoyHeaders,omitempty" yaml:"enableEnvoyHeaders,omitempty"`

// WithUnderscoresAction configures the action to take when an HTTP header with underscores
// is encountered. The default action is to reject the request.
// Refer to https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/core/v3/protocol.proto#envoy-v3-api-enum-config-core-v3-httpprotocoloptions-headerswithunderscoresaction
WithUnderscoresAction WithUnderscoresAction `json:"withUnderscoresAction,omitempty" yaml:"withUnderscoresAction,omitempty"`
}

// ClientTimeout sets the timeout configuration for downstream connections
Expand Down
16 changes: 15 additions & 1 deletion internal/xds/translator/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (t *Translator) addXdsHTTPFilterChain(xdsListener *listenerv3.Listener, irL
MergeSlashes: irListener.Path.MergeSlashes,
PathWithEscapedSlashesAction: translateEscapePath(irListener.Path.EscapedSlashesAction),
CommonHttpProtocolOptions: &corev3.HttpProtocolOptions{
HeadersWithUnderscoresAction: corev3.HttpProtocolOptions_REJECT_REQUEST,
HeadersWithUnderscoresAction: translateHeadersWithUnderscoresAction(irListener.Headers),
},
Tracing: hcmTracing,
}
Expand Down Expand Up @@ -737,3 +737,17 @@ func buildTCPProxyHashPolicy(lb *ir.LoadBalancer) []*typev3.HashPolicy {

return nil
}

func translateHeadersWithUnderscoresAction(in *ir.HeaderSettings) corev3.HttpProtocolOptions_HeadersWithUnderscoresAction {
if in != nil {
switch in.WithUnderscoresAction {
case ir.WithUnderscoresActionAllow:
return corev3.HttpProtocolOptions_ALLOW
case ir.WithUnderscoresActionRejectRequest:
return corev3.HttpProtocolOptions_REJECT_REQUEST
case ir.WithUnderscoresActionDropHeader:
return corev3.HttpProtocolOptions_DROP_HEADER

Check warning on line 749 in internal/xds/translator/listener.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/listener.go#L744-L749

Added lines #L744 - L749 were not covered by tests
}
}
return corev3.HttpProtocolOptions_REJECT_REQUEST
}
12 changes: 12 additions & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ _Appears in:_
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `enableEnvoyHeaders` | _boolean_ | false | EnableEnvoyHeaders configures Envoy Proxy to add the "X-Envoy-" headers to requests and responses. |
| `withUnderscoresAction` | _[WithUnderscoresAction](#withunderscoresaction)_ | false | WithUnderscoresAction configures the action to take when an HTTP header with underscores is encountered. The default action is to reject the request. |


#### HealthCheck
Expand Down Expand Up @@ -2613,6 +2614,17 @@ _Appears in:_



#### WithUnderscoresAction

_Underlying type:_ _string_

WithUnderscoresAction configures the action to take when an HTTP header with underscores is encountered.

_Appears in:_
- [HeaderSettings](#headersettings)



#### XDSTranslatorHook

_Underlying type:_ _string_
Expand Down
4 changes: 2 additions & 2 deletions test/helm/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ spec:
fieldPath: metadata.namespace
- name: KUBERNETES_CLUSTER_DOMAIN
value: cluster.local
image: docker.io/envoyproxy/gateway-dev:latest
image: envoyproxy/gateway-dev:latest
imagePullPolicy: Always
livenessProbe:
httpGet:
Expand Down Expand Up @@ -513,7 +513,7 @@ spec:
fieldPath: metadata.namespace
- name: KUBERNETES_CLUSTER_DOMAIN
value: cluster.local
image: docker.io/envoyproxy/gateway-dev:latest
image: envoyproxy/gateway-dev:latest
imagePullPolicy: Always
name: envoy-gateway-certgen
restartPolicy: Never
Expand Down

0 comments on commit 0c6b66e

Please sign in to comment.