|
| 1 | +#!/usr/bin/env -S uv run python |
| 2 | +""" |
| 3 | +--- |
| 4 | +title: Secrets with Devbox (Create, Inject, Verify, Delete) |
| 5 | +slug: secrets-with-devbox |
| 6 | +use_case: Create a secret, inject it into a devbox as an environment variable, verify access, and clean up. |
| 7 | +workflow: |
| 8 | + - Create a secret with a test value |
| 9 | + - Create a devbox with the secret mapped to an env var |
| 10 | + - Execute a command that reads the secret from the environment |
| 11 | + - Verify the value matches |
| 12 | + - Update the secret and verify |
| 13 | + - List secrets and verify the secret appears |
| 14 | + - Shutdown devbox and delete secret |
| 15 | +tags: |
| 16 | + - secrets |
| 17 | + - devbox |
| 18 | + - environment-variables |
| 19 | + - cleanup |
| 20 | +prerequisites: |
| 21 | + - RUNLOOP_API_KEY |
| 22 | +run: uv run python -m examples.secrets_with_devbox |
| 23 | +test: uv run pytest -m smoketest tests/smoketests/examples/ |
| 24 | +--- |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +from runloop_api_client import RunloopSDK |
| 30 | + |
| 31 | +from ._harness import run_as_cli, unique_name, wrap_recipe |
| 32 | +from .example_types import ExampleCheck, RecipeOutput, RecipeContext |
| 33 | + |
| 34 | +# Note: do NOT hardcode secret values in your code! |
| 35 | +# This is example code only; use environment variables instead! |
| 36 | +_EXAMPLE_SECRET_VALUE = "my-secret-value" |
| 37 | +_UPDATED_SECRET_VALUE = "updated-secret-value" |
| 38 | + |
| 39 | + |
| 40 | +def recipe(ctx: RecipeContext) -> RecipeOutput: |
| 41 | + """Create a secret, inject it into a devbox, and verify it is accessible.""" |
| 42 | + cleanup = ctx.cleanup |
| 43 | + |
| 44 | + sdk = RunloopSDK() |
| 45 | + resources_created: list[str] = [] |
| 46 | + checks: list[ExampleCheck] = [] |
| 47 | + |
| 48 | + secret_name = unique_name("RUNLOOP_SDK_EXAMPLE").upper().replace("-", "_") |
| 49 | + |
| 50 | + secret = sdk.secret.create(name=secret_name, value=_EXAMPLE_SECRET_VALUE) |
| 51 | + resources_created.append(f"secret:{secret_name}") |
| 52 | + cleanup.add(f"secret:{secret_name}", lambda: secret.delete()) |
| 53 | + |
| 54 | + secret_info = secret.get_info() |
| 55 | + checks.append( |
| 56 | + ExampleCheck( |
| 57 | + name="secret created successfully", |
| 58 | + passed=secret.name == secret_name and secret_info.id.startswith("sec_"), |
| 59 | + details=f"name={secret.name}, id={secret_info.id}", |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + devbox = sdk.devbox.create( |
| 64 | + name=unique_name("secrets-example-devbox"), |
| 65 | + secrets={ |
| 66 | + "MY_SECRET_ENV": secret.name, |
| 67 | + }, |
| 68 | + launch_parameters={ |
| 69 | + "resource_size_request": "X_SMALL", |
| 70 | + "keep_alive_time_seconds": 60 * 5, |
| 71 | + }, |
| 72 | + ) |
| 73 | + resources_created.append(f"devbox:{devbox.id}") |
| 74 | + cleanup.add(f"devbox:{devbox.id}", devbox.shutdown) |
| 75 | + |
| 76 | + result = devbox.cmd.exec("echo $MY_SECRET_ENV") |
| 77 | + stdout = result.stdout().strip() |
| 78 | + checks.append( |
| 79 | + ExampleCheck( |
| 80 | + name="devbox can read secret as env var", |
| 81 | + passed=result.exit_code == 0 and stdout == _EXAMPLE_SECRET_VALUE, |
| 82 | + details=f'exit_code={result.exit_code}, stdout="{stdout}"', |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + updated_info = sdk.secret.update(secret, _UPDATED_SECRET_VALUE).get_info() |
| 87 | + checks.append( |
| 88 | + ExampleCheck( |
| 89 | + name="secret updated successfully", |
| 90 | + passed=updated_info.name == secret_name, |
| 91 | + details=f"update_time_ms={updated_info.update_time_ms}", |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + secrets = sdk.secret.list() |
| 96 | + found = next((s for s in secrets if s.name == secret_name), None) |
| 97 | + checks.append( |
| 98 | + ExampleCheck( |
| 99 | + name="secret appears in list", |
| 100 | + passed=found is not None, |
| 101 | + details=f"found name={found.name}" if found else "not found", |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + return RecipeOutput(resources_created=resources_created, checks=checks) |
| 106 | + |
| 107 | + |
| 108 | +run_secrets_with_devbox_example = wrap_recipe(recipe) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + run_as_cli(run_secrets_with_devbox_example) |
0 commit comments