From 661ef8dcbf87876d788eaa87961bfe139931b830 Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:52:07 -0500 Subject: [PATCH] significantly improved ldapsearch with jq improve ldapsearch secondary queries --- modules/common/shell/default.nix | 1 + modules/common/shell/work.nix | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 modules/common/shell/work.nix diff --git a/modules/common/shell/default.nix b/modules/common/shell/default.nix index efbdd84..4ac2f1d 100644 --- a/modules/common/shell/default.nix +++ b/modules/common/shell/default.nix @@ -12,5 +12,6 @@ ./nixpkgs.nix ./starship.nix ./utilities.nix + ./work.nix ]; } diff --git a/modules/common/shell/work.nix b/modules/common/shell/work.nix new file mode 100644 index 0000000..ea600ba --- /dev/null +++ b/modules/common/shell/work.nix @@ -0,0 +1,42 @@ +{ config, pkgs, lib, ... }: + +{ + + home-manager.users.${config.user} = lib.mkIf pkgs.stdenv.isDarwin { + + home.packages = let + ldap_scheme = "ldaps"; + magic_number = "2"; + magic_end_seq = "corp"; + magic_prefix = "take"; + ldap_host = + "${magic_prefix}${magic_number}.t${magic_number}.${magic_end_seq}"; + ldap_port = 636; + ldap_dc_1 = "${magic_prefix}${magic_number}"; + ldap_dc_2 = "t${magic_number}"; + ldap_dc_3 = magic_end_seq; + ldap_script = pkgs.writeShellScriptBin "ldap" '' + SEARCH_FILTER="$@" + ldapsearch -LLL \ + -B -o ldif-wrap=no \ + -H "${ldap_scheme}://${ldap_host}:${builtins.toString ldap_port}" \ + -D "${pkgs.lib.toUpper magic_prefix}${magic_number}\\${ + pkgs.lib.toLower config.user + }" \ + -w "$(${pkgs._1password}/bin/op item get T${magic_number} --fields label=password)" \ + -b "DC=${ldap_dc_1},DC=${ldap_dc_2},DC=${ldap_dc_3}" \ + -s "sub" -x "(cn=$SEARCH_FILTER)" \ + | jq --slurp \ + --raw-input 'split("\n\n")|map(split("\n")|map(select(.[0:1]!="#" and length>0)) |select(length > 0)|map(capture("^(?[^:]*:?): *(?.*)") |if .key[-1:.key|length] == ":" then .key=.key[0:-1]|.value=(.value|@base64d) else . end)| group_by(.key) | map({key:.[0].key,value:(if .|length > 1 then [.[].value] else .[].value end)}) | from_entries)' | jq -r 'del(.[].thumbnailPhoto)' + ''; + ldapm_script = pkgs.writeShellScriptBin "ldapm" '' + ${ldap_script}/bin/ldap "$@" | jq '[ .[].memberOf] | add' + ''; + ldapg_script = pkgs.writeShellScriptBin "ldapg" '' + ${ldap_script}/bin/ldap "$@" | jq '[ .[].member] | add' + ''; + in [ ldap_script ldapm_script ldapg_script ]; + + }; + +}