-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cors mapping fix Signed-off-by: Alix Cook <[email protected]> * fake test Signed-off-by: Alix Cook <[email protected]> * changelog Signed-off-by: Alix Cook <[email protected]> Signed-off-by: Luke Shumaker <[email protected]>
- Loading branch information
Showing
9 changed files
with
251 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package entrypoint_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/datawire/ambassador/v2/cmd/entrypoint" | ||
envoy "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2" | ||
route "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/route" | ||
bootstrap "github.com/datawire/ambassador/v2/pkg/api/envoy/config/bootstrap/v2" | ||
http "github.com/datawire/ambassador/v2/pkg/api/envoy/config/filter/network/http_connection_manager/v2" | ||
"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/resource/v2" | ||
"github.com/datawire/ambassador/v2/pkg/envoy-control-plane/wellknown" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMappingCORSOriginsSlice(t *testing.T) { | ||
f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true}, nil) | ||
f.UpsertYAML(` | ||
--- | ||
apiVersion: getambassador.io/v2 | ||
kind: Mapping | ||
metadata: | ||
name: foo | ||
namespace: default | ||
spec: | ||
prefix: /foo | ||
service: foo.default | ||
cors: | ||
origins: | ||
- foo.example.com | ||
- bar.example.com | ||
`) | ||
f.Upsert(makeService("default", "foo")) | ||
f.Flush() | ||
snap := f.GetSnapshot(HasMapping("default", "foo")) | ||
assert.NotNil(t, snap) | ||
|
||
config := f.GetEnvoyConfig(func(config *bootstrap.Bootstrap) bool { | ||
return FindCluster(config, ClusterNameContains("cluster_foo_default_default")) != nil | ||
}) | ||
|
||
listener := findListener(config, func(l *envoy.Listener) bool { | ||
return l.Name == "ambassador-listener-8080" | ||
}) | ||
|
||
assert.NotNil(t, listener) | ||
|
||
routeAction := findVirtualHostRoute(listener, func(r *route.RouteAction) bool { | ||
return r.GetCluster() == "cluster_foo_default_default" | ||
}) | ||
assert.NotNil(t, routeAction) | ||
assert.NotNil(t, routeAction.Cors) | ||
assert.Equal(t, len(routeAction.Cors.AllowOriginStringMatch), 2) | ||
for _, m := range routeAction.Cors.AllowOriginStringMatch { | ||
assert.Contains(t, []string{"bar.example.com", "foo.example.com"}, m.GetExact()) | ||
|
||
} | ||
} | ||
|
||
func TestMappingCORSOriginsString(t *testing.T) { | ||
f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true}, nil) | ||
f.UpsertYAML(` | ||
--- | ||
apiVersion: getambassador.io/v2 | ||
kind: Mapping | ||
metadata: | ||
name: foo | ||
namespace: default | ||
spec: | ||
prefix: /foo | ||
service: foo.default | ||
cors: | ||
origins: "foo.example.com,bar.example.com" | ||
`) | ||
f.Upsert(makeService("default", "foo")) | ||
f.Flush() | ||
snap := f.GetSnapshot(HasMapping("default", "foo")) | ||
assert.NotNil(t, snap) | ||
|
||
config := f.GetEnvoyConfig(func(config *bootstrap.Bootstrap) bool { | ||
return FindCluster(config, ClusterNameContains("cluster_foo_default_default")) != nil | ||
}) | ||
|
||
listener := findListener(config, func(l *envoy.Listener) bool { | ||
return l.Name == "ambassador-listener-8080" | ||
}) | ||
|
||
assert.NotNil(t, listener) | ||
|
||
routeAction := findVirtualHostRoute(listener, func(r *route.RouteAction) bool { | ||
return r.GetCluster() == "cluster_foo_default_default" | ||
}) | ||
assert.NotNil(t, routeAction) | ||
assert.NotNil(t, routeAction.Cors) | ||
assert.Equal(t, len(routeAction.Cors.AllowOriginStringMatch), 2) | ||
for _, m := range routeAction.Cors.AllowOriginStringMatch { | ||
assert.Contains(t, []string{"bar.example.com", "foo.example.com"}, m.GetExact()) | ||
|
||
} | ||
} | ||
|
||
func findVirtualHostRoute(listener *envoy.Listener, predicate func(*route.RouteAction) bool) *route.RouteAction { | ||
for _, fc := range listener.FilterChains { | ||
for _, filter := range fc.Filters { | ||
if filter.Name != wellknown.HTTPConnectionManager { | ||
continue | ||
} | ||
hcm := resource.GetHTTPConnectionManager(filter) | ||
if hcm != nil { | ||
rs, ok := hcm.RouteSpecifier.(*http.HttpConnectionManager_RouteConfig) | ||
if ok { | ||
for _, vh := range rs.RouteConfig.VirtualHosts { | ||
for _, vhr := range vh.Routes { | ||
routeAction, ok := vhr.Action.(*route.Route_Route) | ||
if ok { | ||
if predicate(routeAction.Route) { | ||
return routeAction.Route | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
return nil | ||
|
||
} | ||
|
||
func findListener(envoyConfig *bootstrap.Bootstrap, predicate func(*envoy.Listener) bool) *envoy.Listener { | ||
for _, listener := range envoyConfig.StaticResources.Listeners { | ||
if predicate(listener) { | ||
return listener | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.