[Kong] Batch change SNIs’ certificate

Kong 0.13.1, I have a few snis bind to a cert which will be expired soon. So write a sh to bind these snis to a new cert (need install jq first):

#!/bin/sh
SNIS=`curl -s "http://kong-admin.kong:8001/snis"`
LEN=`echo $SNIS | jq '.data | length'`
# bash # for (( i=0; i<LEN; i++ ))
for i in $(seq 0 $(($LEN-1)))
do
  sni=$(echo $SNIS | jq .data[$i] | jq -r .name)
  found=0

  echo $sni | grep domain1.com

  if [ $? -eq 0 ]; then
    found=1
  else
    echo $sni | grep domain2.com

    if [ $? -eq 0 ]; then
      found=1
    fi    
  fi

  if [ $found -eq 1 ]; then
    curl -X PATCH "http://kong-admin.kong:8001/snis/${sni}" -H "Content-Type: application/json" --data "{ \"ssl_certificate_id\": \"CHANGE TO YOUR NEW CERT ID\"}"
  fi
done

Leave a Comment