Skip to content

Commit

Permalink
Improve default identity generation by adding environment variable to…
Browse files Browse the repository at this point in the history
… set it on start, but only on start
  • Loading branch information
scx1332 committed Sep 26, 2024
1 parent e08b0cd commit c5eea51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
12 changes: 5 additions & 7 deletions core/identity/src/autoconf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ use ya_core_model::NodeId;
use crate::id_key::IdentityKey;
use anyhow::Context;

// autoconfiguration
const ENV_AUTOCONF_PK: &str = "YAGNA_AUTOCONF_ID_SECRET";
const ENV_AUTOCONF_APP_KEY: &str = "YAGNA_AUTOCONF_APPKEY";

pub fn preconfigured_identity(password: Protected) -> anyhow::Result<Option<IdentityKey>> {
let secret_hex: Vec<u8> = match env::var(ENV_AUTOCONF_PK) {
pub fn identity_from_env(password: Protected, env_name: &str) -> anyhow::Result<Option<IdentityKey>> {
let secret_hex: Vec<u8> = match env::var(env_name) {
Ok(v) => v
.from_hex()
.with_context(|| format!("Failed to parse identity from {}", ENV_AUTOCONF_PK))?,
.with_context(|| format!("Failed to parse identity from {}", env_name))?,
Err(_) => return Ok(None),
};
let secret = SecretKey::from_raw(&secret_hex)?;
Ok(Some(IdentityKey::from_secret(None, secret, password)))
}

pub fn preconfigured_node_id() -> anyhow::Result<Option<NodeId>> {
let secret_hex: Vec<u8> = match env::var(ENV_AUTOCONF_PK) {
pub fn preconfigured_node_id(env_name: &str) -> anyhow::Result<Option<NodeId>> {
let secret_hex: Vec<u8> = match env::var(env_name) {
Ok(v) => v.from_hex()?,
Err(_) => return Ok(None),
};
Expand Down
2 changes: 1 addition & 1 deletion core/identity/src/service/appkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub async fn activate(db: &DbExecutor, gsb: Arc<GsbBindPoints>) -> anyhow::Resul

let create_tx = tx.clone();
let preconfigured_appkey = crate::autoconf::preconfigured_appkey();
let preconfigured_node_id = crate::autoconf::preconfigured_node_id()?;
let preconfigured_node_id = crate::autoconf::preconfigured_node_id("YAGNA_AUTOCONF_ID_SECRET")?;
let start_datetime = Utc::now().naive_utc();

{
Expand Down
56 changes: 39 additions & 17 deletions core/identity/src/service/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ use std::convert::{TryFrom, TryInto};
use std::rc::Rc;
use std::sync::Arc;

use anyhow::bail;
use anyhow::{bail};
use chrono::Utc;
use ethsign::{KeyFile, Protected, PublicKey};
use futures::lock::Mutex;
use futures::prelude::*;

use structopt::lazy_static::lazy_static;
use ya_client_model::NodeId;
use ya_core_model::bus::GsbBindPoints;
use ya_service_bus::{typed as bus, RpcEndpoint, RpcMessage};

use ya_core_model::identity as model;
use ya_core_model::identity::event::IdentityEvent;
use ya_persistence::executor::DbExecutor;

use crate::dao::identity::Identity;
use crate::dao::{Error as DaoError, IdentityDao};
use crate::dao::{Error as DaoError, Error, IdentityDao};
use crate::id_key::{default_password, generate_identity_key, IdentityKey};

lazy_static! (
static ref DEFAULT_IDENTITY_INIT_PRIVATE_KEY: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
);

#[derive(Default)]
struct Subscription {
subscriptions: Vec<String>,
Expand Down Expand Up @@ -93,7 +96,7 @@ impl IdentityService {
}

let default_key =
if let Some(key) = crate::autoconf::preconfigured_identity(default_password())? {
if let Some(key) = crate::autoconf::identity_from_env(default_password(), "YAGNA_AUTOCONF_ID_SECRET")? {
db.as_dao::<IdentityDao>()
.init_preconfigured(Identity {
identity_id: key.id(),
Expand All @@ -109,18 +112,37 @@ impl IdentityService {
} else {
db.as_dao::<IdentityDao>()
.init_default_key(|| {
log::info!("generating new default identity");
let key: IdentityKey = generate_identity_key(None, "".into(), None);

Ok(Identity {
identity_id: key.id(),
key_file_json: key.to_key_file().map_err(DaoError::internal)?,
is_default: true,
is_deleted: false,
alias: None,
note: None,
created_date: Utc::now().naive_utc(),
})
match crate::autoconf::identity_from_env(default_password(), "YAGNA_DEFAULT_SECRET_KEY") {
Ok(Some(key)) => {
log::info!("Using default identity from given private key YAGNA_DEFAULT_SECRET_KEY, id: {}", key.id());
Ok(Identity {
identity_id: key.id(),
key_file_json: key.to_key_file().map_err(DaoError::internal)?,
is_default: true,
is_deleted: false,
alias: None,
note: None,
created_date: Utc::now().naive_utc(),
})
}
Ok(None) => {
let key: IdentityKey = generate_identity_key(None, "".into(), None);
log::info!("Generated new default identity: {}", key.id());

Ok(Identity {
identity_id: key.id(),
key_file_json: key.to_key_file().map_err(DaoError::internal)?,
is_default: true,
is_deleted: false,
alias: None,
note: None,
created_date: Utc::now().naive_utc(),
})
},
Err(err) => {
Err(Error::internal(format!("Failed to get default secret key from env: {:?}", err)))
}
}
})
.await?
.identity_id
Expand Down

0 comments on commit c5eea51

Please sign in to comment.