Skip to content

Commit

Permalink
Merge pull request #1750 from demergent-labs/reloadjs_auth
Browse files Browse the repository at this point in the history
add authentication to autoreload, get rid of warning, add replica log…
lastmjs authored Apr 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents f4fd486 + 72105f4 commit 98381d7
Showing 7 changed files with 12 additions and 31 deletions.
8 changes: 4 additions & 4 deletions examples/large_files/test/tests.ts
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ export function getTests(canisterId: string): Test[] {
} catch (err: any) {
return {
Ok: err.message.includes(
'Not Authorized: must be a controller to call this method'
'Not Authorized: only controllers of this canister may call this method'
)
};
}
@@ -62,7 +62,7 @@ export function getTests(canisterId: string): Test[] {
} catch (err: any) {
return {
Ok: err.message.includes(
'Not Authorized: must be a controller to call this method'
'Not Authorized: only controllers of this canister may call this method'
)
};
}
@@ -81,7 +81,7 @@ export function getTests(canisterId: string): Test[] {
} catch (err: any) {
return {
Ok: err.message.includes(
'Not Authorized: must be a controller to call this method'
'Not Authorized: only controllers of this canister may call this method'
)
};
}
@@ -100,7 +100,7 @@ export function getTests(canisterId: string): Test[] {
} catch (err: any) {
return {
Ok: err.message.includes(
'Not Authorized: must be a controller to call this method'
'Not Authorized: only controllers of this canister may call this method'
)
};
}
3 changes: 0 additions & 3 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import { getCandidAndCanisterMethods } from './get_candid_and_canister_methods';
import { getCanisterJavaScript } from './get_canister_javascript';
import { getNamesAfterCli, getNamesBeforeCli } from './get_names';
import { handleCli } from './handle_cli';
import { logAutoreloadWarning } from './log_auto_reload_warning';
import { prepareDockerImage } from './prepare_docker_image';
import { prepareRustStagingArea } from './prepare_rust_staging_area';
import { logSuccess, time, unwrap } from './utils';
@@ -128,8 +127,6 @@ async function azle() {
);

logSuccess(canisterName, canisterId, replicaWebServerPort);

logAutoreloadWarning();
}

function createAzleDirectories() {
14 changes: 0 additions & 14 deletions src/compiler/log_auto_reload_warning.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/compiler/rust/canister_methods/src/hash_file.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub fn get_hash_file() -> proc_macro2::TokenStream {
hash_file_by_parts(&path, 0)
}

#[ic_cdk_macros::query(guard = is_authenticated)]
#[ic_cdk_macros::query(guard = guard_against_non_controllers)]
pub fn get_file_hash(path: String) -> Option<String> {
Some(
load_hashes()
@@ -19,7 +19,7 @@ pub fn get_hash_file() -> proc_macro2::TokenStream {
)
}

#[ic_cdk_macros::query(guard = is_authenticated)]
#[ic_cdk_macros::query(guard = guard_against_non_controllers)]
pub fn get_hash_status(path: String) -> Option<(u64, u64)> {
Some((get_bytes_hashed(&path), get_file_size(&path)?))
}
6 changes: 2 additions & 4 deletions src/compiler/rust/canister_methods/src/reload_js.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use quote::quote;

// TODO there is no authentication on this method
// TODO it is up to the developer to not deploy with this function
// TODO in the binary if they are worried about it
pub fn get_reload_js(env_vars: &Vec<(String, String)>) -> proc_macro2::TokenStream {
let azle_autoreload_env_var = env_vars.iter().find(|(key, _)| key == "AZLE_AUTORELOAD");

if let Some((_, value)) = azle_autoreload_env_var {
if value == "true" {
return quote! {
#[ic_cdk_macros::update]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
fn reload_js(timestamp: u64, chunk_number: u64, js_bytes: Vec<u8>, total_len: u64) {
RELOADED_JS_TIMESTAMP.with(|reloaded_js_timestamp| {
let mut reloaded_js_timestamp_mut = reloaded_js_timestamp.borrow_mut();
@@ -33,6 +30,7 @@ pub fn get_reload_js(env_vars: &Vec<(String, String)>) -> proc_macro2::TokenStre
if reloaded_js_complete_bytes.len() as u64 == total_len {
let js_string = String::from_utf8_lossy(&reloaded_js_complete_bytes);
initialize_js(&js_string, false);
ic_cdk::println!("Azle: Reloaded canister JavaScript");
}
});
}
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ pub fn get_upload_file_chunk() -> proc_macro2::TokenStream {
#check_if_latest_version_src
#hash_file_src

#[ic_cdk_macros::update(guard = is_authenticated)]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
pub fn upload_file_chunk(
dest_path: String,
timestamp: u64,
@@ -46,11 +46,11 @@ pub fn get_upload_file_chunk() -> proc_macro2::TokenStream {
}
}

pub fn is_authenticated() -> Result<(), String> {
pub fn guard_against_non_controllers() -> Result<(), String> {
if ic_cdk::api::is_controller(&ic_cdk::api::caller()) {
return Ok(());
}
return Err("Not Authorized: must be a controller to call this method".to_string());
return Err("Not Authorized: only controllers of this canister may call this method".to_string());
}

pub fn start_hash(dest_path: String) {
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ pub fn get_check_if_latest_version_src() -> proc_macro2::TokenStream {
})
}

#[ic_cdk_macros::update(guard = is_authenticated)]
#[ic_cdk_macros::update(guard = guard_against_non_controllers)]
pub fn clear_file_and_info(path: String) {
reset_for_new_upload(&path, 0).unwrap()
}

0 comments on commit 98381d7

Please sign in to comment.