Skip to content

Commit

Permalink
Merge pull request #3290 from golemfactory/test-environment-identity-…
Browse files Browse the repository at this point in the history
…with-base-gsb-address

Test environment identity with base gsb address
  • Loading branch information
nieznanysprawiciel authored Aug 9, 2024
2 parents b2cd4ea + 15f8bb1 commit 039a743
Show file tree
Hide file tree
Showing 32 changed files with 619 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/system-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --test '*' -p yagna -p ya-exe-unit -p ya-transfer -p ya-payment --features framework-test
args: --test '*' -p yagna -p ya-exe-unit -p ya-transfer -p ya-payment -p ya-identity --features framework-test
7 changes: 6 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static-openssl = ["openssl/vendored", "openssl-probe"]
dummy-driver = ['ya-dummy-driver']
erc20-driver = ['ya-erc20-driver']
tos = []
framework-test = ['ya-exe-unit/framework-test', 'ya-payment/framework-test']
framework-test = ['ya-exe-unit/framework-test', 'ya-payment/framework-test', 'ya-identity/framework-test']
# Temporary to make goth integration tests work
central-net = ['ya-net/central-net']
packet-trace-enable = [
Expand Down
8 changes: 8 additions & 0 deletions core/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ description = "Yagna identity management"
authors = ["Golem Factory <contact@golem.network>"]
edition = "2018"

[features]
framework-test = []


[dependencies]
ya-client-model = { version = "0.6", features = ["with-diesel"] }
ya-core-model = { version = "^0.9", features = ["identity", "appkey"] }
Expand Down Expand Up @@ -39,6 +43,8 @@ yansi = "0.5.0"
[dev-dependencies]
ya-service-api-derive = "0.2"
ya-sb-router = { workspace = true }
ya-framework-basic = { version = "0.1" }
ya-framework-mocks = { version = "0.1" }

actix-rt = "2.7"
actix-service = "2"
Expand All @@ -47,6 +53,8 @@ awc = "3"
base64 = "0.12"
dotenv = "0.15"
env_logger = "0.7.1"
serial_test = { git = "https://github.com/tworec/serial_test.git", branch = "actix_rt_test", features = ["actix-rt2"] }
test-context = "0.1.4"

[lints]
workspace = true
2 changes: 1 addition & 1 deletion core/identity/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod appkey;
mod identity;

pub use appkey::AppKeyCommand;
pub use identity::IdentityCommand;
pub use identity::{IdentityCommand, NodeOrAlias};
use structopt::StructOpt;
use ya_service_api::{CliCtx, CommandOutput};

Expand Down
30 changes: 20 additions & 10 deletions core/identity/src/cli/appkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use anyhow::Result;
use structopt::*;

use ya_core_model::appkey as model;
use ya_core_model::bus::GsbBindPoints;
use ya_core_model::identity as idm;
use ya_core_model::identity::IdentityInfo;
use ya_service_api::{CliCtx, CommandOutput, ResponseTable};
use ya_service_bus::{typed as bus, RpcEndpoint};
use ya_service_bus::RpcEndpoint;

#[derive(StructOpt, Debug)]
#[structopt(setting = clap::AppSettings::DeriveDisplayOrder)]
Expand Down Expand Up @@ -41,16 +42,19 @@ pub enum AppKeyCommand {
}

impl AppKeyCommand {
async fn get_identity(get_by: idm::Get) -> anyhow::Result<IdentityInfo> {
bus::service(idm::BUS_ID)
async fn get_identity(gsb: GsbBindPoints, get_by: idm::Get) -> anyhow::Result<IdentityInfo> {
gsb.local()
.send(get_by)
.await
.map_err(anyhow::Error::msg)?
.map_err(anyhow::Error::msg)?
.ok_or_else(|| anyhow::Error::msg("Identity not found"))
}

pub async fn run_command(&self, _ctx: &CliCtx) -> Result<CommandOutput> {
pub async fn run_command(&self, ctx: &CliCtx) -> Result<CommandOutput> {
let gsb = ctx.gsb.service(model::BUS_SERVICE_NAME);
let ident_gsb = ctx.gsb.service(idm::BUS_SERVICE_NAME);

match &self {
AppKeyCommand::Create {
name,
Expand All @@ -63,36 +67,41 @@ impl AppKeyCommand {
if id.starts_with("0x") {
id.parse()?
} else {
Self::get_identity(idm::Get::ByAlias(id.into()))
Self::get_identity(ident_gsb.clone(), idm::Get::ByAlias(id.into()))
.await?
.node_id
}
}
None => Self::get_identity(idm::Get::ByDefault).await?.node_id,
None => {
Self::get_identity(ident_gsb.clone(), idm::Get::ByDefault)
.await?
.node_id
}
};
let create = model::Create {
name: name.clone(),
role: role.clone(),
identity,
allow_origins: allow_origin.clone(),
};
let key = bus::service(model::BUS_ID).send(create).await??;
let key = gsb.local().send(create).await??;
Ok(CommandOutput::Object(serde_json::to_value(key)?))
}
AppKeyCommand::Drop { name, id } => {
let remove = model::Remove {
name: name.clone(),
identity: id.clone(),
};
bus::service(model::BUS_ID)
gsb.local()
.send(remove)
.await
.map_err(anyhow::Error::msg)?
.unwrap();
Ok(CommandOutput::NoOutput)
}
AppKeyCommand::Show { name } => {
let appkey = bus::service(model::BUS_ID)
let appkey = gsb
.local()
.send(model::GetByName { name: name.clone() })
.await
.map_err(anyhow::Error::msg)?
Expand All @@ -105,7 +114,8 @@ impl AppKeyCommand {
page: *page,
per_page: *per_page,
};
let result: (Vec<model::AppKey>, u32) = bus::service(model::BUS_ID)
let result: (Vec<model::AppKey>, u32) = gsb
.local()
.send(list)
.await
.map_err(anyhow::Error::msg)?
Expand Down
26 changes: 15 additions & 11 deletions core/identity/src/cli/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,14 @@ fn to_private_key(key_file_json: &str) -> Result<[u8; 32], anyhow::Error> {
}

impl IdentityCommand {
pub async fn run_command(&self, _ctx: &CliCtx) -> Result<CommandOutput> {
pub async fn run_command(&self, ctx: &CliCtx) -> Result<CommandOutput> {
let gsb = ctx.gsb.service(identity::BUS_SERVICE_NAME);
match self {
IdentityCommand::List { .. } => list::list().await,
IdentityCommand::List { .. } => list::list(&gsb).await,
IdentityCommand::Show { node_or_alias } => {
let command: identity::Get = node_or_alias.clone().unwrap_or_default().into();
CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(command)
.await
.map_err(anyhow::Error::msg)?,
Expand All @@ -239,7 +240,7 @@ impl IdentityCommand {
IdentityCommand::PubKey { node_or_alias } => {
let node_id = node_or_alias.clone().unwrap_or_default().resolve().await?;
CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(identity::GetPubKey(node_id))
.await
.map_err(anyhow::Error::msg)?
Expand Down Expand Up @@ -282,7 +283,7 @@ impl IdentityCommand {
let payload = sha256.finalize().to_vec();

CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(identity::Sign { node_id, payload })
.await
.map_err(anyhow::Error::msg)?
Expand All @@ -298,7 +299,8 @@ impl IdentityCommand {
set_default,
} => {
let node_id = alias_or_id.resolve().await?;
let id = bus::service(identity::BUS_ID)
let id = gsb
.local()
.send(
identity::Update::with_id(node_id)
.with_alias(alias.clone())
Expand Down Expand Up @@ -358,7 +360,8 @@ impl IdentityCommand {
crate::id_key::generate_new_keyfile(password, from_private_key_slice)?
};

let id = bus::service(identity::BUS_ID)
let id = gsb
.local()
.send(identity::CreateGenerated {
alias: alias.clone(),
from_keystore: Some(key_file),
Expand All @@ -384,7 +387,7 @@ impl IdentityCommand {
None
};
CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(identity::Lock::with_id(node_id).with_set_password(password))
.await
.map_err(anyhow::Error::msg)?,
Expand All @@ -401,7 +404,7 @@ impl IdentityCommand {
rpassword::read_password_from_tty(Some("Password: "))?
};
CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(identity::Unlock::with_id(node_id, password))
.await
.map_err(anyhow::Error::msg)?,
Expand All @@ -410,14 +413,15 @@ impl IdentityCommand {
IdentityCommand::Drop {
node_or_alias,
force,
} => drop_id::drop_id(node_or_alias, *force).await,
} => drop_id::drop_id(&gsb, node_or_alias, *force).await,
IdentityCommand::Export {
node_or_alias,
file_path,
plain,
} => {
let node_id = node_or_alias.clone().unwrap_or_default().resolve().await?;
let mut key_file = bus::service(identity::BUS_ID)
let mut key_file = gsb
.local()
.send(identity::GetKeyFile(node_id))
.await?
.map_err(anyhow::Error::msg)?;
Expand Down
14 changes: 10 additions & 4 deletions core/identity/src/cli/identity/drop_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{bus, identity, CommandOutput, NodeOrAlias, Result, RpcEndpoint};
use super::{identity, CommandOutput, NodeOrAlias, Result, RpcEndpoint};
use ya_core_model::bus::GsbBindPoints;

async fn prompt(message: &str, question: &str) -> anyhow::Result<bool> {
use tokio::io::{self, AsyncWriteExt};
Expand All @@ -19,9 +20,14 @@ async fn prompt(message: &str, question: &str) -> anyhow::Result<bool> {
Ok(v)
}

pub async fn drop_id(node_or_alias: &NodeOrAlias, force: bool) -> Result<CommandOutput> {
pub async fn drop_id(
gsb: &GsbBindPoints,
node_or_alias: &NodeOrAlias,
force: bool,
) -> Result<CommandOutput> {
let command: identity::Get = node_or_alias.clone().into();
let id = bus::service(identity::BUS_ID)
let id = gsb
.local()
.send(command)
.await
.map_err(anyhow::Error::msg)?;
Expand Down Expand Up @@ -63,7 +69,7 @@ pub async fn drop_id(node_or_alias: &NodeOrAlias, force: bool) -> Result<Command
}

CommandOutput::object(
bus::service(identity::BUS_ID)
gsb.local()
.send(identity::DropId::with_id(id.node_id))
.await
.map_err(anyhow::Error::msg)?,
Expand Down
6 changes: 4 additions & 2 deletions core/identity/src/cli/identity/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
pub async fn list() -> Result<CommandOutput> {
let mut identities: Vec<identity::IdentityInfo> = bus::service(identity::BUS_ID)
use ya_core_model::bus::GsbBindPoints;
pub async fn list(gsb: &GsbBindPoints) -> Result<CommandOutput> {
let mut identities: Vec<identity::IdentityInfo> = gsb
.local()
.send(identity::List::default())
.await
.map_err(anyhow::Error::msg)
Expand Down
18 changes: 15 additions & 3 deletions core/identity/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use futures::lock::Mutex;
use std::sync::Arc;

use ya_core_model as model;
use ya_core_model::bus::GsbBindPoints;
use ya_persistence::executor::DbExecutor;
use ya_service_api_interfaces::{Provider, Service};

Expand All @@ -18,16 +20,26 @@ impl Service for Identity {

impl Identity {
pub async fn gsb<Context: Provider<Self, DbExecutor>>(context: &Context) -> anyhow::Result<()> {
Self::gsb_prefixed(context, None).await
}

pub async fn gsb_prefixed<Context: Provider<Self, DbExecutor>>(
context: &Context,
base: Option<GsbBindPoints>,
) -> anyhow::Result<()> {
let db = context.component();
let gsb = base.unwrap_or_default();
let gsb_ident = Arc::new(gsb.clone().service(model::identity::BUS_SERVICE_NAME));

let service = Arc::new(Mutex::new(
identity::IdentityService::from_db(db.clone()).await?,
));
identity::IdentityService::bind_service(service);
identity::IdentityService::bind_service(service, gsb_ident.clone());

identity::wait_for_default_account_unlock().await?;
identity::wait_for_default_account_unlock(gsb_ident.clone()).await?;

appkey::activate(&db).await?;
let gsb_appkey = Arc::new(gsb.service(model::appkey::BUS_SERVICE_NAME));
appkey::activate(&db, gsb_appkey.clone()).await?;
Ok(())
}
}
Loading

0 comments on commit 039a743

Please sign in to comment.