Skip to content

Commit

Permalink
Refactor due to clippy
Browse files Browse the repository at this point in the history
This suggestion was hard to apply so I moved the code:

```
57 |   fn fetch_secret_key_base_from_store(store: &Option<Store>) -> (String, Store) {
   |   ^                                          -------------- help: change this to: `Option<&Store>`
   |  _|
   | |
58 | |     let mut store = store.clone().unwrap_or_default();
59 | |     let default_secret_key_base = store
60 | |         .metadata
...  |
72 | |     (default_secret_key_base, store)
73 | | }
   | |_^
```

I also added an integration test to assert that the SECRET_KEY_BASE is preserved
  • Loading branch information
schneems committed Dec 11, 2024
1 parent 5b0f1e5 commit 13d69bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
33 changes: 14 additions & 19 deletions buildpacks/ruby/src/steps/default_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ pub(crate) fn default_env(
env.insert(k, v);
}

let (default_secret_key_base, store) = fetch_secret_key_base_from_store(&context.store);
let mut store = context.store.clone().unwrap_or_default();
let default_secret_key_base = store
.metadata
.entry("SECRET_KEY_BASE")
.or_insert_with(|| {
let mut rng = rand::thread_rng();

(0..64)
.map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
.collect::<String>()
.into()
})
.to_string();

let layer_ref = context.uncached_layer(
layer_name!("env_defaults"),
UncachedLayerDefinition {
Expand Down Expand Up @@ -53,21 +66,3 @@ pub(crate) fn default_env(

Ok((env, store))
}

fn fetch_secret_key_base_from_store(store: &Option<Store>) -> (String, Store) {
let mut store = store.clone().unwrap_or_default();
let default_secret_key_base = store
.metadata
.entry("SECRET_KEY_BASE")
.or_insert_with(|| {
let mut rng = rand::thread_rng();

(0..64)
.map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
.collect::<String>()
.into()
})
.to_string();

(default_secret_key_base, store)
}
9 changes: 9 additions & 0 deletions buildpacks/ruby/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ fn test_default_app_latest_distro() {

assert_contains!(context.pack_stdout, "Installing puma");

let secret_key_base = context.run_shell_command("echo \"${SECRET_KEY_BASE:?No SECRET_KEY_BASE set}\"").stdout;
assert!(!secret_key_base.trim().is_empty(), "Expected {secret_key_base:?} to not be empty but it is");

let config = context.config.clone();
context.rebuild(config, |rebuild_context| {
println!("{}", rebuild_context.pack_stdout);
Expand All @@ -114,6 +117,12 @@ fn test_default_app_latest_distro() {
assert_contains!(body, "ruby_version");
},
);

// Assert SECRET_KEY_BASE is preserved between invocations
assert_eq!(
secret_key_base,
rebuild_context.run_shell_command("echo \"${SECRET_KEY_BASE:?No SECRET_KEY_BASE set}\"").stdout
);
});
},
);
Expand Down

0 comments on commit 13d69bc

Please sign in to comment.