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

Naming convention for "internal" variables and rules #5

Closed
ievgenii-shepeliuk opened this issue May 25, 2022 · 1 comment · Fixed by #32
Closed

Naming convention for "internal" variables and rules #5

ievgenii-shepeliuk opened this issue May 25, 2022 · 1 comment · Fixed by #32

Comments

@ievgenii-shepeliuk
Copy link

ievgenii-shepeliuk commented May 25, 2022

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
}
@anderseknert
Copy link
Member

anderseknert commented May 25, 2022

For variables declared inside of rules, there's no reason I can think of to use _ for a prefix. They aren't visible outside of the rule anyway, and will shadow variables of the same name declared in the outer scope, so that e.g.

x := 5

my_rule {
    x := 10
    print(x) # prints 10
}

However, one place where I think this question is highly relevant, and one I know have come up in the past, is whether there should be a way to mark top level variables and rules as "private". This could either be done by naming convention, like you suggest:

allow {
    _my_helper_rule
}

_my_helper_rule {
    # some logic here
}

While this would provide a hint to the reader, there's currently nothing to "enforce" this, unless one wants to be creative with e.g. an authz policy that denies requests to any path with a _ prefix.

package system.authz

default allow := {"allowed": true}

allow := {"allowed": false, "reason": "access to documents prefixed with '_' not allowed"} {
    startswith(input.path[count(input.path) -1], "_")
}

allow := {"allowed": false, "reason": "access to 'data' not allowed"} {
    input.path == ["v1", "data"]
}

Although you could easily retrieve these by querying for a parent document, so you'd need to expand on the above for a real use case, whether a good idea or not.

For the future, we could either consider a keyword to mark rules as private/internal, or extend the metadata annotations to include an attribute that would do this, e.g.

# METADATA
# description: helper rule that helps
# visibility: private
my_helper_rule {
    # some logic here
}

This could be done today already using custom annotations, but if we wanted the server to enforce this, we'd need to work on making that happen. I think this approach is probably the one I'd prefer.

@anderseknert anderseknert changed the title Naming convention for "internal" variable names Naming convention for "internal" variables and rules May 25, 2022
anderseknert added a commit that referenced this issue May 27, 2024
Also, move the section on future keywords into an "Older Advice"
category.

Fixes #5

Signed-off-by: Anders Eknert <anders@styra.com>
anderseknert added a commit that referenced this issue May 28, 2024
)

Also, move the section on future keywords into an "Older Advice"
category.

Fixes #5

Signed-off-by: Anders Eknert <anders@styra.com>
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 a pull request may close this issue.

2 participants