Skip to content

Commit

Permalink
update secrets page
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 committed Mar 16, 2024
1 parent 126bbe9 commit 5112d50
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions resources/shuttle-secrets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ icon: "user-secret"
This plugin manages secrets on [Shuttle](https://www.shuttle.rs).

## Usage
Add `shuttle-secrets` to the dependencies for your service, and add a `Secrets.toml` to the root of your project
with the secrets you'd like to store. Make sure to add `Secrets*.toml` to a `.gitignore` to omit your secrets from version control.

Add a `Secrets.toml` to the crate root or workspace root of your Shuttle service with the secrets you'd like to store.
Make sure to add `Secrets*.toml` to a `.gitignore` to omit your secrets from version control.

The format of the Secrets.toml file is a key-value mapping with string values.

Expand All @@ -17,31 +18,35 @@ MY_API_KEY = 'the contents of my API key'
MY_OTHER_SECRET = 'some other secret'
```

Next, pass `#[shuttle_secrets::Secrets] secret_store: SecretStore` as an argument to your `shuttle_runtime::main` function.
Next, pass `#[shuttle_runtime::Secrets] secrets: shuttle_runtime::SecretStore` as an argument to your `shuttle_runtime::main` function.
`SecretStore::get` can now be called to retrieve your API keys and other secrets at runtime.

## Local secrets
When developing locally with `cargo shuttle run`, you can use a different set of secrets. If you add a `Secrets.dev.toml` to the
root of your project, these secrets will be used for local runs. The `Secrets.dev.toml` file will *only* be used for local runs.

If you don't have a `Secrets.dev.toml` file, `Secrets.toml` will be used locally as well as for deployments. If you want to have
both secret files with some of the same secrets for both local runs and deployments, you have to duplicate the secret across both
files.
When developing locally with `cargo shuttle run`, you can use a different set of secrets by adding a `Secrets.dev.toml` file.

If you don't have a `Secrets.dev.toml` file, `Secrets.toml` will be used locally as well as for deployments.
If you want to have both secret files with some of the same secrets for both local runs and deployments, you have to duplicate the secret across both files.

## Different secrets file

You can also use other secrets files (in TOML format) by using the `--secrets [file]` argument on the `run` and `deploy` commands.

When deploying with the `--secrets` arg, that file is renamed to `Secrets.toml` before being packed and sent to Shuttle. Therefore, it has to be in the same folder that a normal `Secrets.toml` file would have been placed.

## Example
This snippet shows a Shuttle rocket main function that uses the `shuttle_secrets::Secrets` attribute to gain access to a `SecretStore`.

This snippet shows a Shuttle rocket main function that uses the `shuttle_runtime::Secrets` attribute to gain access to a `SecretStore`.

```rust main.rs
use shuttle_runtime::SecretStore;

#[shuttle_runtime::main]
async fn rocket(
#[shuttle_secrets::Secrets] secret_store: SecretStore,
) -> ShuttleRocket {
#[shuttle_runtime::Secrets] secrets: SecretStore,
) -> shuttle_rocket::ShuttleRocket {
// get secret defined in `Secrets.toml` file.
let secret = if let Some(secret) = secret_store.get("MY_API_KEY") {
secret
} else {
return Err(anyhow!("secret was not found").into());
};
let secret = secrets.get("MY_API_KEY").context("secret was not found")?;

let state = MyState { secret };
let rocket = rocket::build().mount("/", routes![secret]).manage(state);
Expand Down

0 comments on commit 5112d50

Please sign in to comment.