Commit Graph

854 Commits

Author SHA1 Message Date
Anders Eknert 037101cd7c Linter configuration cleanup (#8397)
And enable more staticcheck linters. I saw staticcheck failures
mentioned in another PR, so thought I'd check it out.

- `WriteString(fmt.Sprintf)` -> `fmt.Fprintf`
- Rewrite calls to deprecated `*Rule.Path()`
- Don't use `==` to compare `time.Time`
- Use inline ignores over config exclusions of paths
- Remove 'varcheck' ignores as no longer used
- Remove v0 topdown/graphql.go (!)

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
2026-03-06 22:07:35 +00:00
Anders Eknert e03ac2f200 Bump golangci-lint, more gocritic linters (#8052)
- Bump golangci-lint -> 2.6.2
- Fix all `deprecatedComment` "notices should be in a dedicated paragraph, separated from the rest" reports
- Enable `appendCombine` and fix all "appendCombine: can combine chain of X appends into one" notices
- Enable `preferFprint` and fix the few reported issues
- Fix various issues reported only once or twice, like `zeroByteRepeat`

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
2025-11-17 11:08:39 +01:00
Anders Eknert 78a5ca2ab4 Simplify interning (#7714)
Use a single generic entrypoint for obtaining interned
terms regardless of type.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-06-23 11:40:00 +02:00
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00
Anders Eknert afb30d3f9d Add gocritic linter, fix a bunch of stuff (#7377)
Brace yourselves! For there are many touched files here. No changes
in semantics however.

Spent a long time trying out the various optional rules gocritic
provides, and settled for a few of them. There are more I really
like, but that would take many hours to address across the codebase.

Perhaps others find gocritic too pedantic? If so, we can merge the
fixes without enabling the rule.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-02-24 16:28:41 +01:00
Anders Eknert bdb11d4c7e Fix: add missing BuildAnnotationSet to ast v0 (#7349)
Fixes #7347

Signed-off-by: Anders Eknert <anders@styra.com>
2025-02-09 20:35:03 +01:00
Anders Eknert 55e87e79ae Add perfsprint linter (#7334)
And update code to conform to the rule.

- Replace unnecessary fmt.Sprintf with string concatenation
- Replace fmt.Sprint with more efficient strconv.Itoa
- Replace static fmt.Errorf calls with more efficient errors.New

Thanks @srenatus for pushing me down this rabbit hole!

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-31 20:24:05 +01:00
Anders Eknert 97b8572fdc Use byte for RuleKind and DocKind (#7332)
Saving 7 bytes per index result. Not much, but adds up to ~10Mb in the
`regal lint` benchmark. Changing the underlying type is technically a
breaking change I suppose, but in the unlikely case anyone used this
outside of OPA (we barely use it ourselves!), they'd still have to do
and int() conversion to work with it as an int, which will still work.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-31 13:17:10 +01:00
Johan Fylling 563321d26b Rego v1 capabilities and keywords update (#7216)
* Separating v0- and v1 keywords
* Adding `rego_v1` capability feature

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-17 11:50:11 +01:00
Johan Fylling a179a24c48 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>
2024-12-12 15:27:34 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00
Anders Eknert 26ad67812d More reduced allocations (#7193)
For the first time, down  under a 100 million allocations
when running `regal lint bundle` 🎈

**main**
```
BenchmarkLintAllEnabled-10    1	2538350916 ns/op	6182626816 B/op	108424249 allocs/op
```

**pr**
```
BenchmarkLintAllEnabled-10    1	2282894416 ns/op	5310032744 B/op	93674054 allocs/op
```

But there's more to it than just the number of allocations:

```
➜ hyperfine -i --warmup 1 'regal lint bundle' 'regal-new lint bundle'
Benchmark 1: regal lint bundle
  Time (mean ± σ):      2.822 s ±  0.055 s    [User: 19.299 s, System: 0.603 s]
  Range (min … max):    2.743 s …  2.961 s    10 runs

Benchmark 2: regal-new lint bundle
  Time (mean ± σ):      2.373 s ±  0.040 s    [User: 15.940 s, System: 0.575 s]
  Range (min … max):    2.315 s …  2.435 s    10 runs

Summary
  regal-new lint bundle ran
    1.19 ± 0.03 times faster than regal lint bundle
```

Most notable changes:

- Reuse trieTraversalResult in indexing, as these were expensive
  and short-lived. This had the most dramatic impact on the number
  of reduced allocations of all the changes here.

- Optimize *set, *object and *Array operations to minimize
  allocations by using "primitive" form iteration instead of
  the function literal counterparts internally, and to only
  reset the sort guard when needed.

- New Array.Equal implementation does not remove any allocations
  as the old implementation didn't allocate either. It did however
  perform much better for the case where the compared arrays were
  not equal.

Signed-off-by: Anders Eknert <anders@styra.com>

# Conflicts:
#	topdown/casts.go
2024-12-04 16:05:36 +01:00
Anders Eknert a60ef72799 Even less allocs (#7190)
**main**
```
BenchmarkLintAllEnabled-10    1	2640715625 ns/op	6385110200 B/op	116296633 allocs/op
```

**pr**
```
BenchmarkLintAllEnabled-10    1	2597179708 ns/op	6183614112 B/op	108421141 allocs/op
```

(I renamed the benchmark, but this is the same as "regal linting itself"
used in the past)

Another 8 million allocations cut off from `regal lint bundle`,
and a whopping 10% improvements to wall clock time!

The most significant improvement is the Equal implementation for
refs, since that is called all over the place. But there are many
other fixes here, and they all contribute something substantial
(and fixes that only have had marginal impact have been left out).

Signed-off-by: Anders Eknert <anders@styra.com>
2024-11-24 11:24:29 +01:00
Stephan Renatus 20885fe4a9 golangci: bump version, addess all new findings
The previous version has been failing without any good reason for me,
so let's try this.

About the version pick: It's not the latest version (v1.62.0 at the
moment), because that would introduce a new revive rule,
redeclares-builtin-id, and that flags every variable called `min` or
`max` in the code base. I had started addressing these, but they were
just too many.

The new issues related to this version are mostly that it complains
whenever it finds a non-static string that makes its way into a printf-
like function. However, that's a common pattern in some place here, so
I've sprinkled some nolint:govet on it.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-11-14 20:36:50 +01:00
Anders Eknert d3f5102aa4 Tweaks to reduce number of allocations in regal lint hot path
Concluding my quest to reduce the number of allocations in the
hot path for `regal lint` for this time around. This PR mainly
does so by reusing pointers to boolean and integer terms where
these are determined not to be mutated later.

The result is another ~4 million allocations reduced when
linting Regal against its own bundle. These improvements should
however help reduce allocations in pretty much any evaluation.

**opa main**
```
BenchmarkRegalLintingItself-10    1	3195257584 ns/op	6496097784 B/op	120108808 allocs/op
```

**PR branch**
```
BenchmarkRegalLintingItself-10    1	3132126333 ns/op	6376318224 B/op	116163318 allocs/op
```

Signed-off-by: Anders Eknert <anders@styra.com>
2024-11-14 11:30:58 +01:00
Tyler Schade c47b8c1723 provide a more useful error message when there are conflicting default rules
Signed-off-by: Tyler Schade <tylerschade99@gmail.com>
2024-11-12 13:38:42 -08:00
berdanA 26e2db328d topdown: improve scientific notation parsing in extractNumAndUnit
- Updated extractNumAndUnit to recognize and correctly parse scientific notation, including cases with 'e' or 'E' followed by an exponent (e.g., "1e10", "3.2E4").
- Ensures that 'e' or 'E' is treated as part of the number if followed by digits, enhancing compatibility with scientific notation inputs.
- Scientific notation with units now also parses correctly (e.g., "1e10GB" extracts "1e10" as the number and "GB" as the unit).
- Maintains behavior for cases without units, allowing either the number or unit portion to be empty, while improving overall string parsing logic.
- Added tests for scientific notation parsing in `units.parse_bytes`, including both SI and binary units (e.g., KB, MiB, GiB, KiB).
- Included cases for uppercase, lowercase, and mixed case formats with scientific notation.
- Added validation for numbers without units defaulting to bytes.

Fixes #7142

Signed-off-by: bakar <berdan.akar@rise-world.com>
2024-11-08 12:48:02 -08:00
Anders Eknert 60ea2e7429 Add description to all built-in function args and return values (#7153)
Fixes #7151

Signed-off-by: Anders Eknert <anders@styra.com>
2024-11-04 14:00:23 +01:00
Johan Fylling 6af5e79bd9 storage: Optimized read mode for default data storage
A new optimized read mode has been added to the default in-memory store, where data written to the store is eagerly converted to AST values (the data format used during evaluation). This pre-converted data is faster to read, and won’t cause memory spikes during load; but comes with slower data writes (affects startup and bundle load/update time) and a larger lowest overall memory footprint for OPA. Can be enabled for `opa run`, `opa eval`, and `opa bench` by setting the `—optimize-store-for-read-speed`. See http://localhost:8888/docs/edge/policy-performance/#storage-optimization.

Implements: #4147

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Co-authored-by: Ashutosh Narkar <anarkar4387@gmail.com>
2024-10-30 12:12:21 +01:00
Anders Eknert 58ec50b4b0 Fix location for multivalue rules with generated bodies (#7129)
Since there is no body, the location of the head is a better option
than simply using the fist scanned token for location.

Fixes #7128

Signed-off-by: Anders Eknert <anders@styra.com>
2024-10-21 17:57:01 +02:00
Tyler Schade 555fe84094 only check schemas when schemas are provided (#7124)
only check schemas when schemas are provided

Signed-off-by: Tyler Schade <tyler.schade@solo.io>
2024-10-18 14:00:32 +02:00
Anders Eknert 1d51c1df61 Replace use of yaml.v2 library with yaml.v3 (#7091)
Some quirks encountered in the upgrade:
- Errors reported by the parser are now zero-indexed
- A few errors seemingly reported on the line after the
  error rather than where it happened
- Also a few tests where the line number reported
  *previously* seemed wrong but now is right
- Something different in how the parser unmarshalled to
  the "raw" schema annotation type we used. Changed to
  use a a map instead of that type alias.. but I'm not
  really sure why that had to be done

Perhaps worth looking into this further. But pushing this
now to start that discussion.

Fixes #7090

Signed-off-by: Anders Eknert <anders@styra.com>
2024-10-06 03:54:06 +02:00
Johan Fylling 4ba95d0cc4 format: Bracketing keyword ref elements in formatter output (#7010)
Also future-proofing format pkg tests to be 1.0 compatible.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-26 10:24:19 +02:00
Johan Fylling 5521453d87 cmd: Adding --v0-compatible flag (#7065)
To make OPA behave as v0.x post v1.0 release.
If used simultaneously with `--v1-compatible` flag, the `--v0-compatible` flag takes precedence.

Also, future-proofing `cmd` package tests for 1.0.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-26 00:45:51 +02:00
Anders Eknert e959bce141 Fix: attach annotation to static part of rule ref (#7052)
Previously, annotations were missing on rules that had variable
components in the ref head. Now we annotate the rule using the
path up to the first var.

Fixes #7050

Signed-off-by: Anders Eknert <anders@styra.com>
2024-09-23 13:33:14 +02:00
Johan Fylling acb32722b7 rego-v1: Future-proofing repl pkg tests to be 1.0 compatible (#7026)
Also making some updates to the repl implementation to properly deal with v1 as the default rego-version.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-19 11:13:15 +02:00
Johan Fylling 546eeac58e rego-v1: Future-proofing ast package tests to be 1.0 compatible (#6992)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-18 10:22:15 +02:00
Johan Fylling ebe5b01827 Assigning rule key also for if-rules with 2-part non-ground refs (#7004)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-12 15:45:20 +02:00
Johan Fylling ddcd2801a6 ast: Fixing nil pointer dereference for no-arg assign() calls in Rego v1 (#7005)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-10 10:10:18 +02:00
Johan Fylling 2515b7173f ast: Asserting future-keywords capabilities are valid also for Rego v1 (#7003)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-10 09:59:55 +02:00
Johan Fylling 7cd3fecae7 ast: Make Module.String() include if/contains for v1 modules (#7000)
Fixes: #6973
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-06 15:09:02 +02:00
Johan Fylling 91874a1561 rego-v1: Future-proofing compiler pkg tests (#6985)
* Updating `compile` package tests
* Respect optimizer rego-version for optimized support modules
* Setting parser options to use module rego-version when `bundle.FormatModulesForRegoVersion()` should preserve parsed module rego-version
* Enforcing requested rego-version for partial-eval support modules

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-05 10:50:28 +02:00
Nikita Pivkin 76f7038ea2 perf: cache schema types
Signed-off-by: Nikita Pivkin <nikita.pivkin@smartforce.io>
2024-09-03 14:10:41 -07:00
Stephan Renatus caa62a3103 ast/compile: also replace declared vars in with's target (#6980)
It might be uncommon to do this, but it's not wrong to expect this to work.
One peculiarity is that we've consciously made an exception for
```
input := "whatever"
allow with input as true
```
where the with-target "input" was meant not to be replaced although there's
a local variable shadowing "input". This change has become a bit trickier
because this behaviour is kept intact. Note that it's only possible in non-strict
mode (Rego v0).

Fixes #6979

Small refactoring included:
* ast/compile: remove With from recursive term rewriting

   I could find no term where this actually mattered, so I've simplified the code
   a little bit.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-09-02 10:20:54 +02:00
Anders Eknert f10cc1f4c8 Change required scope of entrypoint from rule to document (#6963)
And automatically change implied `scope` from `rule` to `document` when
no `scope` is provided (on rule metadata).

Fixes #6798

Signed-off-by: Anders Eknert <anders@styra.com>
2024-08-28 13:22:30 +02:00
Stephan Renatus 25d21f50d3 ast/parser: add hint to future-proof imports (#6968)
Just an idea, if we ever introduce an

    import awesome

and people start receiving "unknown import" errors on outdated
OPA versions, they'll at least give a hint.

This could be extended to cover the other synthetic imports, like
`rego.*` and `future.*`, but it's a start...

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-08-28 10:49:34 +02:00
Anders Eknert 7b535a79a6 Docs: suggest using opa-config.yaml as name for config file (#6966)
And update some references throughout the docs.

Fixes #6959

Signed-off-by: Anders Eknert <anders@styra.com>
2024-08-27 23:57:15 +02:00
Anders Eknert e6aa584a6d Make type checker copy method copy all values (#6949)
Just a small improvement from looking into #6946

This doesn't necessarily solve all issues reported there, but I figured
I might as well commit this anyway.

Signed-off-by: Anders Eknert <anders@styra.com>
2024-08-21 15:06:03 +02:00
Johan Fylling 3e7e6a00de test: Creating v1 yaml tests from existing v0 tests (#6924)
Fixes: #6864

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-08-20 12:13:25 +02:00
Anders Eknert 25f4cb6db7 Include term locations in rule heads when requested (#6943)
When the JSON option to include term locations have been set, they should be
included in all parts of the head where terms appear.

Fixes #6860

Signed-off-by: Anders Eknert <anders@styra.com>
2024-08-20 11:38:18 +02:00
Michael Sorens 07c7bb8e16 typos and clarification on signing commits (#6940)
* cosmetic: fix typos
* Add missing piece to the PR template

Signed-off-by: Michael Sorens <msorens@styra.com>
2024-08-19 13:47:31 +02:00
Johan Fylling 1f4a27bfc0 ast: Fixing nil pointer dereference in compiler for partial rule edge case (#6931)
Only appending rule to trieNode values list if it has declared value.

Fixes: #6930

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-08-15 10:43:00 +02:00
Ashutosh Narkar b62ae6bfed Prepare v0.67.0 release
Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2024-07-25 14:34:02 -07:00
Johan Fylling e8aa24c262 format: Produce error when --rego-v1 formatted module has rule name conflicting with keyword (#6867)
Fixes: #6833
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-07-11 10:31:14 +02:00
Manish Giri c70544045b Implement a built-in function for String count
Fixes #6827

Signed-off-by: Manish Giri <manish.giri.me@gmail.com>
2024-07-03 15:50:34 -07:00
Johan Fylling c2cede76b3 ast: expanding nested expressions in every domain (#6832)
Fixes: #6790
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-26 19:45:49 +02:00
Johan Fylling 96ecf38a90 trace+tester: Adding local var values to trace and test report (#6815)
Fixing: #2546
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-25 14:31:12 +02:00
Johan Fylling 5464b005e8 Bumping golangci-lint to v1.59.1 (#6817)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-19 15:13:43 +02:00
Johan Fylling b4679c25c6 Fix/inspect unknowns in with stmt (#6824)
Fixes: #6812
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-19 12:45:44 +02:00
Anders Eknert ecc134bb23 Fix wrong location on metadata parse errors on first line (#6807)
Fixes #6587

Signed-off-by: Anders Eknert <anders@styra.com>
2024-06-11 10:29:00 +02:00