Hi all! Is this correct to use prefect's secret fu...
# prefect-community
w
Hi all! Is this correct to use prefect's secret function? By following the prefect's docs, but i got the message like this. (full of error message)
Copy code
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 551, in _send_request
    response.raise_for_status()
  File "C:\Users\user\anaconda3\lib\site-packages\requests\models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: <http://localhost:4200/> 

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".\label_test.py", line 19, in <module>
    client.set_secret(name="mysql", value="<mysql+pymysql://user:pwd@host/database>")
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 1643, in set_secret
    result = self.graphql(
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 452, in graphql
    result = <http://self.post|self.post>(
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 407, in post
    response = self._request(
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 641, in _request
    response = self._send_request(
  File "C:\Users\user\anaconda3\lib\site-packages\prefect\client\client.py", line 564, in _send_request
    raise ClientError(f"{exc}\n{graphql_msg}") from exc
prefect.exceptions.ClientError: 400 Client Error: Bad Request for url: <http://localhost:4200/>

The following error messages were provided by the GraphQL server:

    GRAPHQL_VALIDATION_FAILED: Unknown type "set_secret_input". Did you mean
        "agent_set_input", "edge_set_input", "log_set_input", "task_set_input", or
        "agent_insert_input"?
    GRAPHQL_VALIDATION_FAILED: Cannot query field "set_secret" on type "Mutation".

The GraphQL query was:

    mutation($input: set_secret_input!) {
            set_secret(input: $input) {
                success
        }
    }

The passed variables were:

    {"input": {"name": "mysql", "value": "<mysql+pymysql://user:pwd@host/database>"}}
And here is my code of the part that I think is a problem. (code block moved to thread) The information to be contained in 'secret' is connection information that can access the database used. I used the Prefect 1.2.2. In this common case, How can i use the secret fuction well?
1
a
can you move the code blocks to the thread?
the idea with the Secrets is more that you configure it once via UI and then you can use it in your flow: you shouldn't need to use the client to set it, otherwise it kinds of defeats its purpose because right now the Secret is still in your code try setting it from the UI and then do:
Copy code
from prefect import Flow, task, resource_manager, case
from prefect.tasks.control_flow import merge
from prefect.tasks.secrets import PrefectSecret

with Flow('test') as flow:
    my_secret = PrefectSecret("mysql")
    new_data, decision = task1(credential=my_secret)

    with case(decision, True):
        processing = statement_update(credential=my_secret, list=new_data)
    with case(decision, False):
        processing = None
w
Here Anna~
Copy code
from prefect import Flow, task, resource_manager, case
from prefect.tasks.control_flow import merge
from prefect import Client
from prefect.tasks.secrets import PrefectSecret

client = Client()
client.set_secret(name="mysql", value="<mysql+pymysql://user:pwd@host/database>")

@task(nout=2)
def task1(credential):
    connection = create_engine(credential).connnect()
    ~
    ~~
    return out1, out2
    
@task
def task2(credential, list):
    connection = create_engine(credential).connect()
    ~
    ~~
    need2processing = a_function(list)
    ~~
    ~

with Flow('test') as flow:
    my_secret = PrefectSecret("mysql")
    new_data, decision = task1(credential=my_secret)

    with case(decision, True):
        processing = task2(credential=my_secret, list=new_data)
    with case(decision, False):
        processing = None
Oh, i have to set it up in the UI first. I'll try that. After the set secret at UI, Isn't it necessary to define where to use the secret in each task? Or did you just omit the task part codes? Thank you for writing the code yourself, but I asked because I was confused if I was talking about omitting the secret written in the task.
One more thing! In order to use the secret function, the information to be stored in secret must first be saved in the UI. Then, Is it possible to execute the flow only in the UI with
flow.register(~~~)
code? Can't I use the secret function when I run flow immediately in the console with
flow.run()
?
a
yup, you totally can! even your local run is able to make an API call to the Cloud backend as long as the CLI from which you run it is authenticated with Cloud
w
Thank you Anna. Have a good day 🤗
🙌 1
a
thanks, you too! 🙌