Search code examples
pythonamazon-web-servicesjwt

How do I make JWT read "\n" as a new line instead of a literal in a container on EC2?


I have the key -----BEGIN PRIVATE KEY-----\nnwb945yptghjs8hg45954hg945h\n/gsnohuoi5gh549usgh5498gh4p95sog\nosgn54ui3ghns459uigh54g98s45ghy4598\ngkeot84\n-----END PRIVATE KEY----- (for illustration purposes) and when running jws.sign() on my home computer it works as expected.

However, when I store the secret in AWS Secrets Manager and retrieve it in a container on EC2, even after confirming (by logging it) that it looks the exact same way, I get a Could not deserialize key data error, or more specifically InvalidData(InvalidByte(0, 92)).

Byte 92 is the "\" character, so the issue seems to lie in Jose treating the "\n" as literals instead of new lines for some reason. How do I fix that?


Solution

  • What was suggested fixed that specific issue, but I kept getting failure to deserialize errors, despite it working fine retrieving and using the same key on my local PC. What ended up fixing it is replacing new lines with spaces when storing it in AWS and then re-building it programmatically, like so

    f"-----BEGIN PRIVATE KEY-----\n{secrets.PRIVATE_KEY.replace(" ", "\n")}\n-----END PRIVATE KEY-----"
    

    Still don't know what could cause such an issue but this works.