-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-vault-lastpass
executable file
·201 lines (183 loc) · 5.21 KB
/
ansible-vault-lastpass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2018, Mattias Bengtsson <[email protected]>
LASTPASS_GROUP=AnsibleVaults
ANSIBLE_CALL=false
# shellcheck disable=SC2009
case "$(ps aux | grep "${PPID}" | awk "\$2 == ${PPID} {print \$11, \$12}")" in
*ansible-playbook*)
ANSIBLE_CALL=true
;;
*)
ANSIBLE_CALL=false
;;
esac
function strip-dot-git {
case "${1}" in
*.git) echo "${1%.*}" ;;
*) echo "${1}" ;;
esac
}
function strip-username {
case "${1}" in
*@*) echo "${1##*@}" ;;
*) echo "${1}" ;;
esac
}
function git-get-hostname-from-remote {
url="$(strip-dot-git "${1}")"
case "${url}" in
http://*|https://*)
IFS='/'
read -ra split <<< "${url}"
strip-username "${split[*]:2:1}"
;;
*:*)
IFS=':'
read -ra split <<< "${url}"
strip-username "${split[*]:0:1}"
;;
*)
error "Weird remote: [${url}]"
;;
esac
}
function git-get-project-from-remote {
url="$(strip-dot-git "${1}")"
case "${url}" in
http://*|https://*)
IFS='/'
read -ra split <<< "${url}"
echo "${split[*]:3}"
;;
*:*)
IFS=':'
read -ra split <<< "${url}"
echo "${split[*]:1}"
;;
*)
error "Weird remote: [${url}]"
;;
esac
}
function error {
if [ "${ANSIBLE_CALL}" = true ]; then
echo "${@}" | systemd-cat -p err
else
echo "${@}" > /dev/stderr
fi
}
function usage {
echo "Usage: $(basename "${0}") [-h|--help] [add|edit|generate|rm]"
echo
echo "Store passwords for ansible-vault in lastpass."
}
function full-usage {
usage
echo
echo "Notes:"
echo
echo " $(basename "${0}") assumes the vault file is sitting in a"
echo " git-repository with a configured upstream- or origin remote."
echo
echo "Quick start:"
echo
echo " Inside a git-repo:"
echo " $ export ANSIBLE_VAULT_PASSWORD_FILE=${0}"
echo " $ $(basename "${0}") generate"
echo
echo " After typing in your password and closing your editor you should now"
echo " have the vault password for this repo stored in lastpass under the "
echo " ${LASTPASS_GROUP} group."
echo
echo " If everything works as expected all ansible-vault commands"
echo " (inside the same repo) should now fetch this password from lastpass."
}
function check-args {
if [ $# -lt 1 ]; then
return 0
fi
case "${1}" in
add|edit|generate|rm)
return 0
;;
*)
return 2
;;
esac
}
if [[ " ${*} " == *" -h "* ]]; then
usage
exit 0
elif [[ " ${*} " == *" --help "* ]]; then
full-usage
exit 0
elif ! check-args "${@}"; then
error "'${1}' is not a known command. See '$(basename "${0}") --help'."
exit 2
fi
if [ ! "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
# TODO: make it possible to fall back to other method.
echo "$(basename "${0}"): not a git repository"
exit 0
fi
# TODO: Thin about how to output stuff here. Maybe pipe to some info function
# that either sends it to the journal or to stdout/stderr
if ! lpass status -q &>/dev/null; then
if ! lpass login "${LASTPASS_ACCOUNT}" &>/dev/null; then
error "Failed to login to lastpass"
exit 4
fi
fi
# TODO: Thin about how to output stuff here. Maybe pipe to some error function
# that either sends it to the journal or to stdout/stderr
if ! remote=$(git remote get-url upstream 2>/dev/null); then
if ! remote=$(git remote get-url origin 2>/dev/null); then
error "Failed to find a suitable remote"
exit 5
fi
fi
host="$(git-get-hostname-from-remote "${remote}")"
project="$(git-get-project-from-remote "${remote}")"
key="${LASTPASS_GROUP}\\${host}\\${project}"
if [ $# -gt 0 ]; then
case "${1}" in
add|generate)
lpass show --username "${key}" &> /dev/null && {
error "The '${key}' key already exists!"
exit 6
}
case "${1}" in
add)
lpass add --password "${key}"
;;
generate)
lpass generate "${key}" 15 > /dev/null
;;
esac
;;
edit|rm)
lpass show --username "${key}" &>/dev/null || {
error "The '${key}' key doesn't exist!"
exit 7
}
case "${1}" in
edit)
lpass edit --password "${key}"
;;
rm)
read -p "Are you sure you want to delete ${key} [y/N]?" \
-n 1 \
-r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lpass rm "${key}"
fi
;;
esac
;;
esac
else
lpass show --password "${key}"
fi