babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
borkdude 2020-11-20T10:36:13.382Z

Created a news page for babashka-related items: https://github.com/borkdude/babashka/blob/master/doc/news.md

2020-11-20T10:39:57.382400Z

nice!

helios 2020-11-20T13:08:30.382700Z

what's the best way of generating a hmac-sha1 using bb?

borkdude 2020-11-20T13:13:12.383400Z

@helios This question came up recently. The crypto class for that is currently missing, but the person solved this by shelling out to openssl

Travis Jefferson 2020-11-20T13:13:20.383600Z

Hey that was me!

helios 2020-11-21T08:38:52.410600Z

@mkvlr it's about getting the hmac-sha1 of a string given a specific key for API signature 😢

helios 2020-11-21T08:38:58.410800Z

but i got my answer 😄

helios 2020-11-20T13:14:28.383700Z

👋 do you have an example?

Travis Jefferson 2020-11-20T13:14:35.383900Z

(defn sign-with-string [key msg]
  (-> (sh "openssl" "dgst" "-sha256" "-mac" "hmac" "-macopt" (str "key:" key) :in msg)
      :out
      str/trim))

(defn sign-with-hex [key msg]
    (-> (sh "openssl" "dgst" "-sha256" "-mac" "hmac" "-macopt" (str "hexkey:" key) :in msg)
        :out
        str/trim))

helios 2020-11-20T13:14:48.384100Z

thanks!

Travis Jefferson 2020-11-20T13:15:03.384300Z

I needed sha256 but I believe swapping in -sha1 is as easy as it sounds

borkdude 2020-11-20T13:15:04.384500Z

We should add docs for this

Travis Jefferson 2020-11-20T13:15:57.384700Z

annoying bit for me was knowing which bits support binary data and which are limited to string

Travis Jefferson 2020-11-20T13:16:54.384900Z

you can pass bytes to sh with :in, which is cool, but I couldn’t figure out a way to pass bytes in for a command option/flag (I had binary data that I wanted to use for a key)

borkdude 2020-11-20T13:17:14.385100Z

oh

Travis Jefferson 2020-11-20T13:17:15.385300Z

so I ended up hex-encoding my binary keys and passing as a string with sign-with-hex

Travis Jefferson 2020-11-20T13:18:09.385600Z

there are probably ways to work around, perhaps by spinning up a file to hold the binary key, or maybe some fancy shell tricks to pipe in binary data

Travis Jefferson 2020-11-20T13:18:19.385800Z

but this was good enough for me

borkdude 2020-11-20T13:18:38.386Z

Since there are now two people needing this, https://github.com/borkdude/babashka/issues/656 Please post which classes / Clojure examples should be needed for this

borkdude 2020-11-20T13:19:35.386500Z

We could also make a small babashka.crypto namespace (or some other name) with util functions

Travis Jefferson 2020-11-20T13:23:59.386700Z

posted an example! it’s java code 🙈

borkdude 2020-11-20T13:24:32.386900Z

no problem

Travis Jefferson 2020-11-20T13:27:23.387100Z

making classes available and (optionally) providing a small utility ns both sound solid to me

Travis Jefferson 2020-11-20T13:29:33.387400Z

at a glance, buddy-core’s APIs look nice; adding that might be a good longer-term goal if it’s reasonably compatible I suspect @borkdude would have the best intuition for what’s a good fit or not 😁

helios 2020-11-20T13:33:14.387600Z

i also posted some more clojure code (that uses java)

helios 2020-11-20T13:33:23.387800Z

a little offtopic, and what about base64 encoding? 😄

skuro 2020-11-20T13:46:52.390100Z

so I have this problem at hand: I've a 1.3G json file with stuff I need to feed an ElasticSearch instance with. The json itself contains a huuuuge JSON array. I'd like bb to lazily parse such array, chunk the contained objects into smaller batches then feed each batch to ES for ingestion

skuro 2020-11-20T13:47:05.390500Z

seems to me that this is not possible with bb tho, correct me if I'm wrong

skuro 2020-11-20T13:47:51.391300Z

as in, when I try to reach into the internals of cheshire which I need to lazily parse the array, it fails to resolve names / classes (even the basic jackson classes)

borkdude 2020-11-20T14:11:46.391400Z

@helios

(deftest Base64-test
  (is (= "babashka"
         (bb nil "(String. (.decode (java.util.Base64/getDecoder) (.encode (java.util.Base64/getEncoder) (.getBytes \"babashka\"))))"))))

helios 2020-11-20T14:11:59.391600Z

thanks 🙂

borkdude 2020-11-20T14:12:52.392600Z

@skuro afaik cheshire has an option to parse an array lazily, but I'm not sure if this works for an array that is embedded into something else

borkdude 2020-11-20T14:13:26.393200Z

for this amount of data it's probably better to just use Clojure on the JVM

borkdude 2020-11-20T14:15:02.393400Z

If the top-level object is an array, it will be parsed lazily (use `parse-strict' if strict parsing is required for top-level arrays.

borkdude 2020-11-20T14:25:16.393500Z

I think I prefer the babashka.crypto namespace. There we can collect functions that are most frequently used for scripting. Instead of including buddy which can grow babashka bigger than necessary. Also offering babashka.crypto will help swapping out different implementations in the future.

👍 2
mkvlr 2020-11-20T15:00:28.395100Z

can also shell out to shasum if it’s just about hashing…

shasum -a 256 tests.edn                                                                                                                                              
c0050533cb04ed9b5b3fc851b062830bac1c0c8f5569a3e4cec8bca9bb6dd54f  tests.edn

👍 1
borkdude 2020-11-20T15:04:05.395400Z

good one

skuro 2020-11-20T15:13:39.396400Z

oh, I see... I did json/parsed-seq but json/parse seems be be the only one doing lazy stuff

skuro 2020-11-20T15:13:41.396600Z

tnx

borkdude 2020-11-20T15:22:48.396900Z

parsed-seq parses multiple top-level values

borkdude 2020-11-20T17:03:33.398200Z

Is anyone using the smile functions from cheshire? I think I will remove them for the next release to free up 200kb from the binary, since I haven't seen anyone use them and I also don't use them myself. If there are any objections, I'll revert.

nate 2020-11-20T17:32:45.398400Z

no objections from me, I haven't used the smile functions ever, even in jvm clojure

nate 2020-11-20T17:33:41.399500Z

I'm curious, has anyone tried running buddy in babashka yet? I have need for doing some crypto stuff with it, and it would be convenient to be able to use bb

borkdude 2020-11-21T19:05:27.434400Z

And...? :)

nate 2020-11-21T19:11:27.435300Z

Haha. Haven't had a spare moment yet. First chance is tomorrow evening.

borkdude 2020-11-21T19:12:44.435500Z

No worries! :)

nate 2020-11-20T17:34:56.399600Z

specifically, buddy.core.mac buddy.core.codecs and buddy.core.nonce, to create an HMAC signature to pass to a curl call

borkdude 2020-11-20T17:35:29.399800Z

it's funny that you mention this! hmac has been coming up two times earlier this week. it's in the air :) https://github.com/borkdude/babashka/issues/656

borkdude 2020-11-20T17:35:54.400200Z

could you post in that issue what specifics you would need?

nate 2020-11-20T17:36:44.400400Z

cool, I'll try and make a small example

borkdude 2020-11-20T17:37:07.400600Z

note that currently you could shell out to openssl and shasum.

nate 2020-11-20T18:15:47.400800Z

commented with my JVM code, will comment again with my openssl-calling code, if I can get it to work 🤞

Dig 2020-11-20T21:12:01.401200Z

same here never needed them

yubrshen 2020-11-20T22:25:03.402900Z

I'd like to learn to use babashka to make http request. If you have some sample code or reference, would you please share. Thanks!

borkdude 2020-11-20T22:26:39.403100Z

@yubrshen check out the docs here: https://github.com/borkdude/babashka.curl

yubrshen 2020-11-20T23:17:14.403700Z

@borkdude Thanks! I'll try them.