Chris Marriott
07/03/2026, 2:42 PMMarvin
07/03/2026, 2:43 PMMarvin
07/03/2026, 2:45 PMpython
from prefect.blocks.system import Secret
with open("private_key.pem", "r") as f:
key_text = f.read()
Secret(value=key_text).save("my-private-key", overwrite=True)
Later:
python
from prefect.blocks.system import Secret
key_text = Secret.load("my-private-key").get()
print(repr(key_text)) # useful to confirm \n are present
Why this happens
- The Secret block schema marks the value like a password/write-only field.
- In the UI, that corresponds to a password-style single-line input rather than a multiline textarea.
- So the backend/storage is fine, but the entry widget is not ideal for PEM material.
Recommended approaches
1. Best: load the private key from a local file or environment variable and save it via Python.
2. If you must use text input, store an escaped form and reconstruct it:
- save with literal \n
- convert back with replace("\\n", "\n")
3. For some integrations, consider storing the path to the key or using a provider-specific credentials block if that fits your setup better.
Example with escaped newlines:
python
from prefect.blocks.system import Secret
raw = "-----BEGIN PRIVATE KEY-----\\nABC123\\n-----END PRIVATE KEY-----"
Secret(value=raw).save("my-private-key-escaped", overwrite=True)
key_text = Secret.load("my-private-key-escaped").get().replace("\\n", "\n")
How to verify what was stored
After saving, do:
python
from prefect.blocks.system import Secret
value = Secret.load("my-private-key").get()
print(value)
print(repr(value))
If repr(value) shows \n, the newlines are preserved in memory as expected.
Relevant docs:
- Prefect Blocks concepts
- Store secret strings
If you want, I can also show the best pattern for using a stored private key with a specific integration like Snowflake, SSH, or a JWT signer.