Skip to content
Snippets Groups Projects
Commit 1e233226 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

compare hashes; remove FQDN from altnames; logging

parent a2d1c068
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
# - acme.sh client # - acme.sh client
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2021-02-02 # 2021-02-02 <axel.hahn@iml.unibe.ch> first lines
# 2021-02-10 <axel.hahn@iml.unibe.ch> compare hashes, logging
# ====================================================================== # ======================================================================
...@@ -24,9 +25,12 @@ ...@@ -24,9 +25,12 @@
selfdir="$( dirname $0 )" selfdir="$( dirname $0 )"
touchfile="${selfdir}/log/lastchange.txt" touchfile="${selfdir}/log/lastchange.txt"
logfile="${selfdir}/log/certmanager-$( date +"%Y%M%d" ).log" # logfile="${selfdir}/log/certmanager-$( date +"%Y%m%d" ).log"
logfile="${selfdir}/log/certmanager.log"
line="_______________________________________________________________________________" line="_______________________________________________________________________________"
showdebug=1 showdebug=1
writelog=1
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -35,10 +39,21 @@ showdebug=1 ...@@ -35,10 +39,21 @@ showdebug=1
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# internal function; list certificates incl. creation date and renew date
function _listCerts(){
$ACME --list
}
# internal function; checks if a certificate for a given FQDN already exists
# used in _certMustExist, _certMustNotExist
# param string FQDN
function _certExists(){ function _certExists(){
_listCerts | awk '{ print $1 }' | grep "^${CM_fqdn}$" >/dev/null _listCerts | awk '{ print $1 }' | grep "^${CM_fqdn}$" >/dev/null
} }
# internal function; a certificate of a given FQDN must exist - otherwise
# the script will be aborted
# param string FQDN
function _certMustExist(){ function _certMustExist(){
_certExists _certExists
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
...@@ -46,6 +61,10 @@ function _certMustExist(){ ...@@ -46,6 +61,10 @@ function _certMustExist(){
exit 1 exit 1
fi fi
} }
# internal function; a certificate of a given FQDN must not exist - otherwise
# the script will be aborted
# param string FQDN
function _certMustNotExist(){ function _certMustNotExist(){
_certExists _certExists
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
...@@ -57,6 +76,7 @@ function _certMustNotExist(){ ...@@ -57,6 +76,7 @@ function _certMustNotExist(){
# internal function: transfer generated/ updated cert data to a # internal function: transfer generated/ updated cert data to a
# known directory (based on CM_diracme - see inc_config.sh) # known directory (based on CM_diracme - see inc_config.sh)
# used in public_add and public_renew # used in public_add and public_renew
# used in ADD and RENEW action
function _certTransfer(){ function _certTransfer(){
_wd "--- acme internal data - ~/.acme.sh/${CM_fqdn}" _wd "--- acme internal data - ~/.acme.sh/${CM_fqdn}"
ls -l ~/.acme.sh/${CM_fqdn} ls -l ~/.acme.sh/${CM_fqdn}
...@@ -80,13 +100,21 @@ function _certTransfer(){ ...@@ -80,13 +100,21 @@ function _certTransfer(){
# internal function; show md5 hashsums for certificate, csr and key # internal function; show md5 hashsums for certificate, csr and key
# for visual comparison if the match # for visual comparison if the match
# TODO: script a comparison to write out MATCH or FAIL
function _certMatching(){ function _certMatching(){
local md5_cert=$( openssl x509 -noout -modulus -in ${CM_outfile_cert} | openssl md5 | cut -f 2 -d " " )
local md5_csr=$( openssl req -noout -modulus -in ${CM_filecsr} | openssl md5 | cut -f 2 -d " " )
local md5_key=$( openssl rsa -noout -modulus -in ${CM_outfile_key} | openssl md5 | cut -f 2 -d " " )
echo echo
echo "--- compare hashes to see if they match" echo "--- compare hashes"
echo -n "cert : "; openssl x509 -noout -modulus -in ${CM_outfile_cert} | openssl md5 echo "cert : $md5_cert"
echo -n "csr : "; openssl req -noout -modulus -in ${CM_filecsr} | openssl md5 echo "csr : $md5_csr"
echo -n "key : "; openssl rsa -noout -modulus -in ${CM_outfile_key} | openssl md5 echo "key : $md5_key"
if [ "$md5_key" = "$md5_cert" -a "$md5_key" = "$md5_csr" ]; then
echo "OK, they match :-)"
else
echo "ERROR: they do NOT MATCH!"
fi
echo echo
} }
...@@ -121,8 +149,9 @@ function _gencsr(){ ...@@ -121,8 +149,9 @@ function _gencsr(){
_checkDig $CM_fqdn _checkDig $CM_fqdn
for myalt in $* for myalt in $*
do do
altdns="${altdns},DNS:$myalt" altdns="${altdns}DNS:$myalt,"
done done
altdns=$( echo $altdns | sed "s#,\$##" )
_wd "--- $CM_fqdn" _wd "--- $CM_fqdn"
_wd "DNS alternative names: $altdns" _wd "DNS alternative names: $altdns"
...@@ -143,11 +172,8 @@ function _gencsr(){ ...@@ -143,11 +172,8 @@ function _gencsr(){
ls -ltr $CM_filecnf $CM_filekey $CM_filecsr ls -ltr $CM_filecnf $CM_filekey $CM_filecsr
} }
# internal function; list certificates incl. creation date and renew date # internal function; check if a required 2nd CLI parameter was given
function _listCerts(){ # if not the script will abort
$ACME --list
}
function _requiresFqdn(){ function _requiresFqdn(){
if [ -z "$CM_fqdn" ]; then if [ -z "$CM_fqdn" ]; then
echo "ERROR: 2nd parameter must be a FQDN for Main_Domain." echo "ERROR: 2nd parameter must be a FQDN for Main_Domain."
...@@ -155,6 +181,9 @@ function _requiresFqdn(){ ...@@ -155,6 +181,9 @@ function _requiresFqdn(){
fi fi
} }
# internal function; it shows a message if the current instance uses a stage
# server. It shows a message that it is allowed to test arround ... or to be
# careful with LE requests on a production system
function _testStaging(){ function _testStaging(){
echo $ACME_Params | grep "\-\-staging" >/dev/null echo $ACME_Params | grep "\-\-staging" >/dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
...@@ -170,7 +199,8 @@ function _testStaging(){ ...@@ -170,7 +199,8 @@ function _testStaging(){
# set update message in a file # set update message in a file
# param string(s) message # param string(s) message
function _update(){ function _update(){
echo $( date ) $* > ${touchfile} echo "[$( date )] $*" > ${touchfile}
test ${writelog} && echo "[$( date )] $*" >> ${logfile}
} }
# write debug output if showdebug is set to 1 # write debug output if showdebug is set to 1
...@@ -220,7 +250,7 @@ function public_add-or-renew(){ ...@@ -220,7 +250,7 @@ function public_add-or-renew(){
_requiresFqdn _requiresFqdn
_certExists _certExists
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
_wd "--- cert was found ... renew it" _wd "--- cert was found ... renew it (ignore --force - it comes from acme.sh)"
public_renew public_renew
else else
_wd "--- cert does mot exist ... add it" _wd "--- cert does mot exist ... add it"
...@@ -242,7 +272,7 @@ function public_delete(){ ...@@ -242,7 +272,7 @@ function public_delete(){
$ACME --remove -d ${CM_fqdn} $ACME_Params $ACME --remove -d ${CM_fqdn} $ACME_Params
_wd "--- delete local data" _wd "--- delete local data"
rm -rf ${CM_dircerts} ${CM_filecnf} ${CM_filekey} ${CM_filecsr} ~/.acme.sh/${CM_fqdn} rm -rf ${CM_dircerts} ${CM_filecnf} ${CM_filekey} ${CM_filecsr} ~/.acme.sh/${CM_fqdn}
_update "deleted ${CM_fqdn}" _update "deleted ${CM_fqdn}"
} }
...@@ -264,7 +294,7 @@ function public_renew(){ ...@@ -264,7 +294,7 @@ function public_renew(){
_certTransfer _certTransfer
_certMatching _certMatching
_update "renew ${CM_fqdn}" _update "renewed ${CM_fqdn}"
} }
# #
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
Wrapper for acme.sh to create Let's Encrypt certificates based on CSR files using DNS authentication Wrapper for acme.sh to create Let's Encrypt certificates based on CSR files using DNS authentication
source: https://git-repo.iml.unibe.ch/open-source/iml-certman source: <https://git-repo.iml.unibe.ch/open-source/iml-certman>
## Installation ## Installation
* Install acme.sh client: https://github.com/acmesh-official/acme.sh * Install acme.sh client: <https://github.com/acmesh-official/acme.sh>
* Clone or extract files of iml-certman * Clone or extract files of iml-certman
* Make your changes by copying *dist files to file without ".dist" extension and edit * Make your changes by copying *dist files to file without ".dist" extension and edit
* inc_config.sh * inc_config.sh
......
...@@ -26,7 +26,7 @@ UNITNAME = Department for magic things ...@@ -26,7 +26,7 @@ UNITNAME = Department for magic things
FQDN = __FQDN__ FQDN = __FQDN__
# subjectAltName entries: to add DNS aliases to the CSR, delete # subjectAltName entries: to add DNS aliases to the CSR, delete
ALTNAMES = DNS:$FQDN__ALTNAMES__ ALTNAMES = __ALTNAMES__
[ req ] [ req ]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment