Skip to content

Commit

Permalink
feature: Improved support for more data types
Browse files Browse the repository at this point in the history
Added deep equals and 'in' operators for slices
  • Loading branch information
kmesiab committed Oct 17, 2024
1 parent dd1a2bb commit 1578a4c
Show file tree
Hide file tree
Showing 11 changed files with 2,108 additions and 31 deletions.
97 changes: 97 additions & 0 deletions POLICIES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Creating Policy JSON Files and Understanding Operators in go-policy-enforcer

## Table of Contents

- [Policy JSON File Structure](#policy-json-file-structure)
- [Policy Operators](#policy-operators)

## Policy JSON File Structure

Policy JSON files should follow the following structure:

```json
{
"name": "Policy Name",
"rules": [
{
"field": "Field Name",
"operator": "Operator",
"value": "Value"
},
{
"field": "Field Name",
"operator": "Operator",
"value": "Value"
},
...
]
}
```

- `name`: The name of the policy.
- `rules`: An array of rules that define the conditions for enforcing the policy.
- `field`: The name of the field to compare.
- `operator`: The operator to use for comparison.
- `value`: The value to compare against.

## Policy Operators

The library supports the following policy operators:

- `==`: Equal to.
- `!=`: Not equal to.
- `>`: Greater than.
- `<`: Less than.
- `>=`: Greater than or equal to.
- `<=`: Less than or equal to.
- `in`: Check if a value is present in a slice.
- `not in`: Check if a value is not present in a slice.

When evaluating a policy, the library compares the value of the specified
field with the provided value using the specified operator. If the comparison
is true, the policy is enforced; otherwise, it is not enforced.

For example, consider the following policy rule:

```json
{
"field": "age",
"operator": ">",
"value": 18
}
```

In this case, the policy will be enforced if the `age` field of the asset
is greater than 18.

To enforce a policy on an asset, you can use the `Enforce` method of
the `PolicyEnforcer` struct. The method takes an asset as input and
returns a boolean value indicating whether the policy is enforced or not.

Here's an example of how to enforce a policy on an asset:

```go
func main() {
// Load a policy from a JSON file
policy, err := gopolicyenforcer.LoadPolicy("path/to/policy.json")
if err != nil {
log.Fatalf("Error loading policy: %v", err)
}

// Create an asset to test enforcement
asset := &Asset{ID: 1, Name: "John Doe", Age: 25}

// Create a PolicyEnforcer instance with the policy
e := gopolicyenforcer.NewPolicyEnforcer(policy)

// Enforce the policy on the asset and print the result
if e.Enforce(asset) {
fmt.Println("Policy is enforced")
} else {
fmt.Println("Policy is not enforced")
}
}
```

In this example, the policy will be enforced because the `age` field
of the asset is greater than 18.
2 changes: 1 addition & 1 deletion example/asset.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type Asset struct {
ID string `json:"id"`
ID int `json:"id"`
Type string `json:"type"`
Finalized bool `json:"state"`
}
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func main() {
}

// Create assets to test enforcement
allowedAsset := &Asset{ID: "1", Type: "asset", Finalized: true}
deniedAsset := &Asset{ID: "1", Type: "asset", Finalized: false}
allowedAsset := &Asset{ID: 1, Type: "asset", Finalized: true}
deniedAsset := &Asset{ID: 2, Type: "asset", Finalized: false}
assetList := []*Asset{allowedAsset, deniedAsset}

// Create a PolicyEnforcer instance with the policies
Expand Down
4 changes: 2 additions & 2 deletions example/policies/finalized_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "FinalizedPolicy",
"rules": [
{
"field": "finalized",
"operator": "eq",
"field": "Finalized",
"operator": "==",
"value": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion example/policies/id_required_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "IdRequiredPolicy",
"rules": [
{
"field": "id",
"field": "ID",
"operator": ">",
"value": 0
}
Expand Down
Loading

0 comments on commit 1578a4c

Please sign in to comment.