regorewriter

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

package regorewriter rewrites import and package refs for a set of rego modules.

Rego modules are divided into two categories: libraries and constraint templates. The libraries will have both package path, imports and data references updated while the constraint templates will only have imports and data references updated.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors []error

Errors is a list of error.

func (Errors) Error

func (errs Errors) Error() string

Error implements error.

func (Errors) Format

func (errs Errors) Format(s fmt.State, verb rune)

Format implements fmt.Formatter to make this play nice with handling stack traces produced from github.com/pkg/errors

type FilePath

type FilePath struct {
	// contains filtered or unexported fields
}

FilePath represents a path on the filesystem and handles reparenting the file relative to a path prefix.

func (*FilePath) Path

func (f *FilePath) Path() string

Path returns the current path value.

func (*FilePath) Reparent

func (f *FilePath) Reparent(old, new string) error

Reparent adjusts the parent from a current path prefix to a new path prefix.

type Module

type Module struct {
	FilePath

	// Module is the rego module produced from the ast parser.
	Module *ast.Module
}

Module represents a rego module

func (*Module) Content

func (m *Module) Content() ([]byte, error)

Content returns the module as a byte slice of rego source code.

func (*Module) IsTestFile

func (m *Module) IsTestFile() bool

IsTestFile returns true if the module corresponds to a unit test.

func (*Module) Write

func (m *Module) Write() error

Write writes the module to the path specified in FilePath.

type PackagePrefixer

type PackagePrefixer struct {
	// contains filtered or unexported fields
}

PackagePrefixer is an implementation of PackageTransformer that prepends a prefix to the package path after the 'data' reference, for example, if prefix is specified as ["x", "y", "z"], then the path "data.whatever.checkpolicy" will be updated to "data.x.y.z.whatever.checkpolicy".

func NewPackagePrefixer

func NewPackagePrefixer(pkgPrefix string) *PackagePrefixer

NewPackagePrefixer returns a new PackagePrefixer

func (*PackagePrefixer) Transform

func (p *PackagePrefixer) Transform(ref ast.Ref) ast.Ref

Transform implements PackageTransformer

type PackageTransformer

type PackageTransformer interface {
	// Transform returns a modified ast.Ref with an updated package path.
	Transform(ref ast.Ref) ast.Ref
}

PackageTransformer takes a package path and transforms it to the new package path it will be re-written to.

type RegoRewriter

type RegoRewriter struct {
	// contains filtered or unexported fields
}

RegoRewriter rewrites rego code by updating library package paths by prepending a prefix and updating references to library code accordingly.

func New

func New(pt PackageTransformer, libs []string, externs []string) (*RegoRewriter, error)

New returns a new RegoRewriter args:

it - the PackageTransformer that will be used for updating the path
libs - a list of package prefixes that are allowed for library use
externs - a list of packages that the rego is allowed to reference but not declared in any libs

func (*RegoRewriter) AddBaseFromFs

func (r *RegoRewriter) AddBaseFromFs(path string) error

AddBaseFromFs adds a base source which will not have it's package path rewritten. These correspond to the rego that will be populated into a ConstraintTemplate with the 'violation' rule.

func (*RegoRewriter) AddEntryPoint

func (r *RegoRewriter) AddEntryPoint(path, src string) error

AddEntryPoint adds a base source which will not have it's package path rewritten. These correspond to the rego that will be populated into a ConstraintTemplate with the 'violation' rule.

func (*RegoRewriter) AddEntryPointModule

func (r *RegoRewriter) AddEntryPointModule(path string, m *ast.Module)

func (*RegoRewriter) AddLib

func (r *RegoRewriter) AddLib(path, src string) error

AddLib adds a library source which will have the package path updated.

func (*RegoRewriter) AddLibFromFs

func (r *RegoRewriter) AddLibFromFs(path string) error

AddLibFromFs adds a library source which will have the package path updated.

func (*RegoRewriter) Rewrite

func (r *RegoRewriter) Rewrite() (*Sources, error)

Rewrite will check the input source and update the package paths and refs as appropriate.

type Sources

type Sources struct {
	// EntryPoints are the sources which contain the constraint template violation rule.
	EntryPoints []*Module
	// Libs are libraries imported by the entry point.
	Libs []*Module
	// TestData is any json/yaml that was loaded from the filesystem (FS operation only) and will
	// be copied to the destination unmodified for testing that the re-written sources pass unit tests.
	TestData []*TestData
}

Sources represents all modules that have been fed into the

func (*Sources) AsMap

func (s *Sources) AsMap() (map[string]string, error)

AsMap returns a map of path to content as represented in Module.

func (*Sources) ForEachModule

func (s *Sources) ForEachModule(fn func(m *Module) error) error

ForEachModule applys fn to each EntryPoint and Lib

func (*Sources) Reparent

func (s *Sources) Reparent(old, new string) error

Reparent will reparent the sources from the root specified at old to a root specified at new.

func (*Sources) Write

func (s *Sources) Write() error

Write will write the sources to the filesystem.

type TestData

type TestData struct {
	FilePath
	// contains filtered or unexported fields
}

TestData represents a json or yaml data file used in unit tests.

func (*TestData) Content

func (t *TestData) Content() ([]byte, error)

Content implements sourceFile

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL