Skip to content

Commit

Permalink
Moving back to original Factory and PluginName for authz backwards co…
Browse files Browse the repository at this point in the history
…mpatibility

Signed-off-by: pweiber <periclesweiber@ciandt.com>
  • Loading branch information
pweiber committed Nov 18, 2024
1 parent 04db36e commit c0220b7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/opa-envoy-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

func main() {
runtime.RegisterPlugin("envoy.ext_authz.grpc", plugin.AuthZFactory{}) // for backwards compatibility
runtime.RegisterPlugin(plugin.AuthZPluginName, plugin.AuthZFactory{})
runtime.RegisterPlugin("envoy.ext_authz.grpc", plugin.Factory{}) // for backwards compatibility
runtime.RegisterPlugin(plugin.PluginName, plugin.Factory{})
runtime.RegisterPlugin(plugin.ExtProcPluginName, plugin.ExtProcFactory{})

if err := cmd.RootCommand.Execute(); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ const (
defaultGRPCServerMaxReceiveMessageSize = 1024 * 1024 * 4
defaultGRPCServerMaxSendMessageSize = math.MaxInt32

// AuthZPluginName and ExtProcPluginName Respective names to register with the OPA plugin manager
AuthZPluginName = "envoy_ext_authz_grpc"
// PluginName and ExtProcPluginName Respective names to register with the OPA plugin manager
PluginName = "envoy_ext_authz_grpc"
ExtProcPluginName = "envoy_ext_proc_grpc"
)

Expand Down Expand Up @@ -146,7 +146,7 @@ func Validate(m *plugins.Manager, bs []byte) (*Config, error) {
}

// New returns a Plugin that implements the Envoy ext_authz API.
func NewAuthZ(m *plugins.Manager, cfg *Config) plugins.Plugin {
func New(m *plugins.Manager, cfg *Config) plugins.Plugin {
grpcOpts := []grpc.ServerOption{
grpc.MaxRecvMsgSize(cfg.GRPCMaxRecvMsgSize),
grpc.MaxSendMsgSize(cfg.GRPCMaxSendMsgSize),
Expand Down Expand Up @@ -202,7 +202,7 @@ func NewAuthZ(m *plugins.Manager, cfg *Config) plugins.Plugin {
plugin.manager.PrometheusRegister().MustRegister(errorCounter)
}

m.UpdatePluginStatus(AuthZPluginName, &plugins.Status{State: plugins.StateNotReady})
m.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateNotReady})

return plugin
}
Expand Down Expand Up @@ -297,14 +297,14 @@ func (p *envoyExtAuthzGrpcServer) CreatePreparedQueryOnce(opts envoyauth.Prepare
}

func (p *envoyExtAuthzGrpcServer) Start(ctx context.Context) error {
p.manager.UpdatePluginStatus(AuthZPluginName, &plugins.Status{State: plugins.StateNotReady})
p.manager.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateNotReady})
go p.listen()
return nil
}

func (p *envoyExtAuthzGrpcServer) Stop(ctx context.Context) {
p.server.Stop()
p.manager.UpdatePluginStatus(AuthZPluginName, &plugins.Status{State: plugins.StateNotReady})
p.manager.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateNotReady})
}

func (p *envoyExtAuthzGrpcServer) Reconfigure(ctx context.Context, config interface{}) {
Expand Down Expand Up @@ -361,15 +361,15 @@ func (p *envoyExtAuthzGrpcServer) listen() {
"enable-reflection": p.cfg.EnableReflection,
}).Info("Starting gRPC server.")

p.manager.UpdatePluginStatus(AuthZPluginName, &plugins.Status{State: plugins.StateOK})
p.manager.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateOK})

if err := p.server.Serve(l); err != nil {
logger.WithFields(map[string]interface{}{"err": err}).Error("Listener failed.")
return
}

logger.Info("Listener exited.")
p.manager.UpdatePluginStatus(AuthZPluginName, &plugins.Status{State: plugins.StateNotReady})
p.manager.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateNotReady})
}

// Check is envoy.service.auth.v3.Authorization/Check
Expand Down
26 changes: 13 additions & 13 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1850,12 +1850,12 @@ func TestPluginStatusLifeCycle(t *testing.T) {
t.Fatalf("Unexpected error: %s", err)
}

p := NewAuthZ(m, &Config{
p := New(m, &Config{
Addr: ":0",
})
m.Register(AuthZPluginName, p)
m.Register(PluginName, p)

assertAuthZPluginState(t, m, plugins.StateNotReady)
assertPluginState(t, m, plugins.StateNotReady)

ctx := context.Background()
err = m.Start(ctx)
Expand All @@ -1865,44 +1865,44 @@ func TestPluginStatusLifeCycle(t *testing.T) {

// Wait a short time for the plugin to reach OK state
// If it hits this timeout something bad has almost definitely happened
waitForAuthZPluginState(t, m, plugins.StateOK, 5*time.Second)
waitForPluginState(t, m, plugins.StateOK, 5*time.Second)

m.Stop(ctx)

assertAuthZPluginState(t, m, plugins.StateNotReady)
assertPluginState(t, m, plugins.StateNotReady)
}

func waitForAuthZPluginState(t *testing.T, m *plugins.Manager, desired plugins.State, timeout time.Duration) {
func waitForPluginState(t *testing.T, m *plugins.Manager, desired plugins.State, timeout time.Duration) {
after := time.After(timeout)
tick := time.Tick(10 * time.Microsecond)
for {
select {
case <-after:
t.Fatal("Plugin failed to reach OK state in time")
case <-tick:
state, err := getAuthZPluginState(t, m)
state, err := getPluginState(t, m)
if err == nil && state == desired {
return
}
}
}
}

func getAuthZPluginState(t *testing.T, m *plugins.Manager) (plugins.State, error) {
func getPluginState(t *testing.T, m *plugins.Manager) (plugins.State, error) {
t.Helper()
status, ok := m.PluginStatus()[AuthZPluginName]
status, ok := m.PluginStatus()[PluginName]
if !ok {
return plugins.StateNotReady, fmt.Errorf("expected plugin %s to be in manager plugin status map", AuthZPluginName)
return plugins.StateNotReady, fmt.Errorf("expected plugin %s to be in manager plugin status map", PluginName)
}
if status == nil {
return plugins.StateNotReady, errors.New("expected a non-nil status value")
}
return status.State, nil
}

func assertAuthZPluginState(t *testing.T, m *plugins.Manager, expected plugins.State) {
func assertPluginState(t *testing.T, m *plugins.Manager, expected plugins.State) {
t.Helper()
state, err := getAuthZPluginState(t, m)
state, err := getPluginState(t, m)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2008,7 +2008,7 @@ func testAuthzServerWithModule(module string, path string, customConfig *Config,
}
}

s := NewAuthZ(m, &cfg)
s := New(m, &cfg)
return s.(*envoyExtAuthzGrpcServer)
}

Expand Down
20 changes: 10 additions & 10 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ import (
"github.com/open-policy-agent/opa-envoy-plugin/internal"
)

// AuthZFactory defines the factory for the AuthZ plugin.
type AuthZFactory struct{}
// Factory defines the interface OPA uses to instantiate a plugin.
type Factory struct{}

// ExtProcFactory defines the factory for the ExtProc plugin.
type ExtProcFactory struct{}

// Plugin names to register with the OPA plugin manager.
const (
AuthZPluginName = "envoy_ext_authz_grpc"
PluginName = internal.PluginName
ExtProcPluginName = "envoy_ext_proc_grpc"
)

// New method for AuthZFactory.
func (AuthZFactory) New(m *plugins.Manager, config interface{}) plugins.Plugin {
return internal.NewAuthZ(m, config.(*internal.Config))
// New returns the object initialized with a valid plugin configuration.
func (Factory) New(m *plugins.Manager, config interface{}) plugins.Plugin {
return internal.New(m, config.(*internal.Config))
}

// Validate method for AuthZFactory.
func (AuthZFactory) Validate(m *plugins.Manager, configBytes []byte) (interface{}, error) {
// Validate returns a valid configuration to instantiate the plugin.
func (Factory) Validate(m *plugins.Manager, configBytes []byte) (interface{}, error) {
return internal.Validate(m, configBytes)
}

// New method for ExtProcFactory.
// New returns the object initialized with a valid plugin configuration.
func (ExtProcFactory) New(m *plugins.Manager, config interface{}) plugins.Plugin {
return internal.NewExtProc(m, config.(*internal.Config))
}

// Validate method for ExtProcFactory.
// Validate returns a valid configuration to instantiate the plugin.
func (ExtProcFactory) Validate(m *plugins.Manager, configBytes []byte) (interface{}, error) {
return internal.Validate(m, configBytes)
}

0 comments on commit c0220b7

Please sign in to comment.