<commit_before>#!/bin/bash -u

export LC_CTYPE=C

# default value should be override
CRT_CN=${CRT_CN:-"default"}

while getopts t:n:p:h opt
do
    case "$opt" in
        t) # Certificate Type (client/server)
           case $OPTARG in
               'client')
                   CERTYPE=$OPTARG
                   NO_PASSWD=${NO_PASSWD:-false} # <= PrivateKey with password by default for Client certificate
                   ;;
               'server')
                   CERTYPE=$OPTARG
                   NO_PASSWD=${NO_PASSWD:-true} # <= passwordless PrivateKey by default for Server certificate
                   ;;
               *)
                   echo >&2 "Unknown option '$1' (available values are 'client' or 'server'"
                   exit 1
                   ;;
           esac
           ;;
        p) PROJECT_NAME="$OPTARG" ;; # Project Name
        n) CRT_CN="$OPTARG"       ;; # Name (client: User fullname / server: Website URL)
        *)
           echo >&2 "
Usage: $0 -p projectname -t certype [-n name]

  -p projectname

        String to identify the project or client. Only used to create a
        distinct folder to separate the generated files. This _projectname_
        won't be used in the generated certificates.

  -t certype

        Allowed values are 'server' or 'client'
        Choose 'server' to create a x509 file to be installed on your server
        Choose 'client' to create a pkcs12 file to be installed on your browser

  -n name

        String to be used in the Common Name (CN) of the generated certificates
        For 'server' type it should be the URL of the server
        For 'client' type it should be the FULLNAME of the end user
"
           exit 1
           ;;
    esac
done

: $PROJECT_NAME
: $CERTYPE

[ ! -d "$PROJECT_NAME" ] && echo >&2 "FATAL: The project does exist yet" && exit 1
cd "$PROJECT_NAME"

CRT_UID="${CERTYPE}Certificate_$( date +%Y%m%d_%H%M%S )_$( echo ${CRT_CN} | tr -dc A-Za-z0-9 )"
PASSWD_STRING="$( $NO_PASSWD && echo "-nodes" || echo "-passout file:${CRT_UID}.key.pass" )"

umask 377
< /dev/urandom tr -dc A-Za-z0-9 | head -c30 > ${CRT_UID}.key.pass

# .key / .csr
openssl req \
    -new \
    -newkey rsa:4096 -keyout ${CRT_UID}.key $PASSWD_STRING \
    -out ${CRT_UID}.csr -subj "/C=${CRT_C:-"FR"}/L=${CRT_L:-"Paris"}/O=${CRT_O:-"Ekino"}/OU=${CRT_OU:-"DevOps"}/CN=${CRT_CN}"

# What to do here :
#  - TRUSTED CA => stop here, then send the generated .csr to your trusted CA
#  - CUSTOM CA  => continue and generate the .crt/.p12 file(s)<commit_msg>Allow CRT_SUBJ to override all other CRT_XXX variables<commit_after>#!/bin/bash -u

export LC_CTYPE=C

while getopts t:n:p:h opt
do
    case "$opt" in
        t) # Certificate Type (client/server)
           case $OPTARG in
               'client')
                   CERTYPE=$OPTARG
                   NO_PASSWD=${NO_PASSWD:-false} # <= PrivateKey with password by default for Client certificate
                   ;;
               'server')
                   CERTYPE=$OPTARG
                   NO_PASSWD=${NO_PASSWD:-true} # <= passwordless PrivateKey by default for Server certificate
                   ;;
               *)
                   echo >&2 "Unknown option '$1' (available values are 'client' or 'server'"
                   exit 1
                   ;;
           esac
           ;;
        p) PROJECT_NAME="$OPTARG" ;; # Project Name
        n) CRT_CN="$OPTARG"       ;; # Name (client: User fullname / server: Website URL)
        *)
           echo >&2 "
Usage: $0 -p projectname -t certype [-n name]

  -p projectname

        String to identify the project or client. Only used to create a
        distinct folder to separate the generated files. This _projectname_
        won't be used in the generated certificates.

  -t certype

        Allowed values are 'server' or 'client'
        Choose 'server' to create a x509 file to be installed on your server
        Choose 'client' to create a pkcs12 file to be installed on your browser

  -n name

        String to be used in the Common Name (CN) of the generated certificates
        For 'server' type it should be the URL of the server
        For 'client' type it should be the FULLNAME of the end user
"
           exit 1
           ;;
    esac
done

: $PROJECT_NAME
: $CERTYPE

# default value should be override
: ${CRT_CN:="default"}
: ${CRT_SUBJ:="/C=${CRT_C:-"FR"}/L=${CRT_L:-"Paris"}/O=${CRT_O:-"Ekino"}/OU=${CRT_OU:-"DevOps"}/CN=${CRT_CN}"}

[ ! -d "$PROJECT_NAME" ] && echo >&2 "FATAL: The project does exist yet" && exit 1
cd "$PROJECT_NAME"

CRT_UID="${CERTYPE}Certificate_$( date +%Y%m%d_%H%M%S )_$( echo ${CRT_CN} | tr -dc A-Za-z0-9 )"
PASSWD_STRING="$( $NO_PASSWD && echo "-nodes" || echo "-passout file:${CRT_UID}.key.pass" )"

umask 377
< /dev/urandom tr -dc A-Za-z0-9 | head -c30 > ${CRT_UID}.key.pass

# .key / .csr
openssl req \
    -new \
    -newkey rsa:4096 -keyout ${CRT_UID}.key $PASSWD_STRING \
    -out ${CRT_UID}.csr -subj "$CRT_SUBJ"

# What to do here :
#  - TRUSTED CA => stop here, then send the generated .csr to your trusted CA
#  - CUSTOM CA  => continue and generate the .crt/.p12 file(s)
{'fraction_non_alphanumeric': 0.12113552401322185, 'fraction_numerical': 0.010888586428154774, 'mean_word_length': 2.1365853658536587, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2083341', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1590, 'n_words': 543}