Fork me on GitHub
#babashka
<
2023-10-10
>
alpox09:10:58

I was trying to get a websocket running with https://github.com/babashka/http-client but did not find a way to create a private key from a pem-formatted file such that I could include it in the keystore. It seems that java.security.KeyFactory is not included in babashka. Is there a known workaround? Sidenote: I’m creating the SSLContext by hand and not through the library as I’m building the keystore and truststore dynamically. I don’t have files for them.

borkdude09:10:25

These related classes are in bb: https://github.com/babashka/babashka/blob/7ecd2fe59e2f33c4aa98bfdcc54d4c5d5f1b1694/src/babashka/impl/classes.clj#L397-L402 Could you provide an example of how you are using KeyFactory? We can of course include it

borkdude09:10:37

Let me just add this class and you can try to use the master version when the build finishes, would that be ok?

alpox09:10:05

Sure, that works too 🙂 thanks!

borkdude09:10:22

what OS are you on?

alpox09:10:35

I’m on MacOS Ventura

alpox09:10:43

I just see it might be missing more classes for this 😕

alpox09:10:55

Let me check for short

borkdude09:10:56

Then this should already work now:

bash <(curl ) --dev-build --dir /tmp

borkdude09:10:02

ok, just let me know

alpox09:10:04

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/security/spec/PKCS8EncodedKeySpec.html would also be missing for enabling the generation of the private key from PEM

borkdude09:10:05

Maybe you could give me a full snippet that I can try locally

alpox09:10:14

(ns client
  (:require
   [clojure.string :as str])
  (:import 
   [java.util Base64]
   [java.security KeyFactory]
   [java.security.spec PKCS8EncodedKeySpec]))

(defn load-pem-pk [file]
  (let [key (slurp file)
        private-key-pem (-> key
                            (str/replace "-----BEGIN PRIVATE KEY-----" "")
                            (str/replace "-----END PRIVATE KEY-----" "")
                            (str/replace (System/getProperty "line.separator") ""))
        encoded (-> private-key-pem
                     (Base64/decodeBase64))]
    (with-open [input-stream (java.io.ByteArrayInputStream. encoded)]
      (let [key-factory (KeyFactory/getInstance "RSA")
            key-spec (PKCS8EncodedKeySpec. (.readAllBytes input-stream))]
        (.generatePrivate key-factory key-spec)))))

borkdude09:10:36

including imports

alpox09:10:37

Its untested as of the missing classes but thats the goal

alpox09:10:58

(ns client
  (:require
   [clojure.string :as str])
  (:import 
   [java.util Base64]
   [java.security KeyFactory]
   [java.security.spec PKCS8EncodedKeySpec]))

borkdude09:10:06

hehe no prob ;)

borkdude09:10:55

do you perhaps have a file that I can test this function on?

alpox09:10:58

I can also hand you a test (random) key:

-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDNTlYgSqTUgY4H
LNDkk9TRrMXwTTR6UBdG63z6VC1RuaIsB+X20qrgnkWaDoMmn/BBg5YoPA3LI95C
sOCYJtFGHRulVmyQTTjiaLBx2wa7oh3YsbLQbmpvtqw0qSU3SEiVmHOSCu31xxG0
guJG7CTjFM2scH4o1NoUP3CQTG2b5cjMWHNVmDfe9y6aLO4DGifQavufg7rSC1TN
5qNlGK707UiNtcSMD91Z2G/yuDFnAi6zRwru8enOVW8oLxLiY6Hk3lWs5bXFKUdR
TVDIKG2a+yutrqYUAhcIL3JwprUyrj7/nKnCdFY5NrK5hzo4alT9hh81kBpMClA/
/VKRi8OXYeO/5Utpe7+lAEHEgm0sOGAFzipUiGAaKoazYRniYyg4kQeuAed7SdEp
yS+8JTVQSIuOGnjGIchddIdlkjZA+c6wS4lK6je/TZQbsSqPcmEosUof0g9zMEI7
WRbTff6CE2LhdOd+GiX+gwkdJHMKrcUIgA1Rsyfe05lzxTn8gS+MTJCRZf7nAsPs
rawQ2YlKGy7Z0REc7Z4RE7JIcAemZ39bRdi+cT0zuLuoW5TZtFc9N11oG4Ar63wC
J/17plK/wqpraQ7vnFzMhFJ84Bb8kR1o8ZvFLKADriOJio8oYqCJdsb1MzNEqWxo
7Y2kgfabeQA29cx7807tq2jD/jc6jwIDAQABAoICADuun5vghx0dklekoA8qqG3R
j3MFRpGd8b5Um667xLbiw3GQVDoV0MXeTLZ1rIuQaAvrgD2vA4V/pk910rx4YFLF
CHHed03VnQk4TL60YLcv8AQ72BL+XiOek131o9N3yF3whPw6ACX4Gj4C5FugrzJy
LD4KL6qYAdNsA8F27lApEt/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DpQ/9SKBLiHcJunrtvhr2nWsSFTPlZnVYqsn9nr1TX9TzHng2
hy1au3GPOgexL81uMDs5PHyX5ALyzd1zO/+Tmg7bOzOJjggxYioCjyUaWom8DXNM
p0o468Znlgp+xcm4JolgkijqVlRE
-----END PRIVATE KEY-----

borkdude09:10:14

nice, thanks

borkdude09:10:28

When I run this with JVM Clojure I get:

$ clj -M /tmp/dude.bb
Syntax error (IllegalArgumentException) compiling . at (/tmp/dude.bb:16:22).
No matching method decodeBase64 found taking 1 args for class java.util.Base64

borkdude09:10:59

This works:

encoded (->> private-key-pem
                     (.decode (Base64/getDecoder)))

alpox09:10:06

Oh sorry, got that part wrong - I didn’t finish up testing with it before

borkdude09:10:26

and it also works when I add that spec class

borkdude09:10:30

so I'll also push that one

alpox09:10:02

Awesome, thank you a lot!

borkdude09:10:11

Alright, repeat :

bash <(curl ) --dev-build --dir /tmp
and it should work now

alpox09:10:43

Looks good, I can read in the private key now just fine. Thanks a lot again @U04V15CAJ! That is some fast support here

👍 1
pez13:10:56

Hi! I am constructing a path like so:

(-> (fs/temp-dir) (str fs/file-separator "pango.png"))
Is there a way I can construct it without involving the separator? Like using only file objects and then str everything, or something.

borkdude13:10:29

euh yeah... (fs/path (fs/temp-dir) "pango.png")

borkdude13:10:28

in fact, I would discourage creating strings yourself, just provide multiple paths to fs/path

pez13:10:25

I had a feeling I was holding it wrong. Thanks! 🙏

📱 1
Matthew Twomey23:10:38

I just recently did a bunch of stuff with babashka-fs myself. I had a very mild interest in keeping it windows compatible. I assumed if I avoided any strings of my own, it would remain OS independent is that true?

👍 1