Skip to content

Instantly share code, notes, and snippets.

@bzed
Created January 9, 2019 12:15
Show Gist options
  • Save bzed/e1e6c76e98225f997062c57aacecd2fd to your computer and use it in GitHub Desktop.
Save bzed/e1e6c76e98225f997062c57aacecd2fd to your computer and use it in GitHub Desktop.
haproxy pem formatted ssl client cert fetch - ssl_c_pem - like ssl_c_der
require("base64")
core.register_fetches("ssl_c_pem", function(txn)
local der = txn.f:ssl_c_der()
local wrap = ('.'):rep(64);
local envelope = "-----BEGIN %s-----\n%s\n-----END %s-----\n"
local typ = "CERTIFICATE";
der = base64.encode(data);
return string.format(envelope, typ, der:gsub(wrap, '%0\n', (#der-1)/64), typ);
end)
@helavatar
Copy link

Hi. What are dependencies besides 'base64' package for this script? When I try to use it I get following in HAProxy error log.

haproxy[2263]: Lua sample-fetch 'ssl_c_pem': runtime error: /etc/haproxy/ssl_c_pem.lua:9: attempt to index a nil value (global 'base64') /etc/haproxy/ssl_c_pem.lua:9 C function line 3.

@bzed
Copy link
Author

bzed commented Apr 20, 2021

That basically means that it can't find the base64 functions.

Maybe try

local base64 = require'base64'

somewhere. probably in the function?

@helavatar
Copy link

That basically means that it can't find the base64 functions.

Maybe try

local base64 = require'base64'

somewhere. probably in the function?

I forgot to mention that I installed base64 from luarocks:
https://luarocks.org/modules/iskolbin/base64

Before that I had this error message:

[ALERT] 108/233244 (20196) : parsing [/etc/haproxy/haproxy.conf:14] : lua runtime error: /etc/haproxy/ssl_c_der.lua:1: module 'base64' not found:
no field package.preload['base64']
no file '/usr/share/lua/5.3/base64.lua'
no file '/usr/share/lua/5.3/base64/init.lua'
no file '/usr/lib64/lua/5.3/base64.lua'
no file '/usr/lib64/lua/5.3/base64/init.lua'
no file './base64.lua'
no file './base64/init.lua'
no file '/usr/lib64/lua/5.3/base64.so'
no file '/usr/lib64/lua/5.3/loadall.so'
no file './base64.so'

@gkatev
Copy link

gkatev commented Sep 22, 2021

Where does the 'data' variable on line 9 get defined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment