def main(): """main""" global secret global pidFile inp = sys.stdin.readline() try: secret = json.loads(inp) except json.JSONDecodeError: msg = "invalid JSON on stdin, send encryption key as JSON to stdin" log.error(msg) sys.exit(1) if 'agent_token' not in secret: log.error("invalid JSON, must have: agent_token: %s", secret) if 'master_key' not in secret: log.error("invalid JSON, must have: master_key:%s", secret) sys.exit() log.debug("secret recieved:%s", secret) if 'foreground' in secret: log.debug("not daemonizing") pidFile = None daemonizedMain(secret) else: log.debug("Daemonizing...") wd = standardpaths.get_writable_path('app_local_data') with daemon.DaemonContext(working_directory=wd, files_preserve=["agent.log"], umask=0o002, pidfile=pidFile): daemonizedMain(secret)
def isAgentRunning(self): """return pid if agent is running, else None """ pidFile = os.path.join( standardpaths.get_writable_path('app_local_data'), 'agent.pid') if os.path.exists(pidFile): # agent already running, not so good for us. pid = int(open(pidFile, 'r').read()) if psutil.pid_exists(pid): log.debug("Agent running:%s", pid) return pid else: # cleanup os.unlink(pidFile) return None
def main(db, lib, with_color): """Open a database to manipulate on. The opened database will be assigned to `db` to be used in the REPL shell. """ if lib is not None: ejdb.init(lib) data_path = standardpaths.get_writable_path('app_local_data') if not data_path.exists(): data_path.mkdir(parents=True) with ejdb.Database(path=db, options=(ejdb.WRITE | ejdb.CREATE)) as db: run_repl_loop(db, data_path, with_color) print('Bye!')
def main(db, lib): """Open a database to manipulate on. The opened database will be assigned to `db` to be used in the REPL shell. """ if lib is not None: ejdb.init(lib) data_path = standardpaths.get_writable_path('app_local_data') if not data_path.exists(): data_path.mkdir(parents=True) with ejdb.Database(path=db, options=(ejdb.WRITE | ejdb.CREATE)) as db: run_repl_loop(db, data_path) print('Bye!')
def cli(ctx, url, identurl, debug, db): """Bitwarden CLI program.""" if not db: writePath = standardpaths.get_writable_path('app_local_data') filePath = os.path.join(writePath, 'bitwarden.sqlite') db = "sqlite:///{}".format(filePath) if not os.path.exists(writePath): # create config dir and make secure as possible. os.makedirs(writePath) os.chmod(writePath, 0o0700) if not os.path.exists(filePath): download_url = "https://fossil.birl.ca/bitwarden-cli/doc/trunk/tools/bitwarden.sqlite" msg = "Database does not exist." + os.linesep msg += "You can use Liquibase and generate it," + os.linesep msg += "or for the lazy:" + os.linesep msg += "curl -o {} {}" print(msg.format(filePath, download_url)) sys.exit(2) cli = CLI(url, identurl, debug, db) ctx.obj = cli
def master_key(self, value): """setter for master key -- starts agent set value to None will stop agent and not restart it. """ pid = self.isAgentRunning() if pid: log.debug("stopping agent, since we are being asked to set a new master_key.") os.kill(pid, signal.SIGTERM) if value is None: log.debug("value of none: shutdown agent, not starting it again") return key = base64.b64encode(value).decode('utf-8') agent_token = base64.b64encode(os.urandom(16)).decode('utf-8') cmd = [self.agent_location, '127.0.0.1:{}'.format(self.agent_port)] log.debug("running agent:%s", cmd) p = subprocess.Popen( cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) data = { 'master_key': key, 'agent_token': agent_token, 'port': self.agent_port, "timeout":0, 'agent_location': str(standardpaths.get_writable_path('app_local_data')) } timeout = self.agent_timeout if timeout > 0: data['timeout'] = timeout else: log.debug("sending timeout of 0 because:%s", timeout) log.debug("sending to agent:%s", pprint.pformat(data)) out = json.dumps(data) + "\n" p.stdin.write(out.encode('utf-8')) self.agent_token = agent_token # self.agent_timeout = time.time() + timeout out = p.communicate() log.debug("agent returned:%s:%s", out[0], out[1]) return True
def _test_against_qt(self, location, qt_location): path = get_writable_path(location) assert_is_instance(path, pathlib.Path) eq_(path.as_posix(), QStandardPaths.writableLocation(qt_location))
def test_unix__log(self): eq_(get_standard_paths(Location.log), [get_writable_path(Location.cache) / 'log'])
def test_windows__log(self): eq_(get_standard_paths(Location.log), [get_writable_path(Location.app_local_data) / 'log'])
def test_get_writable_path_string(): eq_(get_writable_path('desktop'), get_writable_path(Location.desktop))
def test_unix__log(self): eq_(get_writable_path(Location.log), get_writable_path(Location.cache) / 'log')
def test_windows__log(self): eq_(get_writable_path(Location.log), get_writable_path(Location.app_local_data) / 'log')
def test_osx__log(self): eq_(get_writable_path(Location.log), pathlib.Path(os.path.expanduser('~/Library/Logs/uranusjr/Yksom')))
""" import os import logging import json import sys import threading import daemon import daemon.pidfile import standardpaths import web standardpaths.configure(application_name='bitwarden', organization_name='birl.org') logFile = os.path.join(standardpaths.get_writable_path('app_local_data'), 'agent.log') lh = logging.FileHandler(logFile) lh.setFormatter( logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) log = logging.getLogger(__name__) log.addHandler(lh) if os.getenv('DEBUG', False): # print("debug ls on") log.setLevel(logging.DEBUG) # print("logging to:%s" % logFile) pidPath = os.path.join(standardpaths.get_writable_path('app_local_data'), 'agent.pid') pidFile = daemon.pidfile.PIDLockFile(pidPath)