I am trying to set an RSA private key via an environment variable, but am having trouble with it being read/parsed correctly when I spin my Go app up in a docker container. It works fine natively via go run ...
, so I feel like I am missing something here.
I am just reading in my key
variable as a string and then trying to decode it to use later on. I’ve found that hardcoding the private key value inline exactly the same format as the .env file allows it to be decoded correctly, but for whatever reason reading it in from the .env causes errors.
My code:
block, rest := pem.Decode([]byte(key))
parseResult, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatal("private key parsing error: ", err)
}
privateKeyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(parseResult),
},
)
It fails on the second line, but the error is just panic: runtime error: invalid memory address or nil pointer dereference
, so it seems like the decoding in the first line is not happening correctly which causes it to fail there. Is there a specific way to format a private key within a .env file or a different way to read/parse it that I’m not doing?
1