Skip to content

Commit 664f2e6

Browse files
authored
Merge pull request #82 from DrDaveD/add-httokensh
Add httokensh, update to 1.19
2 parents bbe60a1 + 34258c1 commit 664f2e6

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed

htgettoken

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from __future__ import print_function
1818

1919
prog = "htgettoken"
20-
version = "1.18"
20+
version = "1.19"
2121

2222
import os
2323
import sys

htgettoken.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Summary: Get OIDC bearer tokens by interacting with Hashicorp vault
44
Name: htgettoken
5-
Version: 1.18
5+
Version: 1.19
66
Release: 1%{?dist}
77
License: BSD
88
Group: Applications/System
@@ -108,6 +108,7 @@ exec %{_libexecdir}/%{name}/%{name} "$@"
108108
cp htdestroytoken $RPM_BUILD_ROOT%{_bindir}
109109
cp httokendecode $RPM_BUILD_ROOT%{_bindir}
110110
ln -s httokendecode $RPM_BUILD_ROOT%{_bindir}/htdecodetoken
111+
cp httokensh $RPM_BUILD_ROOT%{_bindir}
111112
chmod +x $RPM_BUILD_ROOT%{_bindir}/*
112113
gzip -c %{name}.1 >$RPM_BUILD_ROOT%{_datadir}/man/man1/%{name}.1.gz
113114

@@ -125,6 +126,9 @@ rm -rf $RPM_BUILD_ROOT
125126

126127

127128
%changelog
129+
* Thu Jul 27 2023 Dave Dykstra <dwd@fnal.gov> 1.19-1
130+
- Add httokensh command.
131+
128132
* Wed May 24 2023 Dave Dykstra <dwd@fnal.gov> 1.18-1
129133
- Fix crash introduced in 1.17 when using --nobearertoken while the
130134
credkey is not known.

httokensh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/bin/bash
2+
#
3+
# Run htgettoken and then start a shell command and keep the access
4+
# token updated for as long as the command runs. If there is no
5+
# -o/--outfile htgettoken option and BEARER_TOKEN_FILE is not already
6+
# set, choose a unique location and set BEARER_TOKEN_FILE to point
7+
# to the token. If there is no --vaulttokenfile option, the vault token
8+
# will be stored in a file name based on a hash of the arguments given,
9+
# so that multiple httokensh commands run by the same user on the same
10+
# machine with the same options will share a vault token and otherwise
11+
# will get a different vault token. The access token will be renewed
12+
# just under --minsecs seconds (default 60) before it is set to expire.
13+
# Output from the background htgettoken goes to $BEARER_TOKEN_FILE.log.
14+
15+
usage()
16+
{
17+
echo "Usage: httokensh [-h] [htgettokenoptions] -- [command]"
18+
echo
19+
echo "Runs htgettoken with given options, starts the command, and runs"
20+
echo "htgettoken again in the background as needed to renew the token"
21+
echo "until the command exits."
22+
echo
23+
echo "Options:"
24+
echo " -h, --help show this help message and exit"
25+
echo
26+
echo "command defaults to \$SHELL"
27+
exit 1
28+
} >&2
29+
30+
if [ $# = 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
31+
usage
32+
fi
33+
34+
HTGETOKENARGS=()
35+
COMMANDARGS=()
36+
GOTSEP=false
37+
MINSECS=60
38+
GOTVERBOSE=false
39+
GOTOUTFILE=false
40+
GOTVTFILE=false
41+
for ARG; do
42+
if $GOTSEP; then
43+
COMMANDARGS+=("$ARG")
44+
elif [ "$ARG" = "--" ]; then
45+
GOTSEP=true
46+
else
47+
HTGETTOKENARGS+=("$ARG")
48+
case "$ARG" in
49+
--minsecs=*)
50+
MINSECS="${ARG/--minsecs=/}"
51+
;;
52+
-v|--verbose)
53+
GOTVERBOSE=true
54+
;;
55+
-o|--outfile=*)
56+
GOTOUTFILE=true
57+
;;
58+
--vaulttokenfile=*)
59+
GOTVTFILE=true
60+
;;
61+
esac
62+
fi
63+
done
64+
65+
if ! $GOTSEP; then
66+
echo "No -- separator given" >&2
67+
usage
68+
fi
69+
70+
if [ ${#HTGETTOKENARGS[@]} = 0 ]; then
71+
echo "No htgettoken options given" >&2
72+
usage
73+
fi
74+
75+
if [ ${#COMMANDARGS[@]} = 0 ]; then
76+
COMMANDARGS=("$SHELL")
77+
fi
78+
79+
if [ -z "$BEARER_TOKEN_FILE" ] && ! $GOTOUTFILE; then
80+
BTFILE="bt_u$(id -u).sh-$$"
81+
if [ -n "$XDG_RUNTIME_DIR" ]; then
82+
BEARER_TOKEN_FILE=$XDG_RUNTIME_DIR/$BTFILE
83+
else
84+
BEARER_TOKEN_FILE=/tmp/$BTFILE
85+
fi
86+
export BEARER_TOKEN_FILE
87+
fi
88+
89+
if ! $GOTVTFILE; then
90+
ARGHASH="$(echo "${HTGETTOKENARGS[@]}"|md5sum -)"
91+
ARGHASH="${ARGHASH%% *}"
92+
VTFILE="/tmp/vt_u$(id -u).sh-$ARGHASH"
93+
HTGETTOKENARGS+=("--vaulttokenfile=$VTFILE")
94+
fi
95+
96+
gettoken()
97+
{
98+
htgettoken "${HTGETTOKENARGS[@]}"
99+
RETVAL="$?"
100+
if [ $RETVAL != 0 ]; then
101+
echo "htgettoken failed, $1" >&2
102+
exit $RETVAL
103+
fi
104+
105+
TOKENJSON="$(htdecodetoken)"
106+
RETVAL="$?"
107+
if [ $RETVAL != 0 ]; then
108+
echo "htdecodetoken failed, $1" >&2
109+
exit $RETVAL
110+
fi
111+
112+
EXP="$(echo $TOKENJSON|jq .exp)"
113+
NOW="$(date +%s)"
114+
let SLEEPSECS="$EXP - $MINSECS - $NOW + 2"
115+
if [ "$SLEEPSECS" -lt $2 ]; then
116+
echo "Calculated renewal time of $SLEEPSECS seconds is less than $2, $1"
117+
exit 1
118+
fi
119+
}
120+
121+
# The first time it is possible to get a cached token that is barely
122+
# beyond the minsecs, so reduce the minimum to just 1 second
123+
gettoken "not running command" 1
124+
125+
# make sure the logged info is verbose for easier diagnosis
126+
if ! $GOTVERBOSE; then
127+
HTGETTOKENARGS+=("-v")
128+
fi
129+
130+
# enable job control so background processes get their own process group
131+
set -m
132+
133+
echo "Renewal log is at \$BEARER_TOKEN_FILE.log"
134+
{
135+
echo htgettoken args are "${HTGETTOKENARGS[@]}"
136+
while true; do
137+
date
138+
echo "Renewal scheduled in $SLEEPSECS seconds"
139+
sleep $SLEEPSECS
140+
date
141+
if kill -0 $PPID; then
142+
gettoken "exiting" 60
143+
else
144+
echo "Parent process $PPID not running, exiting"
145+
exit 0
146+
fi
147+
done
148+
} >$BEARER_TOKEN_FILE.log 2>&1 &
149+
150+
BACKGROUND_PID=$!
151+
152+
cleanup()
153+
{
154+
if kill -- -$BACKGROUND_PID 2>/dev/null; then
155+
wait 2>/dev/null
156+
rm -f $BEARER_TOKEN_FILE $BEARER_TOKEN_FILE.log
157+
else
158+
echo >&2
159+
echo "Renewal background process failed, see $BEARER_TOKEN_FILE.log" >&2
160+
exit 1
161+
fi
162+
}
163+
164+
trap cleanup 0
165+
166+
"${COMMANDARGS[@]}"

0 commit comments

Comments
 (0)