Compare commits

...

42 Commits

Author SHA1 Message Date
f7ed3b3867 Updated arch_list_aws4 2025-09-19 19:33:46 +08:00
6341ecb1b9 update arch utils 2025-08-08 01:58:54 +08:00
9bee126e82 Fixed kcontext use s3-arch broken on passphrase 2025-08-07 00:43:46 +08:00
65d9193da7 Updated arch_download_aws4 2025-07-28 08:51:07 +08:00
b0e98bad0a Added type for pwgen 2025-05-20 04:24:17 +08:00
33a411b203 Update arch_list_aws4 signature 2025-05-19 22:26:55 +08:00
c23b94a7b4 Update arch_list_aws4 signature 2025-05-19 14:04:32 +08:00
aece66a1d1 Migrate pwgen.py to bash 2025-04-25 14:22:40 +08:00
47afa44ed3 klabels can now filter key & value 2025-02-14 22:58:38 +08:00
b19df9bdf3 Added 19 k8s helper functions 2025-02-14 04:00:29 +08:00
f82aa85f70 Update arch_upload_aws4 2025-02-04 23:12:33 +08:00
a7037ab76d Removed hard-coded table name 2024-11-11 06:28:53 +08:00
80ec3e8691 reg-rename can also rename dirs 2024-11-04 05:35:55 +08:00
f69c09a37d Do not ask to save password for unsupported os 2024-07-11 23:14:55 +08:00
9a9425b1e4 Use file as a global bool 2024-07-11 07:22:43 +08:00
ec489a9530 Reset secret if decrypt failed 2024-07-11 06:55:03 +08:00
7257ab62bc Show admin symbol in cygwin 2024-04-12 02:11:55 +09:00
15e97dcc40 Added kstore autocomplete 2023-10-13 20:43:09 +08:00
6d1b390256 update kcontext autocomplete 2023-10-13 20:15:41 +08:00
811b56705c Cygwin support for kstore-secret-auto 2023-10-09 05:00:21 +09:00
9221c199ae Fixed kcontext k8s path for cygwin 2023-10-09 04:11:58 +09:00
585254f1af Wrong redirect order 2023-10-09 02:39:12 +08:00
da241a1027 Validate some kstore args 2023-10-09 02:37:42 +08:00
f6d744fda5 kstore added upload / download db 2023-10-09 02:28:27 +08:00
13dc0770b8 Added kcontext-k8s 2023-10-09 01:30:56 +08:00
f13fe397b6 Always download when called 2023-10-08 01:51:13 +08:00
d4ce0501a1 Rename arch-s3 binaries 2023-10-08 01:47:13 +08:00
e1aa01423f Update s3 arch utils 2023-10-08 01:42:47 +08:00
8e368db056 Skip create table in kstore-init if db exist 2023-09-23 23:33:01 +08:00
b134d89d40 17_kcontext draft 2023-09-23 23:11:09 +08:00
3395589b9e Wrong redirect order 2023-09-17 23:42:07 +09:00
612ab0a434 Update s3-arch sum 2023-09-17 23:34:40 +09:00
a9a13d6b39 Fixed spaces in path not working 2022-10-08 22:15:12 +09:00
6b079328d3 Added init-mintty 2022-09-21 18:05:26 +09:00
248dc44eec Make some go compiler commands available 2022-09-12 18:41:23 +08:00
100162f4b0 Fixed mkuserdirs not working properly 2022-09-10 22:26:51 +09:00
ab7c656cf5 Auto select node & nmap 2022-08-30 17:56:49 +09:00
1dce4b450c Download arch utils 2022-08-15 22:32:16 +08:00
e7562bf01f Fixed unary operator error 2022-08-15 21:56:55 +08:00
d9a5f57aaa Remove redundant codes 2022-08-15 21:54:43 +08:00
ac7c199a7b oopsies 2022-08-15 21:32:20 +08:00
6d8d38f2a0 default to username instead 2022-08-15 21:26:33 +08:00
12 changed files with 718 additions and 177 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.DS_Store
*.prod *.prod
*.log *.log

View File

@@ -15,14 +15,15 @@ RBASH_REMOTE="https://git.k8s.astropenguin.net"
RBASH_REPO="penguin/utils" RBASH_REPO="penguin/utils"
RBASH_PATH="bash" RBASH_PATH="bash"
RBASH_MERGED="$RBASH_SOURCES/merged" RBASH_MERGED="$RBASH_SOURCES/merged"
RBASH_BIN="$RBASH_HOME/bin"
RHOSTNAME="Penguins-Mac-Studio" RHOSTNAME="<HOSTNAME>"
RDOMAIN="@local" RDOMAIN="<DOMAIN>"
RCOLOR="4" RCOLOR="<RCOLOR>"
if [ $RHOSTNAME == "<HOSTNAME"">" ]; then if [ "$RHOSTNAME" == "<HOSTNAME"">" ]; then
RHOSTNAME=$( hostname -s ) RHOSTNAME="\u"
RDOMAIN="@"$( hostname -d ) RDOMAIN="@"$( hostname -s )
RCOLOR=$(( $RANDOM * 6 / 32767 + 1 )) RCOLOR=$(( $RANDOM * 6 / 32767 + 1 ))
fi fi
@@ -90,13 +91,13 @@ function __download {
which curl 2>&1 > /dev/null which curl 2>&1 > /dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
curl -s "$1" curl -s "$1"
return return $?
fi fi
which wget 2>&1 > /dev/null which wget 2>&1 > /dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
wget -qO- "$1" wget -qO- "$1"
return return $?
fi fi
} }
@@ -115,7 +116,7 @@ function rbash_cache {
} }
_RBASH_LOADED= _RBASH_LOADED=
mkdir -p "$RBASH_SOURCES" mkdir -p "$RBASH_SOURCES" "$RBASH_BIN"
if [ ! -f "$RBASH_SOURCES/merged" ]; then if [ ! -f "$RBASH_SOURCES/merged" ]; then
_RBASH_MERGE_START=1 _RBASH_MERGE_START=1
@@ -289,8 +290,19 @@ function r2dissource {
rm -r "$_dis" "$RBASH_SOURCES" rm -r "$_dis" "$RBASH_SOURCES"
} }
export PS1="This is ${RHOSTNAME}\e[1;3${RCOLOR}m${RDOMAIN}\e[0m: \w\n\$ " _SHELLD="\$"
case $OSTYPE in
cygwin)
net session > /dev/null 2>&1
if [ $? -eq 0 ]; then
_SHELLD="#"
fi
;;
esac
export PS1="This is ${RHOSTNAME}\e[1;3${RCOLOR}m${RDOMAIN}\e[0m: \w\n$_SHELLD "
export EDITOR=vim export EDITOR=vim
export PATH="$RBASH_BIN:$PATH"
# Source for custom bash env, allows overriding # Source for custom bash env, allows overriding
if [ -f "$RBASH_ENV" ]; then if [ -f "$RBASH_ENV" ]; then
@@ -310,4 +322,3 @@ ___DEFAULT___
fi fi
source "$RBASH_CONFIG" source "$RBASH_CONFIG"
unset _RBASH_MERGE_START

View File

@@ -17,3 +17,24 @@ function kres {
kubectl get $1 -A $_O | grep "$_D*$2$_D*\s\+$_D*$3$_D*" kubectl get $1 -A $_O | grep "$_D*$2$_D*\s\+$_D*$3$_D*"
fi fi
} }
function pwgen {
local _LEN=$1 _TYPE=$2
case $_LEN in
*[!0-9]*)
echo "Enter a valid number" > /dev/stderr
return 1
;;
'')
echo "Usage: pwgen [LEN] [TYPE]"
return 1
;;
esac
if [ -z "$_TYPE" ]; then
_TYPE="graph"
fi
LC_ALL=C tr -dc "[:$_TYPE:]" </dev/urandom | head -c $_LEN
if [ -t 1 ]; then
echo
fi
}

View File

@@ -15,27 +15,29 @@ fi
_AUTH_DB=$RBASH_HOME/keystore.db _AUTH_DB=$RBASH_HOME/keystore.db
_AUTH_SECRET= _AUTH_SECRET=
_KSTORE_DEF_PROP=${_KSTORE_DEF_PROP:-val} _KSTORE_DEF_PROP=${_KSTORE_DEF_PROP:-val}
_KSTORE_DEC_FAILED="$RBASH_HOME/.decypt-failed"
_KSTORE_TABLE=store
if [ -f "$RBASH_HOME/keystore.secret" ]; then if [ -f "$RBASH_HOME/keystore.secret" ]; then
_AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" ) _AUTH_SECRET=$( cat "$RBASH_HOME/keystore.secret" )
fi fi
function kstore { function kstore {
case $1 in case $1 in
add) shift; kstore-add "$@" ;; add) shift; _kstoreadd "$@" ;;
del) shift; kstore-del "$@" ;; del) shift; _kstoredel "$@" ;;
get) shift; kstore-get "$@" ;; get) shift; _kstoreget "$@" ;;
list) shift; kstore-list "$@" ;; list) shift; _kstorelist "$@" ;;
query) shift; kstore-query "$@" ;; query) shift; _kstorequery "$@" ;;
update) shift; kstore-update "$@" ;; update) shift; _kstoreupdate "$@" ;;
search) shift; kstore-search $@ ;; search) shift; _kstoresearch $@ ;;
secret) shift; kstore-secret $@ ;; secret) shift; _kstoresecret $@ ;;
upload) shift; kstore-upload $@ ;;
*) *)
__func_head "add [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]" __func_head "add [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
__func_help "update [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]" __func_help "update [key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
__func_help "get [key] [prop, default: $_KSTORE_DEF_PROP]" __func_help "get [key] [prop, default: $_KSTORE_DEF_PROP]"
__func_help "del [key]" __func_help "del [key] [prop, default: $_KSTORE_DEF_PROP]"
__func_help "list" __func_help "list"
__func_help "search [key] [prop]" __func_help "search [key] [prop]"
__func_help "secret ..." __func_help "secret ..."
@@ -47,11 +49,39 @@ function kstore {
return $? return $?
} }
function kstore-secret { function _kstore {
local CUR=${COMP_WORDS[COMP_CWORD]}
local SCOPE=${COMP_WORDS[COMP_CWORD-1]}
local t
COMPREPLY=()
case "$SCOPE" in
kstore)
COMPREPLY=( $(compgen -W "add del get list query update search secret" -- $CUR) )
return
;;
get|update)
COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT key FROM $_KSTORE_TABLE" )" -- $CUR) )
;;
esac
t=${COMP_WORDS[COMP_CWORD-2]}
case "$t" in
get|update)
t=`_kstorequote "$SCOPE"`
COMPREPLY=( $(compgen -W "$( _kstorequery "SELECT DISTINCT prop FROM $_KSTORE_TABLE WHERE key = '$t'" )" -- $CUR) )
;;
esac
}
complete -F _kstore kstore
function _kstoresecret {
case "$1" in case "$1" in
clear) shift; kstore-secret-clear "$@" ;; clear) shift; _kstoresecret-clear "$@" ;;
config) shift; kstore-secret-config "$@" ;; config) shift; _kstoresecret-config "$@" ;;
change) shift; kstore-secret-change "$@" ;; change) shift; _kstoresecret-change "$@" ;;
*) *)
__func_head "clear" __func_head "clear"
__func_help "config" __func_help "config"
@@ -60,23 +90,26 @@ function kstore-secret {
esac esac
} }
function kstore-init { function _kstoreinit {
cat <<___SQL___ | $_SQLITE "$_AUTH_DB" if [ ! -f "$_AUTH_DB" ]; then
CREATE TABLE IF NOT EXISTS store ( cat <<___SQL___ | $_SQLITE "$_AUTH_DB"
CREATE TABLE IF NOT EXISTS $_KSTORE_TABLE (
key TEXT NOT NULL key TEXT NOT NULL
, prop TEXT NOT NULL , prop TEXT NOT NULL
, data TEXT , data TEXT
, PRIMARY KEY( key ASC, prop ASC ) , PRIMARY KEY( key ASC, prop ASC )
); );
___SQL___ ___SQL___
chmod 600 "$_AUTH_DB"
fi
kstore secret config kstore secret config
} }
function kstore-quote { function _kstorequote {
echo -n "$1" | sed -e "s/'/''/g" echo -n "$1" | sed -e "s/'/''/g"
} }
function kstore-enc { function _kstoreenc {
if [ -z "$_AUTH_SECRET" ]; then if [ -z "$_AUTH_SECRET" ]; then
echo "Secret key is not set yet" >&2 echo "Secret key is not set yet" >&2
return 1 return 1
@@ -91,25 +124,60 @@ function kstore-enc {
fi fi
} }
function kstore-dec { function _kstoredec {
if [ -z "$_AUTH_SECRET" ]; then if [ -z "$_AUTH_SECRET" ]; then
echo "Secret key is not set yet" >&2 echo "Secret key is not set yet" >&2
return 1 return 1
fi fi
openssl enc -d -aes-256-cbc -pbkdf2 -k "$_AUTH_SECRET" -a -A openssl enc -d -aes-256-cbc -pbkdf2 -k "$_AUTH_SECRET" -a -A
_code=$?
if [ $_code -ne 0 ]; then
touch $_KSTORE_DEC_FAILED
return $_code
fi
} }
function kstore-secret-auto { function _kstoresecret-auto {
case $OSTYPE in case $OSTYPE in
darwin*) kstore-secret-macos "$@" ;; darwin*) _kstoresecret-macos "$@" ;;
cygwin) _kstoresecret-cygwin "$@" ;;
*) *)
echo "$OSTYPE is Not supported yet" >&2 echo "$OSTYPE is Not supported yet" >&2
;; ;;
esac esac
} }
function kstore-secret-macos { function _kstoresecret-cygwin {
local _A
which kstorecred 2>/dev/null > /dev/null
if [ $? -ne 0 ]; then
_kstoredl-kstorecred
fi
case $1 in
get)
_A=$( kstorecred get )
if [ $? -ne 0 ]; then
return 1
fi
_AUTH_SECRET="$_A"
;;
set)
kstorecred set "$_AUTH_SECRET"
;;
del)
kstorecred del
;;
*)
echo "Unknown action: $1" >&2
return 1
;;
esac
}
function _kstoresecret-macos {
local _A local _A
case $1 in case $1 in
get) get)
@@ -132,19 +200,15 @@ function kstore-secret-macos {
esac esac
} }
function kstore-secret-windows { function _kstoresecret-config {
# Not impelemented yet
if [ "$1" == "get" ]; then
return 1
fi
return 1
}
function kstore-secret-config {
local _CONFIRM local _CONFIRM
if [ -f "$_KSTORE_DEC_FAILED" ]; then
rm $_KSTORE_DEC_FAILED
_AUTH_SECRET=
fi
if [ -z "$_AUTH_SECRET" ]; then if [ -z "$_AUTH_SECRET" ]; then
kstore-secret-auto get _kstoresecret-auto get
if [ -n "$_AUTH_SECRET" ]; then if [ -n "$_AUTH_SECRET" ]; then
return 0 return 0
fi fi
@@ -156,17 +220,20 @@ function kstore-secret-config {
return 1 return 1
fi fi
read -p "Save this password to OS's keystore? (y/n): " _CONFIRM case $OSTYPE in
if [ "$_CONFIRM" != "y" ]; then cygwin|darwin*)
return 0 read -p "Save this password to OS's keystore? (y/n): " _CONFIRM
fi if [ "$_CONFIRM" != "y" ]; then
return 0
kstore-secret-auto set fi
_kstoresecret-auto set
;;
esac
fi fi
} }
function kstore-secret-change { function _kstoresecret-change {
kstore-secret-config _kstoresecret-config
local _NEW_SECRET i _key _prop _O_SECRET _BAK local _NEW_SECRET i _key _prop _O_SECRET _BAK
read -sp "Enter the new passphrase: " _NEW_SECRET read -sp "Enter the new passphrase: " _NEW_SECRET
@@ -184,12 +251,12 @@ function kstore-secret-change {
echo "Backed up at $_BAK" echo "Backed up at $_BAK"
_O_SECRET="$_AUTH_SECRET" _O_SECRET="$_AUTH_SECRET"
for i in `$_SQLITE -list "$_AUTH_DB" "SELECT _ROWID_ FROM store;"`; do for i in `$_SQLITE -list "$_AUTH_DB" "SELECT _ROWID_ FROM $_KSTORE_TABLE;"`; do
_AUTH_SECRET=$_O_SECRET _AUTH_SECRET=$_O_SECRET
_key=`$_SQLITE -list "$_AUTH_DB" "SELECT key FROM store WHERE _ROWID_ = $i;"` _key=`$_SQLITE -list "$_AUTH_DB" "SELECT key FROM $_KSTORE_TABLE WHERE _ROWID_ = $i;"`
_key=`kstore-quote "$_key"` _key=`_kstorequote "$_key"`
_prop=`$_SQLITE -list "$_AUTH_DB" "SELECT prop FROM store WHERE _ROWID_ = $i;"` _prop=`$_SQLITE -list "$_AUTH_DB" "SELECT prop FROM $_KSTORE_TABLE WHERE _ROWID_ = $i;"`
_prop=`kstore-quote "$_prop"` _prop=`_kstorequote "$_prop"`
_val=`kstore get "$_key" "$_prop"` _val=`kstore get "$_key" "$_prop"`
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Current passphrase is incorrect?" >&2 echo "Current passphrase is incorrect?" >&2
@@ -201,12 +268,12 @@ function kstore-secret-change {
kstore update "$_key" "$_val" "$_prop" kstore update "$_key" "$_val" "$_prop"
done done
kstore-secret-auto get _kstoresecret-auto get
if [ -n "$_AUTH_SECRET" ]; then if [ -n "$_AUTH_SECRET" ]; then
kstore-secret-auto del 2>&1 > /dev/null _kstoresecret-auto del 2>&1 > /dev/null
_AUTH_SECRET=$_NEW_SECRET _AUTH_SECRET=$_NEW_SECRET
kstore-secret-auto set _kstoresecret-auto set
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "New passphrase saved in OS's keystore." echo "New passphrase saved in OS's keystore."
@@ -216,68 +283,81 @@ function kstore-secret-change {
_AUTH_SECRET=$_NEW_SECRET _AUTH_SECRET=$_NEW_SECRET
} }
function kstore-secret-clear { function _kstoresecret-clear {
_AUTH_SECRET= unset _AUTH_SECRET
case $OSTYPE in _kstoresecret-auto del
darwin*)
security delete-generic-password -a default -s rbash-kstore 2>&1 > /dev/null
;;
esac
} }
function kstore-update { function _kstoreupdate {
kstore-init || return 1 if [ -z "$1" ]; then
__func_head "[key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
_kstoreinit || return 1
local _key _val _prop local _key _val _prop
_key=`kstore-quote "$1"` _key=`_kstorequote "$1"`
_val=`kstore-enc "$2"` _val=`_kstoreenc "$2"`
_val=`kstore-quote "$_val"` _val=`_kstorequote "$_val"`
_prop=`kstore-quote "${3:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
$_SQLITE "$_AUTH_DB" "UPDATE store SET data = '$_val' WHERE $_cond;" $_SQLITE "$_AUTH_DB" "UPDATE $_KSTORE_TABLE SET data = '$_val' WHERE $_cond;"
} }
function kstore-add { function _kstoreadd {
kstore-init || return 1 if [ -z "$1" ]; then
__func_head "[key] [value|file|-] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
_kstoreinit || return 1
local _key _val _prop local _key _val _prop
_key=`kstore-quote "$1"` _key=`_kstorequote "$1"`
_val=`kstore-enc "$2"` _val=`_kstoreenc "$2"`
_val=`kstore-quote "$_val"` _val=`_kstorequote "$_val"`
_prop=`kstore-quote "${3:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${3:-$_KSTORE_DEF_PROP}"`
sqlite3 "$_AUTH_DB" \ $_SQLITE "$_AUTH_DB" \
"INSERT INTO store ( key, prop, data ) "INSERT INTO $_KSTORE_TABLE ( key, prop, data )
VALUES( '$_key', '$_prop', '$_val' );" VALUES( '$_key', '$_prop', '$_val' );"
} }
function kstore-get { function _kstoreget {
kstore-init || return 1 if [ -z "$1" ]; then
__func_head "[key] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
_kstoreinit || return 1
local _key _prop _cond local _key _prop _cond
_key=`kstore-quote "$1"` _key=`_kstorequote "$1"`
_prop=`kstore-quote "${2:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
sqlite3 "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111 $_SQLITE "$_AUTH_DB" "SELECT 1111 FROM $_KSTORE_TABLE WHERE $_cond;" | grep -q 1111
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
$_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM store WHERE $_cond;" | kstore-dec $_SQLITE -list "$_AUTH_DB" "SELECT ( data ) FROM $_KSTORE_TABLE WHERE $_cond;" | _kstoredec
else else
echo "\"$1\" not found" >&2 echo "\"$1\" not found (prop: $_prop)" >&2
return 1
fi fi
} }
function kstore-del { function _kstoredel {
kstore-init || return 1 if [ -z "$1" ]; then
__func_head "[key] [prop, default: $_KSTORE_DEF_PROP]"
return 1
fi
_kstoreinit || return 1
local _CONFIRM _key _prop _cond local _CONFIRM _key _prop _cond
_key=`kstore-quote "$1"` _key=`_kstorequote "$1"`
_prop=`kstore-quote "${2:-$_KSTORE_DEF_PROP}"` _prop=`_kstorequote "${2:-$_KSTORE_DEF_PROP}"`
_cond="key = '$_key' AND prop = '$_prop'" _cond="key = '$_key' AND prop = '$_prop'"
sqlite3 "$_AUTH_DB" "SELECT 1111 FROM store WHERE $_cond;" | grep -q 1111 $_SQLITE "$_AUTH_DB" "SELECT 1111 FROM $_KSTORE_TABLE WHERE $_cond;" | grep -q 1111
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
sqlite3 "$_AUTH_DB" "SELECT * FROM store WHERE $_cond;" $_SQLITE "$_AUTH_DB" "SELECT * FROM $_KSTORE_TABLE WHERE $_cond;"
echo echo
read -p "Delete this entry (yes/no)? " _CONFIRM read -p "Delete this entry (yes/no)? " _CONFIRM
if [ "$_CONFIRM" == "yes" ]; then if [ "$_CONFIRM" == "yes" ]; then
sqlite3 "$_AUTH_DB" "DELETE FROM store WHERE $_cond;" $_SQLITE "$_AUTH_DB" "DELETE FROM $_KSTORE_TABLE WHERE $_cond;"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "deleted" echo "deleted"
fi fi
@@ -285,23 +365,132 @@ function kstore-del {
echo "action canceled" echo "action canceled"
fi fi
else else
echo "\"$1\" not found" >&2 echo "\"$1\" not found (prop: $_prop)" >&2
fi fi
} }
function kstore-search { function _kstoredl-s3au {
local p tmp _NAME
p="https://git.k8s.astropenguin.net/penguin/s3-arch-utils/raw/branch/master"
_CSUM=$1
_NAME=$2
tmp=$( mktemp )
__download "$p/$_NAME" > $tmp
sha256sum $tmp | grep -q "$_CSUM"
if [ $? -eq 0 ]; then
_NAME=$( basename "${_NAME//_/-}" .sh )
mv $tmp "$RBASH_BIN/$_NAME"
chmod +x "$RBASH_BIN/$_NAME"
echo "> $RBASH_BIN/$_NAME"
else
echo "$_NAME: Signature mismatch"
return 1
fi
}
function _kstoredl-kstorecred {
local p tmp _NAME
_NAME="kstorecred"
p="https://penguins-workspace.s3.ap-southeast-1.astropenguin.net/keystore/kstorecred.exe"
_CSUM="031b4474b8eb8deafbb96df73b79f8b654fd3c2209f041fd34cb6f494791bcd4"
tmp=$( mktemp )
__download "$p" > $tmp
sha256sum $tmp | grep -q "$_CSUM"
if [ $? -eq 0 ]; then
mv $tmp "$RBASH_BIN/$_NAME"
chmod +x "$RBASH_BIN/$_NAME"
echo "> $RBASH_BIN/$_NAME"
else
echo "$_NAME: Signature mismatch"
return 1
fi
}
function _kstoreinit-s3au {
_kstoredl-s3au "574d106cdced150fa6e04a9437d356d8688035cb2c63a045fa0d4ead8b3ec941" "arch_delete_aws4.sh" || return 1
_kstoredl-s3au "ce04b3f90b7d9a2578587c8ff841186810d977545943fb4a8234a6d6e9f7b568" "arch_delete_many_aws4.sh" || return 1
_kstoredl-s3au "4c2a3d67fd97f1b54847424a596f08e2901ee183301f268e2ffd92a4f0ccef32" "arch_download_aws4.sh" || return 1
_kstoredl-s3au "0bee6f925a41f496f66654062b49ea2a1cc55d877f4617d56bf91244aaf0be51" "arch_getacl_aws4.sh" || return 1
_kstoredl-s3au "4eb3aba45d9d20b15b8aab575955084bf525be62661e5b7de2bdeac0260ec9fc" "arch_list_aws4.sh" || return 1
_kstoredl-s3au "e90d197e4f965f88c30f8ff4b0a43ee4b5d656032781f1647770637fff0b9481" "arch_upload_aws4.sh" || return 1
}
function _kstoreupload-db {
local _T _W
_kstoreinit || return 1
_T=$( date +%Y%m%d%H%M%S )
echo $_T > "$RBASH_HOME/keystore.latest"
which arch-upload-aws4 2>&1 > /dev/null
if [ $? -ne 0 ]; then
_kstoreinit-s3au
fi
_kstoreenc $_AUTH_DB > "$RBASH_HOME/$_T.enc"
arch-upload-aws4 "keystore/$_T.enc" "$RBASH_HOME/$_T.enc"
arch-upload-aws4 "keystore/latest" "$RBASH_HOME/keystore.latest"
rm "$RBASH_HOME/$_T.enc"
rm "$RBASH_HOME/keystore.latest"
}
function _kstoredownload-db {
local _T _DOMAIN _URL _TMP _CONFIRM
kstore secret config || return 1
_DOMAIN="penguins-workspace.s3.ap-southeast-1.astropenguin.net"
_T=$( __download "https://$_DOMAIN/keystore/latest" )
if [ $? -ne 0 ]; then
return 1
fi
_TMP=$( mktemp )
__download "https://$_DOMAIN/keystore/$_T.enc" | _kstoredec > $_TMP
if [ $? -ne 0 ]; then
return 1
fi
_T=$( $_SQLITE "$_TMP" "SELECT COUNT( * ) FROM $_KSTORE_TABLE" )
if [ $? -ne 0 ]; then
return 1
fi
if [ -f "$_AUTH_DB" ]; then
read -p "Replace existing db? (yes/no): " _CONFIRM
if [ "$_CONFIRM" == "yes" ]; then
_T="$_AUTH_DB.old.$( date +%Y%m%d%H%M%S )"
mv "$_AUTH_DB" "$_T"
mv $_TMP $_AUTH_DB
echo "Original copy: $_T"
else
echo "action canceled"
rm $_TMP
fi
else
mv $_TMP $_AUTH_DB
fi
}
function _kstoresearch {
local _termk _termp _cond local _termk _termp _cond
_termk=`kstore-quote "$1"` _termk=`_kstorequote "$1"`
_termp=`kstore-quote "$2"` _termp=`_kstorequote "$2"`
_cond="key LIKE '%$_termk%' AND prop LIKE '%$_termp%'" _cond="key LIKE '%$_termk%' AND prop LIKE '%$_termp%'"
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store WHERE $_cond;" $_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM $_KSTORE_TABLE WHERE $_cond;"
} }
function kstore-list { function _kstorelist {
$_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM store;" $_SQLITE -header -column "$_AUTH_DB" "SELECT key, prop, length( data ) FROM $_KSTORE_TABLE;"
} }
function kstore-query { function _kstorequery {
sqlite3 "$_AUTH_DB" "$@" $_SQLITE "$_AUTH_DB" "$@"
} }

220
bash/sources/17_kcontext Normal file
View File

@@ -0,0 +1,220 @@
#!/bin/bash
declare -f kstore > /dev/null
if [ $? -ne 0 ]; then
echo "kcontext depends on kstore"
return 1
fi
function kcontext {
case $1 in
k8s) shift; _kcontext-k8s "$@" ;;
docker) shift; _kcontext-docker "$@" ;;
s3-arch) shift; _kcontext-s3-arch "$@" ;;
*)
__func_head "k8s ..."
__func_help "s3-arch ..."
__func_help "docker ..."
return 1
;;
esac
return $?
}
function _kcontext {
local CUR=${COMP_WORDS[COMP_CWORD]}
local SCOPE=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=()
case "$SCOPE" in
kcontext)
COMPREPLY=( $(compgen -W "k8s docker s3-arch" -- $CUR) )
;;
k8s|s3-arch)
COMPREPLY=( $(compgen -W "use del list save" -- $CUR) )
return
;;
use|del)
SCOPE=${COMP_WORDS[COMP_CWORD-2]}
;;
esac
case "$SCOPE" in
k8s|s3-arch)
COMPREPLY=( $(compgen -W "$( kcontext $SCOPE list )" -- $CUR) )
;;
esac
}
complete -F _kcontext kcontext
function _kcontext-k8s {
local _NAME _CONF
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 5 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 'k8s.%'"
return $?
;;
save)
_NAME=$( kubectl config current-context )
if [ $? -ne 0 ]; then
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kubectl config view --minify --flatten | kstore update "kcontext" - "k8s.$_NAME"
return $?
else
kubectl config view --minify --flatten | kstore add "kcontext" - "k8s.$_NAME"
return $?
fi
;;
use)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
_CONF="$RBASH_HOME/k8s.conf"
kstore get "kcontext" "k8s.$_NAME" > "$_CONF"
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
chmod 600 "$_CONF"
echo $KUBECONFIG | grep "$_CONF" -q
case $OSTYPE in
cygwin)
_CONF=$( cygpath -w "$_CONF" )
;;
esac
if [ $? -eq 0 ]; then
export KUBECONFIG="$_CONF"
else
export KUBECONFIG="$_CONF:$KUBECONFIG"
fi
;;
del)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'"
return $?
else
echo "No such context: $_NAME" >&2
fi
;;
*)
__func_head "list"
__func_help "save"
__func_help "use NAME"
__func_help "del NAME"
;;
esac
return 1
}
function _kcontext-s3-arch {
local _NAME _CONFIRM _URL _AUTH
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 16 ) FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'"
return $?
;;
save)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
if [ -n "$ARCH_S3_BUCKET_URL" ] && [ -n "$ARCH_S3_AUTH" ]; then
kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kstore update "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME"
kstore update "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME"
return $?
else
kstore add "kcontext" "$ARCH_S3_BUCKET_URL" "s3-arch.bucket.$_NAME"
kstore add "kcontext" "$ARCH_S3_AUTH" "s3-arch.auth.$_NAME"
return $?
fi
else
echo "No s3-arch settings can be found in env" >&2
fi
;;
use)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
_kstoreinit || return 1
_URL=$( kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null )
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
_AUTH=$( kstore get "kcontext" "s3-arch.auth.$_NAME" 2> /dev/null )
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
export ARCH_S3_BUCKET_URL=$_URL
export ARCH_S3_AUTH=$_AUTH
;;
del)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
kstore get "kcontext" "s3-arch.bucket.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.bucket.$_NAME'"
kstore query "DELETE FROM $_KSTORE_TABLE WHERE key = 'kcontext' AND prop = 's3-arch.auth.$_NAME'"
return $?
else
echo "No such context: $_NAME" >&2
fi
;;
*)
__func_head "list"
__func_help "save NAME"
__func_help "use NAME"
__func_help "del NAME"
;;
esac
return 1
}

67
bash/sources/19_k8s_funcs Normal file
View File

@@ -0,0 +1,67 @@
#!/bin/bash
# Written with the help of chatgpt o3-mini
function klabels {
local header=
local padding=
local trim_length=
local line_text=
local first_line=
local max_key_length=0
local max_label_length=0
local key=
local value=
local filter_value="$1"
local filter_key="$2"
while IFS= read -r line; do
if [ -z "$header" ]; then
header="$line"
padding="${header//LABELS/}"
padding="${padding//?/ }"
echo "$header"
trim_length=${#padding}
continue
fi
line_text="${line:0:trim_length}"
first_line=
IFS=',' read -ra labels <<< "${line:trim_length}"
# Calculate the maximum key length to align key=value pairs
for label in "${labels[@]}"; do
key="${label%%=*}"
max_key_length=$(( ${#key} > max_key_length ? ${#key} : max_key_length ))
done
# Loop through labels and align them
for label in "${labels[@]}"; do
key="${label%%=*}"
value="${label#*=}"
if [ -n "$filter_key" ] && ! [[ "$key" == *$filter_key* ]] ; then
continue
fi
if [ -n "$filter_value" ] && ! [[ "$value" == *$filter_value* ]] ; then
continue
fi
# Format the label to align the '=' sign
printf -v formatted_label "%-${max_key_length}s = %s" "$key" "$value"
if [ -z "$first_line" ]; then
first_line=1
# Print a line separator based on the longest key length
separator_length=$((max_key_length + trim_length))
printf "%${separator_length}s\n" | tr ' ' "-"
echo "$line_text$formatted_label"
else
echo "$padding$formatted_label"
fi
done
done
}

View File

@@ -2,15 +2,62 @@
function mkuserdirs { function mkuserdirs {
local PROFILE PKG_DIR i local PKG_DIR i IFS=$'\n'
PROFILE=$( cygpath $USERPROFILE ) for i in $( reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" \
for i in $( ls $PROFILE | grep -o "^[A-Z][a-z]\+$" ); do | grep "^ [A-Za-z ]\+" \
ln -vs "$PROFILE/$i" $HOME/ | tr -d '\r' \
| sed -e "s/.\+REG_EXPAND_SZ[ ]\+//g"
); do
if [[ "$i" =~ .*"\\AppData\\".* ]]; then
continue
fi
if [[ "$i" =~ "%USERPROFILE".* ]]; then
i=$USERPROFILE${i/\%USERPROFILE\%/}
fi
ln -vs "$( cygpath "$i" )" $HOME/
done done
# Package Directory # Package Directory
PKG_DIR="$APPDATA/../Local/Packages" PKG_DIR=$( cygpath "$APPDATA/../Local/Packages" )
if [ -d "$PKG_DIR" ]; then if [ -d "$PKG_DIR" ]; then
ln -vs "$PKG_DIR" $HOME/ ln -vs "$PKG_DIR" $HOME/
fi fi
}
function init-mintty {
cat <<___MINTTY___ > $HOME/.minttyrc
BoldAsFont=no
CursorType=block
Columns=120
Rows=30
ForegroundColour=131,148,150
BackgroundColour=0,20,43
CursorColour=220,50,47
Black=7,54,66
BoldBlack=0,43,54
Red=220,50,47
BoldRed=203,75,22
Green=133,153,0
BoldGreen=88,110,117
Yellow=181,137,0
BoldYellow=101,123,131
Blue=38,139,210
BoldBlue=131,148,150
Magenta=211,54,130
BoldMagenta=108,113,196
Cyan=42,161,152
BoldCyan=147,161,161
White=238,232,213
BoldWhite=253,246,227
Transparency=low
Locale=C
Charset=UTF-8
Font=Consolas
Scrollbar=none
FontHeight=14
___MINTTY___
} }

43
bash/sources/23_win-binaries Executable file
View File

@@ -0,0 +1,43 @@
#!/bin/bash
for i in $( find /opt/ -maxdepth 1 -name node-v\*-win-x64 -type d ); do
echo " Selected node binary @$i"
export PATH=$PATH:"$i"
break
done
for i in $( find /opt/ -maxdepth 1 -name nmap-\* -type d ); do
echo " Selected nmap binary @$i"
export PATH=$PATH:"$i"
break
done
if [ -n "$FrameworkVersion" ]; then
alias vsshell='echo "FrameworkVersion $FrameworkVersion"'
return
fi
BAT_DIR=~/.local/lib/bat
mkdir -p $BAT_DIR
VS_SHELL_BAT=$BAT_DIR/vs-env.bat
function vsshell {
_SHELL=$( cygpath -w -a $VS_SHELL_BAT )
cmd /c "$_SHELL"
exit
}
if [ -f "$VS_SHELL_BAT" ]; then
_PATH=$( cygpath "$( grep -o "^@REM FILE_PATH: .\+" "$VS_SHELL_BAT" | cut -c 17- )" )
if [ ! -f "$_PATH" ]; then
START_MENU="$( cygpath $ProgramData )/Microsoft/Windows/Start Menu/"
_PATH=$( find "$START_MENU" -name "*Command Prompt*" | grep "Visual Studio" | grep x64 | grep Native | xargs -I % readshortcut % -r | grep -o "\"\([^\"]\+\)\"" )
echo "@echo off" > "$VS_SHELL_BAT"
echo "@REM FILE_PATH: ${_PATH:1:-1}" >> "$VS_SHELL_BAT"
echo "CALL $_PATH" >> "$VS_SHELL_BAT"
echo "START $( where mintty ) -i /Cygwin-Terminal.ico -" >> "$VS_SHELL_BAT"
fi
fi

View File

@@ -1,33 +0,0 @@
#!/bin/bash
if [ -n "$FrameworkVersion" ]; then
alias vsshell='echo "FrameworkVersion $FrameworkVersion"'
return
fi
BAT_DIR=~/.local/lib/bat
mkdir -p $BAT_DIR
VS_SHELL_BAT=$BAT_DIR/vs-env.bat
function vsshell {
_SHELL=$( cygpath -w -a $VS_SHELL_BAT )
cmd /c "$_SHELL"
exit
}
if [ -f "$VS_SHELL_BAT" ]; then
_PATH=$( cygpath "$( grep -o "^@REM FILE_PATH: .\+" "$VS_SHELL_BAT" | cut -c 17- )" )
if [ -f "$_PATH" ]; then
return
fi
fi
START_MENU="$( cygpath $ProgramData )/Microsoft/Windows/Start Menu/"
_PATH=$( find "$START_MENU" -name "*Command Prompt*" | grep "Visual Studio" | grep x64 | grep Native | xargs -I % readshortcut % -r | grep -o "\"\([^\"]\+\)\"" )
echo "@echo off" > "$VS_SHELL_BAT"
echo "@REM FILE_PATH: ${_PATH:1:-1}" >> "$VS_SHELL_BAT"
echo "CALL $_PATH" >> "$VS_SHELL_BAT"
echo "START $( where mintty ) -i /Cygwin-Terminal.ico -" >> "$VS_SHELL_BAT"

View File

@@ -29,19 +29,25 @@ function _begin_sel() {
} }
function go() { function go() {
local i local i _GO
ARG1=$1; ARG2=$2; ARG3=$3; ARG1=$1; ARG2=$2; ARG3=$3;
if [[ -z "$ARG1" ]]; then if [[ -z "$ARG1" ]]; then
__func_head "[MODE] SITE TARGET_DIR [..SLICE]" __func_head "[MODE] SITE TARGET_DIR [..SLICE]"
__func_help "up [MODE] TARGET_DIR" __func_help "up [MODE] TARGET_DIR"
__func_help "down [MODE] TARGET_DIR [..SLICE]" __func_help "down [MODE] TARGET_DIR [..SLICE]"
__func_help "compiler commands use \"go help\""
echo echo
return 1 return 1
fi fi
NAV=false NAV=false
case "$ARG1" in case "$ARG1" in
bug|build|clean|doc|env|fix|fmt|generate|get|install|list|mod|work|run|test|tool|version|vet|help)
_GO=$( which go )
"$_GO" $@
return $?
;;
p) p)
shift shift
CC='pushd' ;; CC='pushd' ;;

View File

@@ -1,32 +0,0 @@
#!/usr/bin/env python3
import string;
import random;
import os;
import sys;
if len(sys.argv) < 2:
sys.exit('Usage: ' + os.path.basename(__file__) + ' length');
l = sys.argv[1];
if not l.isdigit():
sys.exit('"' + l + '" is not a valid length.');
# cast l into intergers
l = int(l);
# define the character set
charSet = string.ascii_letters + string.digits;
# generate stack
out_stack = ''.join(random.choice(charSet) for x in range(l));
# Am I being piped?
if sys.stdout.isatty():
print(out_stack);
else:
sys.stdout.write(out_stack);

View File

@@ -117,7 +117,8 @@ class RegReplace:
sys.stdout.write( "\033[K" ) # Clear line sys.stdout.write( "\033[K" ) # Clear line
sys.stdout.write( "%s ... %s" % ( bb[ bb_i ], root ) ) sys.stdout.write( "%s ... %s" % ( bb[ bb_i ], root ) )
actions = self._compile_actions( root, files ) actions = self._compile_actions( root, dirs )
actions.extend( self._compile_actions( root, files ) )
sys.stdout.write("\r") # Goto line start sys.stdout.write("\r") # Goto line start
sys.stdout.write( "\033[F" ) # Move up sys.stdout.write( "\033[F" ) # Move up