Skip to content
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

Add clippy and cargo fmt CI check for the issuer #2071

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
run: |
rustup component add rustfmt
cargo fmt

- name: Cargo fmt VC Issuer
working-directory: demos/vc_issuer
run: |
rustup component add rustfmt
cargo fmt

- name: Commit Formatting changes
uses: EndBug/add-and-commit@v9
# We don't want to commit formatting changes to main
Expand Down Expand Up @@ -47,6 +54,15 @@ jobs:
cargo clippy -- -D clippy::all -D warnings -A clippy::manual_range_contains
cargo clippy --tests --benches -- -D clippy::all -D warnings -A clippy::manual_range_contains

- name: Cargo clippy VC Issuer
working-directory: demos/vc_issuer
# We run clippy twice (once without tests), so that it accurately reports dead code in the non-test configuration.
# `manual_range_contains` is disabled because a >= x && a < y reads more clearly than (x..y).contains(a) and
# there are additional caveats for floating point numbers (https://github.com/rust-lang/rust-clippy/issues/6455)
run: |
cargo clippy -- -D clippy::all -D warnings -A clippy::manual_range_contains
cargo clippy --tests --benches -- -D clippy::all -D warnings -A clippy::manual_range_contains

check-lockfile:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 1 addition & 2 deletions demos/vc_issuer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn apply_config(init: IssuerInit) {
}

fn authorize_vc_request(alias: &SignedIdAlias) -> Result<AliasTuple, IssueCredentialError> {
let alias_tuple = extract_id_alias(&alias)?;
let alias_tuple = extract_id_alias(alias)?;

if caller() != alias_tuple.id_dapp {
return Err(IssueCredentialError::UnauthorizedSubject(format!(
Expand Down Expand Up @@ -304,7 +304,6 @@ fn verify_single_argument(

let unexpected_arguments: Vec<&String> = arguments
.keys()
.into_iter()
.filter(|k| k.as_str() != expected_argument)
.collect();
if !unexpected_arguments.is_empty() {
Expand Down
60 changes: 19 additions & 41 deletions demos/vc_issuer/tests/issue_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use assert_matches::assert_matches;
use candid::{CandidType, Deserialize, Principal};
use canister_sig_util::{extract_raw_root_pk_from_der, CanisterSigPublicKey};
use canister_tests::api::internet_identity::vc_mvp as ii_api;
use canister_tests::framework::{
env, get_wasm_path, principal_1, test_principal, II_WASM,
};
use canister_tests::framework::{env, get_wasm_path, principal_1, test_principal, II_WASM};
use canister_tests::{flows, match_value};
use ic_cdk::api::management_canister::provisional::CanisterId;
use ic_test_state_machine_client::{call_candid, call_candid_as};
Expand All @@ -26,7 +24,9 @@ use vc_util::issuer_api::{
Icrc21VcConsentMessageRequest, IssueCredentialError, PrepareCredentialRequest,
PrepareCredentialResponse, SignedIdAlias as SignedIssuerIdAlias,
};
use vc_util::{did_for_principal, verify_credential_jws_with_canister_id, get_verified_id_alias_from_jws};
use vc_util::{
did_for_principal, get_verified_id_alias_from_jws, verify_credential_jws_with_canister_id,
};

const DUMMY_ROOT_KEY: &str ="308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c05030201036100adf65638a53056b2222c91bb2457b0274bca95198a5acbdadfe7fd72178f069bdea8d99e9479d8087a2686fc81bf3c4b11fe275570d481f1698f79d468afe0e57acc1e298f8b69798da7a891bbec197093ec5f475909923d48bfed6843dbed1f";
const DUMMY_II_CANISTER_ID: &str = "rwlgt-iiaaa-aaaaa-aaaaa-cai";
Expand All @@ -35,7 +35,8 @@ const DUMMY_II_CANISTER_ID: &str = "rwlgt-iiaaa-aaaaa-aaaaa-cai";
/// id dapp: nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz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2d-80NLXpxOs-YszCLd4yvrGBtLEGqe6rp6khNthCCBFgg0sz_P8xdqTDewOhKJUHmWFFrS7FQHnDotBDmmGoFfWCCBFggaAMB9TDaAhXeQPY8DCCUq90vqJJDqpDAVwU-0WdA9OmCBFgghh7VsiTOqTlAiY8hcsbF1pFnG5t1x4kQ7rt2bae_6iGCBFgggcqzMKDpDQKcyRl6xrGy4SIYEtgVJgSLlHGFvHN6zuSCBFggBNxwNVuf0_gTaiM6hbpNNCcEIBfxLHoor0N1mpX-uNeCBFggICEcda6JC5WRFIbzoGGJdJINoas-EWtoCU0lysCe3OGDAYIEWCA1U_ZYHVOz3Sdkb2HIsNoLDDiBuFfG3DxH6miIwRPra4MCRHRpbWWCA0mAuK7U3YmkvhZpc2lnbmF0dXJlWDCY_kVxXw7Wk8HlA0FqOpX-3WMdI0mmxAtY9DJv8xEkfitcTOR0FcE412IftkdH48hkdHJlZYMBggRYIPKxlnFAySvK4ahA_Q0IkEopYPh8H4_IRCFRGb2i23QRgwJDc2lngwJYIFcsa4eb-HMrTnmGWNje_RfErQYi0wNCJvGDrzqazq0OgwGCBFggg7ijRBePgPVau7zffNEvAXThew-FqcBH_cB-fF7722eDAlgg3ikzXLDphmWB8YbAxZDjZfLFd6bDS-sLAPzmVj0nlvSCA0A";
const DUMMY_ALIAS_ID_DAPP_PRINCIPAL: &str ="nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe";
const DUMMY_ALIAS_ID_DAPP_PRINCIPAL: &str =
"nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe";

lazy_static! {
/** The gzipped Wasm module for the current VC_ISSUER build, i.e. the one we're testing */
Expand Down Expand Up @@ -115,15 +116,15 @@ mod api {
canister_id: CanisterId,
employee_id: Principal,
) -> Result<String, CallError> {
call_candid(env, canister_id, "add_employee", (employee_id,)).map(|(x,)| x)
call_candid(env, canister_id, "add_employee", (employee_id,)).map(|(x,)| x)
}

pub fn add_graduate(
env: &StateMachine,
canister_id: CanisterId,
employee_id: Principal,
) -> Result<String, CallError> {
call_candid(env, canister_id, "add_graduate", (employee_id,)).map(|(x,)| x)
call_candid(env, canister_id, "add_graduate", (employee_id,)).map(|(x,)| x)
}

pub fn prepare_credential(
Expand Down Expand Up @@ -340,12 +341,7 @@ fn should_fail_get_credential_for_wrong_sender() {
let issuer_id = install_issuer(&env, &DUMMY_ISSUER_INIT);
let signed_id_alias = DUMMY_SIGNED_ID_ALIAS.clone();
let authorized_principal = Principal::from_text(DUMMY_ALIAS_ID_DAPP_PRINCIPAL).unwrap();
api::add_employee(
&env,
issuer_id,
authorized_principal,
)
.expect("failed to add employee");
api::add_employee(&env, issuer_id, authorized_principal).expect("failed to add employee");
let unauthorized_principal = test_principal(2);

match_value!(
Expand All @@ -371,8 +367,7 @@ fn should_fail_get_credential_for_wrong_sender() {
},
)
.expect("API call failed");
assert_matches!(get_credential_response,
GetCredentialResponse::Err(IssueCredentialError::UnauthorizedSubject(e)) if e.contains("Caller sl5og-mycaa-aaaaa-aaaap-4 does not match id alias dapp principal nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe."));
assert_matches!(get_credential_response, GetCredentialResponse::Err(IssueCredentialError::UnauthorizedSubject(e)) if e.contains("Caller sl5og-mycaa-aaaaa-aaaap-4 does not match id alias dapp principal nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe."));
}

#[test]
Expand All @@ -389,8 +384,7 @@ fn should_fail_prepare_credential_for_anonymous_caller() {
},
)
.expect("API call failed");
assert_matches!(response,
PrepareCredentialResponse::Err(IssueCredentialError::UnauthorizedSubject(e)) if e.contains("Caller 2vxsx-fae does not match id alias dapp principal nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe."));
assert_matches!(response, PrepareCredentialResponse::Err(IssueCredentialError::UnauthorizedSubject(e)) if e.contains("Caller 2vxsx-fae does not match id alias dapp principal nugva-s7c6v-4yszt-koycv-5b623-an7q6-ha2nz-kz6rs-hawgl-nznbe-rqe."));
}

#[test]
Expand Down Expand Up @@ -450,8 +444,7 @@ fn should_prepare_employee_credential_for_authorized_principal() {
let env = env();
let issuer_id = install_issuer(&env, &DUMMY_ISSUER_INIT);
let authorized_principal = Principal::from_text(DUMMY_ALIAS_ID_DAPP_PRINCIPAL).unwrap();
api::add_employee(&env, issuer_id, authorized_principal)
.expect("API call failed");
api::add_employee(&env, issuer_id, authorized_principal).expect("API call failed");
let response = api::prepare_credential(
&env,
issuer_id,
Expand All @@ -470,8 +463,7 @@ fn should_prepare_degree_credential_for_authorized_principal() {
let env = env();
let issuer_id = install_issuer(&env, &DUMMY_ISSUER_INIT);
let authorized_principal = Principal::from_text(DUMMY_ALIAS_ID_DAPP_PRINCIPAL).unwrap();
api::add_graduate(&env, issuer_id, authorized_principal)
.expect("API call failed");
api::add_graduate(&env, issuer_id, authorized_principal).expect("API call failed");
let response = api::prepare_credential(
&env,
issuer_id,
Expand Down Expand Up @@ -549,27 +541,16 @@ fn should_issue_credential_e2e() -> Result<(), CallError> {
&canister_sig_pk.canister_id,
&root_pk_raw,
)
.expect("Invalid ID alias");
.expect("Invalid ID alias");

api::add_employee(
&env,
issuer_id,
alias_tuple.id_dapp,
)?;
api::add_graduate(
&env,
issuer_id,
alias_tuple.id_dapp,
)?;
api::add_employee(&env, issuer_id, alias_tuple.id_dapp)?;
api::add_graduate(&env, issuer_id, alias_tuple.id_dapp)?;

for credential_spec in vec![employee_credential_spec(), degree_credential_spec()] {
for credential_spec in [employee_credential_spec(), degree_credential_spec()] {
let prepare_credential_response = api::prepare_credential(
&env,
issuer_id,
id_alias_credentials
.issuer_id_alias_credential
.id_dapp
.clone(),
id_alias_credentials.issuer_id_alias_credential.id_dapp,
&PrepareCredentialRequest {
credential_spec: credential_spec.clone(),
signed_id_alias: SignedIssuerIdAlias {
Expand All @@ -593,10 +574,7 @@ fn should_issue_credential_e2e() -> Result<(), CallError> {
let get_credential_response = api::get_credential(
&env,
issuer_id,
id_alias_credentials
.issuer_id_alias_credential
.id_dapp
.clone(),
id_alias_credentials.issuer_id_alias_credential.id_dapp,
&GetCredentialRequest {
credential_spec: credential_spec.clone(),
signed_id_alias: SignedIssuerIdAlias {
Expand Down