Skip to content

Commit

Permalink
Exposes the http router to the plugin manager
Browse files Browse the repository at this point in the history
In order to expose the http router to plugins a private router property was added to the plugin manager along with a GetRouter method to access it. A router is also initialized in the NewRuntime function if one is not provided in the runtime params. Fixes #2777

Signed-off-by: Branden Horiuchi <Branden.Horiuchi@blackline.com>
  • Loading branch information
branden-blackline authored and tsandall committed Dec 1, 2021
1 parent 7a93658 commit cfa4c5a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions plugins/plugins.go
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/gorilla/mux"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/config"
Expand Down Expand Up @@ -185,6 +186,7 @@ type Manager struct {
serverInitializedOnce sync.Once
printHook print.Hook
enablePrintStatements bool
router *mux.Router
}

type managerContextKey string
Expand Down Expand Up @@ -336,6 +338,12 @@ func PrintHook(h print.Hook) func(*Manager) {
}
}

func WithRouter(r *mux.Router) func(*Manager) {
return func(m *Manager) {
m.router = r
}
}

// New creates a new Manager using config.
func New(raw []byte, id string, store storage.Store, opts ...func(*Manager)) (*Manager, error) {

Expand Down Expand Up @@ -519,6 +527,13 @@ func (m *Manager) setCompiler(compiler *ast.Compiler) {
m.compiler = compiler
}

// GetRouter returns the managers router if set
func (m *Manager) GetRouter() *mux.Router {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.router
}

// RegisterCompilerTrigger registers for change notifications when the compiler
// is changed.
func (m *Manager) RegisterCompilerTrigger(f func(txn storage.Transaction)) {
Expand Down
7 changes: 6 additions & 1 deletion runtime/runtime.go
Expand Up @@ -296,6 +296,10 @@ func NewRuntime(ctx context.Context, params Params) (*Runtime, error) {
consoleLogger = params.ConsoleLogger
}

if params.Router == nil {
params.Router = mux.NewRouter()
}

manager, err := plugins.New(config,
params.ID,
inmem.New(),
Expand All @@ -307,7 +311,8 @@ func NewRuntime(ctx context.Context, params Params) (*Runtime, error) {
plugins.ConsoleLogger(consoleLogger),
plugins.Logger(logger),
plugins.EnablePrintStatements(logger.GetLevel() >= logging.Info),
plugins.PrintHook(loggingPrintHook{logger: logger}))
plugins.PrintHook(loggingPrintHook{logger: logger}),
plugins.WithRouter(params.Router))
if err != nil {
return nil, errors.Wrap(err, "config error")
}
Expand Down

0 comments on commit cfa4c5a

Please sign in to comment.