Skip to content

Commit

Permalink
feat: validate parameters passed to perform
Browse files Browse the repository at this point in the history
  • Loading branch information
freaz committed Aug 17, 2023
1 parent fc96e08 commit e8abb90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ MAP_STD=integration/map-std/dist/map_std.js
PROFILE_VALIDATOR=integration/profile-validator/dist/profile_validator.js
CORE_SCHEMA_ASSETS=core/core/assets/schemas
CORE_SCHEMA_ASSETS_SECURITY_VALUES=${CORE_SCHEMA_ASSETS}/security_values.json
CORE_SCHEMA_ASSETS_PARAMETERS_VALUES=${CORE_SCHEMA_ASSETS}/parameters_values.json
SECURITY_VALUES_JSON_SCHEMA=core/json_schemas/src/schemas/security_values.json
PARAMETERS_VALUES_JSON_SCHEMA=core/json_schemas/src/schemas/parameters_values.json
# Hosts
HOST_JAVASCRIPT_ASSETS=host/javascript/assets
HOST_PYTHON_ASSETS=host/python/src/one_sdk/assets
Expand All @@ -73,7 +75,7 @@ git_hooks:
## CORE ##
##########
ifeq ($(CORE_DOCKER),1)
${CORE_DIST}: ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES}
${CORE_DIST}: ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES} ${CORE_SCHEMA_ASSETS_PARAMETERS_VALUES}
mkdir -p ${CORE_DIST}
docker build ./core -o ${CORE_DIST}
${CORE_WASM}: ${CORE_DIST}
Expand All @@ -83,7 +85,7 @@ deps_core: ${WASI_SDK_FOLDER}
rustup target add wasm32-wasi
curl https://wasmtime.dev/install.sh -sSf | bash

${CORE_DIST}: ${WASI_SDK_FOLDER} ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES}
${CORE_DIST}: ${WASI_SDK_FOLDER} ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES} ${CORE_SCHEMA_ASSETS_PARAMETERS_VALUES}
mkdir -p ${CORE_DIST}
touch ${CORE_DIST}

Expand All @@ -106,7 +108,7 @@ ${TEST_CORE_ASYNCIFY_WASM}: ${TEST_CORE_WASM}
${WASI_SDK_FOLDER}:
wget -qO - ${WASI_SDK_URL} | tar xzvf - -C core

test_core: ${WASI_SDK_FOLDER} ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES}
test_core: ${WASI_SDK_FOLDER} ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROFILE_VALIDATOR} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES} ${CORE_SCHEMA_ASSETS_PARAMETERS_VALUES}
cd core && cargo test -- -- --nocapture
endif

Expand All @@ -126,6 +128,10 @@ ${CORE_SCHEMA_ASSETS_SECURITY_VALUES}:
mkdir -p ${CORE_SCHEMA_ASSETS}
cp ${SECURITY_VALUES_JSON_SCHEMA} ${CORE_SCHEMA_ASSETS_SECURITY_VALUES}

${CORE_SCHEMA_ASSETS_PARAMETERS_VALUES}:
mkdir -p ${CORE_SCHEMA_ASSETS}
cp ${PARAMETERS_VALUES_JSON_SCHEMA} ${CORE_SCHEMA_ASSETS_PARAMETERS_VALUES}

clean_core:
rm -rf ${CORE_DIST} core/target

Expand Down
26 changes: 19 additions & 7 deletions core/core/src/sf_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ pub struct OneClientCore {
provider_cache: DocumentCache<ProviderJsonCacheEntry>,
map_cache: DocumentCache<MapCacheEntry>,
security_validator: JsonSchemaValidator,
parameters_validator: JsonSchemaValidator,
mapstd_config: MapStdImplConfig,
}
impl OneClientCore {
const MAP_STDLIB_JS: &str = include_str!("../assets/js/map_std.js");
const SECURITY_VALUES_JSON_SCHEMA: &str =
include_str!("../assets/schemas/security_values.json");
const PARAMETERS_VALUES_JSON_SCHEMA: &str =
include_str!("../assets/schemas/parameters_values.json");

// TODO: Use thiserror and define specific errors
pub fn new(config: &CoreConfiguration) -> anyhow::Result<Self> {
Expand All @@ -73,6 +76,11 @@ impl OneClientCore {
.expect("Valid JSON"),
)
.expect("Valid JSON Schema for security values exists"),
parameters_validator: JsonSchemaValidator::new(
&serde_json::Value::from_str(&OneClientCore::PARAMETERS_VALUES_JSON_SCHEMA)
.expect("Valid JSON"),
)
.expect("Valid JSON Schema for parameters values exists"),
mapstd_config: MapStdImplConfig {
log_http_transactions: config.user_log,
log_http_transactions_body_max_size: config.user_log_http_body_max_size,
Expand Down Expand Up @@ -203,19 +211,23 @@ impl OneClientCore {
// tracing::error!("Input validation error: {}", err);
// }

// TODO: Validate Parameters
// Validate parameters values against json schema
self.parameters_validator
.validate(&perform_input.map_parameters)
.map_err(|err| {
PerformException::from_json_schema_validation_error(
err,
Some("Parameters".to_string().as_ref()),
)
})?;

let mut map_parameters = match perform_input.map_parameters {
HostValue::Object(o) => MapValueObject::from_iter(
o.into_iter()
.map(|(k, v)| (k, self.host_value_to_map_value(v))),
),
HostValue::None => MapValueObject::new(),
_ => {
try_metrics!(Err(PerformException {
error_code: PerformExceptionErrorCode::ParametersFormatError,
message: "Parameters must be an Object or None".to_owned(),
}))
}
_ => unreachable!("Object or None ensured with JSON Schema validation"),
};

// Validate security values against json schema
Expand Down

0 comments on commit e8abb90

Please sign in to comment.