Skip to content

Commit

Permalink
Add '--log-viewer' argument for creating canister.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-dfinity committed Sep 24, 2024
1 parent cb345d8 commit a93a3f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
17 changes: 16 additions & 1 deletion e2e/tests-dfx/create.bash
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ teardown() {
assert_contains 'Log visibility: controllers'
}

@test "create with log allowed viewer list" {
@test "create with multiple log allowed viewer list in dfx.json" {
# Create two identities
assert_command dfx identity new --storage-mode plaintext alice
assert_command dfx identity new --storage-mode plaintext bob
Expand Down Expand Up @@ -382,3 +382,18 @@ teardown() {
assert_contains "${ALICE_PRINCIPAL}"
assert_contains "${BOB_PRINCIPAL}"
}

@test "create with multiple log allowed viewer list" {
# Create two identities
assert_command dfx identity new --storage-mode plaintext alice
assert_command dfx identity new --storage-mode plaintext bob
ALICE_PRINCIPAL=$(dfx identity get-principal --identity alice)
BOB_PRINCIPAL=$(dfx identity get-principal --identity bob)

dfx_start
assert_command dfx canister create --all --log-viewer "${ALICE_PRINCIPAL}" --log-viewer "${BOB_PRINCIPAL}" --no-wallet
assert_command dfx deploy e2e_project_backend --no-wallet
assert_command dfx canister status e2e_project_backend
assert_contains "${ALICE_PRINCIPAL}"
assert_contains "${BOB_PRINCIPAL}"
}
19 changes: 13 additions & 6 deletions src/dfx/src/commands/canister/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::lib::ic_attributes::{
use crate::lib::operations::canister::create_canister;
use crate::lib::root_key::fetch_root_key_if_needed;
use crate::util::clap::parsers::{
compute_allocation_parser, freezing_threshold_parser, memory_allocation_parser,
reserved_cycles_limit_parser, wasm_memory_limit_parser,
compute_allocation_parser, freezing_threshold_parser, log_visibility_parser,
memory_allocation_parser, reserved_cycles_limit_parser, wasm_memory_limit_parser,
};
use crate::util::clap::parsers::{cycle_amount_parser, icrc_subaccount_parser};
use crate::util::clap::subnet_selection_opt::SubnetSelectionOpt;
Expand All @@ -21,6 +21,7 @@ use clap::{ArgAction, Parser};
use dfx_core::error::identity::InstantiateIdentityFromNameError::GetIdentityPrincipalFailed;
use dfx_core::identity::CallSender;
use ic_agent::Identity as _;
use ic_utils::interfaces::management_canister::LogVisibility;
use icrc_ledger_types::icrc1::account::Subaccount;
use slog::info;

Expand Down Expand Up @@ -90,8 +91,14 @@ pub struct CanisterCreateOpts {
#[arg(long, value_parser = wasm_memory_limit_parser, hide = true)]
wasm_memory_limit: Option<Byte>,

#[command(flatten)]
log_visibility_opt: Option<LogVisibilityOpt>,
/// Specifies who is allowed to read the canister's logs.
/// Can be either "controllers" or "public".
#[arg(long, value_parser = log_visibility_parser, conflicts_with("log_viewer"))]
log_visibility: Option<LogVisibility>,

/// Specifies the identity name or the principal of the new controller.
#[arg(long, action = ArgAction::Append, conflicts_with("log_visibility"))]
log_viewer: Option<Vec<String>>,

/// Performs the call with the user Identity as the Sender of messages.
/// Bypasses the Wallet canister.
Expand Down Expand Up @@ -201,7 +208,7 @@ pub async fn exec(
.with_context(|| format!("Failed to read Wasm memory limit of {canister_name}."))?;
let log_visibility = get_log_visibility(
env,
opts.log_visibility_opt.as_ref(),
LogVisibilityOpt::from(&opts.log_visibility, &opts.log_viewer).as_ref(),
None,
Some(config_interface),
Some(canister_name),
Expand Down Expand Up @@ -286,7 +293,7 @@ pub async fn exec(
.with_context(|| format!("Failed to read Wasm memory limit of {canister_name}."))?;
let log_visibility = get_log_visibility(
env,
opts.log_visibility_opt.as_ref(),
LogVisibilityOpt::from(&opts.log_visibility, &opts.log_viewer).as_ref(),
None,
Some(config_interface),
Some(canister_name),
Expand Down
25 changes: 25 additions & 0 deletions src/dfx/src/lib/canister_logs/log_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ impl LogVisibilityOpt {
self.add_log_viewer.is_some() || self.remove_log_viewer.is_some()
}

pub fn from(
log_visibility: &Option<LogVisibility>,
log_viewer: &Option<Vec<String>>,
) -> Option<LogVisibilityOpt> {
if let Some(log_visibility) = log_visibility {
return Some(LogVisibilityOpt {
log_visibility: Some(log_visibility.clone()),
add_log_viewer: None,
remove_log_viewer: None,
set_log_viewer: None,
});
}

if let Some(log_viewer) = log_viewer {
return Some(LogVisibilityOpt {
log_visibility: None,
add_log_viewer: None,
remove_log_viewer: None,
set_log_viewer: Some(log_viewer.clone()),
});
}

None
}

pub fn to_log_visibility(
&self,
env: &dyn Environment,
Expand Down

0 comments on commit a93a3f9

Please sign in to comment.