From 09d840031233002a3d154760639d1ead3775a66c Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Wed, 17 Jul 2024 12:05:21 -0500 Subject: [PATCH] Add vpn alias for Xonsh Adding a set of aliases to my Xonsh work to start up a VPN that uses BitWarden for OTP storage Changed my Linux to specifically use Python 3.12 because the flag for NamedTemporaryFile is introduced in 3.12 --- home/xonsh_footer.xsh | 32 +++++++++++++++++++++++++++----- overlays/default.nix | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/home/xonsh_footer.xsh b/home/xonsh_footer.xsh index 26baea5..2009a22 100644 --- a/home/xonsh_footer.xsh +++ b/home/xonsh_footer.xsh @@ -1,5 +1,7 @@ # vim: set ft=python : +from tempfile import NamedTemporaryFile + def bw_unlock(): """Unlocks the BitWarden CLI and adds the resulting session code to the current environment variables. Also returns the code for them.""" @@ -15,6 +17,24 @@ def bw_unlock(): $BW_SESSION = token return token +def vpn(con, bwname): + bw_unlock() + base=$(bw get password @(bwname)) + secret=$(bw get totp @(bwname)) + #echo vpn.secrets.password:${base}$(oathtool -b -d "${digits}" -s "${period}" --totp "${secret}") > "${f}" + + with NamedTemporaryFile(delete_on_close=False) as fp: + secret = f"vpn.secrets.password:{base}{secret}" + fp.write(secret.encode("utf-8")) + fp.close() + nmcli c up @(con) passwd-file @(fp.name) + +def _unlock(args): + bw_unlock() + +def _ivr(args): + vpn("350Main", "IVR Technology") + def _glrestart(args): sudo nixos-container run gitlab -- systemctl restart gitlab sudo nixos-container run gitlab -- systemctl restart nginx @@ -73,16 +93,18 @@ def _bake(args): copier copy @(str(templates / args[0])) . aliases['glrestart'] = _glrestart -aliases['cfetch'] = _cfetch aliases['bake'] = _bake -aliases['rebuild'] = _rebuild -aliases['yaml2json'] = _yaml2json +aliases['unlock'] = _unlock +aliases['cfetch'] = _cfetch +aliases['ivr'] = _ivr +aliases['newdock'] = _newdock +aliases['pip_extras'] = _pip_extras aliases['py2env'] = _py2env aliases['py3env'] = _py3env +aliases['rebuild'] = _rebuild aliases['rundock'] = _rundock -aliases['newdock'] = _newdock aliases['unknown_host'] = _unknown_host -aliases['pip_extras'] = _pip_extras +aliases['yaml2json'] = _yaml2json ### # # Other random nice-to-have things diff --git a/overlays/default.nix b/overlays/default.nix index d8f7454..ce58c26 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -18,7 +18,7 @@ let virtualenv ]; - myPython = prev.python3.withPackages myPackages; + myPython = prev.python312.withPackages myPackages; macOver = file: og: if prev.stdenv.isDarwin then (prev.callPackage file {}) else