def get_item(uuid, fields=None): """ Helper function to get a certain field, you can find the UUID you need using list_items :param uuid: uuid of the item you wish to get, no vault needed :type uuid: str or bytes :param fields: to return only certain detail use either a specific field or list of them (optional, default=None which means all fields returned) :type fields: str or bytes or list :return: item :obj: `dict`: dict of the item with requested fields """ if isinstance(fields, list): item = json.loads( read_bash_return("op get item {} --fields {}".format( uuid, ",".join(fields)))) elif isinstance(fields, str): item = { fields: read_bash_return("op get item {} --fields {}".format( uuid, fields)) } else: item = json.loads(read_bash_return("op get item {}".format(uuid))) return item
def get_document(self, docname, vault="Private"): # pragma: no cover """ Helper function to get a document :param docname: title of the document (not it's filename) :type docname: str :param vault: vault the document is in (optional, default=Private) :type vault: str :returns: :obj:`dict`: document or None if doesn't exist """ docid = self.get_uuid(docname, vault=vault) try: return json.loads( read_bash_return("op get document {} --vault='{}'".format( docid, vault), single=False)) except JSONDecodeError: yaml_attempt = yaml.safe_load( read_bash_return("op get document {} --vault='{}'".format( docid, vault), single=False)) if isinstance(yaml_attempt, dict): return yaml_attempt else: print("File {} does not exist in 1Password vault: {}".format( docname, vault)) return None
def __init__(self, install_only=False, domain=None, email=None, secret=None, password=None, override_platform=None): # pragma: no cover self.signin_domain = domain self.email_address = email self.secret_key = secret self.override_platform = override_platform if isinstance(override_platform, str): self.system = override_platform else: self.system = str(platform.system()) self.user_home = read_bash_return("echo $HOME") # if self.system == "Linux": # self.local_bin = os.path.join(self.user_home, ".local/bin/") # elif self.system == "Darwin": self.local_bin = "/usr/local/bin/" # Get most recent version self.sub_soup, self.link, self.version = self.get_link_version() self.major_version = self.version.split(".")[1] # Check if installed: if true, get update, if false install if self.check_cli(): self.encrypted_master_password, self.session_key = self.signin_wrapper( master_password=password) else: self.install() if install_only: pass else: self.first_use(master_password=password)
def delete_document(self, title, vault="Private"): # pragma: no cover """ Helper function to delete a document :param title: title of the document you wish to remove :type title: str :param vault: vault the document is in (optional, default=Private) :type vault: str """ docid = self.get_uuid(title, vault=vault) cmd = "op delete item {} --vault='{}'".format(docid, vault) response = read_bash_return(cmd) if len(response) > 0: self._signin() read_bash_return(cmd)
def list_items(vault="Private"): """ Helper function to list all items in a certain vault :param vault: vault the items are in (optional, default=Private) :type vault: str :returns: items :obj:`dict`: dict of all items """ items = json.loads( read_bash_return("op list items --vault='{}'".format(vault))) return items
def put_document(self, filename, title, vault="Private"): # pragma: no cover """ Helper function to put a document :param filename: path and filename of document (must be saved locally already) :type filename: str :param title: title you wish to call the document :type title: str :param vault: vault the document is in (optional, default=Private) :type vault: str """ cmd = "op create document {} --title={} --vault='{}'".format( filename, title, vault) # [--tags=<tags>] response = read_bash_return(cmd) if len(response) == 0: self._signin() read_bash_return(cmd)
def check_cli(self): # pragma: no cover """ Helper function to check if op cli is already installed :returns: :obj:`bool`: True or False """ if self.override_platform == self.system: return False else: op = read_bash_return("op --version") if op == "": return False elif op.split(".")[1] == self.major_version: return True else: return False
def list_vaults(): """ Helper function to list all vaults """ return read_bash_return('op list vaults')
def signout(): """ Helper function to sign out of 1pass """ read_bash_return('op signout')
def test_check_cli(self): self.assertFalse(self.op.check_cli()) self.op.install() _, _, v = self.op.get_link_version() self.assertEqual(read_bash_return("op --version"), v)