Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --force for overwriting existing secrets #1343

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modal/cli/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ async def list(env: Optional[str] = ENV_OPTION, json: Optional[bool] = False):
display_table(column_names, rows, json, title=f"Secrets{env_part}")


@secret_cli.command("create", help="Create a new secret")
@secret_cli.command("create", help="Create a new secret. Use `--force` to overwrite any existing one.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add this to the option doc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think boolean options support documentation – I wasn't able to get it working and at least one Github Isssue suggests this is a missing feature in typer. Could be wrong though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it! #1344

@synchronizer.create_blocking
async def create(
secret_name,
keyvalues: List[str] = typer.Argument(..., help="Space-separated KEY=VALUE items"),
env: Optional[str] = ENV_OPTION,
force: bool = typer.Option(False, "--force"),
):
env = ensure_env(env)
env_dict = {}
Expand All @@ -72,7 +73,7 @@ async def create(
raise click.UsageError("You need to specify at least one key for your secret")

# Create secret
await _Secret.create_deployed(secret_name, env_dict)
await _Secret.create_deployed(secret_name, env_dict, overwrite=force)

# Print code sample
console = Console()
Expand Down
7 changes: 6 additions & 1 deletion modal/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,19 @@ async def create_deployed(
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
client: Optional[_Client] = None,
environment_name: Optional[str] = None,
overwrite: bool = False,
) -> str:
if client is None:
client = await _Client.from_env()
if overwrite:
object_creation_type = api_pb2.OBJECT_CREATION_TYPE_CREATE_OVERWRITE_IF_EXISTS
else:
object_creation_type = api_pb2.OBJECT_CREATION_TYPE_CREATE_FAIL_IF_EXISTS
request = api_pb2.SecretGetOrCreateRequest(
deployment_name=deployment_name,
namespace=namespace,
environment_name=_get_environment_name(environment_name),
object_creation_type=api_pb2.OBJECT_CREATION_TYPE_CREATE_FAIL_IF_EXISTS,
object_creation_type=object_creation_type,
env_dict=env_dict,
)
resp = await retry_transient_errors(client.stub.SecretGetOrCreate, request)
Expand Down
1 change: 1 addition & 0 deletions modal_proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ enum ObjectCreationType {
OBJECT_CREATION_TYPE_UNSPECIFIED = 0; // just lookup
OBJECT_CREATION_TYPE_CREATE_IF_MISSING = 1;
OBJECT_CREATION_TYPE_CREATE_FAIL_IF_EXISTS = 2;
OBJECT_CREATION_TYPE_CREATE_OVERWRITE_IF_EXISTS = 3;
}

enum ProgressType {
Expand Down
6 changes: 6 additions & 0 deletions test/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def test_secret_create(servicer, set_env_client):
_run(["secret", "create", "foo", "bar=baz"])
assert len(servicer.secrets) == 1

# Creating the same one again should fail
_run(["secret", "create", "foo", "bar=baz"], expected_exit_code=1)

# But it should succeed with --force
_run(["secret", "create", "foo", "bar=baz", "--force"])


def test_secret_list(servicer, set_env_client):
res = _run(["secret", "list"])
Expand Down
14 changes: 11 additions & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,15 +826,23 @@ async def SecretGetOrCreate(self, stream):
request: api_pb2.SecretGetOrCreateRequest = await stream.recv_message()
k = (request.deployment_name, request.namespace, request.environment_name)
if request.object_creation_type == api_pb2.OBJECT_CREATION_TYPE_CREATE_FAIL_IF_EXISTS:
secret_id = "st-" + str(len(self.secrets))
self.secrets[secret_id] = request.env_dict
self.deployed_secrets[k] = secret_id
if k in self.deployed_secrets:
raise GRPCError(Status.ALREADY_EXISTS, "Already exists")
secret_id = None
elif request.object_creation_type == api_pb2.OBJECT_CREATION_TYPE_CREATE_OVERWRITE_IF_EXISTS:
secret_id = None
elif request.object_creation_type == api_pb2.OBJECT_CREATION_TYPE_UNSPECIFIED:
if k not in self.deployed_secrets:
raise GRPCError(Status.NOT_FOUND, "No such secret")
secret_id = self.deployed_secrets[k]
else:
raise Exception("unsupported creation type")

if secret_id is None: # Create one
secret_id = "st-" + str(len(self.secrets))
self.secrets[secret_id] = request.env_dict
self.deployed_secrets[k] = secret_id

await stream.send_message(api_pb2.SecretGetOrCreateResponse(secret_id=secret_id))

async def SecretList(self, stream):
Expand Down
Loading