Skip to content

Commit 7bf4aa9

Browse files
committed
added token and cookie auth
1 parent 088de46 commit 7bf4aa9

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

src/cli/com/yetanalytics/datasim/cli/generate.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
(def password-desc
5757
"The Basic Auth password for the LRS.")
5858

59+
(def token-desc
60+
"An optional bearer token to use in the Authorization header (overrides username and password if present).")
61+
62+
(def cookie-desc
63+
"An optional cookie string to send in the Cookie header.")
64+
5965
(def batch-size-desc
6066
"The batch size, i.e. how many statements to send at a time, for POSTing.")
6167

@@ -76,6 +82,12 @@
7682
["-P" "--password URI" "LRS password"
7783
:id :password
7884
:desc password-desc]
85+
[nil "--token TOKEN" "Bearer Token"
86+
:id :token
87+
:desc token-desc]
88+
[nil "--cookie COOKIE" "Authentication Cookie"
89+
:id :cookie
90+
:desc cookie-desc]
7991
["-B" "--batch-size SIZE" "LRS POST batch size"
8092
:id :batch-size
8193
:default 25
@@ -154,6 +166,8 @@
154166
(let [{:keys [endpoint
155167
username
156168
password
169+
token
170+
cookie
157171
batch-size
158172
concurrency
159173
post-limit
@@ -164,7 +178,9 @@
164178
{:endpoint endpoint
165179
:batch-size batch-size
166180
:username username
167-
:password password}]
181+
:password password
182+
:token token
183+
:cookie cookie}]
168184
(if async
169185
(post-async! input post-options post-limit select-agents concurrency)
170186
(post-sync! input post-options post-limit select-agents))))

src/main/com/yetanalytics/datasim/client.clj

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
(format "%s/statements" endpoint))
2626

2727
(defn- post-options [http-options batch]
28-
(merge default-http-options
29-
http-options
30-
{:body (json/encode batch)
31-
:as :stream}))
28+
(merge-with merge
29+
default-http-options
30+
http-options
31+
{:body (json/encode batch)
32+
:as :stream}))
3233

3334
;; `http/post` cannot be resolved since it's defined using `http/defreq`
3435
#_{:clj-kondo/ignore [:unresolved-var]}
@@ -41,21 +42,34 @@
4142
(post-options http-options batch)
4243
callback-fn)))
4344

45+
(defn- auth-options
46+
[{:keys [username
47+
password
48+
token
49+
cookie]}]
50+
(cond
51+
(and username password)
52+
{:basic-auth [username password]}
53+
token
54+
{:headers {"Authorization" (format "Bearer %s" token)}}
55+
cookie
56+
{:headers {"Cookie" token}}
57+
:else {}))
58+
4459
(defn post-statements
4560
"Given LRS options and a `statement-seq`, send them to an LRS in synchronous
4661
batches. If `print-ids?` is `true`, returned statement IDs will be printed
4762
to stdout. `username` and `password` in the options map are the Basic Auth
4863
credentials of the LRS."
4964
[{:keys [endpoint
50-
batch-size
51-
username
52-
password]
65+
batch-size]
66+
:as options
5367
:or {batch-size 25}}
5468
statement-seq
5569
& {:keys [print-ids?]
5670
:or {print-ids? true}}]
5771
;; TODO: Exponential backoff, etc
58-
(let [http-options {:basic-auth [username password]}]
72+
(let [http-options (auth-options options)]
5973
(loop [batches (partition-all batch-size statement-seq)
6074
success 0
6175
fail []]
@@ -82,14 +96,14 @@
8296
(defn post-statements-async
8397
"Given LRS options and a channel with statements, send them to an LRS in
8498
asynchronous batches. `username` and `password` in the options map are the
85-
Basic Auth credentials of the LRS.
99+
Basic Auth credentials of the LRS. Other auth methods are supported via
100+
`token` and `cookie`.
86101
87102
Returns a channel that will reciveve `[:success <list of statement ids>]`
88103
for each batch or `[:fail <failing request>]`. Will stop sending on failure."
89104
[{:keys [endpoint
90-
batch-size
91-
username
92-
password]
105+
batch-size]
106+
:as options
93107
:or {batch-size 25}}
94108
statement-chan
95109
& {:keys [concurrency
@@ -98,7 +112,7 @@
98112
:or {concurrency 4
99113
buffer-in 100 ; 10x default batch size
100114
buffer-out 100}}]
101-
(let [http-opts {:basic-auth [username password]}
115+
(let [http-opts (auth-options options)
102116
run? (atom true)
103117
in-chan (a/chan buffer-in (partition-all batch-size))
104118
out-chan (a/chan buffer-out) ; is this.. backpressure?

0 commit comments

Comments
 (0)