# Private/public key generation for self-hosted openFN

**URL:** <https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454>\
**Category:** Self-Hosting\
**Created:** [June 19, 2024, 11:48am UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454 "2024-06-19T11:48:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibrahimwickama](https://community.openfn.org/user_avatar/community.openfn.org/ibrahimwickama/32/200_2.png) [@ibrahimwickama](https://community.openfn.org/u/ibrahimwickama)\
**Post date:** [June 19, 2024, 11:48am UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/1 "2024-06-19T11:48:08Z")

</div>

Hello team,

I have tried using the production images, but i’m facing a challenge on private key public key on env file. How are this keys generated or do you already have predefined keys.?

 ![Screenshot 2024-06-19 at 13.47.34](https://community.openfn.org/uploads/default/original/1X/9368932aa30695aa25fcb1a9e9ae4f998766afed.jpeg)

---

<div class="post-metadata">

**Author:** ![taylordowns2000](https://community.openfn.org/user_avatar/community.openfn.org/taylordowns2000/32/165_2.png) [@taylordowns2000](https://community.openfn.org/u/taylordowns2000)\
**Post date:** [June 19, 2024, 2:39pm UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/2 "2024-06-19T14:39:46Z")

</div>

Hey @ibrahimwickama ! Great to see you on the forum ❤ .

Here’s the main docs on generating keys: [Deployment — Lightning v2.13.4](https://openfn.github.io/lightning/deployment.html)

And for worker keys in particular there’s a mix task (`mix lightning.gen_worker_keys`) that will do it for you.

Please let me know if this works.

---

<div class="post-metadata">

**Author:** ![ibrahimwickama](https://community.openfn.org/user_avatar/community.openfn.org/ibrahimwickama/32/200_2.png) [@ibrahimwickama](https://community.openfn.org/u/ibrahimwickama)\
**Post date:** [June 19, 2024, 5:33pm UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/3 "2024-06-19T17:33:30Z")

</div>

Hey @taylordowns2000

Thanks for the assist, am glad to say that has work great. Just bumped to another Error message for failure to decode PEM.

 ![Screenshot 2024-06-19 at 19.30.05](https://community.openfn.org/uploads/default/original/1X/dee0780a6ccbbcb8d8b55c4c9f33faa7b10256e0.jpeg)

Any ideas on this..?

---

<div class="post-metadata">

**Author:** ![taylordowns2000](https://community.openfn.org/user_avatar/community.openfn.org/taylordowns2000/32/165_2.png) [@taylordowns2000](https://community.openfn.org/u/taylordowns2000)\
**Post date:** [June 19, 2024, 8:03pm UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/4 "2024-06-19T20:03:19Z")

</div>

failure to decode PEM? oh man, may be beyond my expertise.

@stu , @elias , @rogerio , do you guys have ideas here?

i will look more tomorrow AM too. did you generate all those keys with the mix command? and did you pass them into the ENV?

here’s the line that’s complaining: [lightning/config/runtime.exs at main · OpenFn/lightning · GitHub](https://github.com/OpenFn/lightning/blob/main/config/runtime.exs#L141-L150)

looks like your key should be base64 encoded before you pass it into the `WORKER_RUNS_PRIVATE_KEY` environment variable.

hope this helps 🙏

---

<div class="post-metadata">

**Author:** ![ibrahimwickama](https://community.openfn.org/user_avatar/community.openfn.org/ibrahimwickama/32/200_2.png) [@ibrahimwickama](https://community.openfn.org/u/ibrahimwickama)\
**Post date:** [June 19, 2024, 8:54pm UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/5 "2024-06-19T20:54:37Z")

</div>

Yeah sure thing, the variables were included on .env with base64 key generated by the mix command outputs.

Which were WORKER\_RUNS\_PRIVATE\_KEY,WORKER\_SECRET,WORKER\_LIGHTNING\_PUBLIC\_KEY and PRIMARY\_ENCRYPTION\_KEY

Thus after booting again now it yield’s the above new error faced.

Hope @stu , @elias may help shade some light.

Regards.

---

<div class="post-metadata">

**Author:** ![taylordowns2000](https://community.openfn.org/user_avatar/community.openfn.org/taylordowns2000/32/165_2.png) [@taylordowns2000](https://community.openfn.org/u/taylordowns2000)\
**Post date:** [June 20, 2024, 6:19am UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/6 "2024-06-20T06:19:45Z")

</div>

Hrmm. “Could not decode PEM” is the result we get when

`your_key_here |> Base.decode64(padding: false)` returns `:error`.

The whole code block is doing this:

```ex
encoded
  |> Base.decode64(padding: false)
  |> case do
    {:ok, pem} -> pem
    :error -> raise "Could not decode PEM"
  end

```

(src: [lightning/config/runtime.exs, line 141-150](https://github.com/OpenFn/lightning/blob/main/config/runtime.exs#L141-L150))

So there is something about how that key is getting set as an environment variable that your system is not liking.

I just did a quick test: if I paste the base64 key that’s generated by the mix command into an interactive Elixir shell (`iex`) and pipe it into `Base.decode64(padding: false)` it works like a charm.

If, however, I have a space or a line break before or after the string it returns `:error`. I hope I’m not sending you on a wild goose chase, but could you possible double check that they key you’re setting as your `WORKER_RUNS_PRIVATE_KEY` is _just_ the base64 string itself, no spaces or line breaks before or after?

Then two more things that might help debug (if this fails):

1. What operating system are you using?
2. Does your `.env` file look like this: `WORKER_RUNS_PRIVATE_KEY="blahblahblahwithnospacesorlinebreaks"`

---

<div class="post-metadata">

**Author:** ![midigofrank](https://community.openfn.org/user_avatar/community.openfn.org/midigofrank/32/202_2.png) [@midigofrank](https://community.openfn.org/u/midigofrank)\
**Post date:** [June 20, 2024, 6:34am UTC](https://community.openfn.org/t/private-public-key-generation-for-self-hosted-openfn/454/7 "2024-06-20T06:34:45Z")

</div>

> [@taylordowns2000](#):
>
> I just did a quick test: if I paste the base64 key that’s generated by the mix command into an interactive Elixir shell (`iex` ) and pipe it into `Base.decode64(padding: false)` it works like a charm.

Yeah, I have also tried this and it’s working fine.  
@ibrahimwickama in addition to double checking spaces, you can also try getting rid of the quotes. i.e. `WORKER_RUNS_PRIVATE_KEY=blahblah` instead of `"blahblah"`
