Skip to content

Commit

Permalink
Responded to Doug's comment, added (Un)RegisterForTesting()
Browse files Browse the repository at this point in the history
  • Loading branch information
zasweq committed Sep 16, 2021
1 parent 1df3a91 commit fa9383d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
13 changes: 5 additions & 8 deletions xds/internal/httpfilter/httpfilter.go
Expand Up @@ -23,7 +23,6 @@ package httpfilter
import (
"github.com/golang/protobuf/proto"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/xds/env"
)

// FilterConfig represents an opaque data structure holding configuration for a
Expand Down Expand Up @@ -95,16 +94,14 @@ func Register(b Filter) {
}
}

// UnregisterForTesting unregisters the HTTP Filter for testing purposes.
func UnregisterForTesting(typeURL string) {
delete(m, typeURL)
}

// Get returns the HTTPFilter registered with typeURL.
//
// If no filter is register with typeURL, nil will be returned.
func Get(typeURL string) Filter {
// Hardcoded here vs. rbac http filter package because rbac HTTP Filter test
// is in integration test.
if !env.RBACSupport {
if typeURL == "type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBAC" || typeURL == "type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBACPerRoute" {
return nil
}
}
return m[typeURL]
}
19 changes: 19 additions & 0 deletions xds/internal/httpfilter/rbac/rbac.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/xds/env"
"google.golang.org/grpc/internal/xds/rbac"
"google.golang.org/grpc/xds/internal/httpfilter"
"google.golang.org/protobuf/types/known/anypb"
Expand All @@ -37,9 +38,27 @@ import (
)

func init() {
if env.RBACSupport {
httpfilter.Register(builder{})
}
}

// RegisterForTesting registers the RBAC HTTP Filter for testing purposes, regardless
// of the RBAC environment variable. This is needed because there is no way to set the RBAC
// environment variable to true in a test before init() in this package is run.
func RegisterForTesting() {
httpfilter.Register(builder{})
}

// UnregisterForTesting unregisters the RBAC HTTP Filter for testing purposes. This is needed because
// there is no way to unregister the HTTP Filter after registering it solely for testing purposes using
// rbac.RegisterForTesting()
func UnregisterForTesting() {
for _, typeURL := range builder.TypeURLs(builder{}) {
httpfilter.UnregisterForTesting(typeURL)
}
}

type builder struct {
}

Expand Down
4 changes: 3 additions & 1 deletion xds/internal/test/xds_server_integration_test.go
Expand Up @@ -37,7 +37,7 @@ import (
"google.golang.org/grpc/internal/xds/env"
"google.golang.org/grpc/status"
"google.golang.org/grpc/xds"
_ "google.golang.org/grpc/xds/internal/httpfilter/rbac"
"google.golang.org/grpc/xds/internal/httpfilter/rbac"
"google.golang.org/grpc/xds/internal/testutils/e2e"

v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
Expand Down Expand Up @@ -722,6 +722,8 @@ func (s) TestRBACHTTPFilter(t *testing.T) {
defer func() {
env.RBACSupport = oldRBAC
}()
rbac.RegisterForTesting()
defer rbac.UnregisterForTesting()
tests := []struct {
name string
rbacCfg *rpb.RBAC
Expand Down

0 comments on commit fa9383d

Please sign in to comment.