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

Enable policy opts via http-api #445

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/fluree/db/json_ld/policy.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
{:rdf/type [:_id]}
{:f/allow [:* {:f/targetRole [:_id]}]}
{:f/property [:* {:f/allow [:* {:f/targetRole [:_id]}]}]}]}
:where [['?s :rdf/type :f/Policy]]}))))
:where [['?s :rdf/type :f/Policy]]
:opts {:context-type :keyword}}))))



(defn policies-for-roles*
Expand Down Expand Up @@ -334,7 +336,9 @@
async/merge
(async/reduce
(fn [acc result]
(into acc result)) [])
(if (instance? Throwable result)
(reduced result)
(into acc result))) [])
<?))))

;; TODO - exceptions in here won't be caught!
Expand All @@ -349,7 +353,9 @@
async/merge
(async/reduce
(fn [acc result]
(into acc result)) [])
(if (instance? Throwable result)
(reduced result)
(into acc result))) [])
<?)))


Expand Down Expand Up @@ -427,8 +433,9 @@
(map #(compile-policy db %))
async/merge
(async/reduce (fn [acc compiled-policy]
(into acc compiled-policy)) [])))

(if (instance? Throwable compiled-policy)
(reduced compiled-policy)
(into acc compiled-policy))) [])))

(defn policy-map
"perm-action is a set of the action(s) being filtered for."
Expand All @@ -441,7 +448,7 @@
role-sids (if (sequential? role)
(->> (<? (subids db role))
(into #{}))
#{(<? (dbproto/-subid db role))})
#{(<? (dbproto/-subid db role ))})
policies {:ident ident-sid
:roles role-sids
:cache (atom {})}
Expand Down
27 changes: 25 additions & 2 deletions test/fluree/db/policy/parsing_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,31 @@
:roles #{sid-userRole}}
policy-alice)
"Policies for only :ex/userRole should return")))


(testing "Root policy contains {:root? true} for each applicable :f/action"
(let [policy-root (-> @(fluree/promise-wrap (policy/policy-map db root-did :ex/rootRole nil))
replace-policy-fns)
sid-root-did @(fluree/internal-id db root-did)
dpetran marked this conversation as resolved.
Show resolved Hide resolved
sid-rootRole @(fluree/internal-id db :ex/rootRole)]
(is (= {:f/modify {:root? true}
:f/view {:root? true}
:ident sid-root-did
:roles #{sid-rootRole}}
policy-root))))))
(testing "Testing query policy with strings"
(let [conn (test-utils/create-conn)
ledger @(fluree/create conn "policy-parse/a" {:defaultContext ["" {"ex" "http://example.org/ns/"}]
:context-type :string})
root-did (:id (did/private->did-map "8ce4eca704d653dec594703c81a84c403c39f262e54ed014ed857438933a2e1c"))
db @(fluree/stage
(fluree/db ledger)
[{"id" root-did
"f:role" {"id" "ex:rootRole"}}
{"id" "ex:rootPolicy",
"type" ["f:Policy"],
"f:targetNode" {"id" "f:allNodes"}
"f:allow" [{"id" "ex:rootAccessAllow"
"f:targetRole" {"id" "ex:rootRole"}
"f:action" [{"id" "f:view"} {"id" "f:modify"}]}]}])]
(testing "Root policy contains {:root? true} for each applicable :f/action"
(let [policy-root (-> @(fluree/promise-wrap (policy/policy-map db root-did :ex/rootRole nil))
replace-policy-fns)
Expand Down