Skip to content
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

feat: allow to load modules dynamically #18

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 37 additions & 3 deletions internal/policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,28 @@ import (
"github.com/reposaur/reposaur/pkg/output"
)

type Option func(*Engine)
type EngineOption func(*Engine)

type Engine struct {
modules map[string]*ast.Module
compiler *ast.Compiler
enableTracing bool
}

func Load(ctx context.Context, policyPaths []string, opts ...Option) (*Engine, error) {
func NewEngine(opts ...EngineOption) *Engine {
engine := &Engine{
modules: map[string]*ast.Module{},
compiler: ast.NewCompiler().WithEnablePrintStatements(true),
}

for _, opt := range opts {
opt(engine)
}

return engine
}

func NewEngineWithPolicies(ctx context.Context, policyPaths []string, opts ...EngineOption) (*Engine, error) {
policies, err := loader.NewFileLoader().
WithProcessAnnotation(true).
Filtered(policyPaths, isRegoFile)
Expand Down Expand Up @@ -57,7 +70,7 @@ func Load(ctx context.Context, policyPaths []string, opts ...Option) (*Engine, e

// WithTracingEnabled enables or disables policy
// execution tracing.
func WithTracingEnabled(enabled bool) Option {
func WithTracingEnabled(enabled bool) EngineOption {
return func(e *Engine) {
e.enableTracing = enabled
}
Expand Down Expand Up @@ -90,6 +103,27 @@ func (e *Engine) Modules() map[string]*ast.Module {
return e.modules
}

func (e *Engine) LoadPolicy(filename, policy string) error {
wtfiscrq marked this conversation as resolved.
Show resolved Hide resolved
module, err := ast.ParseModuleWithOpts(filename, policy, ast.ParserOptions{
ProcessAnnotation: true,
})
if err != nil {
return err
}

e.compiler.Compile(map[string]*ast.Module{
filename: module,
})

if e.compiler.Failed() {
return fmt.Errorf("compiler: %w", e.compiler.Errors)
}

e.modules[filename] = module

return nil
}

func (e *Engine) Check(ctx context.Context, namespace string, input interface{}) (output.Report, error) {
report, err := e.check(ctx, namespace, input)
if err != nil {
Expand Down
18 changes: 12 additions & 6 deletions pkg/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Reposaur struct {
//
// The default HTTP client will use the default host `api.github.com`. Can
// be customized using the `GITHUB_HOST` or `GH_HOST` environment variables.
func New(ctx context.Context, policyPaths []string, opts ...Option) (*Reposaur, error) {
sdk := &Reposaur{
func New(ctx context.Context, policyPaths []string, opts ...Option) (sdk *Reposaur, err error) {
sdk = &Reposaur{
logger: zerolog.New(os.Stderr),
}

Expand All @@ -66,11 +66,17 @@ func New(ctx context.Context, policyPaths []string, opts ...Option) (*Reposaur,
// to avoid unexpected side-effects by clients
builtins.RegisterBuiltins(sdk.httpClient)

var err error
engineOpts := []policy.EngineOption{
policy.WithTracingEnabled(sdk.enableTracing),
}

sdk.engine, err = policy.Load(ctx, policyPaths, policy.WithTracingEnabled(sdk.enableTracing))
if err != nil {
return nil, err
if len(policyPaths) == 0 {
sdk.engine = policy.NewEngine(engineOpts...)
} else {
sdk.engine, err = policy.NewEngineWithPolicies(ctx, policyPaths, engineOpts...)
if err != nil {
return nil, err
}
}

return sdk, nil
Expand Down