← history for iron/libexec/iron/iron-client
b99217f2iron/libexec/iron/iron-client123 lines⬡ raw↓ download
  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
#! /bin/bash
# Usage: iron client <service-name> <client-id> <setup|create|delete>
# Summary: manage CSR for certificates creation
# Help: This command groups commands used to setup config create delete a CA

APPNAME="iron"
CURRDIR=$(pwd)

RCDIR=$HOME/.$APPNAME
test -d $RCDIR || mkdir -p $RCDIR
CURRENT_TSTAMP=$(date '+%Y%m%d%H%M')

function client_create_csr()
{
  DOMAIN=$1
  client=$2
  CLIENT_CONFIG_DIR="$RCDIR/${DOMAIN}/conf"
  CLIENT_CONFIG_FILE=${CLIENT_CONFIG_DIR}/client-${client}.conf
  test -f $CLIENT_CONFIG_FILE || (echo "missing client config file"; exit -1)
  CP=$CLIENT_CERTPATH/$client
  mkdir -p $CP
  openssl genrsa -out $CP/$client.key 2048
  openssl req -new -key $CP/$client.key -out $CP/$client.csr -config $CLIENT_CONFIG_FILE
}

function client_home_setup_delete()
{
  DOMAIN=$1
  client=$2
  echo "About to DELETE cert path: [$RCDIR/${DOMAIN}] are you sure? y/n"
  read confirmation
  if [ "$confirmation" = "y" ]; then
    (rm -fr $RCDIR/${DOMAIN}) && echo "CA DELETED"
  else
    echo "SKIPPING"
    exit -1
  fi
}

function client_export_bundle()
{
  DOMAIN=$1
  client=$2
  CAPATH=$RCDIR/CA
  CLIENT_CERTPATH=$RCDIR/${DOMAIN}/certs
  target_dir="cert-$client"
  mkdir -p $target_dir/CA
  mkdir -p $target_dir/$DOMAIN
  mkdir -p $target_dir/$DOMAIN/certs/$client
  cp $CAPATH/ca.crt $target_dir/CA
  cp $CLIENT_CERTPATH/$client/$client.crt $target_dir/$DOMAIN/certs/$client
  openssl rsa -aes256 -in $CLIENT_CERTPATH/$client/$client.key --out $target_dir/$DOMAIN/certs/$client/$client.key
  tar cvfz $target_dir.tar.gz $target_dir && rm -fr $target_dir
}

function client_home_setup_write()
{
  
  DOMAIN=$1
  client=$2
  CAPATH=$RCDIR/CA
  CLIENT_CERTPATH=$RCDIR/${DOMAIN}/certs
  CLIENT_CONFIG_DIR="$RCDIR/${DOMAIN}/conf"
  test -d $CLIENT_CONFIG_DIR || mkdir -p $CLIENT_CONFIG_DIR
  test -d $CLIENT_CERTPATH/client/$client && ( echo "Dir exist, please delete before" && exit -1)
  test -d $CLIENT_CERTPATH/client/$client || mkdir -p $CLIENT_CERTPATH/client/$client
  echo "CLIENT_CONFIG_DIR=$CLIENT_CONFIG_DIR" >> ${RCFILE}
  echo "CLIENT_CERTPATH=$CLIENT_CERTPATH" >> ${RCFILE}
  echo "CLIENT_CONFIG_FILE=${CLIENT_CONFIG_DIR}/client-${client}.conf" >> ${RCFILE}
  echo "CAPATH=${RCDIR}/$DOMAIN/CA"  >> ${RCFILE}
  test -d $CAPATH   	|| mkdir -p $CAPATH
  test -d $CLIENT_CERTPATH || mkdir -p $CLIENT_CERTPATH
  
	cat<<__EOF__ >$RCDIR/${DOMAIN}/conf/client-${client}.conf
#RANDFILE               = $ENV::HOME/.rnd

[ req ]
default_bits           = 2048
default_keyfile        = keyfile.pem
distinguished_name     = req_distinguished_name
attributes             = req_attributes
prompt                 = no
#output_password        = bliblablu

[ req_distinguished_name ]
C                      = SE
ST                     = Sweden
L                      = Göteborg
O                      = ${DOMAIN}
OU                     = ${DOMAIN}
CN                     = ${client}
emailAddress           = ${client}

[ req_attributes ]
#challengePassword      = blablabla
__EOF__
  
}

if [ "$#" = 3 ];then
  operation=$1
  service=$2
  client=$3
  RCFILE="$RCDIR/$service/client-$client"".env"
  if [ "$operation" = "csr" ]; then
    test -f ${RCFILE} || ( client_home_setup_write ; echo "Edit values in ${RCFILE}"; exit -1)
    source ${RCFILE}
    client_create_csr $service $client
  fi
  if [ "$operation" = "setup" ]; then
    test -f ${RCFILE} || ( client_home_setup_write $service $client; echo "Edit values in ${RCFILE}"; exit 0)
  fi
  if [ "$operation" = "export" ]; then
    client_export_bundle $service $client
  fi
  if [ "$operation" = "delete" ]; then
    source ${RCFILE}
    #client_home_setup_delete  $service $client
  fi
else
  echo "Usage: iron client <setup|csr|delete|export> <service> <client-id>"
  exit -1
fi