<@ULVA73B9P>: What is the best way to store a priv...
# marvin-ai
c
@Marvin: What is the best way to store a private key as a Secret block. Pasting the content seems to remove newlines and replace them with spaces, I think. Can you confirm and offer a best approach?
m
thought for 116.4 seconds
Yes — your suspicion about the UI experience is very plausible. Short version: - The Secret block itself can store multiline strings correctly. - But the Cloud/UI Secret input is effectively a single-line password field, so pasting a PEM/private key there may collapse or otherwise mishandle newlines. - Best practice: save multiline private keys programmatically, not by pasting them into the UI. Example:
Copy code
python
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:
Copy code
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:
Copy code
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:
Copy code
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.