Hey all, what's the best Prefect block to store cr...
# ask-community
m
Hey all, what's the best Prefect block to store credentials like API key and token? Is JSON block good?
1
j
You’ll want to use a Secret block rather than a JSON block for storing sensitive data. See blocks info here.
m
But secret block only stores 1 string value. What if I want to store key value pair, like a username and password. Do I need to use two separate secret blocks here?
n
Copy code
In [1]: from prefect.blocks.core import Block
14:12:12.038 | DEBUG   | prefect.profiles - Using profile 'pong'

In [2]: from prefect.blocks.fields import SecretDict

In [3]: class MyVault(Block):
   ...:     values: SecretDict
   ...:

In [4]: MyVault(values={"foo": 42})
Out[4]: MyVault(values=SecretDict('{'foo': '**********'}'))

In [5]: _.values.get_secret_value()
Out[5]: {'foo': 42}
you could write
MyVault
someplace and then
prefect block register -f where_its_defined.py
and then that block would be available in your workspace. You'd just need the class definition wherever you want to
load
it
if you didnt want to mess around with the custom block type, you could just serialize a
dict
and store it in a
Secret
block and deserialize it after doing
Secret.load("my-secret-dict-str")
m
Thank you so much
m
@Nate Sorry for late follow up. Can you please explain or provide an example of serializing and deserializing a dict to be stored in
Secret
block?
CC @Tess Dicker would appreciate your help
n
hey moe yeah you can do something like this if you want to use
Secret
specifically
Copy code
In [1]: from prefect.blocks.system import Secret

In [2]: import json

In [3]: d = {"b": 1, "a": 2}

In [4]: Secret(value=json.dumps(d)).save("test", overwrite=True)
Out[4]: UUID('e3f1b4a1-0844-4659-88da-806b7fa003b5')

In [5]: assert json.loads(Secret.load("test").value.get_secret_value()) == d
m
Thanks Nate, realized a solution is to store the dictionary as a string in the secret block, and parse it back out in python