|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kgateway |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 21 | + "k8s.io/apimachinery/pkg/types" |
| 22 | + "k8s.io/utils/ptr" |
| 23 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// applySSLRedirectPolicy applies SSL redirect by adding a RequestRedirect filter |
| 27 | +// to HTTPRoute rules when SSLRedirect is enabled in the policy. |
| 28 | +// |
| 29 | +// Semantics: |
| 30 | +// - If SSLRedirect is enabled, add a RequestRedirect filter to rule-level Filters |
| 31 | +// - The filter redirects HTTP to HTTPS with a 301 status code |
| 32 | +// - The filter is applied to all rules covered by the policy |
| 33 | +func applySSLRedirectPolicy( |
| 34 | + pol intermediate.Policy, |
| 35 | + httpRouteKey types.NamespacedName, |
| 36 | + httpRouteContext *intermediate.HTTPRouteContext, |
| 37 | + coverage []intermediate.PolicyIndex, |
| 38 | +) { |
| 39 | + if pol.SSLRedirect == nil || !*pol.SSLRedirect { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Get unique rule indices from coverage |
| 44 | + ruleSet := make(map[int]struct{}) |
| 45 | + for _, idx := range coverage { |
| 46 | + ruleSet[idx.Rule] = struct{}{} |
| 47 | + } |
| 48 | + |
| 49 | + // Add RequestRedirect filter to each covered rule |
| 50 | + for ruleIdx := range ruleSet { |
| 51 | + if ruleIdx >= len(httpRouteContext.Spec.Rules) { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + // Check if RequestRedirect filter already exists |
| 56 | + hasRedirect := false |
| 57 | + for _, filter := range httpRouteContext.Spec.Rules[ruleIdx].Filters { |
| 58 | + if filter.Type == gwv1.HTTPRouteFilterRequestRedirect { |
| 59 | + hasRedirect = true |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if !hasRedirect { |
| 65 | + // Add RequestRedirect filter to redirect HTTP to HTTPS |
| 66 | + httpRouteContext.Spec.Rules[ruleIdx].Filters = append( |
| 67 | + httpRouteContext.Spec.Rules[ruleIdx].Filters, |
| 68 | + gwv1.HTTPRouteFilter{ |
| 69 | + Type: gwv1.HTTPRouteFilterRequestRedirect, |
| 70 | + RequestRedirect: &gwv1.HTTPRequestRedirectFilter{ |
| 71 | + Scheme: ptr.To("https"), |
| 72 | + StatusCode: ptr.To(301), |
| 73 | + }, |
| 74 | + }, |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments