be 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #! /bin/bash
  2. # change credentials
  3. #
  4. # Usage: be [options] who
  5. # arguments
  6. # who name of identity
  7. #
  8. # options
  9. #
  10. # Select credentials
  11. #
  12. # -a|--aws change/list aws credentials ONLY
  13. # -g|--gnupg change/list gnupgcredentials ONLY
  14. # -p|--pass change/list pas credentials ONLY
  15. # -s|--ssh change/list ssh credentials ONLY
  16. #
  17. # Select operation
  18. #
  19. # -l|--list list availabe credentials.
  20. # -w|--whoami list current identities
  21. #
  22. # Other
  23. #
  24. # -d|--debug debug output
  25. # -h|--help print usage
  26. # -v|--verbose verbose output
  27. #
  28. # e.g.
  29. #
  30. # If ~/.gnupg.$1 exists, link to ~/.gnupg
  31. # If ~/.ssh/id_{dsa,rsa}.$1 exists, link to ~/.ssh/id_{dsa,rsa} and add to ssh agent
  32. # If ~/.aws/credentials.$1 exists, link to ~/.aws/credentials
  33. #
  34. # TODO
  35. # - deal with git identities
  36. # + Use XDG-CONFIG-HOME to switch identities?
  37. # http://git.661346.n2.nabble.com/What-is-XDG-CONFIG-HOME-for-exactly-td7627117.html
  38. # + See https://gist.github.com/jexchan/2351996
  39. # - Deal with .pem files
  40. set -e; set -u
  41. # Helper functions
  42. PROG=`basename "$0" | tr -d '\n'`
  43. function info() { echo ${PROG}\: info: "$@" 1>&2; }
  44. function warn() { echo ${PROG}\: warning: "$@" 1>&2; }
  45. function error() { echo ${PROG}\: error: "$@" 1>&2; }
  46. function debug() { [[ -v DEBUG ]] && echo ${PROG}\: debug: "$@" 1>&2 || true ; }
  47. function die() { echo ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
  48. function usage() {
  49. debug "in ${FUNCNAME[0]}"
  50. if [[ "$#" -gt 0 ]]; then
  51. warn $@
  52. fi
  53. cat <<END 1>&2
  54. Usage: be [options] who
  55. arguments
  56. who name of identity
  57. options
  58. Select credentials
  59. -a|--aws change/list aws credentials ONLY
  60. -g|--gnupg change/list gnupgcredentials ONLY
  61. -p|--pass change/list pas credentials ONLY
  62. -s|--ssh change/list ssh credentials ONLY
  63. Select operation
  64. -l|--list list availabe credentials.
  65. -w|--whoami list current identities
  66. Other
  67. -d|--debug debug output
  68. -h|--help print usage
  69. -v|--verbose verbose output
  70. END
  71. exit 1
  72. }
  73. function gpg_list() {
  74. # list available gpg credentail sets (diretories)
  75. info available GPG credentials sets
  76. ls -ld ~/.gnupg.*
  77. }
  78. function gpg_whoami() {
  79. # list current gpg identity
  80. info Current gpg credential set
  81. info
  82. ls -ld ~/.gnupg
  83. info
  84. }
  85. function gpg_become() {
  86. # change gpg identity
  87. rm -f ~/.gnupg || true
  88. ln -s ~/.gnupg."${who}" ~/.gnupg
  89. }
  90. function aws_list() {
  91. # list available aws credentials
  92. cd ~/.aws || die "Error connecting to ~/.aws"
  93. info available AWS credentials and configs
  94. ls -1 credentials.* config.*
  95. }
  96. function aws_whoami() {
  97. # list current aws identity
  98. cd ~/.aws || die "Error connecting to ~/.aws"
  99. info Current aws credentials
  100. info
  101. ls -l credentials config
  102. info
  103. }
  104. function aws_become() {
  105. # change aws identity
  106. cd ~/.aws || die "Error connecting to ~/.aws"
  107. aws_creds="credentials.""${who}"
  108. if [ ! -f "${aws_creds}" ]; then
  109. warn file "${aws_creds}" does not exist. Not changing aws identity.
  110. else
  111. [[ -v VERBOSE ]] && set -x
  112. rm -f credentials || true
  113. ln -s "${aws_creds}" credentials
  114. [[ -v VERBOSE ]] && set +x
  115. fi
  116. aws_config="config.""${who}"
  117. if [ ! -f "${aws_config}" ]; then
  118. warn file "${aws_config}" does not exist. Not installing.
  119. else
  120. [[ -v VERBOSE ]] && set -x
  121. rm -f config || true
  122. ln -s "${aws_config}" config
  123. [[ -v VERBOSE ]] && set +x
  124. fi
  125. }
  126. function ssh_list() {
  127. # list available ssh credentials
  128. cd ~/.ssh || die "Error connecting to ~/.ssh"
  129. info available SSH credentials
  130. ls -1 id_rsa.* id_dsa.*
  131. }
  132. function ssh_whoami() {
  133. # list current ssh identity
  134. cd ~/.ssh || die "Error connecting to ~/.ssh"
  135. info Current SSH identities
  136. info
  137. ls -l id_??? || warn "no ~/.ssh/id_{rsa,dsa} file"
  138. info SSH Agent Identities
  139. ssh-add -l
  140. info
  141. }
  142. function ssh_become() {
  143. # change ssh identity
  144. cd ~/.ssh || die "Error connecting to ~/.ssh"
  145. rsa_creds="id_rsa.""${who}"
  146. dsa_creds="id_dsa.""${who}"
  147. if [ -f "${dsa_creds}" ]; then
  148. ssh_creds="${dsa_creds}"
  149. elif [ -f "${rsa_creds}" ]; then
  150. ssh_creds="${rsa_creds}"
  151. else
  152. echo "No ssh creds found. "${rsa_creds}" and "${dsa_creds}" do not exis."
  153. exit 1
  154. fi
  155. target=`basename $ssh_creds ".""${who}"`
  156. if [ -f "${ssh_creds}" ]; then
  157. [[ -v VERBOSE ]] && set +x
  158. rm -f "${target}" || true
  159. ln -s "${ssh_creds}" "${target}"
  160. chmod 400 "${target}"
  161. ssh-add "${ssh_creds}"
  162. [[ -v VERBOSE ]] && set -x
  163. fi
  164. }
  165. function pass_list() {
  166. # list available pass credentail sets (diretories)
  167. info available pass credentials sets
  168. ls -ld ~/.password-store.*
  169. }
  170. function pass_whoami() {
  171. # list current pass identity
  172. info Current pass credential set
  173. info
  174. ls -ld ~/.password-store
  175. info
  176. }
  177. function pass_become() {
  178. # change pass identity
  179. rm -f ~/.password-store || true
  180. ln -s ~/.password-store."${who}" ~/.password-store
  181. gpg_become # need to switch GPG IDs too
  182. }
  183. function org_list() {
  184. # list available org credentail sets (diretories)
  185. info available org credentials sets
  186. ls -ld ~/Org.*
  187. }
  188. function org_whoami() {
  189. # list current org identity
  190. info Current org credential set
  191. info
  192. ls -ld ~/Org
  193. info
  194. }
  195. function org_become() {
  196. # change org identity
  197. rm -f ~/Org || true
  198. ln -s ~/Org."${who}" ~/Org
  199. }
  200. #
  201. # "main()" begins here
  202. #
  203. # Defaults
  204. SSH=1
  205. AWS=1
  206. GPG=1
  207. PASSWORD=1
  208. ORG=1
  209. # parse global options
  210. for i in "$@"
  211. do
  212. case $i in
  213. -a|--aws)
  214. AWS=1
  215. unset SSH
  216. unset GPG
  217. unset PASSWORD
  218. unset ORG
  219. d_flag="-d"
  220. shift # past argument with no value
  221. ;;
  222. -d|--debug)
  223. DEBUG=1
  224. d_flag="-d"
  225. shift # past argument with no value
  226. ;;
  227. -g|--gnupg)
  228. GPG=1
  229. unset AWS
  230. unset SSH
  231. unset PASSWORD
  232. unset ORG
  233. g_flag="-g"
  234. shift # past argument with no value
  235. ;;
  236. -h|--help)
  237. usage
  238. ;;
  239. -l|--list)
  240. LIST=1
  241. d_flag="-d"
  242. shift # past argument with no value
  243. ;;
  244. -o|--org)
  245. unset PASSWORD
  246. unset AWS
  247. unset SSH
  248. unset GPG
  249. ORG=1
  250. p_flag="-p"
  251. shift # past argument with no value
  252. ;;
  253. -p|--pass)
  254. PASSWORD=1
  255. unset AWS
  256. unset SSH
  257. unset GPG
  258. unset ORG
  259. p_flag="-p"
  260. shift # past argument with no value
  261. ;;
  262. -s|--ssh)
  263. SSH=1
  264. unset AWS
  265. unset GPG
  266. unset PASSWORD
  267. unset ORG
  268. d_flag="-d"
  269. shift # past argument with no value
  270. ;;
  271. -v|--verbose)
  272. VERBOSE=1
  273. v_flag="-v"
  274. shift # past argument with no value
  275. ;;
  276. -w|--whoami)
  277. WHOAMI=1
  278. v_flag="-v"
  279. shift # past argument with no value
  280. ;;
  281. -*|--*)
  282. usage "Unknown state option: $i"
  283. ;;
  284. esac
  285. done
  286. # Pull off command line args
  287. if [[ ! -v LIST && ! -v WHOAMI ]]; then
  288. if [ "$#" -ne 1 ]; then
  289. usage need a username
  290. fi
  291. who="${1}"
  292. fi
  293. if [[ ! -v SSH && ! -v AWS && ! -v ORG && ! -v PASSWORD && ! -v GPG ]]; then
  294. die "Must specify at least one of '--aws' '--ssh' '--gnupg' '--pass'"
  295. fi
  296. # Change aws credentials
  297. if [ -v AWS ]; then
  298. if [[ -v LIST ]]; then
  299. aws_list
  300. elif [[ -v WHOAMI ]]; then
  301. aws_whoami
  302. else
  303. aws_become
  304. fi
  305. fi
  306. # Change ssh credentials
  307. if [ -v SSH ]; then
  308. if [[ -v LIST ]]; then
  309. ssh_list
  310. elif [[ -v WHOAMI ]]; then
  311. ssh_whoami
  312. else
  313. ssh_become
  314. fi
  315. fi
  316. # Change GPG credentials
  317. if [ -v GPG ]; then
  318. if [[ -v LIST ]]; then
  319. echo GPG LIST
  320. gpg_list
  321. elif [[ -v WHOAMI ]]; then
  322. gpg_whoami
  323. else
  324. gpg_become
  325. fi
  326. fi
  327. # Change pass credentials
  328. if [ -v PASSWORD ]; then
  329. if [[ -v LIST ]]; then
  330. echo PASSWORD LIST
  331. pass_list
  332. elif [[ -v WHOAMI ]]; then
  333. pass_whoami
  334. else
  335. pass_become
  336. fi
  337. fi
  338. # Change org credentials
  339. if [ -v ORG ]; then
  340. if [[ -v LIST ]]; then
  341. echo ORG LIST
  342. org_list
  343. elif [[ -v WHOAMI ]]; then
  344. org_whoami
  345. else
  346. org_become
  347. fi
  348. fi