Skip to content

A peer-to-peer stack for code collaboration

Authorization for machines. (#404)

Julian Arni16abbef4414f
diff --git a/bin/rad-diff b/bin/rad-diff
index eb36420..651aaa0 100755
--- a/bin/rad-diff
+++ b/bin/rad-diff
@@ -317,7 +317,16 @@
(do
(def edit-state-with-feedback!
(fn [message]
- (simple-edit-diff! machine n {:state new-state})
+ (catch 'daemon-error
+ (simple-edit-diff! machine n {:state new-state})
+ (fn [x]
+ (put-str!
+ (string-append
+ "Diff could not be "
+ (pretty-state new-state)
+ ". Are you authorized to do so?"
+ ))
+ (exit! 1)))
(put-str! message)))
(verify-diff-number n machine)
(if (eq? new-state :accepted)
diff --git a/bin/rad-issues b/bin/rad-issues
index 910d50e..41ae42f 100755
--- a/bin/rad-issues
+++ b/bin/rad-issues
@@ -158,9 +158,13 @@
(def n (read issue-number))
(if (number? n)
(if (integral? n)
- (do
- (verify-issue-number n machine)
- (simple-edit-issue! machine n {:state :closed}))
+ (catch 'daemon-error
+ (do
+ (verify-issue-number n machine)
+ (simple-edit-issue! machine n {:state :closed}))
+ (fn [_]
+ (put-str! "Issue could not be closed. Are you authorized to do so?")
+ (exit! 1)))
(cmd-parse-failure "Issue numbers must be whole numbers!"))
(cmd-parse-failure "To close an issue, please provide the issue number."))))
diff --git a/cloudbuild.yaml b/cloudbuild.yaml
index db4e845..218fe7f 100644
--- a/cloudbuild.yaml
+++ b/cloudbuild.yaml
@@ -2,6 +2,8 @@ timeout: 3600s # Cache misses are slow to rebuild
substitutions:
# Bump the version if you make changes to `ci/Dockerfile
_BUILD_IMAGE: eu.gcr.io/opensourcecoin/radicle-ci:v1
+options:
+ machineType: N1_HIGHCPU_8
steps:
- id: "Build CI base image"
name: gcr.io/cloud-builders/docker
@@ -186,6 +188,7 @@ steps:
# Add the empty entry to the IPFS test network. Radicle requires it
echo '{"radicle": true}' | docker exec -i test_ipfs-test-network_1 ipfs dag put --pin
+
- id: "Integration tests"
waitFor:
- "Start IPFS daemon"
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 7718e27..6203e56 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -1158,11 +1158,17 @@ file doesn't exist.
Creates a new key pair and stores it in ``my-keys.rad``. Returns the
full absolute path of the created file.
+``(set-fake-keys! keys)``
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Bypass reading the keys from ``my-keys.rad``, using instead the provided
+keys. This is intended for testing.
+
``(use-fake-keys!)``
~~~~~~~~~~~~~~~~~~~~
-Bypass reading the keys from ``my-keys.rad``. This is intended for
-testing.
+Bypass reading the keys from ``my-keys.rad``, using newly-generated
+ones. This is intended for testing.
``prelude/machine``
-------------------
@@ -1361,6 +1367,11 @@ Coordinated Universal Time (UTC) timestamp.
A validator which checks that it's argument is a string and less than
the specified length.
+``(always-valid x)``
+~~~~~~~~~~~~~~~~~~~~
+
+A validator that is always valid.
+
``prelude/util``
----------------
diff --git a/nix/shell.nix b/nix/shell.nix
index 8eccd5e..b443252 100644
--- a/nix/shell.nix
+++ b/nix/shell.nix
@@ -21,7 +21,7 @@ stdenv.mkDerivation {
shellHook = ''
export PATH=$PATH:`stack path --local-bin`:$PWD/bin
export RADPATH=$PWD/rad
- export STACK_IN_NIX_EXTRA_ARGS="--system-ghc --no-nix-pure --nix-packages 'zlib fzf moreutils'"
+ export STACK_IN_NIX_EXTRA_ARGS="--system-ghc --nix-packages zlib"
export IS_NIX_SHELL="true"
export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive";
eval $(grep export ${ghc}/bin/ghc)
@@ -29,6 +29,7 @@ stdenv.mkDerivation {
alias mkdocs="pushd $PWD/docs && make html && popd"
alias sb="stack build --fast"
alias se="stack exec"
+ alias se="stack --no-nix-pure exec"
alias sbd="stack build --fast radicle:exe:rad-daemon-radicle"
alias sbrad="stack build --fast radicle:exe:radicle"
alias st="stack test --fast"
diff --git a/package.yaml b/package.yaml
index 9bb3b9d..f64bb0a 100644
--- a/package.yaml
+++ b/package.yaml
@@ -18,7 +18,6 @@ ghc-options:
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wredundant-constraints
- - -threaded
data-files:
- rad/**/*.rad
@@ -87,6 +86,7 @@ tests:
- tasty-quickcheck
- text
- time
+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
doctest:
main: Main.hs
source-dirs:
@@ -95,6 +95,7 @@ tests:
- doctest
- Glob
- hpack
+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
e2e:
main: Main.hs
source-dirs:
diff --git a/rad/monadic/diff-remote.rad b/rad/monadic/diff-remote.rad
index 48adee0..7122b96 100644
--- a/rad/monadic/diff-remote.rad
+++ b/rad/monadic/diff-remote.rad
@@ -8,6 +8,7 @@
(import prelude/patterns :unqualified)
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
+(import prelude/auth '[make-simple-auth add-admin] :unqualified)
(import prelude/machine-remote '[make-updatable-eval] :unqualified)
(def machine-id
@@ -24,6 +25,8 @@
(def noncer (make-noncer))
+(def auth (make-simple-auth [:admin]))
+
(def set-diffs
"A helper to set stuff in the diffs ref."
(fn [l v]
@@ -103,11 +106,24 @@
(def validator/diff-edit-input
"An input to edit an diff."
- (validator/and
- [validator/input
- validator/time-modified
- validator/diff-reference
- (validator/contains-only (edit-keys (keys diff-keys)))]))
+ (fn [ed]
+ (def auth-validation
+ (match (lookup-default :state :nothing ed)
+ :nothing validator/always-valid
+ :retracted (validator/pred "edit author == original author"
+ (fn [c]
+ (match c
+ {:diff-number 'n
+ :author 'a}
+ (eq? (view-ref diffs (... [(@ n) (@ :author)]))
+ a))))
+ _ (auth [:allowed :admin])))
+ ((validator/and
+ [validator/input
+ validator/time-modified
+ validator/diff-reference
+ auth-validation
+ (validator/contains-only (edit-keys (keys diff-keys)))]) ed)))
(def strip-input
"After an entity input has been processed/verified, we no longer need some of
@@ -164,6 +180,7 @@
{'create-diff create-diff
'list-diffs list-diffs
'edit-diff edit-diff
- 'add-comment add-comment})
+ 'add-comment add-comment
+ 'add-admin (add-admin auth)})
-(def eval (make-updatable-eval commands))
+(def eval (make-updatable-eval commands (auth [:allowed :admin])))
diff --git a/rad/monadic/diff.rad b/rad/monadic/diff.rad
index 0426ea6..280afcb 100644
--- a/rad/monadic/diff.rad
+++ b/rad/monadic/diff.rad
@@ -4,7 +4,7 @@
(import prelude/machine '[send-code!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
-(import prelude/key-management '[use-fake-keys!] :unqualified)
+(import prelude/key-management '[set-fake-keys! use-fake-keys! read-keys!] :unqualified)
(def radicle-diff-machine-id
"The name of this machine."
@@ -13,9 +13,11 @@
(def create-diffs-machine!
"Create a remote diff machine. Returns the machine id"
(fn []
+ (def owner-keys (lookup :public-key (read-keys!)))
(def id (machine/new-machine!))
(machine/send-prelude! id)
(send-code! id (find-module-file! "monadic/diff-remote.rad"))
+ (machine/send-signed-command! id radicle-diff-machine-id 'add-admin {:key owner-keys})
id))
(def simple-diff
@@ -79,7 +81,9 @@
"The monadic diffs machine works."
[:setup
(do (machine/install-remote-machine-fake)
- (use-fake-keys!)
+ (def owner-keys (gen-key-pair! (default-ecc-curve)))
+ (def non-owner-keys (gen-key-pair! (default-ecc-curve)))
+ (set-fake-keys! owner-keys)
(def machine (create-diffs-machine!))
(simple-create-diff! machine {:title "title0" :description "desc0" :patch "patch0"})
(simple-create-diff! machine {:title "title1" :description "desc1" :patch "patch1"})
@@ -93,4 +97,10 @@
[ (view (... [(@ 0) (@ :comments) (@nth 0) (@ :body)]) diffs) ==> "comment0" ]
[ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) diffs) ==> "comment1" ]
[ (view (.. (@ 0) (@ :state)) diffs) ==> :accepted ]
- )
+ ;; authorization
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (simple-edit-diff! machine 1 {:state :accepted})
+ (fn [_] :ok))) ==> :ok ]
+)
diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.rad
index 2dd3384..d23943f 100644
--- a/rad/monadic/issue-remote.rad
+++ b/rad/monadic/issue-remote.rad
@@ -11,6 +11,7 @@
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
(import prelude/nonces :unqualified)
+(import prelude/auth '[make-simple-auth add-admin] :unqualified)
(import prelude/machine-remote '[make-updatable-eval] :unqualified)
(def machine-id
@@ -27,6 +28,8 @@
(def noncer (make-noncer))
+(def auth (make-simple-auth [:admin]))
+
(def set-issues
"A helper to set stuff in the issues ref."
(fn [l v]
@@ -123,6 +126,16 @@
validator/time-modified
validator/sub-issue
validator/issue-reference
+ (validator/or
+ [(validator/pred
+ "edit author == original author"
+ (fn [c]
+ (match c
+ {:issue-number 'n
+ :author 'a}
+ (eq? (view-ref issues (... [(@ n) (@ :author)]))
+ a))))
+ (auth [:allowed :admin])])
(validator/contains-only (edit-keys (keys issue-keys)))]))
(def validator/comment-edit-input
@@ -144,14 +157,17 @@
{:comment-index 'i
:issue-number 'n}
(< i (length (view-ref issues (.. (@ n) (@ :comments))))))))
- (validator/pred "edit author == original author"
- (fn [c]
- (match c
- {:issue-number 'n
- :comment-index 'i
- :author 'a}
- (eq? (view-ref issues (... [(@ n) (@ :comments) (@nth i) (@ :author)]))
- a))))]))
+ (validator/or
+ [(validator/pred
+ "edit author == original author"
+ (fn [c]
+ (match c
+ {:issue-number 'n
+ :comment-index 'i
+ :author 'a}
+ (eq? (view-ref issues (... [(@ n) (@ :comments) (@nth i) (@ :author)]))
+ a))))
+ (auth [:allowed :admin])])]))
(def strip-input
"After an entity input has been processed/verified, we no longer need some of
@@ -232,6 +248,8 @@
'edit-issue edit-issue
'add-comment add-comment
'edit-comment edit-comment
- 'list-issues list-issues})
+ 'list-issues list-issues
+ 'add-admin (add-admin auth)})
+
-(def eval (make-updatable-eval commands))
+(def eval (make-updatable-eval commands (auth [:allowed :admin])))
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 0907427..12cd4c2 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -8,7 +8,7 @@
(import prelude/machine '[send-code! send-signed-command!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
-(import prelude/key-management '[use-fake-keys!] :unqualified)
+(import prelude/key-management '[use-fake-keys! read-keys!] :unqualified)
(def radicle-issues-machine-id
"The name of this machine."
@@ -17,9 +17,11 @@
(def create-issues-machine!
"Create a remote issue machine with the given url."
(fn []
+ (def owner-keys (lookup :public-key (read-keys!)))
(def id (machine/new-machine!))
(machine/send-prelude! id)
(send-code! id (find-module-file! "monadic/issue-remote.rad"))
+ (machine/send-signed-command! id radicle-issues-machine-id 'add-admin {:key owner-keys})
id))
(def simple-issue
@@ -135,19 +137,22 @@
"The monadic issues machine works."
[:setup
(do (machine/install-remote-machine-fake)
- (use-fake-keys!)
+ (def owner-keys (gen-key-pair! (default-ecc-curve)))
+ (def non-owner-keys (gen-key-pair! (default-ecc-curve)))
+ (set-fake-keys! owner-keys)
(def machine (create-issues-machine!))
- (use-fake-keys!)
(simple-create-issue! machine "title0" "body0")
(simple-create-issue! machine "title1" "body1")
(simple-add-comment! machine 0 "comment0" "name0")
- (simple-add-comment! machine 1 "comment1" "name1")
(simple-edit-issue! machine 0 {:state :closed})
(simple-edit-comment! machine 0 0 {:body "comment0-edit"})
+ (set-fake-keys! non-owner-keys)
+ (simple-add-comment! machine 1 "comment1" "name1")
(simple-create-issue! machine "new" "new")
(simple-create-issue! machine "newer" "newer")
(simple-add-comment! machine 0 "new" "name0")
- (def is (list-issues machine)))
+ (def is (list-issues machine))
+ (set-fake-keys! owner-keys))
]
[ (length (seq is)) ==> 4 ]
[ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
@@ -157,4 +162,69 @@
[ (short (first (values is))) ==> "0: title0" ]
[ (short (first (list-last machine 1))) ==> "3: newer" ]
[ (view (.. (@ 0) (@ :state)) is) ==> :closed ]
+ ;; authorization
+ ;; owner can edit comments
+ [ (do
+ (set-fake-keys! owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-comment! machine 0 0 {:body "edit"})
+ :valid)
+ (fn [_] :not-valid))) ==> :valid
+ ]
+ ;; author can edit comments
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-comment! machine 0 1 {:body "edit"})
+ :valid)
+ (fn [_] :not-valid))) ==> :valid
+ ]
+ ;; non-owner/non-author cannot edit comments
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-comment! machine 0 0 {:body "edit"})
+ :valid)
+ (fn [_] :not-valid))) ==> :not-valid
+ ]
+ ;; owner can edit issues
+ [ (do
+ (set-fake-keys! owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-issue! machine 2 {:state :closed})
+ :valid)
+ (fn [_] :not-valid))) ==> :valid
+ ]
+ ;; author can edit comments
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-comment! machine 0 1 {:body "edit"})
+ :valid)
+ (fn [_] :not-valid))) ==> :valid
+ ]
+ ;; author can edit issues
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-issue! machine 2 {:state :closed})
+ :valid)
+ (fn [_] :not-valid))) ==> :valid
+ ]
+ ;; non-owner/non-author cannot edit comments
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-comment! machine 0 0 {:body "edit"})
+ :valid)
+ (fn [_] :not-valid))) ==> :not-valid
+ ]
+ ;; non-owner/non-author cannot edit issues
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (catch 'validation-failure
+ (do (simple-edit-issue! machine 0 {:state :closed})
+ :valid)
+ (fn [_] :not-valid))) ==> :not-valid
+ ]
)
diff --git a/rad/monadic/project-remote.rad b/rad/monadic/project-remote.rad
index 012b8c5..a6d480f 100644
--- a/rad/monadic/project-remote.rad
+++ b/rad/monadic/project-remote.rad
@@ -13,6 +13,7 @@
(import prelude/nonces :unqualified)
(import prelude/set :as 'set)
(import prelude/machine-remote '[make-updatable-eval] :unqualified)
+(import prelude/auth '[make-simple-auth add-admin] :unqualified)
(def rsms
"A url-based dict of RSMs."
@@ -28,6 +29,8 @@
(def noncer (make-noncer))
+(def auth (make-simple-auth [:admin]))
+
(def validator/rsm
(validator/keys
{:type (validator/type :keyword)
@@ -42,7 +45,8 @@
(validator/and
[ (validator/keys {:nonce (noncer :validator)
:machine-id (validator/= rsm-id)})
- validator/signed] ))
+ validator/signed
+ (auth [:allowed :admin])] ))
(def add-rsm
(fn [r]
@@ -70,9 +74,10 @@
(def commands
"The set of allowed commands."
- {'add-rsm add-rsm
- 'set-meta set-meta
- 'get-meta get-meta
- 'list-rsms list-rsms })
+ {'add-rsm add-rsm
+ 'set-meta set-meta
+ 'get-meta get-meta
+ 'list-rsms list-rsms
+ 'add-admin (add-admin auth)})
-(def eval (make-updatable-eval commands))
+(def eval (make-updatable-eval commands (auth [:allowed :admin])))
diff --git a/rad/monadic/project.rad b/rad/monadic/project.rad
index afbd53e..8f49ed6 100644
--- a/rad/monadic/project.rad
+++ b/rad/monadic/project.rad
@@ -4,7 +4,7 @@
(import prelude/machine '[send-code! send-signed-command! new-machine!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
-(import prelude/key-management '[use-fake-keys!] :unqualified)
+(import prelude/key-management '[use-fake-keys! read-keys!] :unqualified)
(def get-project-url!
(fn []
@@ -32,10 +32,12 @@
(def create-project!
"Create a remote project RSM with the given url."
(fn [meta]
+ (def owner-keys (lookup :public-key (read-keys!)))
(def id (new-machine!))
(machine/send-prelude! id)
(send-code! id (find-module-file! "monadic/project-remote.rad"))
(set-meta! id meta)
+ (machine/send-signed-command! id rsm-id 'add-admin {:key owner-keys})
id))
(def add-rsm!
@@ -74,9 +76,10 @@
[:setup
(do (machine/install-remote-machine-fake)
+ (def owner-keys (gen-key-pair! (default-ecc-curve)))
+ (def non-owner-keys (gen-key-pair! (default-ecc-curve)))
+ (set-fake-keys! owner-keys)
(install-fake-clock)
- (use-fake-keys!)
- (def machine-name "http://bar")
(def machine (create-project! {:name "test" :description "test project"}))
(add-issues-rsm! machine "a")
(add-diff-rsm! machine "b")
@@ -87,4 +90,15 @@
[ (lookup :id (first-rsm-of-type! machine :rad-issues)) ==> "a" ]
[ (lookup :id (first-rsm-of-type! machine :rad-diff)) ==> "b" ]
[ (lookup :id (first-rsm-of-type! machine :rad-repo)) ==> "c" ]
+ ;; authorization
+ [ (do
+ (set-fake-keys! non-owner-keys)
+ (def originally (length (list-rsms! machine)))
+ (def res (catch 'validation-failure
+ (do (add-diff-rsm! machine "d")
+ :valid)
+ ;; check that the machine has in fact not been added
+ (fn [_] :not-valid)))
+ [res (eq? originally (length (list-rsms! machine)))]) ==> [:not-valid #t]
+ ]
)
diff --git a/rad/prelude.rad b/rad/prelude.rad
index 7f9ad96..15dfe3f 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -23,6 +23,7 @@
(file-module! "prelude/util.rad")
(file-module! "prelude/io-utils.rad")
(file-module! "prelude/time.rad")
+(file-module! "prelude/auth.rad")
(file-module! "prelude/test.rad")
(file-module! "prelude/cmd-parsing.rad")
@@ -40,6 +41,7 @@
(import prelude/nonces :unqualified)
(import prelude/util :unqualified)
(import prelude/io-utils :unqualified)
+(import prelude/auth :as 'auth)
(import prelude/machine :as 'machine)
(import prelude/machine-remote :unqualified)
(import prelude/cmd-parsing :unqualified)
diff --git a/rad/prelude/auth.rad b/rad/prelude/auth.rad
new file mode 100644
index 0000000..62c1e0c
--- /dev/null
+++ b/rad/prelude/auth.rad
@@ -0,0 +1,100 @@
+{:module 'prelude/auth
+ :doc "Functions for easily handling authorization on machines."
+ :exports
+ '[make-simple-auth add-admin]}
+
+(import prelude/validation :as 'validation)
+(import prelude/set :as 'set)
+(import prelude/dict '[dict-from-seq keys] :unqualified)
+(import prelude/patterns :unqualified)
+(import prelude/ref '[modify-ref] :unqualified)
+(import prelude/lens '[over-ref @] :unqualified)
+
+(def make-simple-auth
+ "Creates an authorization scheme with any number of groups of users.
+
+ Example:
+
+ (def auth (make-simple-auth [:admin])) ;; creates auth with initial 'admin group'
+ (auth [:add-to :admin \"alice\"]) ;; adds alice to admin
+ (auth [:is-in :admin \"alice\"]) :: #t
+ (auth [:create-group :regular]) ;; creates 'regular' group
+
+ A common pattern is to set up a machine, and only then set the owner of the
+ machine. Since setting the owner is something only owners can do, we provide
+ a way for *initializing* the authentication mechanism. Prior to
+ initialization, `(auth [:allowed <group> <person>])` always succeeds.
+ After `(auth [:initialize])` is called, it returns whatever
+ `(auth [:is-in <group> <person>])` returns.
+"
+ (fn [set-names]
+ (def groups (ref (dict-from-seq (map (fn [k] [k set/empty]) set-names))))
+ (def initialized? (ref #f))
+ (def insert-in
+ (fn [group pub-key]
+ (over-ref groups (@ group) (fn [x] (set/insert pub-key x)))))
+ (def remove-from
+ (fn [group pub-key]
+ (over-ref groups (@ group) (fn [x] (set/delete pub-key x)))))
+ (def create-group
+ (fn [group]
+ (modify-ref groups (fn [x] (insert group set/empty x)))))
+ (def is-in
+ (fn [group pub-key]
+ (def the-group (lookup group (read-ref groups)))
+ ((validation/member the-group) pub-key)))
+ (fn [command]
+ (match command
+ [:add-to 'group 'pub-key] (do
+ ((validation/member (keys (read-ref groups))) group)
+ (insert-in group pub-key))
+ [:remove-from 'group 'pub-key] (do
+ ((validation/member (keys (read-ref groups))) group)
+ (remove-from group pub-key))
+ [:create-group 'group] (create-group group)
+ [:is-in 'group 'pub-key] (is-in group pub-key)
+ [:initialize] (write-ref initialized? #t)
+ [:allowed 'group] (validation/pred "allowed"
+ (fn [msg]
+ (if (read-ref initialized?)
+ (is-in group (lookup :author msg))
+ msg)))
+ 'x (throw 'invalid-command x)))))
+
+(:test "make-simple-auth"
+ [:setup (do
+ (def auth (make-simple-auth [:admin]))
+ (auth [:create-group :regular])
+ (auth [:add-to :admin "alice"])
+ (auth [:add-to :regular "bob"])
+ (auth [:add-to :regular "carol"])
+ (auth [:remove-from :regular "carol"])
+ )]
+
+ [ (auth [:is-in :admin "alice"]) ==> "alice" ]
+ [ (catch 'validation-failure (auth [:is-in :admin "bob"]) (fn [_] :ok))
+ ==> :ok ]
+ [ (catch 'validation-failure (auth [:is-in :regular "carol"]) (fn [_] :ok))
+ ==> :ok ]
+ [ ((auth [:allowed :admin]) "bob") ==> "bob" ]
+ [ (do
+ (auth [:initialize])
+ (catch 'validation-failure ((auth [:allowed :admin]) {:author "bob"}) (fn [_] :ok)))
+ ==> :ok ]
+)
+
+(def add-admin
+ "Add an admin. If the auth is initialized, only an admin is allowed to do so.
+ Otherwise, this initializes the auth.
+
+ The author is taken from the :author field, and the new admin from the :key
+ field.
+
+ NOTE: The author is assumed to be already verified!"
+ (fn [auth]
+ (fn [pub-key-msg]
+ (def pub-key (lookup :key pub-key-msg))
+ (def author-key (lookup :author pub-key-msg))
+ ((auth [:allowed :admin]) author-key)
+ (auth [:add-to :admin pub-key])
+ (auth [:initialize]))))
diff --git a/rad/prelude/key-management.rad b/rad/prelude/key-management.rad
index dbb2b12..ee9edcf 100644
--- a/rad/prelude/key-management.rad
+++ b/rad/prelude/key-management.rad
@@ -2,7 +2,7 @@
:doc "Providing functions for creating and reading key pairs for signing send
commands. Per default, key pairs are stored in `$HOME/.config/radicle/my-keys.rad`
this can be adjusted by setting `$XDG_CONFIG_HOME`."
- :exports '[read-keys! create-keys! use-fake-keys!]}
+ :exports '[read-keys! create-keys! set-fake-keys! use-fake-keys!]}
(import prelude/io :as 'io)
(import prelude/strings :as 'string)
@@ -10,11 +10,17 @@
(def key-pair-mode (ref {:use-fake-key #f}))
-(def use-fake-keys!
- "Bypass reading the keys from `my-keys.rad`. This is intended for testing."
- (fn []
+(def set-fake-keys!
+ "Bypass reading the keys from `my-keys.rad`, using instead the provided keys.
+ This is intended for testing."
+ (fn [keys]
(set-ref key-pair-mode (@ :use-fake-key) #t)
- (set-ref key-pair-mode (@ :fake-key) (gen-key-pair! (default-ecc-curve)))))
+ (set-ref key-pair-mode (@ :fake-key) keys)))
+
+(def use-fake-keys!
+ "Bypass reading the keys from `my-keys.rad`, using newly-generated ones.
+ This is intended for testing."
+ (fn [] (set-fake-keys! (gen-key-pair! (default-ecc-curve)))))
(def base-path
(fn []
diff --git a/rad/prelude/machine-remote.rad b/rad/prelude/machine-remote.rad
index 249ac1d..daf0fe9 100644
--- a/rad/prelude/machine-remote.rad
+++ b/rad/prelude/machine-remote.rad
@@ -17,17 +17,18 @@
(def updatable-eval
"Given an evaluation function `f`, returns a new one which augments `f` with a new
command `(update expr)` which evaluates arbitrary expression using `base-eval`."
- (fn [sub-eval]
+ (fn [sub-eval validator]
(fn [expr state]
(match expr
(/cons 'cmd _) (if (eq? cmd 'update)
- (base-eval (nth 1 expr) state)
+ (do (validator (nth 1 expr))
+ (base-eval (lookup :eval (nth 1 expr)) state))
(sub-eval expr state))
_ (sub-eval expr state)))))
(def make-updatable-eval
- "Make an updatable eval from a list of command mappings."
- (fn [initial-commands]
+ "Make an updatable eval from a list of command mappings and a validator for updates."
+ (fn [initial-commands validator]
(def commands (ref initial-commands))
(def process-command
"Function that maps post-eval commands to existing functions"
@@ -38,7 +39,8 @@
(apply do-this args)))
(updatable-eval
(fn [expr state]
- (eval-fn-app state process-command expr (fn [x] x))))))
+ (eval-fn-app state process-command expr (fn [x] x)))
+ validator)))
(:test "make-updatable-eval"
[:setup
@@ -47,10 +49,22 @@
(def commands
{'inc (fn [] (modify-ref counter (fn [x] (+ x 1))))
'get (fn [] (read-ref counter))})
- (def ev (make-updatable-eval commands))
+ (def ev
+ (make-updatable-eval
+ commands
+ (fn [x] (if (eq? (lookup :pwd x) "secret")
+ :ok
+ (throw 'validation-failure "nope")))))
(def res (ev '(inc) (get-current-state)))
(def st (nth 1 res))
)
]
[ (nth 0 (ev '(get) st)) ==> 1 ]
+ [ (catch 'validation-failure
+ (ev '(update {:pwd "blah" :eval eval}) st)
+ (fn [_] :ok)) ==> :ok ]
+ [ (catch 'validation-failure
+ (do (ev '(update {:pwd "secret" :eval eval}) st)
+ :ok)
+ (fn [_] :not-ok)) ==> :ok ]
)
diff --git a/rad/prelude/machine.rad b/rad/prelude/machine.rad
index a9b1b6d..1cface5 100644
--- a/rad/prelude/machine.rad
+++ b/rad/prelude/machine.rad
@@ -145,6 +145,7 @@
"prelude/state-machine.rad"
"prelude/validation.rad"
"prelude/nonces.rad"
+ "prelude/auth.rad"
"prelude/machine-remote.rad"
"prelude/util.rad"]))
diff --git a/rad/prelude/validation.rad b/rad/prelude/validation.rad
index ecc6024..73badd1 100644
--- a/rad/prelude/validation.rad
+++ b/rad/prelude/validation.rad
@@ -5,12 +5,17 @@
:exports
'[= member and or type pred integral optional-key contains contains-all
contains-only key optional-keys keys every uuid signed timestamp
- string-of-max-length]}
+ string-of-max-length always-valid]}
(import prelude/dict :unqualified)
(import prelude/dict :as 'dict)
(import prelude/set :as 'set)
+(def always-valid
+ "A validator that is always valid."
+ (fn [x] x))
+
+
(def =
"Given `x`, returns a validator that checks for equality with `x`."
(fn [x]
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index c78e2db..7eff072 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -158,7 +158,7 @@ errorDataToValue e = case e of
SendError se -> makeVal ( "send-error", [("info", String se)] )
Exit code -> makeVal ("exit", [("code", toRad code)])
where
- makeA = quote . Atom
+ makeA = Atom
makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys (Keyword . Ident) . GhcExts.fromList $ v)
newtype Reference = Reference { getReference :: Int }
diff --git a/src/Radicle/Internal/Eval.hs b/src/Radicle/Internal/Eval.hs
index 658f850..4f9f0b5 100644
--- a/src/Radicle/Internal/Eval.hs
+++ b/src/Radicle/Internal/Eval.hs
@@ -86,13 +86,16 @@ specialForms = Map.fromList $ first Ident <$>
, ("catch", \case
[l, form, handler] -> do
mlabel <- baseEval l
- handlerclo <- baseEval handler
+ s <- get
case mlabel of
-- TODO reify stack
Atom label -> baseEval form `catchError` \err@(LangError _stack e) -> do
(thrownLabel, thrownValue) <- errorDataToValue e
if thrownLabel == label || label == Ident "any"
- then handlerclo $$ [thrownValue]
+ then do
+ put s
+ handlerclo <- baseEval handler
+ callFn handlerclo [thrownValue]
else throwError err
_ -> throwErrorHere $ SpecialForm "catch" "first argument must be atom"
xs -> throwErrorHere $ WrongNumberOfArgs "catch" 3 (length xs))