Skip to content

Commit 8189e53

Browse files
committed
fixes validation check logic
Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 32944e7 commit 8189e53

File tree

15 files changed

+54
-52
lines changed

15 files changed

+54
-52
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/expressions/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = { workspace = true }
88
anyhow = { workspace = true }
99
async-trait = { workspace = true }
1010
futures = { workspace = true }
11-
serde = { workspace = true }
1211
spin-locked-app = { path = "../locked-app" }
1312
thiserror = { workspace = true }
1413

crates/expressions/src/lib.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod provider;
22
mod template;
33

4-
use std::{borrow::Cow, collections::HashMap, fmt::Debug};
4+
use std::{borrow::Cow, collections::HashMap, fmt::Debug, vec};
55

66
use spin_locked_app::Variable;
77

@@ -92,28 +92,45 @@ impl ProviderResolver {
9292
Ok(PreparedResolver { variables })
9393
}
9494

95-
/// Prepares the resolver by attempting to resolve all variables, printing warnings for any
96-
/// that cannot be resolved.
97-
pub async fn pre_runtime_prepare(&self) -> Result<()> {
98-
for name in self.internal.variables.keys() {
99-
self.check_variable_existence(name).await?;
95+
/// Validates `Provider` that are `ProviderVariableKind::Static` provides its variable during startup.
96+
pub async fn validate_variable_existence(&self) -> Result<()> {
97+
// If a dynamic provider is available, skip validation.
98+
if self
99+
.providers
100+
.iter()
101+
.any(|p| p.kind() == ProviderVariableKind::Dynamic)
102+
{
103+
return Ok(());
100104
}
101-
Ok(())
102-
}
103105

104-
async fn check_variable_existence(&self, key: &str) -> Result<()> {
105-
for provider in &self.providers {
106-
if provider.kind() == &ProviderVariableKind::Dynamic {
107-
continue;
106+
let mut unvalidated_keys = vec![];
107+
for key in self.internal.variables.keys() {
108+
let mut resolved = false;
109+
110+
for provider in &self.providers {
111+
if provider
112+
.get(&Key(key))
113+
.await
114+
.map_err(Error::Provider)?
115+
.is_some()
116+
{
117+
resolved = true;
118+
break;
119+
}
108120
}
109121

110-
match provider.get(&Key(key)).await {
111-
Ok(Some(_)) => return Ok(()),
112-
Err(_) | Ok(None) => return self.internal.resolve_variable(key).map(|_| ()),
122+
if !resolved {
123+
unvalidated_keys.push(key);
113124
}
114125
}
115126

116-
Ok(())
127+
if unvalidated_keys.is_empty() {
128+
Ok(())
129+
} else {
130+
Err(Error::Provider(anyhow::anyhow!(
131+
"no provider resolved required variables: {unvalidated_keys:?}",
132+
)))
133+
}
117134
}
118135

119136
async fn resolve_variable(&self, key: &str) -> Result<String> {
@@ -351,8 +368,8 @@ mod tests {
351368
}
352369
}
353370

354-
fn kind(&self) -> &ProviderVariableKind {
355-
&ProviderVariableKind::Static
371+
fn kind(&self) -> ProviderVariableKind {
372+
ProviderVariableKind::Static
356373
}
357374
}
358375

crates/expressions/src/provider.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt::Debug;
22

33
use async_trait::async_trait;
4-
use serde::Deserialize;
54

65
use crate::Key;
76

@@ -10,17 +9,15 @@ use crate::Key;
109
pub trait Provider: Debug + Send + Sync {
1110
/// Returns the value at the given config path, if it exists.
1211
async fn get(&self, key: &Key) -> anyhow::Result<Option<String>>;
13-
fn kind(&self) -> &ProviderVariableKind;
12+
fn kind(&self) -> ProviderVariableKind;
1413
}
1514

1615
/// The dynamism of a Provider.
17-
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
16+
#[derive(Clone, Debug, Default, PartialEq)]
1817
pub enum ProviderVariableKind {
1918
/// Variable must be declared on start
20-
#[serde(rename = "static")]
2119
Static,
2220
/// Variable can be made available at runtime
23-
#[serde(rename = "dynamic")]
2421
#[default]
2522
Dynamic,
2623
}

crates/factor-variables/tests/factor_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Provider for MockProvider {
4747
}
4848
}
4949

50-
fn kind(&self) -> &ProviderVariableKind {
51-
&ProviderVariableKind::Dynamic
50+
fn kind(&self) -> ProviderVariableKind {
51+
ProviderVariableKind::Dynamic
5252
}
5353
}

crates/runtime-factors/src/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use spin_runtime_config::ResolvedRuntimeConfig;
88
use spin_trigger::cli::{
99
FactorsConfig, InitialKvSetterHook, KeyValueDefaultStoreSummaryHook, MaxInstanceMemoryHook,
1010
RuntimeFactorsBuilder, SqlStatementExecutorHook, SqliteDefaultStoreSummaryHook,
11-
StdioLoggingExecutorHooks, VariablesPreparationExecutorHook,
11+
StdioLoggingExecutorHooks, VariablesValidatorHook,
1212
};
1313
use spin_variables_static::StaticVariablesProvider;
1414

@@ -72,7 +72,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
7272
executor.add_hooks(InitialKvSetterHook::new(args.key_values.clone()));
7373
executor.add_hooks(SqliteDefaultStoreSummaryHook);
7474
executor.add_hooks(KeyValueDefaultStoreSummaryHook);
75-
executor.add_hooks(VariablesPreparationExecutorHook);
75+
executor.add_hooks(VariablesValidatorHook);
7676

7777
let max_instance_memory = args
7878
.max_instance_memory

crates/trigger/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use sqlite_statements::SqlStatementExecutorHook;
2626
use stdio::FollowComponents;
2727
pub use stdio::StdioLoggingExecutorHooks;
2828
pub use summary::{KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook};
29-
pub use variable::VariablesPreparationExecutorHook;
29+
pub use variable::VariablesValidatorHook;
3030

3131
pub const APP_LOG_DIR: &str = "APP_LOG_DIR";
3232
pub const SPIN_TRUNCATE_LOGS: &str = "SPIN_TRUNCATE_LOGS";

crates/trigger/src/cli/variable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use spin_factors::RuntimeFactors;
33
use spin_factors_executor::ExecutorHooks;
44

55
/// An executor hook that prepares the variables factor before runtime execution.
6-
pub struct VariablesPreparationExecutorHook;
6+
pub struct VariablesValidatorHook;
77

88
#[spin_core::async_trait]
9-
impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for VariablesPreparationExecutorHook {
9+
impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for VariablesValidatorHook {
1010
async fn configure_app(
1111
&self,
1212
configured_app: &spin_factors::ConfiguredApp<F>,
1313
) -> anyhow::Result<()> {
1414
let variables_factor = configured_app.app_state::<VariablesFactor>()?;
1515

1616
let expression_resolver = variables_factor.expression_resolver();
17-
expression_resolver.pre_runtime_prepare().await?;
17+
expression_resolver.validate_variable_existence().await?;
1818

1919
Ok(())
2020
}

crates/variables-azure/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ pub enum AzureKeyVaultAuthOptions {
107107
#[derive(Debug)]
108108
pub struct AzureKeyVaultProvider {
109109
secret_client: SecretClient,
110-
kind: ProviderVariableKind,
111110
}
112111

113112
impl AzureKeyVaultProvider {
@@ -137,7 +136,6 @@ impl AzureKeyVaultProvider {
137136

138137
Ok(Self {
139138
secret_client: SecretClient::new(&vault_url.into(), token_credential)?,
140-
kind: ProviderVariableKind::Dynamic,
141139
})
142140
}
143141
}
@@ -154,8 +152,8 @@ impl Provider for AzureKeyVaultProvider {
154152
Ok(Some(secret.value))
155153
}
156154

157-
fn kind(&self) -> &ProviderVariableKind {
158-
&self.kind
155+
fn kind(&self) -> ProviderVariableKind {
156+
ProviderVariableKind::Dynamic
159157
}
160158
}
161159

crates/variables-env/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub struct EnvVariablesProvider {
3535
env_fetcher: EnvFetcherFn,
3636
dotenv_path: Option<PathBuf>,
3737
dotenv_cache: OnceLock<HashMap<String, String>>,
38-
kind: ProviderVariableKind,
3938
}
4039

4140
impl Default for EnvVariablesProvider {
@@ -45,7 +44,6 @@ impl Default for EnvVariablesProvider {
4544
env_fetcher: Box::new(|s| std::env::var(s)),
4645
dotenv_path: Some(".env".into()),
4746
dotenv_cache: Default::default(),
48-
kind: ProviderVariableKind::Static,
4947
}
5048
}
5149
}
@@ -68,7 +66,6 @@ impl EnvVariablesProvider {
6866
dotenv_path,
6967
env_fetcher: Box::new(env_fetcher),
7068
dotenv_cache: Default::default(),
71-
kind: ProviderVariableKind::Static,
7269
}
7370
}
7471

@@ -136,8 +133,8 @@ impl Provider for EnvVariablesProvider {
136133
tokio::task::block_in_place(|| self.get_sync(key))
137134
}
138135

139-
fn kind(&self) -> &ProviderVariableKind {
140-
&self.kind
136+
fn kind(&self) -> ProviderVariableKind {
137+
ProviderVariableKind::Static
141138
}
142139
}
143140

0 commit comments

Comments
 (0)