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

Builtins emit default values in JSON results #507

Merged
merged 1 commit into from
Dec 3, 2024
Merged

Conversation

ronenh
Copy link
Contributor

@ronenh ronenh commented Dec 3, 2024

The ds.checks builtin returns an array of CheckResponse messages. Each element has a boolean check field indicating the outcome of one check. However, because false is the default value for bool the marshaled JSON object doesn't include the check field at all when it is false.

Dealing with missing fields is difficult in Rego (I couldn't figure it out). The code below takes a resource context of the form:

{
  "object_type": "",
  "checks": [
    {"object_id": "", "relation": ""}
  ]
}

It computes an array of results of the form:

[
  {"object_id": "", "relation": "", "allowed": true/false}
]

If default values are omitted from the returned results, the array will only include checks that evaluated to true.

raw := ds.checks({
  "default": {
    "object_type": input.resource.object_type,
    "subject_type": "user",
    "subject_id": input.user.id
  },
  "checks": checks
})

results := x {
  x := [
    {"object_id": check.object_id, "relation": check.relation, "allowed": result.check } |
    check := checks[i]
    result = raw[i]
  ]
}

This commit adds EmitDefaultValues: true to the protojson.MarshalOptions used by the builtins to marshal rpc responses to JSON.

The `ds.checks` builtin returns an array of `CheckResponse` messages.
Each element has a boolean `check` field indicating the outcome of one check.
However, because `false` is the default value for `bool` the marshaled JSON
object doesn't include the `check` field at all when it is false.

Dealing with missing fields is difficult in Rego (I couldn't figure it out).
The code below takes a resource context of the form:
```json
{
  "object_type": "",
  "checks": [
    {"object_id": "", "relation": ""}
  ]
}
```

It computes an array of results of the form:
```json
[
  {"object_id": "", "relation": "", "allowed": true/false}
]
```

If default values are omitted from the returned results, the array
will only include checks that evaluated to true.

```rego
raw := ds.checks({
  "default": {
    "object_type": input.resource.object_type,
    "subject_type": "user",
    "subject_id": input.user.id
  },
  "checks": checks
})

results := x {
  x := [
    {"object_id": check.object_id, "relation": check.relation, "allowed": result.check } |
    check := checks[i]
    result = raw[i]
  ]
}
```

This commit adds `EmitDefaultValues: true` to the `protojson.MarshalOptions` used by the
builtins to marshal rpc responses to JSON.
@ronenh ronenh force-pushed the emit-default-values branch from 0fd9e2b to 80ed906 Compare December 3, 2024 17:05
@ronenh ronenh requested a review from gertd December 3, 2024 17:08
@coveralls
Copy link

Pull Request Test Coverage Report for Build 12144607274

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 0.0%

Totals Coverage Status
Change from base Build 12144388917: 0.0%
Covered Lines: 0
Relevant Lines: 0

💛 - Coveralls

@gertd gertd merged commit 7627ee2 into main Dec 3, 2024
5 checks passed
@gertd gertd deleted the emit-default-values branch December 3, 2024 17:10
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