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

Conversation

ashutosh-narkar
Copy link
Member

@ashutosh-narkar ashutosh-narkar commented Jan 17, 2025

Fixing an issue where the rego-version for individual modules were lost during bundle deactivation (bundle lifecycle) if this version diverged from the active runtime rego-version. This could cause reloading of v0 bundles to fail when OPA was not running with the v0-compatible flag.

ashutosh-narkar and others added 6 commits January 16, 2025 17:47
…store

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
module, err := ast.ParseModuleWithOpts(id, string(bs), parserOpts)

parserOptsCpy := parserOpts
if regoVersion, ok := getRegoVersion(id); ok {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're doing a store read on every policy id. Is there a performance hit here especially for large bundles? Can we read the entire data once and then use that in the loop? I don't think there would be much performance overhead. Maybe I'm missing something here and we need to structure the data this way. Multiple bundles, maybe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got shifted around a bit when making the tests green, but there should be no issue in moving it back to reading the full thing 👍 .

@@ -766,6 +827,23 @@ func writeDataAndModules(ctx context.Context, store storage.Store, txn storage.T
if err != nil {
return fmt.Errorf("store truncate failed for bundle '%s': %v", name, err)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand why we're doing the store write here? Couldn't we just build a mapping in compileModules? It would be helpful to understand the purpose of the code below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not. The bundle-store/compiler doesn't necessarily agree with the bundle on module naming; if the bundle reader was initialized with a bundle name, it'll get prefixed to the module name during truncation, but if the reader wasn't, the prefix isn't included. This means that, for raw bundles, if the bundle reader wasn't initialized with a bundle name (outside of the store's control), then the modules are compiled with the bundle name as name prefix, but when truncated into the store, they won't have that prefix.
There are a couple of tests that implicitly cause this state (that failed before); I'll add an explicit test.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Copy link

netlify bot commented Jan 20, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit cbdbcd9
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/678f8af159259a0008e02550
😎 Deploy Preview https://deploy-preview-7278--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@johanfylling johanfylling marked this pull request as ready for review January 20, 2025 16:03
@johanfylling johanfylling changed the title WIP: Add info about the correct rego version to parse modules on the store bundle: Add info about the correct rego version to parse modules on the store Jan 20, 2025
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's avoid json roundtrips if we can, though, please 😎

Comment on lines 709 to 717
bs, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("corrupt rego version")
}

err = util.UnmarshalJSON(bs, &versions)
if err != nil {
return nil, fmt.Errorf("corrupt rego version")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to do this roundtripping, exactly? I'd suspect it's a little more annoying, but not harder, to do this via

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 := v["rego_version"]; ok {
        if vs, ok := ver.(int) { // float64?      
          versions[k] = moduleInfo{RegoVersion: fromInt(vs)}
        }
      }
    }
  }
  return versions, nil
}

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

// include all the new bundle modules
for bundleName, b := range bundles {
if legacy {
// FIXME: Do we need to account for legacy mode in moduleIdToRegoVersion?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid this as this has been deprecated for a while now.

@@ -792,7 +892,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) error {
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not using the returned value in the call site, let's not return it.

Copy link
Member Author

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments. The changes lgtm.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

v1/bundle/store.go Outdated Show resolved Hide resolved
johanfylling and others added 5 commits January 21, 2025 10:47
Co-authored-by: Stephan Renatus <stephan@styra.com>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
@johanfylling johanfylling merged commit 5d5329d into open-policy-agent:main Jan 21, 2025
28 checks passed
johanfylling pushed a commit to johanfylling/opa that referenced this pull request Jan 21, 2025
…he store (open-policy-agent#7278)

Fixing an issue where the rego-version for individual modules was lost during bundle deactivation (bundle lifecycle) if this version diverged from the active runtime rego-version. This could cause reloading of v0 bundles to fail when OPA was not running with the `--v0-compatible` flag.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Co-authored-by: Johan Fylling <johan.dev@fylling.se>
(cherry picked from commit 5d5329d)
ashutosh-narkar added a commit that referenced this pull request Jan 21, 2025
…he store (#7278)

Fixing an issue where the rego-version for individual modules was lost during bundle deactivation (bundle lifecycle) if this version diverged from the active runtime rego-version. This could cause reloading of v0 bundles to fail when OPA was not running with the `--v0-compatible` flag.

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
Co-authored-by: Johan Fylling <johan.dev@fylling.se>
(cherry picked from commit 5d5329d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants