|
| 1 | +#!/usr/bin/env -S npm run tsn -T |
| 2 | + |
| 3 | +/** |
| 4 | +--- |
| 5 | +title: Secrets with Devbox (Create, Inject, Verify, Delete) |
| 6 | +slug: secrets-with-devbox |
| 7 | +use_case: Create a secret, inject it into a devbox as an environment variable, verify access, and clean up. |
| 8 | +workflow: |
| 9 | + - Create a secret with a test value |
| 10 | + - Create a devbox with the secret mapped to an env var |
| 11 | + - Execute a command that reads the secret from the environment |
| 12 | + - Verify the value matches |
| 13 | + - Shutdown devbox and delete secret |
| 14 | +tags: |
| 15 | + - secrets |
| 16 | + - devbox |
| 17 | + - environment-variables |
| 18 | + - cleanup |
| 19 | +prerequisites: |
| 20 | + - RUNLOOP_API_KEY |
| 21 | +run: yarn tsn -T examples/secrets-with-devbox.ts |
| 22 | +test: yarn test:examples |
| 23 | +--- |
| 24 | +*/ |
| 25 | + |
| 26 | +import { RunloopSDK } from '@runloop/api-client'; |
| 27 | +import { wrapRecipe, runAsCli } from './_harness'; |
| 28 | +import type { RecipeContext, RecipeOutput } from './types'; |
| 29 | + |
| 30 | +export async function recipe(ctx: RecipeContext): Promise<RecipeOutput> { |
| 31 | + const { cleanup } = ctx; |
| 32 | + |
| 33 | + const sdk = new RunloopSDK({ |
| 34 | + bearerToken: process.env['RUNLOOP_API_KEY'], |
| 35 | + }); |
| 36 | + |
| 37 | + const secretName = 'RUNLOOP_SECRET_EXAMPLE'; |
| 38 | + // Note: do NOT hardcode secret values in your code! |
| 39 | + // this is example code only; use environment variables instead! |
| 40 | + const secretValue = 'my-secret-value'; |
| 41 | + |
| 42 | + const secret = await sdk.secret.create({ |
| 43 | + name: secretName, |
| 44 | + value: secretValue, |
| 45 | + }); |
| 46 | + cleanup.add(`secret:${secretName}`, () => secret.delete()); |
| 47 | + |
| 48 | + const devbox = await sdk.devbox.create({ |
| 49 | + name: 'secrets-example-devbox', |
| 50 | + secrets: { |
| 51 | + MY_SECRET_ENV: secret, |
| 52 | + }, |
| 53 | + launch_parameters: { |
| 54 | + resource_size_request: 'X_SMALL', |
| 55 | + keep_alive_time_seconds: 60 * 5, |
| 56 | + }, |
| 57 | + }); |
| 58 | + cleanup.add(`devbox:${devbox.id}`, () => sdk.devbox.fromId(devbox.id).shutdown()); |
| 59 | + |
| 60 | + const result = await devbox.cmd.exec('echo $MY_SECRET_ENV'); |
| 61 | + const stdout = await result.stdout(); |
| 62 | + |
| 63 | + const updatedSecret = await sdk.secret.update(secret, { |
| 64 | + value: 'updated-secret-value', |
| 65 | + }); |
| 66 | + |
| 67 | + const secrets = await sdk.secret.list(); |
| 68 | + const foundSecret = secrets.find((s) => s.name === secretName); |
| 69 | + |
| 70 | + const secretInfo = await secret.getInfo(); |
| 71 | + const updatedInfo = await updatedSecret.getInfo(); |
| 72 | + |
| 73 | + return { |
| 74 | + resourcesCreated: [`secret:${secretName}`, `devbox:${devbox.id}`], |
| 75 | + checks: [ |
| 76 | + { |
| 77 | + name: 'secret created successfully', |
| 78 | + passed: secret.name === secretName && secretInfo.id.startsWith('sec_'), |
| 79 | + details: `name=${secret.name}, id=${secretInfo.id}`, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: 'devbox can read secret as env var', |
| 83 | + passed: result.exitCode === 0 && stdout.trim() === secretValue, |
| 84 | + details: `exitCode=${result.exitCode}, stdout="${stdout.trim()}"`, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'secret updated successfully', |
| 88 | + passed: updatedSecret.name === secretName, |
| 89 | + details: `update_time_ms=${updatedInfo.update_time_ms}`, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: 'secret appears in list', |
| 93 | + passed: foundSecret !== undefined && foundSecret.name === secretName, |
| 94 | + details: foundSecret ? `found name=${foundSecret.name}` : 'not found', |
| 95 | + }, |
| 96 | + ], |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export const runSecretsWithDevboxExample = wrapRecipe({ recipe }); |
| 101 | + |
| 102 | +if (require.main === module) { |
| 103 | + void runAsCli(runSecretsWithDevboxExample); |
| 104 | +} |
0 commit comments