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 ¶
- Variables
- type Errors
- type FilePath
- type Module
- type PackagePrefixer
- type PackageTransformer
- type RegoRewriter
- func (r *RegoRewriter) AddBaseFromFs(path string) error
- func (r *RegoRewriter) AddEntryPoint(path string, m *ast.Module) error
- func (r *RegoRewriter) AddEntryPointModule(path string, m *ast.Module)
- func (r *RegoRewriter) AddLib(path string, m *ast.Module) error
- func (r *RegoRewriter) AddLibFromFs(path string) error
- func (r *RegoRewriter) Rewrite() (*Sources, error)
- type Sources
- type TestData
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
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.
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) IsTestFile ¶
IsTestFile returns true if the module corresponds to a unit test.
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.
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:
pt - 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 string, m *ast.Module) 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 string, m *ast.Module) 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) ForEachModule ¶
ForEachModule applies fn to each EntryPoint and Lib.