EN VI

Linux - bash-script - merge two files at specific line?

2024-03-11 21:00:07
How to Linux - bash-script - merge two files at specific line

I'm struggling with two files, one contains my base64 certificate chain and the other one the private key and I want to merge them that the private key is added after the first occurence of

-----END CERTIFICATE-----

. I have no clue how to do it in bash.

Would be great to get help with it. Thank you!

Certificate chain file

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the intermediate certificate)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA)...
-----END CERTIFICATE----

private key file

-----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,CFCECC7976725DE5
    <Server Private Key – Passphrase protected>
-----END PRIVATE KEY-----

final merged file

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
    Proc-Type: 4,ENCRYPTED
    DEK-Info: DES-EDE3-CBC,CFCECC7976725DE5
    <Server Private Key – Passphrase protected>
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
... (the intermediate certificate)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA)...
-----END CERTIFICATE----

script snippet:

get_new_certificate() {

    curl --get "$venafi_url$venafi_get_cert_endpoint?Format=Base64&IncludeChain=true&RootFirstOrder=false" --insecure --data-urlencode "CertificateDN=$certificate_identifier" \
    -H "accept: application/json" \
    -H "Authorization: Bearer $auth_token" \
    -o "$certificate_path"

   # Extract the private key block
   #private_key=$( awk '/-----BEGIN PRIVATE KEY-----/,/-----END PRIVATE KEY-----/' "$PRIVATE_KEY_PATH" | tr '\n' '\f')
   private_key=$( cat "$PRIVATE_KEY_PATH")
   # Combine the certificates and private key in the desired order
   #sed -i -z "0,/-----END CERTIFICATE-----/s/ $private_key"  "$certificate_path" |  tr '\f' '\n'
   #awk '/-----END CERTIFICATE-----/ {print; print "'"$private_key"'"; next}1' "$cert_chain_files > "updated_combined.pem
   #cat $certificate_path
   sed -i.bak -e "/-----END CERTIFICATE-----/r $private_key" -e "/-----END CERTIFICATE-----/d" $certificate_path

   echo "Got new Certificate"
}

Solution:

I will add my effort here just for reference:

#!/bin/bash
certfile='chain.txt'
privfile='private.txt'

awk -v file="1" -v occur="1" '
{
    print > (file".txt")
}
/^-----END CERTIFICATE-----$/{
    count++
    if(count%occur==0){
        if(file){
            close(file".txt")
            ++file
        }
    }
}
' $certfile

`cat 1.txt $privfile 2.txt 3.txt > output.txt`

This script uses awk to split the chain.txt file into separate files (1.txt, 2.txt, 3.txt) based on a regular expression, then cat is used to concatenate all of the files in the correct order (1.txt, private.txt, 2.txt, 3.txt). This script is not complete because it should delete the temporary files afterwards, but is otherwise functional. Also, the file names for the temporary files are hard-coded, which is also not recommended.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login