Skip to content

Commit 46d03cc

Browse files
authored
Do not gen new API key on startup (#50)
1 parent a40fbdf commit 46d03cc

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

debian/postinst

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
11

22
systemctl daemon-reload
33

4-
5-
START=0
6-
74
luarocks install lua-resty-http
85
luarocks install lua-cjson
96

7+
API_KEY_REQUIRED=true
8+
BOUNCER_CONFIG_PATH="/etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf"
9+
API_KEY="<API_KEY>"
10+
CROWDSEC_LAPI_URL="<LAPI_URL>"
1011
LAPI_DEFAULT_PORT="8080"
1112

1213
if [ "$1" = "configure" ]; then
1314

14-
type cscli > /dev/null
15+
type cscli
1516

1617
if [ "$?" -eq "0" ] ; then
17-
START=1
18-
echo "cscli/crowdsec is present, generating API key"
19-
unique=`date +%s`
20-
API_KEY=`cscli -oraw bouncers add nginx-${unique}`
21-
PORT=$(cscli config show --key "Config.API.Server.ListenURI"|cut -d ":" -f2)
22-
if [ ! -z "$PORT" ]; then
23-
LAPI_DEFAULT_PORT=${PORT}
18+
# Check if it's an upgrade
19+
if [ "$2" != "" ] ; then
20+
echo "Upgrading, check if there is bouncer configuration"
21+
if [ -f "${BOUNCER_CONFIG_PATH}" ] ; then
22+
API_KEY_REQUIRED=false
23+
fi
2424
fi
25-
CROWDSEC_LAPI_URL="http://127.0.0.1:${LAPI_DEFAULT_PORT}"
26-
if [ $? -eq 1 ] ; then
27-
echo "failed to create API token, service won't be started."
28-
START=0
29-
API_KEY="<API_KEY>"
30-
else
31-
echo "API Key : ${API_KEY}"
25+
API=$(cscli config show --key "Config.API.Server")
26+
if [ "$API" = "nil" ] || [ "$API" = "<nil>" ] ; then
27+
API_KEY_REQUIRED=false
28+
fi
29+
if [ ${API_KEY_REQUIRED} = true ] ; then
30+
echo "cscli/crowdsec is present, generating API key"
31+
unique=$(date +%s)
32+
API_KEY=$(cscli -oraw bouncers add crowdsec-nginx-bouncer-"${unique}")
33+
PORT=$(cscli config show --key "Config.API.Server.ListenURI"|cut -d ":" -f2)
34+
if [ ! -z "$PORT" ]; then
35+
LAPI_DEFAULT_PORT=${PORT}
36+
fi
37+
CROWDSEC_LAPI_URL="http://127.0.0.1:${LAPI_DEFAULT_PORT}"
38+
if [ $? -eq 1 ] ; then
39+
echo "failed to create API key."
40+
API_KEY_REQUIRED=true
41+
API_KEY="<API_KEY>"
42+
else
43+
echo "API Key : ${API_KEY}"
44+
TMP=$(mktemp -p /tmp/)
45+
cp ${BOUNCER_CONFIG_PATH} "${TMP}"
46+
API_KEY="${API_KEY}" CROWDSEC_LAPI_URL="${CROWDSEC_LAPI_URL}" envsubst '$API_KEY $CROWDSEC_LAPI_URL' < "${TMP}" > ${BOUNCER_CONFIG_PATH}
47+
rm "${TMP}"
48+
fi
3249
fi
33-
34-
TMP=`mktemp -p /tmp/`
35-
cp /etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf ${TMP}
36-
API_KEY=${API_KEY} CROWDSEC_LAPI_URL=${CROWDSEC_LAPI_URL} envsubst < ${TMP} > /etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf
37-
rm ${TMP}
3850
fi
3951

4052
mkdir -p /etc/nginx/conf.d/
4153
cp /usr/share/crowdsec-nginx-bouncer/crowdsec_nginx.conf /etc/nginx/conf.d/crowdsec_nginx.conf
4254

4355
else
44-
START=1
56+
API_KEY_REQUIRED=false
4557
fi
4658

47-
48-
if [ ${START} -eq 0 ] ; then
49-
echo "no api key was generated"
59+
if [ ${API_KEY_REQUIRED} = true ] ; then
60+
echo "Can't generate an API key for the bouncer. Please do it manually"
5061
fi
5162

5263
echo "Restart nginx to enable the crowdsec bouncer : sudo systemctl restart nginx"

0 commit comments

Comments
 (0)