Skip to content
21 changes: 16 additions & 5 deletions src/console/src/api/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::cdn::proposals::{
submit_proposal as make_submit_proposal,
};
use crate::cdn::strategies_impls::cdn::CdnHeap;
use crate::cdn::strategies_impls::storage::StorageState;
use crate::cdn::strategies_impls::storage::{StorageCertificate, StorageState};
use crate::guards::caller_is_admin_controller;
use crate::types::interface::DeleteProposalAssets;
use ic_cdk::trap;
Expand Down Expand Up @@ -89,12 +89,23 @@ pub fn list_custom_domains() -> CustomDomains {

#[update(guard = "caller_is_admin_controller")]
pub fn set_custom_domain(domain_name: DomainName, bn_id: Option<String>) {
junobuild_cdn::storage::set_domain_store(&CdnHeap, &StorageState, &domain_name, &bn_id)
.unwrap_or_else(|e| trap(&e));
junobuild_cdn::storage::set_domain_store(
&CdnHeap,
&StorageState,
&StorageCertificate,
&domain_name,
&bn_id,
)
.unwrap_or_else(|e| trap(&e));
}

#[update(guard = "caller_is_admin_controller")]
pub fn del_custom_domain(domain_name: DomainName) {
junobuild_cdn::storage::delete_domain_store(&CdnHeap, &StorageState, &domain_name)
.unwrap_or_else(|e| trap(&e));
junobuild_cdn::storage::delete_domain_store(
&CdnHeap,
&StorageState,
&StorageCertificate,
&domain_name,
)
.unwrap_or_else(|e| trap(&e));
}
4 changes: 2 additions & 2 deletions src/console/src/api/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cdn::strategies_impls::cdn::CdnHeap;
use crate::cdn::strategies_impls::storage::StorageState;
use crate::cdn::strategies_impls::storage::{StorageCertificate, StorageState};
use crate::guards::caller_is_admin_controller;
use crate::types::interface::Config;
use ic_cdk::trap;
Expand All @@ -23,7 +23,7 @@ pub fn get_config() -> Config {

#[update(guard = "caller_is_admin_controller")]
pub fn set_storage_config(config: SetStorageConfig) -> StorageConfig {
junobuild_cdn::storage::set_config_store(&CdnHeap, &StorageState, &config)
junobuild_cdn::storage::set_config_store(&CdnHeap, &StorageState, &StorageCertificate, &config)
.unwrap_or_else(|e| trap(&e))
}

Expand Down
4 changes: 2 additions & 2 deletions src/console/src/cdn/certified_assets/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cdn::strategies_impls::cdn::CdnHeap;
use crate::cdn::strategies_impls::storage::StorageState;
use crate::cdn::strategies_impls::storage::{StorageCertificate, StorageState};

pub fn init_certified_assets() {
junobuild_cdn::storage::init_certified_assets(&CdnHeap, &StorageState);
junobuild_cdn::storage::init_certified_assets(&CdnHeap, &StorageState, &StorageCertificate);
}
12 changes: 11 additions & 1 deletion src/console/src/cdn/strategies_impls/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::cdn::helpers::stable::{
get_asset_stable, insert_asset_encoding_stable, insert_asset_stable,
};
use crate::cdn::strategies_impls::cdn::CdnHeap;
use crate::certification::cert::update_certified_data;
use candid::Principal;
use junobuild_cdn::storage::errors::{
JUNO_CDN_STORAGE_ERROR_CANNOT_GET_ASSET_UNKNOWN_REFERENCE_ID,
Expand All @@ -20,7 +21,8 @@ use junobuild_shared::types::core::Blob;
use junobuild_shared::types::domain::CustomDomains;
use junobuild_shared::types::state::Controllers;
use junobuild_storage::strategies::{
StorageAssertionsStrategy, StorageStateStrategy, StorageUploadStrategy,
StorageAssertionsStrategy, StorageCertificateStrategy, StorageStateStrategy,
StorageUploadStrategy,
};
use junobuild_storage::types::config::StorageConfig;
use junobuild_storage::types::state::FullPath;
Expand Down Expand Up @@ -236,3 +238,11 @@ impl StorageUploadStrategy for StorageUpload {
}
}
}

pub struct StorageCertificate;

impl StorageCertificateStrategy for StorageCertificate {
fn update_certified_data(&self) {
update_certified_data();
}
}
7 changes: 7 additions & 0 deletions src/console/src/certification/cert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use ic_cdk::api::certified_data_set as set_certified_data;
use junobuild_storage::runtime::certified_assets_root_hash;

pub fn update_certified_data() {
let asset_hashes_root_hash = &certified_assets_root_hash();
set_certified_data(&asset_hashes_root_hash[..]);
}
1 change: 1 addition & 0 deletions src/console/src/certification/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cert;
1 change: 1 addition & 0 deletions src/console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod api;
mod cdn;
mod certification;
mod constants;
mod controllers;
mod factory;
Expand Down
5 changes: 3 additions & 2 deletions src/libs/cdn/src/storage/certified_assets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::strategies::CdnHeapStrategy;
use junobuild_storage::certification::types::certified::CertifiedAssetHashes;
use junobuild_storage::certified_assets::extend_and_init_certified_assets;
use junobuild_storage::strategies::StorageStateStrategy;
use junobuild_storage::strategies::{StorageCertificateStrategy, StorageStateStrategy};

pub fn init_certified_assets(
cdn_heap: &impl CdnHeapStrategy,
storage_state: &impl StorageStateStrategy,
certificate: &impl StorageCertificateStrategy,
) {
let mut asset_hashes = CertifiedAssetHashes::default();

Expand All @@ -15,7 +16,7 @@ pub fn init_certified_assets(
asset_hashes.insert(asset, config);
}

extend_and_init_certified_assets(&mut asset_hashes, config, storage_state);
extend_and_init_certified_assets(&mut asset_hashes, config, storage_state, certificate);
});
});
}
11 changes: 7 additions & 4 deletions src/libs/cdn/src/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::storage::heap::{delete_domain, get_config, get_domain, insert_config,
use crate::storage::{assert_set_config, init_certified_assets};
use crate::strategies::CdnHeapStrategy;
use junobuild_shared::types::core::DomainName;
use junobuild_storage::strategies::StorageStateStrategy;
use junobuild_storage::strategies::{StorageCertificateStrategy, StorageStateStrategy};
use junobuild_storage::types::config::StorageConfig;
use junobuild_storage::types::interface::SetStorageConfig;
use junobuild_storage::well_known::update::update_custom_domains_asset;
Expand All @@ -15,6 +15,7 @@ use junobuild_storage::well_known::utils::build_custom_domain;
pub fn set_config_store(
cdn_heap: &impl CdnHeapStrategy,
storage_state: &impl StorageStateStrategy,
certificate: &impl StorageCertificateStrategy,
proposed_config: &SetStorageConfig,
) -> Result<StorageConfig, String> {
let current_config = get_config(cdn_heap);
Expand All @@ -25,7 +26,7 @@ pub fn set_config_store(

insert_config(cdn_heap, &config);

init_certified_assets(cdn_heap, storage_state);
init_certified_assets(cdn_heap, storage_state, certificate);

Ok(config)
}
Expand All @@ -37,12 +38,13 @@ pub fn set_config_store(
pub fn set_domain_store(
cdn_heap: &impl CdnHeapStrategy,
storage_state: &impl StorageStateStrategy,
certificate: &impl StorageCertificateStrategy,
domain_name: &DomainName,
bn_id: &Option<String>,
) -> Result<(), String> {
set_state_domain(cdn_heap, domain_name, bn_id);

update_custom_domains_asset(storage_state)
update_custom_domains_asset(storage_state, certificate)
}

fn set_state_domain(
Expand All @@ -60,9 +62,10 @@ fn set_state_domain(
pub fn delete_domain_store(
cdn_heap: &impl CdnHeapStrategy,
storage_state: &impl StorageStateStrategy,
certificate: &impl StorageCertificateStrategy,
domain_name: &DomainName,
) -> Result<(), String> {
delete_domain(cdn_heap, domain_name);

update_custom_domains_asset(storage_state)
update_custom_domains_asset(storage_state, certificate)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::assets::storage::strategy_impls::StorageState;
use crate::assets::storage::strategy_impls::{StorageCertificate, StorageState};
use crate::memory::internal::STATE;
use crate::types::state::State;
use junobuild_storage::certification::types::certified::CertifiedAssetHashes;
Expand All @@ -21,5 +21,10 @@ fn init_certified_assets_impl(state: &State) {
asset_hashes.insert(&entry.value(), config);
}

extend_and_init_certified_assets(&mut asset_hashes, config, &StorageState)
extend_and_init_certified_assets(
&mut asset_hashes,
config,
&StorageState,
&StorageCertificate,
)
}
3 changes: 2 additions & 1 deletion src/libs/satellite/src/assets/storage/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::assets::storage::state::{
get_asset, get_config, get_rule, insert_asset, insert_asset_encoding,
};
use crate::assets::storage::strategy_impls::StorageCertificate;
use crate::controllers::store::get_controllers;
use junobuild_collections::assert::stores::assert_permission;
use junobuild_collections::types::rules::Rule;
Expand Down Expand Up @@ -84,7 +85,7 @@ fn set_asset_handler_impl(

let config = get_config();

update_runtime_certified_asset(&asset, &config);
update_runtime_certified_asset(&asset, &config, &StorageCertificate);

Ok(())
}
18 changes: 11 additions & 7 deletions src/libs/satellite/src/assets/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::assets::storage::state::{
get_rule as get_state_rule, insert_config as insert_state_config,
insert_domain as insert_state_domain,
};
use crate::assets::storage::strategy_impls::{StorageAssertions, StorageState, StorageUpload};
use crate::assets::storage::strategy_impls::{
StorageAssertions, StorageCertificate, StorageState, StorageUpload,
};
use crate::auth::store::get_config as get_auth_config;
use crate::controllers::store::get_controllers;
use crate::memory::internal::STATE;
Expand Down Expand Up @@ -428,8 +430,10 @@ fn delete_asset_impl(
Some(asset) => {
assert_delete_asset(context, assert_context, &asset)?;

let certificate = &StorageCertificate;

let deleted = delete_state_asset(context.collection, &full_path, assert_context.rule);
delete_runtime_certified_asset(&asset);
delete_runtime_certified_asset(&asset, certificate);

// We just removed the rewrite for /404.html in the certification tree therefore if /index.html exists, we want to reintroduce it as rewrite
if *full_path == *ROOT_404_HTML {
Expand All @@ -438,7 +442,7 @@ fn delete_asset_impl(
&ROOT_INDEX_HTML.to_string(),
assert_context.rule,
) {
update_runtime_certified_asset(&index_asset, config);
update_runtime_certified_asset(&index_asset, config, certificate);
}
}

Expand All @@ -458,7 +462,7 @@ fn delete_assets_impl(
match deleted_asset {
None => {}
Some(deleted_asset) => {
delete_runtime_certified_asset(&deleted_asset);
delete_runtime_certified_asset(&deleted_asset, &StorageCertificate);
}
}
}
Expand Down Expand Up @@ -598,7 +602,7 @@ pub fn commit_batch_store(caller: Principal, commit_batch: CommitBatch) -> Resul

let config = get_config();

update_runtime_certified_asset(&asset, &config);
update_runtime_certified_asset(&asset, &config, &StorageCertificate);

Ok(asset)
}
Expand Down Expand Up @@ -671,13 +675,13 @@ pub fn get_custom_domains_store() -> CustomDomains {
fn delete_domain_impl(domain_name: &DomainName) -> Result<(), String> {
delete_state_domain(domain_name);

update_custom_domains_asset(&StorageState)
update_custom_domains_asset(&StorageState, &StorageCertificate)
}

fn set_domain_impl(domain_name: &DomainName, bn_id: &Option<String>) -> Result<(), String> {
set_state_domain_impl(domain_name, bn_id);

update_custom_domains_asset(&StorageState)
update_custom_domains_asset(&StorageState, &StorageCertificate)
}

fn set_state_domain_impl(domain_name: &DomainName, bn_id: &Option<String>) {
Expand Down
12 changes: 11 additions & 1 deletion src/libs/satellite/src/assets/storage/strategy_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::assets::storage::state::{
delete_asset, get_asset, get_config, get_domains, get_rule, insert_asset, insert_asset_encoding,
};
use crate::assets::storage::store::{get_content_chunks_store, get_public_asset_store};
use crate::certification::cert::update_certified_data;
use crate::hooks::storage::invoke_assert_upload_asset;
use crate::user::usage::assert::increment_and_assert_storage_usage;
use candid::Principal;
Expand All @@ -15,7 +16,8 @@ use junobuild_shared::types::core::Blob;
use junobuild_shared::types::domain::CustomDomains;
use junobuild_shared::types::state::Controllers;
use junobuild_storage::strategies::{
StorageAssertionsStrategy, StorageStateStrategy, StorageUploadStrategy,
StorageAssertionsStrategy, StorageCertificateStrategy, StorageStateStrategy,
StorageUploadStrategy,
};
use junobuild_storage::types::config::StorageConfig;
use junobuild_storage::types::state::FullPath;
Expand Down Expand Up @@ -196,3 +198,11 @@ impl StorageUploadStrategy for StorageUpload {
Ok(asset)
}
}

pub struct StorageCertificate;

impl StorageCertificateStrategy for StorageCertificate {
fn update_certified_data(&self) {
update_certified_data()
}
}
8 changes: 4 additions & 4 deletions src/libs/satellite/src/auth/alternative_origins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::assets::storage::store::get_custom_domains_store;
use crate::assets::storage::strategy_impls::StorageState;
use crate::assets::storage::strategy_impls::{StorageCertificate, StorageState};
use crate::auth::types::config::AuthenticationConfig;
use crate::errors::auth::JUNO_AUTH_ERROR_INVALID_ORIGIN;
use junobuild_shared::ic::api::id;
Expand Down Expand Up @@ -27,7 +27,7 @@ pub fn update_alternative_origins(config: &AuthenticationConfig) -> Result<(), S
}
}

delete_alternative_origins_asset(&StorageState)
delete_alternative_origins_asset(&StorageState, &StorageCertificate)
}

fn set_alternative_origins(
Expand Down Expand Up @@ -62,7 +62,7 @@ fn set_alternative_origins(
custom_domains.extend(external_domains);

if custom_domains.is_empty() {
return delete_alternative_origins_asset(&StorageState);
return delete_alternative_origins_asset(&StorageState, &StorageCertificate);
}

set_alternative_origins_with_custom_domains(&mut custom_domains)
Expand Down Expand Up @@ -105,5 +105,5 @@ fn set_alternative_origins_with_custom_domains(
"Cannot convert custom domains to II alternative origins JSON data.".to_string()
})?;

update_alternative_origins_asset(&json, &StorageState)
update_alternative_origins_asset(&json, &StorageState, &StorageCertificate)
}
7 changes: 7 additions & 0 deletions src/libs/satellite/src/certification/cert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use ic_cdk::api::certified_data_set as set_certified_data;
use junobuild_storage::runtime::certified_assets_root_hash;

pub fn update_certified_data() {
let asset_hashes_root_hash = &certified_assets_root_hash();
set_certified_data(&asset_hashes_root_hash[..]);
}
1 change: 1 addition & 0 deletions src/libs/satellite/src/certification/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cert;
1 change: 1 addition & 0 deletions src/libs/satellite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod api;
mod assets;
mod auth;
mod certification;
mod controllers;
mod db;
mod errors;
Expand Down
7 changes: 1 addition & 6 deletions src/libs/storage/src/certification/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ use crate::certification::tree_utils::response_headers_expression;
use crate::certification::types::certified::CertifiedAssetHashes;
use crate::http::types::HeaderField;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use ic_cdk::api::{certified_data_set as set_certified_data, data_certificate};
use ic_cdk::api::data_certificate;
use junobuild_shared::types::core::Blob;
use serde::Serialize;
use serde_cbor::ser::Serializer;

pub fn update_certified_data(asset_hashes: &CertifiedAssetHashes) {
let prefixed_root_hash = &asset_hashes.root_hash();
set_certified_data(&prefixed_root_hash[..]);
}

pub fn build_asset_certificate_header(
asset_hashes: &CertifiedAssetHashes,
url: String,
Expand Down
Loading