Skip to content
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

refactor: Partially drop anyhow from crate::runtimes:wapc #424

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ pub enum ArtifactHubError {
#[error("annotation \"{0}\" in policy metadata is malformed, must be a string \"true\" or \"false\"")]
MalformedBoolString(String),
}

#[derive(Error, Debug)]
pub enum PolicyEvaluatorError {
#[error("protocol_version is only applicable to a Kubewarden policy")]
InvalidProtocolVersion(),

#[error("protocol_version is only applicable to a Kubewarden policy")]
InvokeWapcProtocolVersion(#[source] crate::runtimes::wapc::errors::WapcRuntimeError),
}
12 changes: 6 additions & 6 deletions src/policy_evaluator/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{anyhow, Result};
use kubewarden_policy_sdk::metadata::ProtocolVersion;
use kubewarden_policy_sdk::settings::SettingsValidationResponse;
use std::fmt;

use crate::admission_response::AdmissionResponse;
use crate::errors::PolicyEvaluatorError;
use crate::evaluation_context::EvaluationContext;
use crate::policy_evaluator::{PolicySettings, ValidateRequest};
use crate::runtimes::rego::Runtime as BurregoRuntime;
Expand Down Expand Up @@ -75,12 +75,12 @@ impl PolicyEvaluator {
}
}

pub fn protocol_version(&mut self) -> Result<ProtocolVersion> {
pub fn protocol_version(&mut self) -> Result<ProtocolVersion, PolicyEvaluatorError> {
match &mut self.runtime {
Runtime::Wapc(ref mut wapc_stack) => WapcRuntime(wapc_stack).protocol_version(),
_ => Err(anyhow!(
"protocol_version is only applicable to a Kubewarden policy"
)),
Runtime::Wapc(ref mut wapc_stack) => Ok(WapcRuntime(wapc_stack)
.protocol_version()
.map_err(PolicyEvaluatorError::InvokeWapcProtocolVersion)?),
_ => Err(PolicyEvaluatorError::InvalidProtocolVersion()),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/runtimes/rego/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ pub enum RegoRuntimeError {

#[error("cannot allocate Rego evaluator: {0}")]
EvaluatorError(String),

#[error("cannot build Rego engine: {0}")]
RegoEngineBuilder(#[source] burrego::errors::BurregoError),
}
7 changes: 4 additions & 3 deletions src/runtimes/rego/stack_pre.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;

use crate::policy_evaluator::RegoPolicyExecutionMode;
use crate::policy_evaluator_builder::EpochDeadlines;
use crate::runtimes::rego::errors::{RegoRuntimeError, Result};

/// This struct allows to follow the `StackPre -> Stack`
/// "pattern" also for Rego policies.
Expand Down Expand Up @@ -50,7 +49,9 @@ impl StackPre {
if let Some(deadlines) = self.epoch_deadlines {
builder = builder.enable_epoch_interruptions(deadlines.wapc_func);
}
let evaluator = builder.build()?;
let evaluator = builder
.build()
.map_err(RegoRuntimeError::RegoEngineBuilder)?;
Ok(evaluator)
}
}
28 changes: 28 additions & 0 deletions src/runtimes/wapc/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use thiserror::Error;

pub type Result<T> = std::result::Result<T, WapcRuntimeError>;

#[derive(Error, Debug)]
pub enum WapcRuntimeError {
#[error("invalid response format: {0}")]
InvalidResponseFormat(#[source] anyhow::Error),

#[error("invalid response from policy: {0}")]
InvalidResponseWithError(#[source] serde_json::Error),

#[error("cannot create ProtocolVersion object from {res:?}: {error}")]
CreateProtocolVersion {
res: std::vec::Vec<u8>,
#[source]
error: wasmtime::Error,
},

#[error("cannot invoke 'protocol_version' waPC function : {0}")]
InvokeProtocolVersion(#[source] wapc::errors::Error),

#[error("cannot build Wasmtime engine: {0}")]
WasmtimeEngineBuilder(#[source] wasmtime_provider::errors::Error),

#[error("cannot build Wapc host: {0}")]
WapcHostBuilder(#[source] wapc::errors::Error),
}
1 change: 1 addition & 0 deletions src/runtimes/wapc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod callback;
pub mod errors;
mod runtime;
mod stack;
mod stack_pre;
Expand Down
23 changes: 9 additions & 14 deletions src/runtimes/wapc/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use crate::runtimes::wapc::errors::{Result, WapcRuntimeError};
use kubewarden_policy_sdk::metadata::ProtocolVersion;
use kubewarden_policy_sdk::response::ValidationResponse as PolicyValidationResponse;
use kubewarden_policy_sdk::settings::SettingsValidationResponse;
Expand Down Expand Up @@ -58,14 +58,17 @@ impl<'a> Runtime<'a> {
match self.0.call("validate", validate_str.as_bytes()) {
Ok(res) => {
let pol_val_resp: Result<PolicyValidationResponse> = serde_json::from_slice(&res)
.map_err(|e| anyhow!("cannot deserialize policy validation response: {:?}", e));
.map_err(WapcRuntimeError::InvalidResponseWithError);
pol_val_resp
.and_then(|pol_val_resp| {
AdmissionResponse::from_policy_validation_response(
uid.to_string(),
req_obj,
&pol_val_resp,
)
.map_err(|e| -> WapcRuntimeError {
WapcRuntimeError::InvalidResponseFormat(e)
})
})
.unwrap_or_else(|e| {
error!(
Expand Down Expand Up @@ -129,7 +132,7 @@ impl<'a> Runtime<'a> {
match self.0.call("validate_settings", settings.as_bytes()) {
Ok(res) => {
let vr: Result<SettingsValidationResponse> = serde_json::from_slice(&res)
.map_err(|e| anyhow!("cannot convert response: {:?}", e));
.map_err(WapcRuntimeError::InvalidResponseWithError);
vr.unwrap_or_else(|e| SettingsValidationResponse {
valid: false,
message: Some(format!("error: {e:?}")),
Expand All @@ -146,17 +149,9 @@ impl<'a> Runtime<'a> {

pub fn protocol_version(&self) -> Result<ProtocolVersion> {
match self.0.call("protocol_version", &[0; 0]) {
Ok(res) => ProtocolVersion::try_from(res.clone()).map_err(|e| {
anyhow!(
"Cannot create ProtocolVersion object from '{:?}': {:?}",
res,
e
)
}),
Err(err) => Err(anyhow!(
"Cannot invoke 'protocol_version' waPC function: {:?}",
err
)),
Ok(res) => ProtocolVersion::try_from(res.clone())
.map_err(|e| WapcRuntimeError::CreateProtocolVersion { res, error: e }),
Err(e) => Err(WapcRuntimeError::InvokeProtocolVersion(e)),
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/runtimes/wapc/stack.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Result;
use std::sync::Arc;

use crate::evaluation_context::EvaluationContext;
use crate::runtimes::wapc::callback::new_host_callback;
use crate::runtimes::wapc::{
callback::new_host_callback,
errors::{Result, WapcRuntimeError},
};

use super::StackPre;

Expand Down Expand Up @@ -56,7 +58,8 @@ impl WapcStack {
) -> Result<wapc::WapcHost> {
let engine_provider = pre.rehydrate()?;
let wapc_host =
wapc::WapcHost::new(Box::new(engine_provider), Some(new_host_callback(eval_ctx)))?;
wapc::WapcHost::new(Box::new(engine_provider), Some(new_host_callback(eval_ctx)))
.map_err(WapcRuntimeError::WapcHostBuilder)?;
Ok(wapc_host)
}
}
11 changes: 8 additions & 3 deletions src/runtimes/wapc/stack_pre.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use wasmtime_provider::wasmtime;

use crate::policy_evaluator_builder::EpochDeadlines;
use crate::runtimes::wapc::errors::{Result, WapcRuntimeError};

/// Reduce allocation time of new `WasmtimeProviderEngine`, see the `rehydrate` method
#[derive(Clone)]
Expand All @@ -22,15 +22,20 @@ impl StackPre {
builder = builder.enable_epoch_interruptions(deadlines.wapc_init, deadlines.wapc_func);
}

let engine_provider_pre = builder.build_pre()?;
let engine_provider_pre = builder
.build_pre()
.map_err(WapcRuntimeError::WasmtimeEngineBuilder)?;
Ok(Self {
engine_provider_pre,
})
}

/// Allocate a new `WasmtimeEngineProvider` instance by using a pre-allocated instance
pub(crate) fn rehydrate(&self) -> Result<wasmtime_provider::WasmtimeEngineProvider> {
let engine = self.engine_provider_pre.rehydrate()?;
let engine = self
.engine_provider_pre
.rehydrate()
.map_err(WapcRuntimeError::WasmtimeEngineBuilder)?;
Ok(engine)
}
}
Loading