mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
v1 API
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package. Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations. Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax. Signed-off-by: Johan Fylling <johan.dev@fylling.se>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
// Copyright 2024 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended.
|
||||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead.
|
||||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information.
|
||||
package loader
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
v1 "github.com/open-policy-agent/opa/v1/loader"
|
||||
)
|
||||
|
||||
// Errors is a wrapper for multiple loader errors.
|
||||
type Errors = v1.Errors
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2024 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended.
|
||||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead.
|
||||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information.
|
||||
package extension
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2023 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package extension
|
||||
|
||||
import (
|
||||
v1 "github.com/open-policy-agent/opa/v1/loader/extension"
|
||||
)
|
||||
|
||||
// Handler is used to unmarshal a byte slice of a registered extension
|
||||
// EXPERIMENTAL: Please don't rely on this functionality, it may go
|
||||
// away or change in the future.
|
||||
type Handler = v1.Handler
|
||||
|
||||
// RegisterExtension registers a Handler for a certain file extension, including
|
||||
// the dot: ".json", not "json".
|
||||
// EXPERIMENTAL: Please don't rely on this functionality, it may go
|
||||
// away or change in the future.
|
||||
func RegisterExtension(name string, handler Handler) {
|
||||
v1.RegisterExtension(name, handler)
|
||||
}
|
||||
|
||||
// FindExtension ios used to look up a registered extension Handler
|
||||
// EXPERIMENTAL: Please don't rely on this functionality, it may go
|
||||
// away or change in the future.
|
||||
func FindExtension(ext string) Handler {
|
||||
return v1.FindExtension(ext)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2024 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Deprecated: This package is intended for older projects transitioning from OPA v0.x and will remain for the lifetime of OPA v1.x, but its use is not recommended.
|
||||
// For newer features and behaviours, such as defaulting to the Rego v1 syntax, use the corresponding components in the [github.com/open-policy-agent/opa/v1] package instead.
|
||||
// See https://www.openpolicyagent.org/docs/latest/v0-compatibility/ for more information.
|
||||
package filter
|
||||
@@ -0,0 +1,5 @@
|
||||
package filter
|
||||
|
||||
import v1 "github.com/open-policy-agent/opa/v1/loader/filter"
|
||||
|
||||
type LoaderFilter = v1.LoaderFilter
|
||||
@@ -0,0 +1,145 @@
|
||||
// Copyright 2017 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package loader contains utilities for loading files into OPA.
|
||||
package loader
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/open-policy-agent/opa/ast"
|
||||
"github.com/open-policy-agent/opa/bundle"
|
||||
v1 "github.com/open-policy-agent/opa/v1/loader"
|
||||
)
|
||||
|
||||
// Result represents the result of successfully loading zero or more files.
|
||||
type Result = v1.Result
|
||||
|
||||
// RegoFile represents the result of loading a single Rego source file.
|
||||
type RegoFile = v1.RegoFile
|
||||
|
||||
// Filter defines the interface for filtering files during loading. If the
|
||||
// filter returns true, the file should be excluded from the result.
|
||||
type Filter = v1.Filter
|
||||
|
||||
// GlobExcludeName excludes files and directories whose names do not match the
|
||||
// shell style pattern at minDepth or greater.
|
||||
func GlobExcludeName(pattern string, minDepth int) Filter {
|
||||
return v1.GlobExcludeName(pattern, minDepth)
|
||||
}
|
||||
|
||||
// FileLoader defines an interface for loading OPA data files
|
||||
// and Rego policies.
|
||||
type FileLoader = v1.FileLoader
|
||||
|
||||
// NewFileLoader returns a new FileLoader instance.
|
||||
func NewFileLoader() FileLoader {
|
||||
return v1.NewFileLoader().WithRegoVersion(ast.DefaultRegoVersion)
|
||||
}
|
||||
|
||||
// GetBundleDirectoryLoader returns a bundle directory loader which can be used to load
|
||||
// files in the directory
|
||||
func GetBundleDirectoryLoader(path string) (bundle.DirectoryLoader, bool, error) {
|
||||
return v1.GetBundleDirectoryLoader(path)
|
||||
}
|
||||
|
||||
// GetBundleDirectoryLoaderWithFilter returns a bundle directory loader which can be used to load
|
||||
// files in the directory after applying the given filter.
|
||||
func GetBundleDirectoryLoaderWithFilter(path string, filter Filter) (bundle.DirectoryLoader, bool, error) {
|
||||
return v1.GetBundleDirectoryLoaderWithFilter(path, filter)
|
||||
}
|
||||
|
||||
// GetBundleDirectoryLoaderFS returns a bundle directory loader which can be used to load
|
||||
// files in the directory.
|
||||
func GetBundleDirectoryLoaderFS(fsys fs.FS, path string, filter Filter) (bundle.DirectoryLoader, bool, error) {
|
||||
return v1.GetBundleDirectoryLoaderFS(fsys, path, filter)
|
||||
}
|
||||
|
||||
// FilteredPaths is the same as FilterPathsFS using the current diretory file
|
||||
// system
|
||||
func FilteredPaths(paths []string, filter Filter) ([]string, error) {
|
||||
return v1.FilteredPaths(paths, filter)
|
||||
}
|
||||
|
||||
// FilteredPathsFS return a list of files from the specified
|
||||
// paths while applying the given filters. If any filter returns true, the
|
||||
// file/directory is excluded.
|
||||
func FilteredPathsFS(fsys fs.FS, paths []string, filter Filter) ([]string, error) {
|
||||
return v1.FilteredPathsFS(fsys, paths, filter)
|
||||
}
|
||||
|
||||
// Schemas loads a schema set from the specified file path.
|
||||
func Schemas(schemaPath string) (*ast.SchemaSet, error) {
|
||||
return v1.Schemas(schemaPath)
|
||||
}
|
||||
|
||||
// All returns a Result object loaded (recursively) from the specified paths.
|
||||
// Deprecated: Use FileLoader.Filtered() instead.
|
||||
func All(paths []string) (*Result, error) {
|
||||
return NewFileLoader().Filtered(paths, nil)
|
||||
}
|
||||
|
||||
// Filtered returns a Result object loaded (recursively) from the specified
|
||||
// paths while applying the given filters. If any filter returns true, the
|
||||
// file/directory is excluded.
|
||||
// Deprecated: Use FileLoader.Filtered() instead.
|
||||
func Filtered(paths []string, filter Filter) (*Result, error) {
|
||||
return NewFileLoader().Filtered(paths, filter)
|
||||
}
|
||||
|
||||
// AsBundle loads a path as a bundle. If it is a single file
|
||||
// it will be treated as a normal tarball bundle. If a directory
|
||||
// is supplied it will be loaded as an unzipped bundle tree.
|
||||
// Deprecated: Use FileLoader.AsBundle() instead.
|
||||
func AsBundle(path string) (*bundle.Bundle, error) {
|
||||
return NewFileLoader().AsBundle(path)
|
||||
}
|
||||
|
||||
// AllRegos returns a Result object loaded (recursively) with all Rego source
|
||||
// files from the specified paths.
|
||||
func AllRegos(paths []string) (*Result, error) {
|
||||
return NewFileLoader().Filtered(paths, func(_ string, info os.FileInfo, _ int) bool {
|
||||
return !info.IsDir() && !strings.HasSuffix(info.Name(), bundle.RegoExt)
|
||||
})
|
||||
}
|
||||
|
||||
// Rego is deprecated. Use RegoWithOpts instead.
|
||||
func Rego(path string) (*RegoFile, error) {
|
||||
return RegoWithOpts(path, ast.ParserOptions{})
|
||||
}
|
||||
|
||||
// RegoWithOpts returns a RegoFile object loaded from the given path.
|
||||
func RegoWithOpts(path string, opts ast.ParserOptions) (*RegoFile, error) {
|
||||
if opts.RegoVersion == ast.RegoUndefined {
|
||||
opts.RegoVersion = ast.DefaultRegoVersion
|
||||
}
|
||||
|
||||
return v1.RegoWithOpts(path, opts)
|
||||
}
|
||||
|
||||
// CleanPath returns the normalized version of a path that can be used as an identifier.
|
||||
func CleanPath(path string) string {
|
||||
return v1.CleanPath(path)
|
||||
}
|
||||
|
||||
// Paths returns a sorted list of files contained at path. If recurse is true
|
||||
// and path is a directory, then Paths will walk the directory structure
|
||||
// recursively and list files at each level.
|
||||
func Paths(path string, recurse bool) (paths []string, err error) {
|
||||
return v1.Paths(path, recurse)
|
||||
}
|
||||
|
||||
// Dirs resolves filepaths to directories. It will return a list of unique
|
||||
// directories.
|
||||
func Dirs(paths []string) []string {
|
||||
return v1.Dirs(paths)
|
||||
}
|
||||
|
||||
// SplitPrefix returns a tuple specifying the document prefix and the file
|
||||
// path.
|
||||
func SplitPrefix(path string) ([]string, string) {
|
||||
return v1.SplitPrefix(path)
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
// Copyright 2024 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/open-policy-agent/opa/ast"
|
||||
"github.com/open-policy-agent/opa/util/test"
|
||||
)
|
||||
|
||||
func TestAll_DefaultRegoVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
note string
|
||||
module string
|
||||
expErrs []string
|
||||
}{
|
||||
{
|
||||
note: "v0", // v0 is the default rego-version
|
||||
module: `package test
|
||||
|
||||
p[x] {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "rego.v1 import",
|
||||
module: `package test
|
||||
import rego.v1
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "v1",
|
||||
module: `package test
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
expErrs: []string{
|
||||
"test.rego:3: rego_parse_error: var cannot be used for rule name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.note, func(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/test.rego": tc.module}
|
||||
|
||||
test.WithTempFS(files, func(rootDir string) {
|
||||
moduleFile := filepath.Join(rootDir, "test.rego")
|
||||
loaded, err := All([]string{moduleFile})
|
||||
|
||||
if len(tc.expErrs) > 0 {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected errors but got nil")
|
||||
}
|
||||
|
||||
for _, expErr := range tc.expErrs {
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/test.rego"])
|
||||
if !expected.Equal(loaded.Modules[CleanPath(moduleFile)].Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Modules[moduleFile])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFiltered_DefaultRegoVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
note string
|
||||
module string
|
||||
expErrs []string
|
||||
}{
|
||||
{
|
||||
note: "v0", // v0 is the default rego-version
|
||||
module: `package test
|
||||
|
||||
p[x] {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "rego.v1 import",
|
||||
module: `package test
|
||||
import rego.v1
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "v1",
|
||||
module: `package test
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
expErrs: []string{
|
||||
"test.rego:3: rego_parse_error: var cannot be used for rule name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.note, func(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/test.rego": tc.module}
|
||||
|
||||
test.WithTempFS(files, func(rootDir string) {
|
||||
moduleFile := filepath.Join(rootDir, "test.rego")
|
||||
filter := func(string, os.FileInfo, int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
loaded, err := Filtered([]string{moduleFile}, filter)
|
||||
|
||||
if len(tc.expErrs) > 0 {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected errors but got nil")
|
||||
}
|
||||
|
||||
for _, expErr := range tc.expErrs {
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/test.rego"])
|
||||
if !expected.Equal(loaded.Modules[CleanPath(moduleFile)].Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Modules[moduleFile])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRego_DefaultRegoVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
note string
|
||||
module string
|
||||
expErrs []string
|
||||
}{
|
||||
{
|
||||
note: "v0", // v0 is the default rego-version
|
||||
module: `package test
|
||||
|
||||
p[x] {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "rego.v1 import",
|
||||
module: `package test
|
||||
import rego.v1
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "v1",
|
||||
module: `package test
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
expErrs: []string{
|
||||
"test.rego:3: rego_parse_error: var cannot be used for rule name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.note, func(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/test.rego": tc.module}
|
||||
|
||||
test.WithTempFS(files, func(rootDir string) {
|
||||
moduleFile := filepath.Join(rootDir, "test.rego")
|
||||
loaded, err := Rego(moduleFile)
|
||||
|
||||
if len(tc.expErrs) > 0 {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected errors but got nil")
|
||||
}
|
||||
|
||||
for _, expErr := range tc.expErrs {
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/test.rego"])
|
||||
if !expected.Equal(loaded.Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Parsed)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllRegos_DefaultRegoVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
note string
|
||||
module string
|
||||
expErrs []string
|
||||
}{
|
||||
{
|
||||
note: "v0", // v0 is the default rego-version
|
||||
module: `package test
|
||||
|
||||
p[x] {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "rego.v1 import",
|
||||
module: `package test
|
||||
import rego.v1
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "v1",
|
||||
module: `package test
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
expErrs: []string{
|
||||
"test.rego:3: rego_parse_error: var cannot be used for rule name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.note, func(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/test.rego": tc.module}
|
||||
|
||||
test.WithTempFS(files, func(rootDir string) {
|
||||
moduleFile := filepath.Join(rootDir, "test.rego")
|
||||
loaded, err := AllRegos([]string{moduleFile})
|
||||
|
||||
if len(tc.expErrs) > 0 {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected errors but got nil")
|
||||
}
|
||||
|
||||
for _, expErr := range tc.expErrs {
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/test.rego"])
|
||||
if !expected.Equal(loaded.Modules[CleanPath(moduleFile)].Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Modules[moduleFile])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRego_DefaultRegoVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
note string
|
||||
module string
|
||||
expErrs []string
|
||||
}{
|
||||
{
|
||||
note: "v0", // v0 is the default rego-version
|
||||
module: `package test
|
||||
|
||||
p[x] {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "rego.v1 import",
|
||||
module: `package test
|
||||
import rego.v1
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
},
|
||||
{
|
||||
note: "v1",
|
||||
module: `package test
|
||||
|
||||
p contains x if {
|
||||
x := "a"
|
||||
}`,
|
||||
expErrs: []string{
|
||||
"test.rego:3: rego_parse_error: var cannot be used for rule name",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.note, func(t *testing.T) {
|
||||
files := map[string]string{
|
||||
"/test.rego": tc.module}
|
||||
|
||||
test.WithTempFS(files, func(rootDir string) {
|
||||
moduleFile := filepath.Join(rootDir, "test.rego")
|
||||
loaded, err := NewFileLoader().All([]string{moduleFile})
|
||||
|
||||
if len(tc.expErrs) > 0 {
|
||||
if err == nil {
|
||||
t.Fatalf("Expected errors but got nil")
|
||||
}
|
||||
|
||||
for _, expErr := range tc.expErrs {
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("Expected error to contain:\n\n%s\n\nbut got:\n\n%s", expErr, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/test.rego"])
|
||||
if !expected.Equal(loaded.Modules[CleanPath(moduleFile)].Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Modules[moduleFile])
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user