-
Notifications
You must be signed in to change notification settings - Fork 593
Add support for CORS configuration in TrafficPolicy and in HTTPRoute #11252
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
Changes from 3 commits
a5a7bd2
1a8f220
26f450a
59a1211
c9bf070
a1af10d
0770abc
043de49
5af9ab9
15a3cee
eec1c0a
b7839b3
cdb5566
8f1c8d3
a42b0bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package trafficpolicy | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| corsv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/cors/v3" | ||
| matcherv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3" | ||
| "github.com/golang/protobuf/ptypes/wrappers" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "github.com/kgateway-dev/kgateway/v2/api/v1alpha1" | ||
| ) | ||
|
|
||
| type CorsIR struct { | ||
| // corsConfig is the envoy cors policy | ||
| corsConfig *corsv3.CorsPolicy | ||
| } | ||
|
|
||
| func (c *CorsIR) Equals(other *CorsIR) bool { | ||
| if c == nil && other == nil { | ||
| return true | ||
| } | ||
| if c == nil || other == nil { | ||
| return false | ||
| } | ||
|
|
||
| return proto.Equal(c.corsConfig, other.corsConfig) | ||
| } | ||
|
|
||
| // corsForSpec translates the cors spec into an envoy cors policy and stores it in the traffic policy IR | ||
| func corsForSpec(spec v1alpha1.TrafficPolicySpec, out *trafficPolicySpecIr) error { | ||
| if spec.Cors == nil { | ||
| return nil | ||
| } | ||
| corsConfig, err := toCorsFilterConfig(spec.Cors) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| out.cors = &CorsIR{ | ||
| corsConfig: corsConfig, | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func toCorsFilterConfig(t *v1alpha1.CorsPolicy) (*corsv3.CorsPolicy, error) { | ||
| if t == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| /* | ||
| Envoy CORS configuration example: | ||
|
|
||
| allow_origin_string_match: | ||
| - exact: "https://example.com" | ||
| allow_methods: "GET, POST, OPTIONS" | ||
| allow_headers: "Content-Type, Authorization" | ||
| max_age: "86400" | ||
| expose_headers: "X-Custom-Header" | ||
| allow_credentials: true | ||
| */ | ||
|
|
||
| corsPolicy := &corsv3.CorsPolicy{} | ||
| if len(t.AllowOrigins) > 0 { | ||
| origins := make([]*matcherv3.StringMatcher, len(t.AllowOrigins)) | ||
| for i, origin := range t.AllowOrigins { | ||
| origins[i] = &matcherv3.StringMatcher{ | ||
| MatchPattern: &matcherv3.StringMatcher_Exact{ | ||
| Exact: origin, | ||
| }, | ||
| } | ||
| } | ||
| corsPolicy.AllowOriginStringMatch = origins | ||
| } | ||
| if len(t.AllowMethods) > 0 { | ||
| corsPolicy.AllowMethods = strings.Join(t.AllowMethods, ", ") | ||
| } | ||
| if len(t.AllowHeaders) > 0 { | ||
| corsPolicy.AllowHeaders = strings.Join(t.AllowHeaders, ", ") | ||
| } | ||
| if t.AllowCredentials { | ||
| corsPolicy.AllowCredentials = &wrappers.BoolValue{Value: t.AllowCredentials} | ||
ymesika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| if len(t.ExposeHeaders) > 0 { | ||
| corsPolicy.ExposeHeaders = strings.Join(t.ExposeHeaders, ", ") | ||
| } | ||
| if t.MaxAge != "" { | ||
| corsPolicy.MaxAge = t.MaxAge | ||
| } | ||
|
|
||
| return corsPolicy, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.