-
Notifications
You must be signed in to change notification settings - Fork 98
feat: add dfx canister set-controller subcommand #1057
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8ea28ce
chore: update ic-agent to latest master
hansl 2bbf077
feat: add set controller
f87e785
add subcommand
73d3f89
correct apostrophe on possesive
41f196e
help message
6e51a6f
fix msg
04200ad
update ic-agent
hansl 25c8124
.
hansl 06dc84f
.
hansl 863862d
.
hansl 2da1ff5
remove long
6b9902f
.
hansl fce4c6a
.
hansl 764774d
test
8a42544
lock file
14f5069
fmt
d623c0a
Merge remote-tracking branch 'origin/pshahi/test-branch' into hansl/u…
hansl a70f41e
remove SDKROOT
90b5eaf
.
hansl 31167d1
.
hansl 1262605
Merge branch 'hansl/update-agent' of github.com:dfinity-lab/sdk into …
74491d1
add e2e test
b8e1b9d
fmt
57b5b83
ref output diff
828e7ad
Merge branch 'master' of github.com:dfinity-lab/sdk into pshahi/setco…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| load utils/_ | ||
|
|
||
| setup() { | ||
| # We want to work from a temporary directory, different for every test. | ||
| cd $(mktemp -d -t dfx-e2e-XXXXXXXX) | ||
|
|
||
| # Each test gets its own home directory in order to have its own identities. | ||
| mkdir $(pwd)/home-for-test | ||
| export HOME=$(pwd)/home-for-test | ||
|
|
||
| dfx_new hello | ||
| } | ||
|
|
||
| teardown() { | ||
| dfx_stop | ||
| rm -rf $(pwd)/home-for-test | ||
| } | ||
|
|
||
| @test "set controller" { | ||
| # Create two identities and get their Principals | ||
| assert_command dfx identity new jose | ||
| assert_command dfx identity new juana | ||
| JOSE_PRINCIPAL=$(dfx --identity jose identity get-principal) | ||
| JUANA_PRINCIPAL=$(dfx --identity juana identity get-principal) | ||
|
|
||
| assert_command dfx identity use jose | ||
|
|
||
| dfx_start | ||
| dfx canister create hello | ||
| dfx build hello | ||
| dfx canister install hello | ||
| ID=$(dfx canister id hello) | ||
|
|
||
| # Set controller using canister name and identity name | ||
| assert_command dfx canister set-controller hello juana | ||
| assert_match "Set \"juana\" as controller of \"hello\"." | ||
|
|
||
| # Juana is controller, Jose cannot reinstall | ||
| assert_command_fail dfx canister install hello -m reinstall | ||
| if [ "$USE_IC_REF" ] | ||
| then | ||
| assert_match "${JOSE_PRINCIPAL} is not authorized to manage canister ${ID}" | ||
| else | ||
| assert_match "Only the controller of canister ${ID} can control it." | ||
| fi | ||
|
|
||
| # Juana can reinstall | ||
| assert_command dfx --identity juana canister install hello -m reinstall | ||
|
|
||
| assert_command dfx identity use juana | ||
| # Set controller using canister id and principal | ||
| assert_command dfx canister set-controller ${ID} ${JOSE_PRINCIPAL} | ||
| assert_match "Set \"${JOSE_PRINCIPAL}\" as controller of \"${ID}\"." | ||
| assert_command_fail dfx canister install hello -m reinstall | ||
|
|
||
| # Set controller using combination of name/id and identity/principal | ||
| assert_command dfx --identity jose canister set-controller hello ${JUANA_PRINCIPAL} | ||
| assert_match "Set \"${JUANA_PRINCIPAL}\" as controller of \"hello\"." | ||
|
|
||
| assert_command dfx --identity juana canister set-controller ${ID} jose | ||
| assert_match "Set \"jose\" as controller of \"${ID}\"." | ||
|
|
||
| # Set controller using invalid principal/identity fails | ||
| assert_command_fail dfx --identity jose canister set-controller hello bob | ||
| assert_match "Identity bob does not exist" | ||
|
|
||
| # Set controller using invalid canister name/id fails | ||
| assert_command_fail dfx --identity jose canister set-controller hello_assets juana | ||
| assert_match "Cannot find canister id. Please issue 'dfx canister create hello_assets'." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use crate::lib::environment::Environment; | ||
| use crate::lib::error::{DfxError, DfxResult}; | ||
| use crate::lib::identity::identity_manager::IdentityManager; | ||
| use crate::lib::message::UserMessage; | ||
| use crate::lib::models::canister_id_store::CanisterIdStore; | ||
| use crate::lib::waiter::waiter_with_timeout; | ||
| use crate::util::expiry_duration; | ||
| use clap::{App, Arg, ArgMatches, SubCommand}; | ||
| use ic_agent::Identity; | ||
| use ic_types::principal::Principal as CanisterId; | ||
| use ic_utils::call::AsyncCall; | ||
| use ic_utils::interfaces::ManagementCanister; | ||
| use tokio::runtime::Runtime; | ||
|
|
||
| pub fn construct() -> App<'static, 'static> { | ||
| SubCommand::with_name("set-controller") | ||
| .about(UserMessage::SetController.to_str()) | ||
| .arg( | ||
| Arg::with_name("canister") | ||
| .takes_value(true) | ||
| .help(UserMessage::SetControllerCanister.to_str()) | ||
| .required(true), | ||
| ) | ||
| .arg( | ||
| Arg::with_name("new-controller") | ||
| .takes_value(true) | ||
| .help(UserMessage::NewController.to_str()) | ||
| .required(true), | ||
| ) | ||
| } | ||
|
|
||
| pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult { | ||
| let canister = args.value_of("canister").unwrap(); | ||
| let canister_id = match CanisterId::from_text(canister) { | ||
| Ok(id) => id, | ||
| Err(_) => CanisterIdStore::for_env(env)?.get(canister)?, | ||
| }; | ||
|
|
||
| let new_controller = args.value_of("new-controller").unwrap(); | ||
| let controller_principal = match CanisterId::from_text(new_controller) { | ||
| Ok(principal) => principal, | ||
| Err(_) => IdentityManager::new(env)? | ||
| .instantiate_identity_from_name(new_controller)? | ||
| .sender()?, | ||
| }; | ||
|
|
||
| let timeout = expiry_duration(); | ||
|
|
||
| let mgr = ManagementCanister::create( | ||
| env.get_agent() | ||
| .ok_or(DfxError::CommandMustBeRunInAProject)?, | ||
| ); | ||
|
|
||
| let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
| runtime.block_on( | ||
| mgr.set_controller(&canister_id, &controller_principal) | ||
| .call_and_wait(waiter_with_timeout(timeout)), | ||
| )?; | ||
|
|
||
| println!("Set {:?} as controller of {:?}.", new_controller, canister); | ||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.