cedar_policy 0.1.0-x86_64-linux → 0.4.0-x86_64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.cross_rubies +0 -4
- data/.release-please-manifest.json +1 -0
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +13 -0
- data/README.md +98 -10
- data/lib/cedar_policy/3.0/cedar_policy.so +0 -0
- data/lib/cedar_policy/3.1/cedar_policy.so +0 -0
- data/lib/cedar_policy/3.2/cedar_policy.so +0 -0
- data/lib/cedar_policy/3.3/cedar_policy.so +0 -0
- data/lib/cedar_policy/context.rb +14 -0
- data/lib/cedar_policy/entities.rb +26 -0
- data/lib/cedar_policy/entity.rb +36 -0
- data/lib/cedar_policy/entity_uid.rb +34 -0
- data/lib/cedar_policy/version.rb +1 -1
- data/lib/cedar_policy.rb +21 -1
- data/release-please-config.json +17 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e88b4b6af42c4f2193942a2617df23db000e0d0f39bd694ed3992956ac3f2936
|
4
|
+
data.tar.gz: 58210782b8b702de789a11c8fe3d6355309ec03f1ad7be34caaf4be324c9c623
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4410cbaf721c6c169e4df32686084b2ed76c26e9734dc0b8ed1893fe192c3e7a154fd4d6c09794079cc10135a7b90659c782e89e19b6f4a852b7f10c810db40e
|
7
|
+
data.tar.gz: 374db2e353a5293677bf69c53012756707663738e8b9f45f1919cb9fd9197596fad540ea7cd4e281d7c98eb1bd422bca28580e51152c40892559d3330c8df9ae
|
data/.cross_rubies
CHANGED
@@ -1,23 +1,19 @@
|
|
1
1
|
3.0.0:aarch64-linux
|
2
|
-
3.0.0:arm-linux
|
3
2
|
3.0.0:arm64-darwin
|
4
3
|
3.0.0:x64-mingw32
|
5
4
|
3.0.0:x86_64-darwin
|
6
5
|
3.0.0:x86_64-linux
|
7
6
|
3.1.0:aarch64-linux
|
8
|
-
3.1.0:arm-linux
|
9
7
|
3.1.0:arm64-darwin
|
10
8
|
3.1.0:x64-mingw-ucrt
|
11
9
|
3.1.0:x86_64-darwin
|
12
10
|
3.1.0:x86_64-linux
|
13
11
|
3.2.0:aarch64-linux
|
14
|
-
3.2.0:arm-linux
|
15
12
|
3.2.0:arm64-darwin
|
16
13
|
3.2.0:x64-mingw-ucrt
|
17
14
|
3.2.0:x86_64-darwin
|
18
15
|
3.2.0:x86_64-linux
|
19
16
|
3.3.0:aarch64-linux
|
20
|
-
3.3.0:arm-linux
|
21
17
|
3.3.0:arm64-darwin
|
22
18
|
3.3.0:x64-mingw-ucrt
|
23
19
|
3.3.0:x86_64-darwin
|
@@ -0,0 +1 @@
|
|
1
|
+
{".":"0.4.0"}
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.4.0](https://github.com/elct9620/cedar-policy-rb/compare/cedar_policy-v0.3.0...cedar_policy/v0.4.0) (2024-10-06)
|
4
|
+
|
5
|
+
|
6
|
+
### Features
|
7
|
+
|
8
|
+
* **deps:** upgrade cedar-policy to v4.1.0 ([61d6fa1](https://github.com/elct9620/cedar-policy-rb/commit/61d6fa1a59ab2edd71972410c1d9d697fde60776))
|
9
|
+
|
10
|
+
|
11
|
+
### Bug Fixes
|
12
|
+
|
13
|
+
* commitizen hook file bundled into gem ([b0c9c77](https://github.com/elct9620/cedar-policy-rb/commit/b0c9c77459ec614bfd4698d804969adb9b4bccc1))
|
data/README.md
CHANGED
@@ -1,24 +1,112 @@
|
|
1
|
-
|
1
|
+
Cedar Policy
|
2
|
+
===
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cedar_policy`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
Ruby bindings for Cedar policy evaluation engine.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
-
|
11
8
|
Install the gem and add to the application's Gemfile by executing:
|
12
9
|
|
13
|
-
$ bundle add
|
10
|
+
$ bundle add cedar_policy
|
14
11
|
|
15
12
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
13
|
|
17
|
-
$ gem install
|
14
|
+
$ gem install cedar_policy
|
18
15
|
|
19
16
|
## Usage
|
20
17
|
|
21
|
-
|
18
|
+
> [!WARNING]
|
19
|
+
> This gem is still under development and the API may change in the future.
|
20
|
+
|
21
|
+
### PolicySet
|
22
|
+
|
23
|
+
Define a policy by Cedar Language:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
policy = <<~POLICY
|
27
|
+
permit(
|
28
|
+
principal == AdminUser::"1",
|
29
|
+
action == Action::"view",
|
30
|
+
resource
|
31
|
+
);
|
32
|
+
POLICY
|
33
|
+
policy_set = CedarPolicy::PolicySet.new(policy)
|
34
|
+
```
|
35
|
+
|
36
|
+
> Currently, the single policy is not supported.
|
37
|
+
|
38
|
+
### Request
|
39
|
+
|
40
|
+
Prepare the Entity's ID via `EntityUid` or an object with `#to_hash` method which returns a hash with `:type` and `:id` keys.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
principal = CedarPolicy::EntityUid.new("User", "1") # or { type: "User", id: "1" }
|
44
|
+
action = CedarPolicy::EntityUid.new("Action", "view")
|
45
|
+
resource = CedarPolicy::EntityUid.new("Image", "1")
|
46
|
+
```
|
47
|
+
|
48
|
+
The `Context` object is used to store the request context. Use `Context` or an object with `#to_hash` method which returns a hash.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ctx = CedarPolicy::Context.new({ ip: "127.0.0.1" }) # or { ip: "127.0.0.1" }
|
52
|
+
```
|
53
|
+
> The `Context` object can initialize without any arguments as an empty context.
|
54
|
+
|
55
|
+
Create a `Request` object with the principal, action, resource, and context.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
request = CedarPolicy::Request.new(principal, action, resource, ctx)
|
59
|
+
```
|
60
|
+
|
61
|
+
### Entities
|
62
|
+
|
63
|
+
Define the entities with related this request. It should be an array of `Entity` objects which have `#to_hash` method returns a hash with `:uid`,`:attrs`, and `:parents` keys.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
entities = CedarPolicy::Entities.new([
|
67
|
+
CedarPolicy::Entity.new(
|
68
|
+
CedarPolicy::EntityUid.new("User", "1"),
|
69
|
+
{ role: "admin" },
|
70
|
+
[] # Parents' EntityUid
|
71
|
+
),
|
72
|
+
{
|
73
|
+
uid: { type: "Image", id: "1" },
|
74
|
+
attrs: {},
|
75
|
+
parents: []
|
76
|
+
}
|
77
|
+
])
|
78
|
+
```
|
79
|
+
|
80
|
+
### Authorizer
|
81
|
+
|
82
|
+
Create an `Authorizer` object and authorize the request with the policy set and entities.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
authorizer = CedarPolicy::Authorizer.new
|
86
|
+
```
|
87
|
+
|
88
|
+
If boolean result is enough, use `#authorize?` method.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
authorizer.authorize?(request, policy_set, entities) # => true
|
92
|
+
```
|
93
|
+
|
94
|
+
If you want to get the decision object, use `#authorize` method.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
response = authorizer.authorize(request, policy_set, entities)
|
98
|
+
response.decision # => CedarPolicy::Decision::ALLOW
|
99
|
+
```
|
100
|
+
|
101
|
+
> The diagnostics is not supported yet in the response.
|
102
|
+
|
103
|
+
## Roadmap
|
104
|
+
|
105
|
+
* [ ] Add DSL to improve developer experience
|
106
|
+
* [ ] Add batch authorization support
|
107
|
+
* [x] Diagnostics return with response
|
108
|
+
* [ ] Validator support
|
109
|
+
* [ ] Schema support
|
22
110
|
|
23
111
|
## Development
|
24
112
|
|
@@ -28,7 +116,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
28
116
|
|
29
117
|
## Contributing
|
30
118
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/cedar-policy-rb.
|
32
120
|
|
33
121
|
## License
|
34
122
|
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CedarPolicy
|
4
|
+
# :nodoc:
|
5
|
+
class Entities
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(entities = [])
|
9
|
+
@entities = Set.new(entities.map do |entity|
|
10
|
+
next entity if entity.is_a?(Entity)
|
11
|
+
|
12
|
+
Entity.new(*entity.values_at(:uid, :attrs, :parents))
|
13
|
+
end)
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(&block)
|
17
|
+
return enum_for(:each) unless block_given?
|
18
|
+
|
19
|
+
@entities.each(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_ary
|
23
|
+
@entities.map { |entity| CedarPolicy.deep_serialize(entity) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CedarPolicy
|
4
|
+
# :nodoc:
|
5
|
+
class Entity
|
6
|
+
attr_reader :uid, :attrs, :parents
|
7
|
+
|
8
|
+
def initialize(uid, attrs = {}, parents = [])
|
9
|
+
raise ArgumentError unless uid.is_a?(EntityUid) || uid.is_a?(Hash)
|
10
|
+
|
11
|
+
@uid = if uid.is_a?(EntityUid)
|
12
|
+
uid
|
13
|
+
else
|
14
|
+
EntityUid.new(*uid.values_at(:type, :id))
|
15
|
+
end
|
16
|
+
@attrs = attrs
|
17
|
+
@parents = Set.new(parents)
|
18
|
+
end
|
19
|
+
|
20
|
+
def eql?(other)
|
21
|
+
hash == other.hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash
|
25
|
+
[self.class, @uid].hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_hash
|
29
|
+
{
|
30
|
+
uid: @uid,
|
31
|
+
attrs: @attrs,
|
32
|
+
parents: @parents.to_a
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CedarPolicy
|
4
|
+
# :nodoc:
|
5
|
+
class EntityUid
|
6
|
+
attr_reader :type_name, :id
|
7
|
+
|
8
|
+
def initialize(type_name, id)
|
9
|
+
@type_name = type_name.to_s
|
10
|
+
@id = id.to_s
|
11
|
+
|
12
|
+
freeze
|
13
|
+
end
|
14
|
+
|
15
|
+
def eql?(other)
|
16
|
+
hash == other.hash
|
17
|
+
end
|
18
|
+
alias == eql?
|
19
|
+
|
20
|
+
def hash
|
21
|
+
[self.class, @type_name, @id].hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_str
|
25
|
+
"#{@type_name}::#{@id.inspect}"
|
26
|
+
end
|
27
|
+
alias to_s to_str
|
28
|
+
alias inspect to_str
|
29
|
+
|
30
|
+
def to_hash
|
31
|
+
{ type: @type_name, id: @id }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/cedar_policy/version.rb
CHANGED
data/lib/cedar_policy.rb
CHANGED
@@ -1,9 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "json"
|
4
|
+
require "set"
|
5
|
+
|
3
6
|
require_relative "cedar_policy/version"
|
4
7
|
require_relative "cedar_policy/cedar_policy"
|
8
|
+
require_relative "cedar_policy/entity_uid"
|
9
|
+
require_relative "cedar_policy/entity"
|
10
|
+
require_relative "cedar_policy/entities"
|
11
|
+
require_relative "cedar_policy/context"
|
5
12
|
|
13
|
+
# :nodoc:
|
6
14
|
module CedarPolicy
|
7
15
|
class Error < StandardError; end
|
8
|
-
|
16
|
+
|
17
|
+
def self.deep_serialize(input)
|
18
|
+
input.to_hash.each_with_object({}) do |(key, value), output|
|
19
|
+
output[key.to_sym] =
|
20
|
+
case value
|
21
|
+
when ->(h) { h.respond_to?(:to_hash) } then deep_serialize(value)
|
22
|
+
when Array
|
23
|
+
value.map { |item| item.respond_to?(:to_hash) ? deep_serialize(item) : item }
|
24
|
+
else
|
25
|
+
value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
9
29
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"release-type": "ruby",
|
3
|
+
"last-release-sha": "7d06a6458e9805cc6ec05f456c650708cc9d9e9f",
|
4
|
+
"packages": {
|
5
|
+
".":{
|
6
|
+
"release-type": "ruby",
|
7
|
+
"version-file": "lib/cedar_policy/version.rb"
|
8
|
+
}
|
9
|
+
},
|
10
|
+
"extra-files": [
|
11
|
+
{
|
12
|
+
"type": "toml",
|
13
|
+
"path": "ext/cedar_policy/Cargo.toml",
|
14
|
+
"jsonpath": "$.package.version"
|
15
|
+
}
|
16
|
+
]
|
17
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cedar_policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: x86_64-linux
|
6
6
|
authors:
|
7
7
|
- Aotokitsuruya
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby bindings for Cedar policy evaluation engine.
|
14
14
|
email:
|
@@ -18,8 +18,10 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- ".cross_rubies"
|
21
|
+
- ".release-please-manifest.json"
|
21
22
|
- ".rspec"
|
22
23
|
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
23
25
|
- LICENSE.txt
|
24
26
|
- README.md
|
25
27
|
- Rakefile
|
@@ -28,7 +30,12 @@ files:
|
|
28
30
|
- lib/cedar_policy/3.1/cedar_policy.so
|
29
31
|
- lib/cedar_policy/3.2/cedar_policy.so
|
30
32
|
- lib/cedar_policy/3.3/cedar_policy.so
|
33
|
+
- lib/cedar_policy/context.rb
|
34
|
+
- lib/cedar_policy/entities.rb
|
35
|
+
- lib/cedar_policy/entity.rb
|
36
|
+
- lib/cedar_policy/entity_uid.rb
|
31
37
|
- lib/cedar_policy/version.rb
|
38
|
+
- release-please-config.json
|
32
39
|
- sig/cedar_policy.rbs
|
33
40
|
homepage: https://github.com/elct9620/cedar-policy-rb
|
34
41
|
licenses:
|