Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundle: Add info about the correct rego version to parse modules on the store #7278

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions v1/bundle/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func activateBundles(opts *ActivateOpts) error {
remainingAndExtra[name] = mod
}

_, err = compileModules(opts.Compiler, opts.Metrics, snapshotBundles, remainingAndExtra, opts.legacy, opts.AuthorizationDecisionRef)
err = compileModules(opts.Compiler, opts.Metrics, snapshotBundles, remainingAndExtra, opts.legacy, opts.AuthorizationDecisionRef)
if err != nil {
return err
}
Expand Down Expand Up @@ -698,26 +698,35 @@ type moduleInfo struct {
}

func readModuleInfoFromStore(ctx context.Context, store storage.Store, txn storage.Transaction) (map[string]moduleInfo, error) {
versions := map[string]moduleInfo{}

value, err := read(ctx, store, txn, ModulesInfoBasePath)
if suppressNotFound(err) != nil {
return nil, err
}

if value != nil {
bs, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("corrupt rego version")
}
if value == nil {
return nil, nil
}

err = util.UnmarshalJSON(bs, &versions)
if err != nil {
return nil, fmt.Errorf("corrupt rego version")
if m, ok := value.(map[string]any); ok {
versions := make(map[string]moduleInfo, len(m))

for k, v := range m {
if m0, ok := v.(map[string]any); ok {
if ver, ok := m0["rego_version"]; ok {
if vs, ok := ver.(json.Number); ok { // float64?
johanfylling marked this conversation as resolved.
Show resolved Hide resolved
if i, err := vs.Int64(); err != nil {
return nil, fmt.Errorf("corrupt rego version")
} else {
versions[k] = moduleInfo{RegoVersion: ast.RegoVersionFromInt(int(i))}
}
}
}
}
}
return versions, nil
}

return versions, nil
return nil, fmt.Errorf("corrupt rego version")
}

func erasePolicies(ctx context.Context, store storage.Store, txn storage.Transaction, parserOpts ast.ParserOptions, roots map[string]struct{}) (map[string]*ast.Module, []string, error) {
Expand Down Expand Up @@ -835,7 +844,7 @@ func writeDataAndModules(ctx context.Context, store storage.Store, txn storage.T
}

if regoVersion, err := b.RegoVersionForFile(mf.Path, ast.RegoUndefined); err == nil && regoVersion != ast.RegoUndefined {
if err := write(ctx, store, txn, moduleRegoVersionPath(path), regoVersion); err != nil {
if err := write(ctx, store, txn, moduleRegoVersionPath(path), regoVersion.Int()); err != nil {
return fmt.Errorf("failed to write rego version for '%s' in bundle '%s': %w", mf.Path, name, err)
}
}
Expand All @@ -859,7 +868,7 @@ func writeDataAndModules(ctx context.Context, store storage.Store, txn storage.T
// 'f.module.Path' contains the module's path as it relates to the bundle root, and can be used for looking up the rego-version.
// 'f.Path' can differ, based on how the bundle reader was initialized.
if regoVersion, err := b.RegoVersionForFile(f.module.Path, ast.RegoUndefined); err == nil && regoVersion != ast.RegoUndefined {
if err := write(ctx, store, txn, moduleRegoVersionPath(p.String()), regoVersion); err != nil {
if err := write(ctx, store, txn, moduleRegoVersionPath(p.String()), regoVersion.Int()); err != nil {
return fmt.Errorf("failed to write rego version for '%s' in bundle '%s': %w", f.Path, name, err)
}
}
Expand Down Expand Up @@ -892,7 +901,7 @@ func writeData(ctx context.Context, store storage.Store, txn storage.Transaction
return nil
}

func compileModules(compiler *ast.Compiler, m metrics.Metrics, bundles map[string]*Bundle, extraModules map[string]*ast.Module, legacy bool, authorizationDecisionRef ast.Ref) (map[string]ast.RegoVersion, error) {
func compileModules(compiler *ast.Compiler, m metrics.Metrics, bundles map[string]*Bundle, extraModules map[string]*ast.Module, legacy bool, authorizationDecisionRef ast.Ref) error {

m.Timer(metrics.RegoModuleCompile).Start()
defer m.Timer(metrics.RegoModuleCompile).Stop()
Expand All @@ -909,38 +918,28 @@ func compileModules(compiler *ast.Compiler, m metrics.Metrics, bundles map[strin
modules[name] = module
}

moduleIDToRegoVersion := map[string]ast.RegoVersion{}

// include all the new bundle modules
for bundleName, b := range bundles {
if legacy {
// FIXME: Do we need to account for legacy mode in moduleIdToRegoVersion?
for _, mf := range b.Modules {
modules[mf.Path] = mf.Parsed
}
} else {
for name, module := range b.ParsedModules(bundleName) {
modules[name] = module

p, err := getFileStoragePath(name)
if err != nil {
return nil, err
}

moduleIDToRegoVersion[strings.TrimLeft(p.String(), "/")] = module.RegoVersion()
}
}
}

if compiler.Compile(modules); compiler.Failed() {
return nil, compiler.Errors
return compiler.Errors
}

if authorizationDecisionRef.Equal(ast.EmptyRef()) {
return moduleIDToRegoVersion, nil
return nil
}

return moduleIDToRegoVersion, iCompiler.VerifyAuthorizationPolicySchema(compiler, authorizationDecisionRef)
return iCompiler.VerifyAuthorizationPolicySchema(compiler, authorizationDecisionRef)
}

func writeModules(ctx context.Context, store storage.Store, txn storage.Transaction, compiler *ast.Compiler, m metrics.Metrics, bundles map[string]*Bundle, extraModules map[string]*ast.Module, legacy bool) error {
Expand Down
Loading
Loading