hi folks, how to specify a secret with key, value...
# ask-community
l
hi folks, how to specify a secret with key, value pairs and to read it with PrefectSecret or Secret().get()? Thanks!
āœ… 1
j
l
yep, it doesn't say there
im speaking of multiple values in a dict or smth that i can access with kwargs
j
If by "key, value" pairs you mean that the secret value is a dictionary, then setting the value in the UI as a json object should be sufficient (likewise in the
config.toml
).
h
Do you mean for the secret to contain e.g. a
dict
?
l
yes, any examples would be great, im stuck
j
What have you tried, and in what ways did it not work?
h
Copy code
from json import loads

@task
def mysterious():
    raw = Secret("OH_VERY_SECRET").get() # e.g. {"hi":42}
    v = loads(raw)
    printf(v["hi"]) # => 42
šŸ™Œ 1
l
secret = {'key1' : value1, ...} secret = {"key":"value",...} secret={"key" = "value",...}
c
Assuming you mean the Prefect Secrets feature on the front end and the tie into a flow, I've used:
Copy code
with Flow("<flow name>") as flow:
client_secret = PrefectSecret("<your name for your secrets class")
on the front end, there is a feature to enter Secrets and it has to be valid nested JSON.
You access it by calling client['key'] and it spits out the value; I have had no issues passing information back and forth with this setup.
l
yes, but i had the problem with how the format should be, but gonna try the example of @haf
j
Copy code
In [1]: from prefect.client import Secret

In [2]: Secret("EXAMPLE").get()
Out[2]: {'key1': 'value1', 'key2': 'value2'}
l
Copy code
secret = 
{"key" = "value"
}
that's my example and when i want to run
Copy code
from prefect.client import Secret
it says
Copy code
toml.decoder.TomlDecodeError: Found invalid character in key name: '"'. Try quoting the key name. (line 13 column 2 char 321)
h
In JSON it's
:
not
=
j
This isn't json though, this is toml. I suggest looking at https://learnxinyminutes.com/docs/toml/, inline toml dicts don't quote keys.
Copy code
[context.secrets]
EXAMPLE = {key1 = "value1", key2 = "value2"}
Copy code
In [1]: from prefect.client import Secret

In [2]: Secret("EXAMPLE").get()
Out[2]: <Box: {'key1': 'value1', 'key2': 'value2'}>
l
nice, thanks! any chance to pass that as **kwargs?
j
To a task? No. But within a task the value will be a normal dict, so you can do whatever you want with it.
Copy code
def my_function(key1=None, key2=None):
    ...

@task
def my_task(secret):
    return my_function(**secret)

with Flow("example") as flow:
    secret = PrefectSecret("EXAMPLE")
    my_task(secret)
šŸ™Œ 1
c
^yep this is what I ended up doing; allows you to pass the same secrets across multiple tasks and/or load multiple sets of secrets.
l
ahh great, but why do you have to declare my_function there? and where do you actually call it?
h
What is the difference between Secret and PrefectSecret?
c
So basically what it's doing in that specific example: • Assumes you set up JSON through Prefect's Secrets (allowing you to call the PrefectSecret() function). • Your JSON is now stored in the "secret" variable. • my_function(key1,key2) would presumably be some sort of authorization function. • with Flow() as flow: passes your new "secret" variable that holds your secrets into a task; that task then calls your auth function and passes it through with the variables.
PrefectSecrets lets you use the Secrets feature in the same dropdown menu (on the front end) as "API Keys" (for RUNNER and TENANT tokens), allows you to hide credentials and call them by name by calling PrefectSecrets('name') in your flow.py. EDIT: The "Create New Secret" screenshot that Jim posted earlier is what I'm referring to.
I haven't used Secrets, but according to the thread so far it seems that would be used for calling secrets through filestorage
j
prefect.client.Secret
is an internal api for loading secrets (this honestly should have been a function like
prefect.secrets.get_secret
).
prefect.tasks.secrets.PrefectSecret
is a task that uses
prefect.client.Secret
internally for loading secrets. You usually want to use the latter (treat secrets as a task) rather than the former.
āœ… 1
l
great! thanks for your help! worked! šŸ™‚