-
Notifications
You must be signed in to change notification settings - Fork 3
feat(sdk): added secrets as first class concepts and examples #739
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a86f9f9
feat(sdk): added secrets as first class concepts and examples
james-rl 865b429
added disclaimer to secret usage
james-rl 6754383
now with get secret by name support!
james-rl ac22750
added missing id field
james-rl a44ec1a
use secrets!
james-rl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| /** | ||
| --- | ||
| title: Secrets with Devbox (Create, Inject, Verify, Delete) | ||
| slug: secrets-with-devbox | ||
| use_case: Create a secret, inject it into a devbox as an environment variable, verify access, and clean up. | ||
| workflow: | ||
| - Create a secret with a test value | ||
| - Create a devbox with the secret mapped to an env var | ||
| - Execute a command that reads the secret from the environment | ||
| - Verify the value matches | ||
| - Shutdown devbox and delete secret | ||
| tags: | ||
| - secrets | ||
| - devbox | ||
| - environment-variables | ||
| - cleanup | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: yarn tsn -T examples/secrets-with-devbox.ts | ||
| test: yarn test:examples | ||
| --- | ||
| */ | ||
|
|
||
| import { RunloopSDK } from '@runloop/api-client'; | ||
| import { wrapRecipe, runAsCli } from './_harness'; | ||
| import type { RecipeContext, RecipeOutput } from './types'; | ||
|
|
||
| export async function recipe(ctx: RecipeContext): Promise<RecipeOutput> { | ||
| const { cleanup } = ctx; | ||
|
|
||
| const sdk = new RunloopSDK({ | ||
| bearerToken: process.env['RUNLOOP_API_KEY'], | ||
| }); | ||
|
|
||
| const secretName = 'RUNLOOP_SECRET_EXAMPLE'; | ||
| // Note: do NOT hardcode secret values in your code! | ||
| // this is example code only; use environment variables instead! | ||
| const secretValue = 'my-secret-value'; | ||
|
|
||
| const secret = await sdk.secret.create({ | ||
| name: secretName, | ||
| value: secretValue, | ||
| }); | ||
| cleanup.add(`secret:${secretName}`, () => secret.delete()); | ||
|
|
||
| const devbox = await sdk.devbox.create({ | ||
| name: 'secrets-example-devbox', | ||
| secrets: { | ||
| MY_SECRET_ENV: secret, | ||
| }, | ||
| launch_parameters: { | ||
| resource_size_request: 'X_SMALL', | ||
| keep_alive_time_seconds: 60 * 5, | ||
| }, | ||
| }); | ||
| cleanup.add(`devbox:${devbox.id}`, () => sdk.devbox.fromId(devbox.id).shutdown()); | ||
|
|
||
| const result = await devbox.cmd.exec('echo $MY_SECRET_ENV'); | ||
| const stdout = await result.stdout(); | ||
|
|
||
| const updatedSecret = await sdk.secret.update(secret, { | ||
| value: 'updated-secret-value', | ||
| }); | ||
|
|
||
| const secrets = await sdk.secret.list(); | ||
| const foundSecret = secrets.find((s) => s.name === secretName); | ||
|
|
||
| const secretInfo = await secret.getInfo(); | ||
| const updatedInfo = await updatedSecret.getInfo(); | ||
|
|
||
| return { | ||
| resourcesCreated: [`secret:${secretName}`, `devbox:${devbox.id}`], | ||
| checks: [ | ||
| { | ||
| name: 'secret created successfully', | ||
| passed: secret.name === secretName && secretInfo.id.startsWith('sec_'), | ||
| details: `name=${secret.name}, id=${secretInfo.id}`, | ||
| }, | ||
| { | ||
| name: 'devbox can read secret as env var', | ||
| passed: result.exitCode === 0 && stdout.trim() === secretValue, | ||
| details: `exitCode=${result.exitCode}, stdout="${stdout.trim()}"`, | ||
| }, | ||
| { | ||
| name: 'secret updated successfully', | ||
| passed: updatedSecret.name === secretName, | ||
| details: `update_time_ms=${updatedInfo.update_time_ms}`, | ||
| }, | ||
| { | ||
| name: 'secret appears in list', | ||
| passed: foundSecret !== undefined && foundSecret.name === secretName, | ||
| details: foundSecret ? `found name=${foundSecret.name}` : 'not found', | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| export const runSecretsWithDevboxExample = wrapRecipe({ recipe }); | ||
|
|
||
| if (require.main === module) { | ||
| void runAsCli(runSecretsWithDevboxExample); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use your new endpoint 🥳