Skip to content

Commit

Permalink
Now loading full vault details
Browse files Browse the repository at this point in the history
  • Loading branch information
dteare committed Mar 9, 2022
1 parent b90d986 commit 35493f0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod op;
mod op7_metadata;

use op::{find_items, find_vaults, load_all_accounts, AccountDetails, ItemOverview, VaultOverview};
use op::{
find_items, load_all_accounts, load_all_vaults, AccountDetails, ItemOverview, VaultDetails,
};
use op7_metadata::write_items;

use clap::Parser;
Expand Down Expand Up @@ -55,8 +57,8 @@ fn generate_opbookmarks(account_user_uuids: &Vec<String>, export_path: &std::pat
}

let accounts = accounts.unwrap();
let mut vaults_by_account: HashMap<AccountDetails, Vec<VaultOverview>> = HashMap::new();
let mut items_by_vault: HashMap<VaultOverview, Vec<ItemOverview>> = HashMap::new();
let mut vaults_by_account: HashMap<AccountDetails, Vec<VaultDetails>> = HashMap::new();
let mut items_by_vault: HashMap<VaultDetails, Vec<ItemOverview>> = HashMap::new();

println!(
"Exporting bookmarks for accounts {:?}",
Expand All @@ -68,7 +70,7 @@ fn generate_opbookmarks(account_user_uuids: &Vec<String>, export_path: &std::pat

// Collect the vaults for each account
for account in accounts.iter() {
let vaults = find_vaults(&account.id);
let vaults = load_all_vaults(&account.id);

match vaults {
Ok(vaults) => {
Expand Down
35 changes: 32 additions & 3 deletions src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct VaultDetails {

pub attribute_version: usize,
pub content_version: usize,
pub items: usize,

#[serde(rename = "type")]
pub vault_type: String,
Expand Down Expand Up @@ -182,6 +181,36 @@ pub fn get_account(user_id: &String) -> Result<AccountDetails, Error> {
serde_json::from_slice(json.as_slice()).map_err(|e| Error::Deserialize(e))
}

pub fn load_all_vaults(account_id: &String) -> Result<Vec<VaultDetails>, Error> {
let vaults = find_vaults(&account_id);

match vaults {
Ok(vaults) => {
let mut details: Vec<VaultDetails> = vec![];
for vault in vaults.iter() {
let ad = get_vault(&account_id, &vault.id);

match ad {
Ok(ad) => details.push(ad),
Err(e) => {
eprint!(
"Error loading vault details for account {}: {:?}",
account_id, e
);
return Err(Error::OPCLI(format!(
"Failed to load details for account {}",
account_id
)));
}
}
}

Ok(details)
}
Err(e) => Err(e),
}
}

pub fn find_vaults(account_id: &String) -> Result<Vec<VaultOverview>, Error> {
let output = Command::new("op")
.arg("--format")
Expand All @@ -205,12 +234,12 @@ pub fn find_vaults(account_id: &String) -> Result<Vec<VaultOverview>, Error> {
}

// op --account BXRGOJ2Z5JB4RMA7FUYUURELUE --format json vault get jnnjfdrzr5rawkimmsvp3zzzxe
pub fn get_vault(account: &AccountOverview, vault_id: String) -> Result<VaultDetails, Error> {
pub fn get_vault(account_id: &String, vault_id: &String) -> Result<VaultDetails, Error> {
let output = Command::new("op")
.arg("--format")
.arg("json")
.arg("--account")
.arg(account.user_uuid.clone())
.arg(account_id)
.arg("vault")
.arg("get")
.arg(vault_id)
Expand Down
10 changes: 5 additions & 5 deletions src/op7_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Create metadata files that conform to the format used by 1Password 7
use crate::op::{AccountDetails, ItemOverview, VaultOverview};
use crate::op::{AccountDetails, ItemOverview, VaultDetails};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct OP7ItemMetaData {
pub fn write_items(
export_path: &std::path::PathBuf,
items: &Vec<ItemOverview>,
vault: &VaultOverview,
vault: &VaultDetails,
account: &AccountDetails,
) {
let mut path = export_path.clone();
Expand Down Expand Up @@ -98,14 +98,14 @@ fn write_file(path: std::path::PathBuf, contents: String) {

fn create_op7_metadata(
item: &ItemOverview,
vault: &VaultOverview,
vault: &VaultDetails,
account_id: &String,
) -> OP7ItemMetaData {
return OP7ItemMetaData {
uuid: item.id.clone(),
item_description: format!("Login from {}", &vault.name.clone().unwrap()),
item_description: format!("Login from {}", &vault.name.clone()),
item_title: item.title.clone(),
vault_name: vault.name.clone().unwrap().clone(),
vault_name: vault.name.clone().clone(),
vault_uuid: vault.id.clone(),
category_plural_name: item.category.clone(), // TODO: Map SECURE_NOTE, etc
profile_uuid: account_id.clone(),
Expand Down

0 comments on commit 35493f0

Please sign in to comment.