build(deps): bump github.com/open-policy-agent/opa from 0.51.0 to 0.59.0
Bumps [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) from 0.51.0 to 0.59.0. - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v0.51.0...v0.59.0) --- updated-dependencies: - dependency-name: github.com/open-policy-agent/opa dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
a6a6c22c14
commit
1f069c7c00
+40
@@ -0,0 +1,40 @@
|
||||
// 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 (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var pluginMtx sync.Mutex
|
||||
var bundleExtensions map[string]Handler
|
||||
|
||||
// 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 func([]byte, any) error
|
||||
|
||||
// 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) {
|
||||
pluginMtx.Lock()
|
||||
defer pluginMtx.Unlock()
|
||||
|
||||
if bundleExtensions == nil {
|
||||
bundleExtensions = map[string]Handler{}
|
||||
}
|
||||
bundleExtensions[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 {
|
||||
pluginMtx.Lock()
|
||||
defer pluginMtx.Unlock()
|
||||
return bundleExtensions[ext]
|
||||
}
|
||||
+69
-34
@@ -8,15 +8,17 @@ package loader
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/open-policy-agent/opa/ast"
|
||||
astJSON "github.com/open-policy-agent/opa/ast/json"
|
||||
"github.com/open-policy-agent/opa/bundle"
|
||||
fileurl "github.com/open-policy-agent/opa/internal/file/url"
|
||||
"github.com/open-policy-agent/opa/internal/merge"
|
||||
@@ -91,6 +93,7 @@ type FileLoader interface {
|
||||
All(paths []string) (*Result, error)
|
||||
Filtered(paths []string, filter Filter) (*Result, error)
|
||||
AsBundle(path string) (*bundle.Bundle, error)
|
||||
WithReader(io.Reader) FileLoader
|
||||
WithFS(fs.FS) FileLoader
|
||||
WithMetrics(metrics.Metrics) FileLoader
|
||||
WithFilter(Filter) FileLoader
|
||||
@@ -98,6 +101,9 @@ type FileLoader interface {
|
||||
WithSkipBundleVerification(bool) FileLoader
|
||||
WithProcessAnnotation(bool) FileLoader
|
||||
WithCapabilities(*ast.Capabilities) FileLoader
|
||||
WithJSONOptions(*astJSON.Options) FileLoader
|
||||
|
||||
WithRegoV1Compatible(bool) FileLoader
|
||||
}
|
||||
|
||||
// NewFileLoader returns a new FileLoader instance.
|
||||
@@ -116,6 +122,7 @@ type fileLoader struct {
|
||||
files map[string]bundle.FileInfo
|
||||
opts ast.ParserOptions
|
||||
fsys fs.FS
|
||||
reader io.Reader
|
||||
}
|
||||
|
||||
// WithFS provides an fs.FS to use for loading files. You can pass nil to
|
||||
@@ -126,6 +133,14 @@ func (fl *fileLoader) WithFS(fsys fs.FS) FileLoader {
|
||||
return fl
|
||||
}
|
||||
|
||||
// WithReader provides an io.Reader to use for loading the bundle tarball.
|
||||
// An io.Reader passed via WithReader takes precedence over an fs.FS passed
|
||||
// via WithFS.
|
||||
func (fl *fileLoader) WithReader(rdr io.Reader) FileLoader {
|
||||
fl.reader = rdr
|
||||
return fl
|
||||
}
|
||||
|
||||
// WithMetrics provides the metrics instance to use while loading
|
||||
func (fl *fileLoader) WithMetrics(m metrics.Metrics) FileLoader {
|
||||
fl.metrics = m
|
||||
@@ -162,6 +177,17 @@ func (fl *fileLoader) WithCapabilities(caps *ast.Capabilities) FileLoader {
|
||||
return fl
|
||||
}
|
||||
|
||||
// WithJSONOptions sets the JSONOptions for use when parsing files
|
||||
func (fl *fileLoader) WithJSONOptions(opts *astJSON.Options) FileLoader {
|
||||
fl.opts.JSONOptions = opts
|
||||
return fl
|
||||
}
|
||||
|
||||
func (fl *fileLoader) WithRegoV1Compatible(compatible bool) FileLoader {
|
||||
fl.opts.RegoV1Compatible = compatible
|
||||
return fl
|
||||
}
|
||||
|
||||
// All returns a Result object loaded (recursively) from the specified paths.
|
||||
func (fl fileLoader) All(paths []string) (*Result, error) {
|
||||
return fl.Filtered(paths, nil)
|
||||
@@ -212,7 +238,14 @@ func (fl fileLoader) AsBundle(path string) (*bundle.Bundle, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bundleLoader, isDir, err := GetBundleDirectoryLoaderWithFilter(path, fl.filter)
|
||||
|
||||
var bundleLoader bundle.DirectoryLoader
|
||||
var isDir bool
|
||||
if fl.reader != nil {
|
||||
bundleLoader = bundle.NewTarballLoaderWithBaseURL(fl.reader, path).WithFilter(fl.filter)
|
||||
} else {
|
||||
bundleLoader, isDir, err = GetBundleDirectoryLoaderFS(fl.fsys, path, fl.filter)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -222,7 +255,8 @@ func (fl fileLoader) AsBundle(path string) (*bundle.Bundle, error) {
|
||||
WithBundleVerificationConfig(fl.bvc).
|
||||
WithSkipBundleVerification(fl.skipVerify).
|
||||
WithProcessAnnotations(fl.opts.ProcessAnnotation).
|
||||
WithCapabilities(fl.opts.Capabilities)
|
||||
WithCapabilities(fl.opts.Capabilities).
|
||||
WithJSONOptions(fl.opts.JSONOptions)
|
||||
|
||||
// For bundle directories add the full path in front of module file names
|
||||
// to simplify debugging.
|
||||
@@ -239,55 +273,57 @@ func (fl fileLoader) AsBundle(path string) (*bundle.Bundle, error) {
|
||||
}
|
||||
|
||||
// GetBundleDirectoryLoader returns a bundle directory loader which can be used to load
|
||||
// files in the directory.
|
||||
// files in the directory
|
||||
func GetBundleDirectoryLoader(path string) (bundle.DirectoryLoader, bool, error) {
|
||||
path, err := fileurl.Clean(path)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("error reading %q: %s", path, err)
|
||||
}
|
||||
|
||||
var bundleLoader bundle.DirectoryLoader
|
||||
|
||||
if fi.IsDir() {
|
||||
bundleLoader = bundle.NewDirectoryLoader(path)
|
||||
} else {
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
bundleLoader = bundle.NewTarballLoaderWithBaseURL(fh, path)
|
||||
}
|
||||
return bundleLoader, fi.IsDir(), nil
|
||||
return GetBundleDirectoryLoaderFS(nil, path, nil)
|
||||
}
|
||||
|
||||
// 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 GetBundleDirectoryLoaderFS(nil, 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) {
|
||||
path, err := fileurl.Clean(path)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
fi, err := os.Stat(path)
|
||||
var fi fs.FileInfo
|
||||
if fsys != nil {
|
||||
fi, err = fs.Stat(fsys, path)
|
||||
} else {
|
||||
fi, err = os.Stat(path)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("error reading %q: %s", path, err)
|
||||
}
|
||||
|
||||
var bundleLoader bundle.DirectoryLoader
|
||||
|
||||
if fi.IsDir() {
|
||||
bundleLoader = bundle.NewDirectoryLoader(path).WithFilter(filter)
|
||||
if fsys != nil {
|
||||
bundleLoader = bundle.NewFSLoaderWithRoot(fsys, path)
|
||||
} else {
|
||||
bundleLoader = bundle.NewDirectoryLoader(path)
|
||||
}
|
||||
} else {
|
||||
fh, err := os.Open(path)
|
||||
var fh fs.File
|
||||
if fsys != nil {
|
||||
fh, err = fsys.Open(path)
|
||||
} else {
|
||||
fh, err = os.Open(path)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
bundleLoader = bundle.NewTarballLoaderWithBaseURL(fh, path).WithFilter(filter)
|
||||
bundleLoader = bundle.NewTarballLoaderWithBaseURL(fh, path)
|
||||
}
|
||||
|
||||
if filter != nil {
|
||||
bundleLoader = bundleLoader.WithFilter(filter)
|
||||
}
|
||||
return bundleLoader, fi.IsDir(), nil
|
||||
}
|
||||
@@ -721,11 +757,10 @@ func loadRego(path string, bs []byte, m metrics.Metrics, opts ast.ParserOptions)
|
||||
|
||||
func loadJSON(path string, bs []byte, m metrics.Metrics) (interface{}, error) {
|
||||
m.Timer(metrics.RegoDataParse).Start()
|
||||
buf := bytes.NewBuffer(bs)
|
||||
decoder := util.NewJSONDecoder(buf)
|
||||
var x interface{}
|
||||
err := decoder.Decode(&x)
|
||||
err := util.UnmarshalJSON(bs, &x)
|
||||
m.Timer(metrics.RegoDataParse).Stop()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user