Skip to content

Commit

Permalink
Ensure label updates work with API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinqian00 committed Feb 4, 2025
1 parent de1bbe5 commit ced1408
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/main/lrsql/admin/interceptors/credentials.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
(if-some [err (or (when key-pair?
(s/explain-data as/key-pair-spec params))
(when scopes?
(s/explain-data as/scopes-spec params)))]
(s/explain-data as/scopes-spec params))
(when (:label params)
(s/explain-data as/label-spec params)))]
;; Invalid parameters - Bad Request
(assoc (chain/terminate ctx)
:response
Expand All @@ -31,6 +33,7 @@
;; Valid parameters - continue
(let [cred-info (select-keys params [:api-key
:secret-key
:label
:scopes])]
(-> ctx
(assoc ::data cred-info)
Expand Down
3 changes: 3 additions & 0 deletions src/main/lrsql/spec/auth.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
(def scopes-spec
(s/keys :req-un [::scopes]))

(def label-spec
(s/keys :req-un [::label]))

(def scoped-key-pair-spec
(s/keys :req-un [::api-key
::secret-key
Expand Down
22 changes: 16 additions & 6 deletions src/test/lrsql/admin/route_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,12 @@
{:headers hdr
:body (u/write-json-str
{"scopes" ["all" "all/read"]})})
{:strs [api-key secret-key scopes]}
{:strs [api-key secret-key label scopes]}
(u/parse-json body)]
(is (= 200 status))
(is (re-matches Base64RegEx api-key))
(is (re-matches Base64RegEx secret-key))
(is (nil? label))
(is (= #{"all" "all/read"} (set scopes)))
(testing "and reading"
(let [{:keys [status body]}
Expand All @@ -581,6 +582,7 @@
(is (= 200 status))
(is (= {"api-key" api-key
"secret-key" secret-key
"label" nil
"scopes" scopes}
(-> body
(u/parse-json :object? false)
Expand All @@ -596,10 +598,12 @@
{:headers hdr
:body (u/write-json-str {"api-key" api-key
"secret-key" secret-key
"label" "My Label"
"scopes" req-scopes})})
{:strs [scopes]}
{:strs [label scopes]}
(u/parse-json body)]
(is (= 200 status))
(is (= "My Label" label))
(is (= #{"all/read" "statements/read" "statements/read/mine"}
(set scopes)))))
(testing "and reading after updating"
Expand All @@ -609,6 +613,7 @@
{:headers hdr})
{api-key' "api-key"
secret-key' "secret-key"
label' "label"
scopes' "scopes"}
(-> body
(u/parse-json :object? false)
Expand All @@ -617,9 +622,10 @@
(is (= 200 status))
(is (= api-key api-key'))
(is (= secret-key secret-key'))
(is (= "My Label" label'))
(is (= #{"all/read" "statements/read" "statements/read/mine"}
(set scopes')))))
(testing "and no-op scope update"
(testing "and no-op label + scope update"
(let [req-scopes
["all/read" "statements/read" "statements/read/mine"]
{:keys [status body]}
Expand All @@ -628,23 +634,27 @@
{:headers hdr
:body (u/write-json-str {"api-key" api-key
"secret-key" secret-key
"label" "My Label"
"scopes" req-scopes})})
{:strs [scopes]}
{:strs [label scopes]}
(u/parse-json body)]
(is (= 200 status))
(is (= "My Label" label))
(is (= #{"all/read" "statements/read" "statements/read/mine"}
(set scopes)))))
(testing "and deleting all scopes"
(testing "and deleting label and all scopes"
(let [{:keys [status body]}
(curl/put
"http://0.0.0.0:8080/admin/creds"
{:headers hdr
;; Not including label key = nil label
:body (u/write-json-str {"api-key" api-key
"secret-key" secret-key
"scopes" []})})
{:strs [scopes]}
{:strs [label scopes]}
(u/parse-json body)]
(is (= 200 status))
(is (= nil label))
(is (= #{} (set scopes)))))
(testing "and deletion"
(let [{:keys [status]}
Expand Down

0 comments on commit ced1408

Please sign in to comment.