|
9 | 9 | "fmt"
|
10 | 10 | "regexp"
|
11 | 11 | "strings"
|
| 12 | + "time" |
12 | 13 |
|
13 | 14 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
14 | 15 | "k8s.io/utils/ptr"
|
@@ -59,6 +60,8 @@ type HTTPFilterIR struct {
|
59 | 60 |
|
60 | 61 | Mirrors []*ir.MirrorPolicy
|
61 | 62 |
|
| 63 | + CORS *ir.CORS |
| 64 | + |
62 | 65 | ExtensionRefs []*ir.UnstructuredRef
|
63 | 66 | }
|
64 | 67 |
|
@@ -98,6 +101,8 @@ func (t *Translator) ProcessHTTPFilters(parentRef *RouteParentContext,
|
98 | 101 | t.processResponseHeaderModifierFilter(filter.ResponseHeaderModifier, httpFiltersContext)
|
99 | 102 | case gwapiv1.HTTPRouteFilterRequestMirror:
|
100 | 103 | err = t.processRequestMirrorFilter(i, filter.RequestMirror, httpFiltersContext, resources)
|
| 104 | + case gwapiv1.HTTPRouteFilterCORS: |
| 105 | + t.processCORSFilter(filter.CORS, httpFiltersContext) |
101 | 106 | case gwapiv1.HTTPRouteFilterExtensionRef:
|
102 | 107 | t.processExtensionRefHTTPFilter(filter.ExtensionRef, httpFiltersContext, resources)
|
103 | 108 | default:
|
@@ -891,6 +896,54 @@ func (t *Translator) processRequestMirrorFilter(
|
891 | 896 | return nil
|
892 | 897 | }
|
893 | 898 |
|
| 899 | +func (t *Translator) processCORSFilter( |
| 900 | + corsFilter *gwapiv1.HTTPCORSFilter, |
| 901 | + filterContext *HTTPFiltersContext, |
| 902 | +) { |
| 903 | + // Make sure the config actually exists |
| 904 | + if corsFilter == nil { |
| 905 | + return |
| 906 | + } |
| 907 | + |
| 908 | + var allowOrigins []*ir.StringMatch |
| 909 | + for _, origin := range corsFilter.AllowOrigins { |
| 910 | + if containsWildcard(string(origin)) { |
| 911 | + regexStr := wildcard2regex(string(origin)) |
| 912 | + allowOrigins = append(allowOrigins, &ir.StringMatch{ |
| 913 | + SafeRegex: ®exStr, |
| 914 | + }) |
| 915 | + } else { |
| 916 | + allowOrigins = append(allowOrigins, &ir.StringMatch{ |
| 917 | + Exact: (*string)(&origin), |
| 918 | + }) |
| 919 | + } |
| 920 | + } |
| 921 | + |
| 922 | + var allowMethods []string |
| 923 | + for _, method := range corsFilter.AllowMethods { |
| 924 | + allowMethods = append(allowMethods, string(method)) |
| 925 | + } |
| 926 | + |
| 927 | + var allowHeaders []string |
| 928 | + for _, header := range corsFilter.AllowHeaders { |
| 929 | + allowHeaders = append(allowHeaders, string(header)) |
| 930 | + } |
| 931 | + |
| 932 | + var exposeHeaders []string |
| 933 | + for _, header := range corsFilter.ExposeHeaders { |
| 934 | + exposeHeaders = append(exposeHeaders, string(header)) |
| 935 | + } |
| 936 | + |
| 937 | + filterContext.CORS = &ir.CORS{ |
| 938 | + AllowOrigins: allowOrigins, |
| 939 | + AllowMethods: allowMethods, |
| 940 | + AllowHeaders: allowHeaders, |
| 941 | + ExposeHeaders: exposeHeaders, |
| 942 | + MaxAge: ptr.To(metav1.Duration{Duration: time.Duration(corsFilter.MaxAge) * time.Second}), |
| 943 | + AllowCredentials: bool(corsFilter.AllowCredentials), |
| 944 | + } |
| 945 | +} |
| 946 | + |
894 | 947 | func (t *Translator) processUnresolvedHTTPFilter(errMsg string, filterContext *HTTPFiltersContext) {
|
895 | 948 | routeStatus := GetRouteStatus(filterContext.Route)
|
896 | 949 | status.SetRouteStatusCondition(routeStatus,
|
|
0 commit comments