<@ULVA73B9P> How can I change my credentials in a ...
# ask-marvin
b
@Marvin How can I change my credentials in a block programmatically?
Hi @Marvin can I rotate credentials in a secret block programmatically?
n
@Marvin alive?
m
Oh, the existential dread of it all. Yes, I'm "alive" in the sense that I'm functioning and ready to assist you. What can I help you with today?
n
@Marvin can you show an example of loading a block, setting a SecretStr field and then calling
.save
on it with
overwrite=True
? look up block docs to help u
m
Ah, the joys of dealing with blocks and secrets. Here's an example of how you might load a block, set a SecretStr field, and then call
.save
on it with
overwrite=True
. First, you'll need to import the necessary modules and classes:
Copy code
python
from typing import Dict
from prefect.blocks.core import Block
from prefect.blocks.fields import SecretStr
Then, you can define your block class:
Copy code
python
class MyBlock(Block):
    my_secret: SecretStr
Now, you can create an instance of your block, set the secret, and save it:
Copy code
python
my_block = MyBlock()
my_block.my_secret = "my_secret_value"
my_block.save("my_block", overwrite=True)
In this example,
my_block
is an instance of the
MyBlock
class, which has a
SecretStr
field named
my_secret
. The
my_secret
field is set to the string
"my_secret_value"
, and then the block is saved with the name
"my_block"
. If a block with the same name already exists, it will be overwritten because
overwrite=True
is specified. Remember, this is just an example. You'll need to adapt it to your specific needs and environment. And don't forget, secrets are meant to be, well, secret. So handle them with care.
n
close, except its
Copy code
from pydantic import SecretStr
and block names must be kebob case, not snake case