MCPcopy Create free account
hub / github.com/docker/buildx / newPolicyModuleLoader

Function newPolicyModuleLoader

policy/tester.go:779–843  ·  view source on GitHub ↗
(fsProvider func() (fs.StatFS, func() error, error))

Source from the content-addressed store, hash-verified

777}
778
779func newPolicyModuleLoader(fsProvider func() (fs.StatFS, func() error, error)) (func(map[string]*ast.Module) (map[string]*ast.Module, error), func() error) {
780 var (
781 root fs.StatFS
782 closeFS func() error
783 )
784 loader := func(resolved map[string]*ast.Module) (map[string]*ast.Module, error) {
785 out := make(map[string]*ast.Module)
786 for k, v := range resolved {
787 for _, imp := range v.Imports {
788 pv := imp.Path.Value.String()
789 pkgPath, ok := strings.CutPrefix(pv, "data.")
790 if !ok {
791 continue
792 }
793 if resolvedHasPackage(resolved, pkgPath) {
794 continue
795 }
796 fn := strings.ReplaceAll(pkgPath, ".", "/") + ".rego"
797 if _, ok := resolved[fn]; ok {
798 continue
799 }
800 if root == nil {
801 if fsProvider == nil {
802 return nil, errors.Errorf("no policy FS defined for import %s", pv)
803 }
804 f, cf, err := fsProvider()
805 if err != nil {
806 return nil, errors.Wrapf(err, "failed to get policy FS for import %s", pv)
807 }
808 root = f
809 closeFS = cf
810 }
811 loadName := fn
812 if _, err := root.Stat(loadName); err != nil {
813 return nil, errors.Wrapf(err, "import %s not found for module %s", pv, k)
814 }
815 dt, err := fs.ReadFile(root, loadName)
816 if err != nil {
817 return nil, errors.Wrapf(err, "failed to read imported policy file %s for module %s", loadName, k)
818 }
819 mod, err := ast.ParseModuleWithOpts(loadName, string(dt), ast.ParserOptions{
820 RegoVersion: ast.RegoV1,
821 })
822 if err != nil {
823 return nil, errors.Wrapf(err, "failed to parse imported policy file %s for module %s", loadName, k)
824 }
825 // rewrite package to be less strict
826 pkgParts := strings.Split(pkgPath, ".")
827 ref := ast.Ref{mod.Package.Path[0]}
828 for _, p := range pkgParts {
829 ref = append(ref, ast.StringTerm(p))
830 }
831 mod.Package = &ast.Package{Path: ref}
832 out[fn] = mod
833 }
834 }
835 return out, nil
836 }

Callers 1

compilePolicyModulesFunction · 0.85

Calls 3

resolvedHasPackageFunction · 0.85
ReadFileMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…