GithubHelp home page GithubHelp logo

styrainc / rego-style-guide Goto Github PK

View Code? Open in Web Editor NEW
194.0 20.0 9.0 60 KB

Style guide for Rego

Home Page: https://docs.styra.com/opa/rego-style-guide

License: Apache License 2.0

rego style-guide best-practices opa open-policy-agent policy-as-code styra

rego-style-guide's People

Contributors

adam-sandor avatar anderseknert avatar charlesdaniels avatar charlieegan3 avatar ericjkao avatar lcarva avatar newatlantis avatar pauly4it avatar philipaconrad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rego-style-guide's Issues

Naming convention for "internal" variables and rules

I would like to have a dedicated section/paragraph/etc explicitly defining naming convention for "internal" variable names inside functions/rules. i.e. should they be prefixed with _ as I saw in some guides.

e.g. - should we use _t or t for those variables

jwt_payload := _t[2] {
  print("jwt_payload")
  _t := io.jwt.decode_verify(jwt_raw, {"cert": jwks_request})
  _t[0] == true
}

Recommend importing individual future.keywords?

Since we've seen a few issues where existing rules or functions have clashed with the "catch all" import future.keywords at OPA version upgrades, it makes sense to recommend the more explicit alternative, i.e. declare one import per keyword used.

Best practices for unit testing

Some thoughts

  • Extend general advice to recommend test driven development
  • And add an "Exceptions" part to the recommendation on imports stating that importing rules directly is fine for tests.

Test section

  • Use different package name, and imports, for tests
    • Recommendation for naming test packages: same as the package under test, with either .test or _test appended?
    • Recommendation for naming test files: _test.rego suffix?

Additionally, I've seen a few questions about how to best organize tests, i.e. should they be in the same directory as packages being tested, or in a separate one? While not strictly about "Rego" the language, I think it makes sense to consider this for the guide as well.

Consider recommendation of pattern `not x == y`?

I've seen this pattern in many policies out in the "wild":

package policy

deny["input.foo must be true"] {
    not input.foo == true
}

Compared to using input.foo != true, this pattern also handles the undefined case, and as such extends the recommendation to use negation to handle undefined.

The question is whether we should include this as a general recommendation, perhaps just as an example included in the current recommendation? ๐Ÿค”

Where things tend to get complicated, is when using "double negation" like not input.foo != true ... and I don't think that's something I'd like to read myself ๐Ÿ˜… But granted that not + equality works without caveats, perhaps it should be preferred over !=?

Consider recommending verification and decoding of JWTs in two separate steps

Although documented, even experienced users get confused by the default constraints (aud in particular) provided by io.jwt.decode_verify. I think we might want to consider recommending verifying and decoding as a two-step process, i.e.

Avoid

claims := payload {
    [valid, _, payload] := io.jwt.decode_verify("my_jwt_token", {"cert": "my certificate"})
    valid
}

Prefer

claims := payload {
    io.jwt.verify_rs256("my_jwt_token", "my certificate")
    [_, payload, _] := io.jwt.decode("my_jwt_token")
}

Package Naming Convention

We should add suggestions for package related subjects:

  • Naming convention
  • How to organize support/helper packages
  • When to split a policy into multiple files
  • How folder structure relates to package hierarchy
  • Where tests are recommended to be put in the package structure

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.